1 | |
---|
2 | |
---|
3 | #!/usr/bin/env python |
---|
4 | import os |
---|
5 | import csmllibs |
---|
6 | import sys |
---|
7 | class fileExtractBuilder: |
---|
8 | def __init__(self, dataset_element, ffmap): |
---|
9 | self.dataset_element=dataset_element |
---|
10 | self.ffmap=ffmap |
---|
11 | self.createFileExtracts() |
---|
12 | |
---|
13 | def createFileExtracts(self): |
---|
14 | #print "REPRESENTATIVE FILES:" |
---|
15 | #print self.ffmap.getRepresentativeFiles() |
---|
16 | |
---|
17 | #given a featurefilemap object, create file extracts from the "representativeFiles" |
---|
18 | #used to store extract/filename/variable relationship |
---|
19 | #referenced when creating features |
---|
20 | self.fileExtractDictionary={} |
---|
21 | |
---|
22 | fileid=0 #used to distinguish extract names for similar variables from different files |
---|
23 | #adlist is an empty list object to hold CSML ArrayDescriptors eg. NetCDFExtract, GRIBExtract etc |
---|
24 | adlist=[] |
---|
25 | for repfile in self.ffmap.getRepresentativeFiles(): |
---|
26 | fileid=fileid+1 |
---|
27 | filename=repfile.getRepresentativeFileName() |
---|
28 | #print 'representative file:' |
---|
29 | #print repfile.getRepresentativeFileName() |
---|
30 | #print 'feature type:' |
---|
31 | #print repfile.getFeatureType() |
---|
32 | DI = csmllibs.csmldataiface.DataInterface() |
---|
33 | DI = DI.getUnknownInterfaceType(filename) |
---|
34 | DI.openFile(filename) |
---|
35 | #creates fileextracts for each variable of each representative file |
---|
36 | self.extractType = DI.extractType |
---|
37 | self.extractPrefix = str(fileid)+'_'+DI.extractPrefix |
---|
38 | #print dir(DI) |
---|
39 | allDimNames=DI.getListOfAxes() |
---|
40 | numDomains=len(allDimNames) |
---|
41 | for j in range (0, len(allDimNames)): |
---|
42 | print self.extractType |
---|
43 | print self.extractPrefix |
---|
44 | if self.extractType=='NASAAmesExtract': |
---|
45 | arrayDescriptor=csmllibs.Parser.NASAAmesExtract() |
---|
46 | if self.extractType=='NetCDFExtract': |
---|
47 | arrayDescriptor=csmllibs.Parser.NetCDFExtract() |
---|
48 | if self.extractType=='GRIBExtract': |
---|
49 | arrayDescriptor=csmllibs.Parser.GRIBExtract() |
---|
50 | if self.extractType=='PPExtract': |
---|
51 | arrayDescriptor=csmllibs.Parser.PPExtract() |
---|
52 | #print self.extractType |
---|
53 | arrayDescriptor.id=str(self.extractPrefix+allDimNames[j]) |
---|
54 | |
---|
55 | #SET variableName attribute of file extract |
---|
56 | arrayDescriptor.variableName=str(allDimNames[j]) |
---|
57 | |
---|
58 | |
---|
59 | #SET arraySize attribute of file extract |
---|
60 | dimValue =DI.getSizeOfAxis(allDimNames[j]) |
---|
61 | if dimValue == None: |
---|
62 | dimValue ='1' #temporary to fix unlimited problem |
---|
63 | arrayDescriptor.arraySize=str(dimValue) |
---|
64 | #SET filename attribute of file extract |
---|
65 | arrayDescriptor.fileName = filename |
---|
66 | |
---|
67 | # #keep record of extracts/filenames/variables in dictionary |
---|
68 | idstr = filename + allDimNames[j] |
---|
69 | self.fileExtractDictionary[idstr] = self.extractPrefix+allDimNames[j] |
---|
70 | adlist.append(arrayDescriptor) |
---|
71 | DI.closeFile() |
---|
72 | #Add all the file extracts to the dataset element |
---|
73 | setattr(self.dataset_element, 'arrayDescriptors', adlist) |
---|
74 | |
---|
75 | |
---|
76 | def createSingleExtract(extractType, fileName, variableName, arraySize): |
---|
77 | #creates a single file extract on demand. (not part of general builder class) |
---|
78 | #need to incorporate check that a file extract of the same name doesn't already exist. |
---|
79 | if extractType=='NASAAmesExtract': |
---|
80 | arrayDescriptor=csmllibs.Parser.NASAAmesExtract() |
---|
81 | if extractType=='NetCDFExtract': |
---|
82 | arrayDescriptor=csmllibs.Parser.NetCDFExtract() |
---|
83 | if extractType=='GRIBExtract': |
---|
84 | arrayDescriptor=csmllibs.Parser.GRIBExtract() |
---|
85 | if extractType=='PPExtract': |
---|
86 | arrayDescriptor=csmllibs.Parser.PPExtract() |
---|
87 | #need to change this so it does the right thing for grib extracts too.. |
---|
88 | arrayDescriptor.id='_%s' % variableName #need prefix system here.. |
---|
89 | arrayDescriptor.fileName = fileName |
---|
90 | arrayDescriptor.arraySize=[arraySize] |
---|
91 | arrayDescriptor.variableName=variableName |
---|
92 | return arrayDescriptor |
---|
93 | |
---|
94 | # sys.exit() |
---|
95 | |
---|
96 | |
---|
97 | |
---|