source: TI02-CSML/trunk/csml/API/ops_GridSeriesFeature.py @ 2073

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI02-CSML/trunk/csml/API/ops_GridSeriesFeature.py@2073
Revision 2073, 9.9 KB checked in by domlowe, 13 years ago (diff)

more subsetting code

Line 
1''' ops_GridSeriesFeature  contains operations for GridSeriesFeatures'''
2
3import csml.parser
4import csml.csmllibs.csmltime
5import csml.csmllibs.csmldocument
6import csml.API.ops_AbstractFeature
7import csml.csmllibs.netCDFWriter
8import csmlutils
9
10import sys  #remove later
11
12def testmethod(self):
13    #print 'testmethod for gridseries feature'
14    return 'testmethod - gridseries'
15
16def getAllowedSubsettings(self):
17    return ['subsetToGridSeries']  #other operations
18
19def getDomain(self):
20    #returns domain as a dictionary of ordinates {name: [values], ...}
21    self.domain={}
22    for gridOrd in self.value.gridSeriesDomain.coordTransformTable.gridOrdinates:
23        name=gridOrd.coordAxisLabel.CONTENT
24        if hasattr(gridOrd.coordAxisValues, '__insertedExtract'):
25            self.domain[name]=gridOrd.coordAxisValues.__insertedExtract.getData()
26        else:
27            vals=gridOrd.coordAxisValues.coordinateList.CONTENT
28            valList=[]
29            print 'splitting'
30            for val in vals.split(): 
31                valList.append(val)
32                print val
33            self.domain[name]=valList
34    return self.domain
35
36def subsetToGridSeries(self, csmlpath=None, ncpath=None,**kwargs):
37    #pathToSubsetCSML = container.csmlpath
38    if ncpath is not None:
39        pathToSubsetNetCDF=ncpath
40    else:
41        pathToSubsetNetCDF='temp.nc'
42   
43    self.getDomain()
44   
45    #TODO - incoporate the crsCatalogue. into this
46    #deal with longitude requests
47    #if the request is in -ve,+ve eg (-30,30) but the data is in (0,360) need to handle this by changing the args.
48    kwargs=csmlutils.fixLongitude(self.domain,kwargs)
49   
50    #deal with times   
51    #need to find the time axis:
52    cat=csml.csmllibs.csmlcrs.CRSCatalogue()
53    crs=cat.getCRS(self.value.gridSeriesDomain.srsName)
54    #get the position of the time axis in crs/ axis labels
55    timeAxisPos=crs.timeAxis
56    #get the name of the time axis in the crs
57    timeName=crs.axes[timeAxisPos]
58    #get the ordinate with that name and find the original time name for the file.
59    for gridOrd in self.value.gridSeriesDomain.coordTransformTable.gridOrdinates:
60        if gridOrd.coordAxisLabel.CONTENT==timeName:
61            timeAxis = gridOrd.gridAxesSpanned.CONTENT  #only works for regular grids atm
62        else: 
63            print 'crs not supported'
64   
65   
66    #here, timeName is the name given to the time axis by the CRS,
67    #and timeAxis is the name given to the time axis in the file.
68    #currently supporting domain subsetting only by CRS name
69    #(but should be easy to extend later)
70    self.times=kwargs[timeName]
71    self.files=[]
72    strTimes=''
73    fulldata=[]
74   
75    #set the reference system for the time axis
76    calset=False
77    for gridOrd in self.value.gridSeriesDomain.coordTransformTable.gridOrdinates:
78        if gridOrd.coordAxisLabel.CONTENT==timeName:
79            try:
80                caltype=gridOrd.coordAxisValues.frame.split(':',1)[0]
81                calunits=gridOrd.coordAxisValues.frame.split(':',1)[1]
82                csml.csmllibs.csmltime.setcdtimeCalendar(caltype)
83                calset=True
84            except:pass
85    if calset!=True:
86        csml.csmllibs.csmltime.setcdtimeCalendar(csml.csmllibs.csmltime.cdtime.DefaultCalendar)   
87    try:
88        caltype=self.domain.domainReference.frame.split(':',1)[0]
89        calunits=self.domain.domainReference.frame.split(':',1)[1]
90        csml.csmllibs.csmltime.setcdtimeCalendar(caltype)
91    except:
92        csml.csmllibs.csmltime.setcdtimeCalendar(csml.csmllibs.csmltime.cdtime.DefaultCalendar)
93 
94       
95    if len(self.times) == 2:
96        #then this is a range of times (t1, tn)
97        try:
98            tone=csml.API.ops_AbstractFeature.__getCDtime(self.times[0])
99        except:
100            tone=self.times[0]
101        try:
102            ttwo=csml.API.ops_AbstractFeature.__getCDtime(self.times[1])
103        except:
104            ttwo=self.times[1]
105        self.times=[]
106           
107        for time in self.domain[timeName]:
108            timeok=csml.API.ops_AbstractFeature.__compareTimes(tone,time,ttwo)
109            if timeok ==1:
110                self.times.append(time)
111     #okay up to here!
112   
113    if hasattr(self.value.rangeSet, 'valueArray'):
114        if hasattr(self.value.rangeSet.valueArray.valueComponent.quantityList, '__insertedExtract'):
115            numFiles= len( csmlutils.listify(self.value.rangeSet.valueArray.valueComponent.quantityList.__insertedExtract.components)[0].fileList.fileNames.CONTENT.split())
116            timeToFileRatio=len(self.domain[timeName])/numFiles
117           
118            #to keep track of files that have already been fetched. eg. if multiple times are in a single file only need to get data from that file once...
119            filesFetched=[]
120            #get data:
121            selection={}
122            for kw in kwargs:
123                        if kw != timeAxis:
124                            selection[kw]=kwargs[kw]
125            for time in self.times:
126                listPosition=self.domain[timeName].index(time)
127                strTimes= strTimes + ' ' + time
128                for comp in csmlutils.listify(self.value.rangeSet.valueArray.valueComponent.quantityList.__insertedExtract.components): 
129                    filePos=int(float(listPosition)/timeToFileRatio)
130                    if filePos in filesFetched:
131                        continue #already got data from this file, try next time
132                    data=comp.getData(fileposition=filePos, **selection)
133                    print comp.fileList.fileNames.CONTENT
134                    self.files.append(comp.fileList.fileNames.CONTENT[filePos]) #TODO, get the right file name
135                    if fulldata ==[]:
136                        fulldata = data.tolist()
137                    else:
138                        for item in data.tolist():
139                            fulldata.append(item)
140                    filesFetched.append(filePos)
141                axisorder = data.getAxisIds()  #will need later!
142    elif hasattr(self.value.rangeSet, 'datablock'): #not tested
143        pass
144
145 
146
147    # define domain  to use in 'value' attribute of feature in csml document#
148   
149   
150    domain=csml.parser.GridSeriesDomain()
151    grid=csml.parser.GridCoordinatesTable()
152    #dc = self.getDomainComplement()
153    ordinates= []
154    i=0
155    valueStore=[]  # use the values again later to generate netcdf
156    arraySize=0
157    totalArraySize=1
158    for key in self.domain.keys():
159        arraySize=0
160        i=i+1
161        god=csml.parser.GridOrdinateDescription()
162        god.gridAxesSpanned='dim%s'%i
163        god.sequenceRule='+x+y+z'
164        god.definesAxis=key
165        straxisValues=''
166        #now deal with each argument:
167   
168        if key in kwargs:
169            if key ==timeName:
170                straxisValues=strTimes
171            elif kwargs[key][0] < kwargs[key][1]:   
172                for val in self.domain[key]:
173                    if val is not '':
174                            if float(val) >= kwargs[key][0]:
175                                if float(val) <= kwargs[key] [1]:
176                                    arraySize=arraySize+1
177                                    straxisValues=straxisValues+ str(val) + ', '
178            else:#this if deals with selections such as longitude (330,30) where the lower limit is 'greater' than the upper limit in a mathematical sense.
179                    for val in self.domain[key]:
180                        if val is not '':
181                            if val >= kwargs[key][0]:
182                                arraySize=arraySize+1
183                                straxisValues=straxisValues+ str(val) + ', '
184                    for val in self.domain[key]:
185                        if val is not '':
186                            if val <= kwargs[key] [1]:
187                                arraySize=arraySize+1
188                                straxisValues=straxisValues+ str(val) + ', '
189        else: # this dimension has not been subsetted at all
190            for val in self.domain[key]:
191                if val is not '':
192                    arraySize=arraySize+1
193                    straxisValues=straxisValues+ str(val) + ', '       
194        totalArraySize=totalArraySize*arraySize
195        god.axisValues=straxisValues[:-2]
196        ordinates.append(god)
197    totalArraySize=totalArraySize*len(self.times)
198    grid.ordinates=ordinates
199    domain.domainComplement=grid
200    rangeSet=csml.parser.RangeSet()
201    rangeSet.arrayDescriptor=csml.parser.NetCDFExtract(id=self.id,fileName=csml.parser.csString(pathToSubsetNetCDF),variableName=csml.parser.csString(self.id),arraySize=csml.parser.csString(arraySize))
202    cvg=csml.parser.GridSeriesCoverage()
203    cvg.rangeSet=rangeSet
204    cvg.gridSeriesDomain=domain   
205    csmlWrap=csml.csmllibs.csmlfeaturewrap.CSMLWrapper()
206    subsettedFeature=csmlWrap.createGridSeriesFeature(value=cvg,datasetID="A",featureID="B",description="C")
207   
208   
209   
210    ### write netcdf using NCWriter class (wraps cdms) ###
211    nc=csml.csmllibs.netCDFWriter.NCwriter(pathToSubsetNetCDF)
212    floatTimes=[]
213    for time in self.times:
214        time=csml.API.ops_AbstractFeature.__getCDtime(time).torel(calunits)
215        floatTimes.append(time.value)
216    nc.addAxis('t',floatTimes,isTime=1,units=calunits,calendar=caltype)
217    #USE CRS!   
218    for ordinate in ordinates:
219        if ordinate.definesAxis=='Time':
220            continue
221        lon,lat=None,None
222        if ordinate.definesAxis=='Lon':
223            lon=1
224        if ordinate.definesAxis=='Lat':
225            lat=1
226        #convert to list
227        vals=[]
228        for val in ordinate.axisValues.split(','):
229            vals.append(float(val))
230        nc.addAxis(ordinate.definesAxis,vals,isLon=lon,isLat=lat,units='')#to do, units attribute for CF compliance
231    if len(ordinates)==3:
232        axes=['t',axisorder[0],axisorder[1],axisorder[2]]
233    elif len(ordinates)==2:
234        axes=['t',axisorder[0],axisorder[1]]
235    nc.addVariable(fulldata,self.id, axes,units='') #to do, units attribute for CF compliance
236    nc.closeFinishedFile()
237    return subsettedFeature, pathToSubsetNetCDF
238   
Note: See TracBrowser for help on using the repository browser.