- Timestamp:
- 31/01/07 15:11:30 (14 years ago)
- Location:
- TI02-CSML/trunk/csml/API
- Files:
-
- 1 added
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
TI02-CSML/trunk/csml/API/ops_AbstractFeature.py
r2061 r2083 2 2 import string 3 3 import cdtime 4 import csml 5 import csmlutils 4 6 5 7 def testmethod(self): … … 48 50 cdTimeVal=cdtime.comptime(year,month,day,hour,minute,second) 49 51 return cdTimeVal 52 53 54 55 -
TI02-CSML/trunk/csml/API/ops_GridSeriesFeature.py
r2081 r2083 5 5 import csml.csmllibs.csmldocument 6 6 import csml.API.ops_AbstractFeature 7 import csml.API.genSubset 7 8 import csml.csmllibs.netCDFWriter 8 9 import csmlutils 10 9 11 10 12 import sys #remove later … … 32 34 return self.domain 33 35 34 def __subsetDomain(self,time=None,times=None,**kwargs): 35 #takes the domain and returns a subset according to the keyword selection criteria 36 strTimes=times 37 subsetDomain={} 38 totalArraySize=1 39 for key in self.domain.keys(): 40 straxisValues='' 41 if key in kwargs: 42 if key ==time: 43 straxisValues=strTimes 44 arraySize=len(strTimes.split()) 45 elif kwargs[key][0] < kwargs[key][1]: 46 for val in self.domain[key]: 47 if val is not '': 48 if float(val) >= kwargs[key][0]: 49 if float(val) <= kwargs[key] [1]: 50 straxisValues=straxisValues+ str(val) + ',' 51 straxisValues=straxisValues[:-1] 52 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. 53 for val in self.domain[key]: 54 if val is not '': 55 if val >= kwargs[key][0]: 56 straxisValues=straxisValues+ str(val) + ',' 57 for val in self.domain[key]: 58 if val is not '': 59 if val <= kwargs[key] [1]: 60 straxisValues=straxisValues+ str(val) + ',' 61 straxisValues=straxisValues[:-1] 62 else: # this dimension has not been subsetted at all - CHECK THIS 63 for val in self.domain[key]: 64 if val is not '': 65 straxisValues=straxisValues+ str(val) + ',' 66 straxisValues=straxisValues[:-1] 67 68 subsetDomain[key]=straxisValues 69 if key != time: 70 arraySize=len(subsetDomain[key].split(',')) 71 #print subsetDomain[key].split(',') 72 #print key + ' ' + str(arraySize) 73 else: 74 arraySize=len(subsetDomain[key].split()) 75 #print key + ' ' + str(arraySize) 76 totalArraySize = totalArraySize * arraySize 77 #print subsetDomain 78 #print totalArraySize 79 #sys.exit() 80 return subsetDomain, totalArraySize 81 82 36 83 37 def subsetToGridSeries(self, csmlpath=None, ncpath=None,**kwargs): 84 #pathToSubsetCSML = container.csmlpath 85 if ncpath is not None: 86 pathToSubsetNetCDF=ncpath 87 else: 88 pathToSubsetNetCDF='temp.nc' 89 38 #set self.domain: 90 39 self.getDomain() 91 #Now get the data subset:92 40 93 #TODO - incoporate the crsCatalogue. into this 94 #deal with longitude requests 95 #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. 96 kwargs=csmlutils.fixLongitude(self.domain,kwargs) 97 #deal with times 98 #need to find the time axis: 99 cat=csml.csmllibs.csmlcrs.CRSCatalogue() 100 crs=cat.getCRS(self.value.gridSeriesDomain.srsName) 101 #get the position of the time axis in crs/ axis labels 102 timeAxisPos=crs.timeAxis 103 #get the name of the time axis in the crs 104 timeName=crs.axes[timeAxisPos] 105 #get the ordinate with that name and find the original time name for the file. 41 #non-feature specific setup code: 42 pathToSubsetNetCDF, kwargs, timeName, calunits, caltype, times=csml.API.genSubset._genericSubset(self, csmlpath, ncpath, self.domain, kwargs) 43 44 #Find the original time name (timeAxis) for the file. 106 45 for gridOrd in self.value.gridSeriesDomain.coordTransformTable.gridOrdinates: 107 46 if gridOrd.coordAxisLabel.CONTENT==timeName: … … 109 48 else: 110 49 print 'crs not supported' 111 50 112 51 113 #here, timeName is the name given to the time axis by the CRS, 114 #and timeAxis is the name given to the time axis in the file. 115 #currently supporting domain subsetting only by CRS name 116 #(but should be easy to extend later) 117 self.times=kwargs[timeName] 52 53 118 54 self.files=[] 119 55 strTimes='' 120 56 fulldata=[] 57 58 timeToFileRatio = csml.API.genSubset._getTimeToFileRatio(self, self.domain, timeName) 59 121 60 122 #set the reference system for the time axis 123 calset=False 61 62 #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... 63 filesFetched=[] 64 #get data: 65 selection={} 66 67 #Get names of variables in file 68 cat=csml.csmllibs.csmlcrs.CRSCatalogue() 69 crs=cat.getCRS(self.value.gridSeriesDomain.srsName) 70 124 71 for gridOrd in self.value.gridSeriesDomain.coordTransformTable.gridOrdinates: 125 if gridOrd.coordAxisLabel.CONTENT==timeName: 126 try: 127 caltype=gridOrd.coordAxisValues.frame.split(':',1)[0] 128 calunits=gridOrd.coordAxisValues.frame.split(':',1)[1] 129 csml.csmllibs.csmltime.setcdtimeCalendar(caltype) 130 calset=True 131 except:pass 132 if calset!=True: 133 csml.csmllibs.csmltime.setcdtimeCalendar(csml.csmllibs.csmltime.cdtime.DefaultCalendar) 134 try: 135 caltype=self.domain.domainReference.frame.split(':',1)[0] 136 calunits=self.domain.domainReference.frame.split(':',1)[1] 137 csml.csmllibs.csmltime.setcdtimeCalendar(caltype) 138 except: 139 csml.csmllibs.csmltime.setcdtimeCalendar(csml.csmllibs.csmltime.cdtime.DefaultCalendar) 140 141 142 if len(self.times) == 2: 143 #then this is a range of times (t1, tn) 144 try: 145 tone=csml.API.ops_AbstractFeature.__getCDtime(self.times[0]) 146 except: 147 tone=self.times[0] 148 try: 149 ttwo=csml.API.ops_AbstractFeature.__getCDtime(self.times[1]) 150 except: 151 ttwo=self.times[1] 152 self.times=[] 153 154 for time in self.domain[timeName]: 155 timeok=csml.API.ops_AbstractFeature.__compareTimes(tone,time,ttwo) 156 if timeok ==1: 157 self.times.append(time) 72 selection[gridOrd.gridAxesSpanned.CONTENT]=kwargs[gridOrd.coordAxisLabel.CONTENT] 158 73 159 if hasattr(self.value.rangeSet, 'valueArray'): 160 if hasattr(self.value.rangeSet.valueArray.valueComponent.quantityList, '__insertedExtract'): 161 numFiles= len( csmlutils.listify(self.value.rangeSet.valueArray.valueComponent.quantityList.__insertedExtract.components)[0].fileList.fileNames.CONTENT.split()) 162 timeToFileRatio=len(self.domain[timeName])/numFiles 74 75 for time in times: 76 listPosition=self.domain[timeName].index(time) 77 strTimes= strTimes + ' ' + time 78 for comp in csmlutils.listify(self.value.rangeSet.valueArray.valueComponent.quantityList.__insertedExtract.components): 79 filePos=int(float(listPosition)/timeToFileRatio) 80 if filePos in filesFetched: 81 continue #already got data from this file, try next time 82 print selection 83 data=comp.getData(fileposition=filePos, **selection) 84 print data 163 85 164 #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... 165 filesFetched=[] 166 #get data: 167 selection={} 168 169 #Get names of variables in file 170 cat=csml.csmllibs.csmlcrs.CRSCatalogue() 171 crs=cat.getCRS(self.value.gridSeriesDomain.srsName) 172 173 for gridOrd in self.value.gridSeriesDomain.coordTransformTable.gridOrdinates: 174 selection[gridOrd.gridAxesSpanned.CONTENT]=kwargs[gridOrd.coordAxisLabel.CONTENT] 175 176 177 for time in self.times: 178 listPosition=self.domain[timeName].index(time) 179 strTimes= strTimes + ' ' + time 180 for comp in csmlutils.listify(self.value.rangeSet.valueArray.valueComponent.quantityList.__insertedExtract.components): 181 filePos=int(float(listPosition)/timeToFileRatio) 182 if filePos in filesFetched: 183 continue #already got data from this file, try next time 184 print selection 185 186 data=comp.getData(fileposition=filePos, **selection) 187 print data 188 189 190 self.files.append(comp.fileList.fileNames.CONTENT[filePos]) #TODO, get the right file name 191 if fulldata ==[]: 192 fulldata = data.tolist() 193 print fulldata 194 else: 195 for item in data.tolist(): 196 fulldata.append(item) 197 filesFetched.append(filePos) 198 axisorder = data.getAxisIds() #will need later! 86 87 self.files.append(comp.fileList.fileNames.CONTENT[filePos]) #TODO, get the right file name 88 if fulldata ==[]: 89 fulldata = data.tolist() 90 print fulldata 91 else: 92 for item in data.tolist(): 93 fulldata.append(item) 94 filesFetched.append(filePos) 95 axisorder = data.getAxisIds() #will need later! 199 96 200 elif hasattr(self.value.rangeSet, 'datablock'): #not tested201 pass97 #elif hasattr(self.value.rangeSet, 'datablock'): #not tested 98 #pass 202 99 203 100 #Okay, got the data now. Need to write out CSML and NetCDF files... … … 209 106 domain=csml.parser.GridSeriesDomain() 210 107 grid=csml.parser.GridCoordinatesTable() 211 domainSubset, totalArraySize= __subsetDomain(self, time=timeName,times=strTimes,**kwargs)108 domainSubset, totalArraySize=csml.API.genSubset._subsetDomain(timeName,strTimes,self.domain, **kwargs) 212 109 cTT=csml.parser.GridCoordinatesTable() 213 110 ords =[] … … 237 134 nc=csml.csmllibs.netCDFWriter.NCwriter(pathToSubsetNetCDF) 238 135 floatTimes=[] 239 for time in self.times:136 for time in times: 240 137 time=csml.API.ops_AbstractFeature.__getCDtime(time).torel(calunits) 241 138 floatTimes.append(time.value)
Note: See TracChangeset
for help on using the changeset viewer.