Changeset 1086 for TI02-CSML/trunk
- Timestamp:
- 02/06/06 16:52:45 (15 years ago)
- Location:
- TI02-CSML/trunk/parser
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
TI02-CSML/trunk/parser/API/CSMLDocument.py
r1041 r1086 1 1 ''' CSMLDocument.py - simple implementation to return a CSML document containing a single type of feature''' 2 from API import * 2 3 3 4 class CSMLDocument: 4 5 def makeGridSeries(self,domain,rangeSet,datasetID=None,featureID=None): 5 6 fms=[] #empty featureMembers list 6 dset=Dataset() 7 feat=GridSeriesFeature() 7 feat=Parser.GridSeriesFeature() 8 8 if featureID: 9 9 feat.id= featureID 10 10 else: 11 11 feat.id='testfeature' 12 feat.description= Description('???')12 feat.description=Parser.Description('???') 13 13 feat.domain=domain 14 14 feat.rangeSet=rangeSet 15 15 fms.append(feat) 16 fc= FeatureCollection(members=fms)16 fc=Parser.FeatureCollection(members=fms) 17 17 #Create an Empty Dataset 18 ds = Dataset()18 ds = Parser.Dataset() 19 19 #Set objects as attributes of dataset 20 20 if datasetID: -
TI02-CSML/trunk/parser/API/__init__.py
r1042 r1086 10 10 except: 11 11 print 'Could not import CSML data interface. ' 12 sys.exit() 13 14 try: 15 import csmllibs.csmltime as csmltime 16 except: 17 print 'Could not import csmltime. ' 12 18 sys.exit() 13 19 … … 27 33 import ops_GridSeriesFeature 28 34 import ops_TrajectoryFeature 35 import CSMLDocument 36 37 38 39 40 29 41 30 42 -
TI02-CSML/trunk/parser/API/ops_AbstractFeature.py
r1028 r1086 1 1 ''' ops_AbstractFeature contains operations for AbstractFeatures''' 2 2 from API import * 3 import cdtime 3 4 4 5 def testmethod(self): … … 8 9 def getAllowedSubsettings(self): 9 10 return ['none'] 11 12 def __compareTimes(timemin, timecheck, timemax): 13 14 year=int(timecheck.split('-')[0]) 15 month=int(timecheck.split('-')[1]) 16 day=int(timecheck.split('-')[2].split('T')[0]) 17 tcheck=cdtime.comptime(year,month,day) 18 timeok=0 19 if tcheck.cmp(timemin) >= 0: 20 if tcheck.cmp(timemax) < 1: 21 print tcheck 22 timeok = 1 23 return timeok 24 25 def __getCDtime(time): 26 year=int(time.split('-')[0]) 27 month=int(time.split('-')[1]) 28 day=int(time.split('-')[2].split('T')[0]) 29 cdTimeVal=cdtime.comptime(year,month,day) 30 return cdTimeVal -
TI02-CSML/trunk/parser/API/ops_AbstractFileExtract.py
r1042 r1086 1 1 ''' ops_AbstractFileExtract contains operations for AbstractFileExtracts''' 2 2 from API import * 3 import sys 3 4 4 5 def testmethod(self): … … 11 12 #file position defines the position of the filename if a list of filenames exists 12 13 #**kwargs can hold subsetting request. 13 print fileposition 14 #print fileposition 15 #print self.fileName 14 16 15 17 #get the right file 16 if fileposition :18 if fileposition is not None: 17 19 file=self.fileName.split()[fileposition] 18 20 else: … … 25 27 26 28 #get the data 27 DI.setAxis(self.variableName) 28 data=DI.getDataForAxis() 29 print self.variableName 30 31 try: 32 DI.setAxis(self.variableName) 33 data=DI.getDataForAxis() 34 except: 35 DI.setVariable(self.variableName) 36 if kwargs: 37 data = DI.getSubsetOfDataForVar(**kwargs) 38 else: 39 data = DI.getDataForVar() 29 40 return data 41 42 def __calendar(file,timedim): 43 #open the file 44 DI = csmldataiface.DataInterface() 45 DI=DI.getUnknownInterfaceType(file) 46 DI.openFile(file) 47 DI.setAxis(timedim) 48 cal=DI.getAxisAttribute('calendar') 49 units=DI.getAxisAttribute('units') 50 return cal, units 51 -
TI02-CSML/trunk/parser/API/ops_GridSeriesFeature.py
r1042 r1086 1 1 ''' ops_GridSeriesFeature contains operations for GridSeriesFeatures''' 2 2 from API import * 3 from CSMLDocument import * 4 from Scientific.IO.NetCDF import * #use this instead of cdms for now for it's simple write interface.. 5 from Numeric import * 6 3 7 4 8 def testmethod(self): … … 42 46 domain[key]=dr[key] 43 47 return domain 44 48 49 50 45 51 46 52 def subsetToGridSeries(self, timeSubset, **kwargs): … … 51 57 self.files=[] 52 58 strTimes='' 53 fulldata=None 59 fulldata=[] 60 if len(self.times) == 2: 61 tone=ops_AbstractFeature.__getCDtime(self.times[0]) 62 ttwo=ops_AbstractFeature.__getCDtime(self.times[1]) 63 dr=getDomainReference(self) 64 self.times=[] 65 for time in dr['t'].split(): 66 timeok=ops_AbstractFeature.__compareTimes(tone,time,ttwo) 67 if timeok ==1: 68 self.times.append(time) 54 69 for time in self.times: 55 70 listPosition=domainref['t'].split().index(time) 56 71 strTimes= strTimes + ' ' + time 57 72 for comp in self.rangeSet.aggregatedArray.component: 58 #comp should be a file extract59 #var = comp.variableName60 #f=CSMLAnyFile(comp.fileName.split()[listPosition])61 #TODO - get this bit working properly:62 73 data=comp.getData(fileposition=listPosition, **kwargs) 63 74 self.files.append(comp.fileName.split()[listPosition]) 64 if fulldata is None:75 if fulldata ==[]: 65 76 fulldata = data.tolist() 66 77 else: 67 78 for item in data.tolist(): 68 79 fulldata.append(item) 69 70 ### create csml document#### 71 domain=GridSeriesDomain() 72 domain.domainReference=TimePositionList(timePositions=strTimes) 73 grid=Grid() 74 dc = self.__getDomainComplement() 80 #get the calendar type 81 try: 82 caltype, calunits = ops_AbstractFileExtract.__calendar(self.rangeSet.aggregatedArray.component[0].fileName.split()[0], 't') #TODO should accept any time dim!! 83 csmltime.setcdtimeCalendar(caltype) 84 except: 85 caltype=cdtime.DefaultCalendar 86 ### define domain and rangeSet to use for feature in csml document#### 87 domain=Parser.GridSeriesDomain() 88 domain.domainReference=Parser.TimePositionList(timePositions=strTimes) 89 grid=Parser.Grid() 90 dc = self.getDomainComplement() 75 91 ordinates= [] 76 92 i=0 77 93 valueStore=[] # use the values again later to generate netcdf 94 arraySize=0 95 totalArraySize=1 78 96 for key in dc.keys(): 97 arraySize=0 79 98 i=i+1 80 god= GridOrdinateDescription()99 god=Parser.GridOrdinateDescription() 81 100 god.gridAxesSpanned='dim%s'%i 82 101 god.sequenceRule='+x+y+z' … … 87 106 if val >= kwargs[key][0]: 88 107 if val <= kwargs[key] [1]: 108 arraySize=arraySize+1 89 109 straxisValues=straxisValues+ str(val) + ', ' 90 110 else: # this dimension has not been subsetted 91 111 for val in dc[key]: 112 arraySize=arraySize+1 92 113 straxisValues=straxisValues+ str(val) + ', ' 114 totalArraySize=totalArraySize*arraySize 93 115 god.axisValues=straxisValues[:-2] 94 116 ordinates.append(god) 117 totalArraySize=totalArraySize*len(self.times) 95 118 grid.ordinates=ordinates 96 119 domain.domainComplement=grid 97 rangeSet=RangeSet() 98 rangeSet.arrayDescriptor=NetCDFExtract(fileName='f',variableName='v',arraySize='as') 99 feat=FeatureBuilder() 100 subsetCSML=feat.makeGridSeries(domain,rangeSet) 120 rangeSet=Parser.RangeSet() 121 rangeSet.arrayDescriptor=Parser.NetCDFExtract(id=self.id,fileName='temp.nc',variableName=self.id,arraySize=[arraySize]) 101 122 102 #### write csml document ##### 123 #### write csml document ##### -move this to the csmldocument module? 124 subsetCSML=CSMLDocument() 125 subsetCSML=subsetCSML.makeGridSeries(domain,rangeSet) 103 126 output=open(pathToSubsetCSML,'w') 104 127 output.write(subsetCSML) 105 128 output.close() 106 129 107 ### create and write netcdf ####130 ### create and write netcdf - uses scientific python#### 108 131 ncfile=NetCDFFile(pathToSubsetNetCDF,'w') 109 132 # create the dimensions 110 ncfile.createDimension ( 'time', len( timeSubset))133 ncfile.createDimension ( 'time', len(self.times)) 111 134 time_var = ncfile.createVariable ( 'time', Float, ('time',) ) 112 135 time_var.longname = 'time' 113 136 floatTimes=[] 114 for time in timeSubset: 115 floatTimes.append(5) # NEED TO CONVERT 'back' from string to standard julian day.. . 137 print len(fulldata[0]) 138 for time in self.times: 139 time=ops_AbstractFeature.__getCDtime(time).torel(calunits) 140 floatTimes.append(time.value) 116 141 time_var[:] =floatTimes[:] 142 time_var.units=calunits 143 time_var.calendar=caltype 117 144 118 145 for ordinate in ordinates: … … 126 153 item_var[:]=vals[:] 127 154 print ordinate.definesAxis 128 #this needs re thinking.155 #this needs reconsidering - do the shapes always match up?? 129 156 if len(ordinates)==3: 130 feature_var = ncfile.createVariable (self. parsedFeature.id, Float, ('time',ordinates[1].definesAxis,ordinates[0].definesAxis,ordinates[2].definesAxis))157 feature_var = ncfile.createVariable (self.id, Float, ('time',ordinates[0].definesAxis,ordinates[1].definesAxis,ordinates[2].definesAxis)) 131 158 elif len(grid.ordinates)==2: 132 feature_var = ncfile.createVariable (self. parsedFeature.id, Float, ('time',ordinates[1].definesAxis,ordinates[0].definesAxis))159 feature_var = ncfile.createVariable (self.id, Float, ('time',ordinates[0].definesAxis,ordinates[1].definesAxis)) 133 160 print shape(feature_var) 134 161 print shape(fulldata) … … 136 163 ncfile.close() 137 164 138 return pathToSubsetCSML, pathToSubsetNetCDF 165 return pathToSubsetCSML, pathToSubsetNetCDF, totalArraySize -
TI02-CSML/trunk/parser/apicalls.py
r1042 r1086 21 21 #get the domain of the feature 22 22 print '\n The feature has domain reference:' 23 print feature.getDomainReference()23 #print feature.getDomainReference() 24 24 25 25 #get the domain complement of the feature … … 29 29 #get combined domain 30 30 print '\n The feature has domain:' 31 print feature.getDomain()31 #print feature.getDomain() 32 32 33 33 #get list of allowed subsettings … … 37 37 38 38 #define a selection (based on the domain ref/complement) 39 timeSelection=['2778-12-14T00:00:00', '2779-12-09T00:00:00', '2780-12-03T00:00:00', '2781-11-28T00:00:00', '2782-11-23T00:00:00' ,'2783-11-18T00:00:00'] 39 #timeSelection=['2881-3-16T0:0:0.0', '2881-4-16T0:0:0.0', '2881-5-16T0:0:0.0', '2881-6-16T0:0:0.0', '2881-7-16T0:0:0.0', '2881-8-16T0:0:0.0', '2881-9-16T0:0:0.0'] 40 timeSelection=['2881-3-16T0:0:0.0', '2881-9-16T0:0:0.0'] 40 41 spatialSubsetDictionary= {} 41 spatialSubsetDictionary['latitude']=(0., 10.0)42 spatialSubsetDictionary['latitude']=(0.,30.0) 42 43 spatialSubsetDictionary['longitude']=(90, 120.0) 43 44 44 45 #request subsetted data from feature 45 subsetCSML, subsetNetCDF =feature.subsetToGridSeries(timeSelection, **spatialSubsetDictionary)46 subsetCSML, subsetNetCDF, arraySize=feature.subsetToGridSeries(timeSelection, **spatialSubsetDictionary) 46 47 47 48 print subsetCSML #csml document (string) 48 49 print subsetNetCDF # netcdf file (file) 50 print 'arraySize: %s' %arraySize
Note: See TracChangeset
for help on using the changeset viewer.