- Timestamp:
- 02/02/07 10:56:19 (14 years ago)
- Location:
- TI02-CSML/trunk/csml
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
TI02-CSML/trunk/csml/API/__init__.py
r1932 r2095 33 33 34 34 ##AbstractFeature methods: 35 addMethods(csml.parser.AbstractFeature, [ops_AbstractFeature.testmethod, ops_AbstractFeature.getAllowedSubsettings , ops_AbstractFeature._identifyTimeAxis])35 addMethods(csml.parser.AbstractFeature, [ops_AbstractFeature.testmethod, ops_AbstractFeature.getAllowedSubsettings]) 36 36 37 37 ##PointFeature methods: -
TI02-CSML/trunk/csml/API/csmlutils.py
r2081 r2095 15 15 def fixLongitude(request, kwargs): 16 16 for key in request.keys(): 17 if key == ' Lon': #how do we test if it is longitude properly?17 if key == 'longitude': #how do we test if it is longitude properly? 18 18 for val in request[key]: 19 19 if val < 0: -
TI02-CSML/trunk/csml/API/genSubset.py
r2091 r2095 6 6 7 7 8 def _subsetDomain(timeaxis,times, domain,**kwargs):8 def subsetDomain(timeaxis,times, domain,**kwargs): 9 9 '''takes the domain and returns a subset according to the keyword selection criteria 10 10 time = name of time dimension … … 79 79 times=timeSelection 80 80 81 tone=csml. API.ops_AbstractFeature.__getCDtime(times[0])81 tone=csml.csmllibs.csmltime.getCDtime(times[0]) 82 82 if len(times) == 2: 83 83 #then this is a range of times (t1, tn), and not a list 84 84 try: 85 tone=csml. API.ops_AbstractFeature.__getCDtime(times[0])85 tone=csml.csmllibs.csmltime.getCDtime(times[0]) 86 86 except: 87 87 tone=times[0] 88 88 try: 89 ttwo=csml. API.ops_AbstractFeature.__getCDtime(times[1])89 ttwo=csml.csmllibs.csmltime.getCDtime(times[1]) 90 90 except: 91 91 ttwo=times[1] 92 92 times=[] 93 93 for time in domain[timeName]: 94 timeok=csml. API.ops_AbstractFeature.__compareTimes(tone,time,ttwo)94 timeok=csml.csmllibs.csmltime.compareTimes(tone,time,ttwo) 95 95 if timeok ==1: 96 96 times.append(time) … … 103 103 return len(domain[timeName])/numFiles 104 104 105 def getCoordTransformTable(domainSubset, crs): 106 cTT=csml.parser.GridCoordinatesTable() 107 ords =[] 108 for key in domainSubset.keys(): 109 go=csml.parser.GridOrdinateDescription() 110 go.coordAxisLabel=csml.parser.csString(key) 111 go.gridAxesSpanned=csml.parser.csString(key) 112 go.coordAxisValues = csml.parser.SpatialOrTemporalPositionList() 113 if key==crs.axes[crs.timeAxis]: 114 go.coordAxisValues.timePositionList=csml.parser.csString(domainSubset[key]) #self.domain.key placeholder 115 else: 116 go.coordAxisValues.coordinateList=csml.parser.csString(domainSubset[key]) #self.domain.key placeholder 117 ords.append(go) #go needs a few more properties setting 118 cTT.gridOrdinates=ords 119 return cTT 105 120 106 def _genericSubset(feature, csmlpath, ncpath, domain, kwargs): 121 122 123 def getTheData(feature, selection, times,timeName): 124 domain = feature.domain 125 value=feature.value 126 files=[] 127 strTimes='' 128 fulldata=[] 129 #get the ratio of times to files 130 timeToFileRatio = _getTimeToFileRatio(feature, domain, timeName) 131 132 133 #list 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... 134 filesFetched=[] 135 136 137 for time in times: 138 listPosition=domain[timeName].index(time) 139 strTimes= strTimes + ' ' + time 140 for comp in csmlutils.listify(value.rangeSet.valueArray.valueComponent.quantityList.__insertedExtract.components): 141 filePos=int(float(listPosition)/timeToFileRatio) 142 if filePos in filesFetched: 143 continue #already got data from this file, try next time 144 data=comp.getData(fileposition=filePos, **selection) 145 files.append(comp.fileList.fileNames.CONTENT[filePos]) #TODO, get the right file name 146 if fulldata ==[]: 147 fulldata = data.tolist() 148 else: 149 for item in data.tolist(): 150 fulldata.append(item) 151 filesFetched.append(filePos) 152 axisorder = data.getAxisIds() #will need later! 153 154 return strTimes, axisorder, fulldata 155 156 def genericSubset(feature, csmlpath, ncpath, domain, kwargs): 107 157 if ncpath is not None: 108 158 pathToSubsetNetCDF=ncpath -
TI02-CSML/trunk/csml/API/ops_AbstractFeature.py
r2083 r2095 12 12 return ['none'] 13 13 14 def _identifyTimeAxis(self,listToCheck):15 #given a list of possible axes, find the one which is most probably the time axis.16 for name in listToCheck:17 if string.upper(name) in ['T', 'TIME']:18 tname =name19 return tname20 21 def __compareTimes(timemin, timecheck, timemax):22 timeok=023 try:24 year=int(timecheck.split('-')[0])25 month=int(timecheck.split('-')[1])26 day=int(timecheck.split('-')[2].split('T')[0])27 tcheck=cdtime.comptime(year,month,day)28 timeok=029 if tcheck.cmp(timemin) >= 0:30 if tcheck.cmp(timemax) < 1:31 timeok = 132 print 'TMIN time %s'%timemin33 print 'CHECK time %s'%tcheck34 print 'TMAX time %s'%timemax35 except:36 #if it's not a proper internet time e.g. time = '6' then just compare numerically37 timeok=038 if timecheck >=timemin:39 if timecheck <=timemax:40 timeok = 141 return timeok42 43 def __getCDtime(time):44 year=int(time.split('-')[0])45 month=int(time.split('-')[1])46 day=int(time.split('-')[2].split('T')[0])47 hour=int(time.split('-')[2].split('T')[1].split(':')[0])48 minute=int(time.split('-')[2].split('T')[1].split(':')[1])49 second=float(time.split('-')[2].split('T')[1].split(':')[2])50 cdTimeVal=cdtime.comptime(year,month,day,hour,minute,second)51 return cdTimeVal52 14 53 15 54 16 55 17 18 -
TI02-CSML/trunk/csml/API/ops_GridSeriesFeature.py
r2091 r2095 44 44 45 45 #non-feature specific setup code, mainly handles the time dimension/calendar 46 pathToSubsetNetCDF, kwargs, timeName, calunits, caltype, times=csml.API.genSubset. _genericSubset(self, csmlpath, ncpath, self.domain, kwargs)46 pathToSubsetNetCDF, kwargs, timeName, calunits, caltype, times=csml.API.genSubset.genericSubset(self, csmlpath, ncpath, self.domain, kwargs) 47 47 48 48 #Find the original time name (timeAxis) for the file. … … 54 54 55 55 56 #get the ratio of times to files 57 timeToFileRatio = csml.API.genSubset._getTimeToFileRatio(self, self.domain, timeName) 58 59 60 #list 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... 61 filesFetched=[] 56 62 57 63 58 ##Get names of variables in file and relate them to the subset selection … … 67 62 68 63 69 self.files=[] 70 strTimes='' 71 fulldata=[] 64 strTimes, axisorder, fulldata=csml.API.genSubset.getTheData(self, selection, times, timeName) 72 65 73 66 74 for time in times: 75 listPosition=self.domain[timeName].index(time) 76 strTimes= strTimes + ' ' + time 77 for comp in csmlutils.listify(self.value.rangeSet.valueArray.valueComponent.quantityList.__insertedExtract.components): 78 filePos=int(float(listPosition)/timeToFileRatio) 79 if filePos in filesFetched: 80 continue #already got data from this file, try next time 81 print selection 82 data=comp.getData(fileposition=filePos, **selection) 83 print data 84 85 86 self.files.append(comp.fileList.fileNames.CONTENT[filePos]) #TODO, get the right file name 87 if fulldata ==[]: 88 fulldata = data.tolist() 89 print fulldata 90 else: 91 for item in data.tolist(): 92 fulldata.append(item) 93 filesFetched.append(filePos) 94 axisorder = data.getAxisIds() #will need later! 95 96 #elif hasattr(self.value.rangeSet, 'datablock'): #not tested 97 #pass 67 #Okay, got the data now. Need to write CSML feature and NetCDF files. 68 #Writing out the CSML feature 98 69 99 #Okay, got the data now. Need to write out CSML and NetCDF files...100 101 102 #Writing out the CSML file103 70 # define domain/coverage to use in 'value' attribute 104 105 71 domain=csml.parser.GridSeriesDomain() 106 grid=csml.parser.GridCoordinatesTable() 107 domainSubset, totalArraySize=csml.API.genSubset._subsetDomain(timeName,strTimes,self.domain, **kwargs) 108 cTT=csml.parser.GridCoordinatesTable() 109 ords =[] 110 for key in domainSubset.keys(): 111 go=csml.parser.GridOrdinateDescription() 112 go.coordAxisLabel=csml.parser.csString(key) 113 go.gridAxesSpanned=csml.parser.csString(key) 114 go.coordAxisValues = csml.parser.SpatialOrTemporalPositionList() 115 if key==crs.axes[crs.timeAxis]: 116 go.coordAxisValues.timePositionList=csml.parser.csString(domainSubset[key]) #self.domain.key placeholder 117 else: 118 go.coordAxisValues.coordinateList=csml.parser.csString(domainSubset[key]) #self.domain.key placeholder 119 ords.append(go) #go needs a few more properties setting 120 cTT.gridOrdinates=ords 121 grid=self.value.gridSeriesDomain.coordTransformTable 72 domainSubset, totalArraySize=csml.API.genSubset.subsetDomain(timeName,strTimes,self.domain, **kwargs) 73 cTT=csml.API.genSubset.getCoordTransformTable(domainSubset, crs) 122 74 domain.coordTransformTable=cTT 123 75 rangeSet=csml.parser.RangeSet() 124 76 rangeSet.arrayDescriptor=csml.parser.NetCDFExtract(id=self.id,fileName=csml.parser.csString(pathToSubsetNetCDF),variableName=csml.parser.csString(self.id),arraySize=csml.parser.csString(totalArraySize)) 77 78 #gridseries coverage 125 79 cvg=csml.parser.GridSeriesCoverage() 126 80 cvg.rangeSet=rangeSet 127 81 cvg.gridSeriesDomain=domain 128 82 csmlWrap=csml.csmllibs.csmlfeaturewrap.CSMLWrapper() 83 84 #create a stand alone gridseries feature containing this coverage 129 85 subsettedFeature=csmlWrap.createGridSeriesFeature(value=cvg,datasetID="A",featureID="B",description="C") 130 86 131 87 132 88 ### write netcdf using NCWriter class (wraps cdms) ### 133 89 nc=csml.csmllibs.netCDFWriter.NCwriter(pathToSubsetNetCDF) 134 floatTimes=[] 135 for time in times: 136 time=csml.API.ops_AbstractFeature.__getCDtime(time).torel(calunits) 137 floatTimes.append(time.value) 138 nc.addAxis('time',floatTimes,isTime=1,units=calunits,calendar=caltype) 90 ords=cTT.gridOrdinates 91 nc.genWriteVar(self.id,ords, times, calunits, caltype, axisorder, fulldata) 92 nc.closeFinishedFile() 139 93 140 for ord in ords:141 vals=[]142 lon,lat=None,None143 if ord.coordAxisLabel.CONTENT=='time':144 continue145 else:146 for val in ord.coordAxisValues.coordinateList.CONTENT.split(','):147 if val != ' ':148 vals.append(float(val))149 if ord.coordAxisLabel.CONTENT=='longitude':150 lon=1151 name='longitude'152 elif ord.coordAxisLabel.CONTENT=='latitude':153 lat=1154 name='latitude'155 else:156 name=ord.coordAxisLabel.CONTENT157 158 nc.addAxis(name,vals,isLon=lon,isLat=lat,units='')#to do, units attribute for CF compliance159 if len(ords)==3:160 axes=['time',axisorder[1],axisorder[2]]161 elif len(ords)==2:162 axes=['time',axisorder[1]]163 nc.addVariable(fulldata,self.id, axes,units='') #to do, units attribute for CF compliance164 nc.closeFinishedFile()165 94 return subsettedFeature, pathToSubsetNetCDF 166 95 -
TI02-CSML/trunk/csml/csmllibs/csmltime.py
r2060 r2095 38 38 elif cal =='julian': 39 39 cdtime.DefaultCalendar= cdtime.JulianCalendar 40 41 42 def identifyTimeAxis(self,listToCheck): 43 #given a list of possible axes, find the one which is most probably the time axis. 44 for name in listToCheck: 45 if string.upper(name) in ['T', 'TIME']: 46 tname =name 47 return tname 48 49 def compareTimes(timemin, timecheck, timemax): 50 timeok=0 51 try: 52 year=int(timecheck.split('-')[0]) 53 month=int(timecheck.split('-')[1]) 54 day=int(timecheck.split('-')[2].split('T')[0]) 55 tcheck=cdtime.comptime(year,month,day) 56 timeok=0 57 if tcheck.cmp(timemin) >= 0: 58 if tcheck.cmp(timemax) < 1: 59 timeok = 1 60 print 'TMIN time %s'%timemin 61 print 'CHECK time %s'%tcheck 62 print 'TMAX time %s'%timemax 63 except: 64 #if it's not a proper internet time e.g. time = '6' then just compare numerically 65 timeok=0 66 if timecheck >=timemin: 67 if timecheck <=timemax: 68 timeok = 1 69 return timeok 70 71 def getCDtime(time): 72 year=int(time.split('-')[0]) 73 month=int(time.split('-')[1]) 74 day=int(time.split('-')[2].split('T')[0]) 75 hour=int(time.split('-')[2].split('T')[1].split(':')[0]) 76 minute=int(time.split('-')[2].split('T')[1].split(':')[1]) 77 second=float(time.split('-')[2].split('T')[1].split(':')[2]) 78 cdTimeVal=cdtime.comptime(year,month,day,hour,minute,second) 79 return cdTimeVal 40 80 41 81 def getFileTimeList(filelist, timedim): -
TI02-CSML/trunk/csml/csmllibs/netCDFWriter.py
r2078 r2095 1 1 import cdms,MV 2 import csml 2 3 import sys 4 5 6 7 8 3 9 class NCwriter(object): 4 10 #This provides a simplified wrapper to CDMS to write a CF compliant NetCDF … … 46 52 self.nc.write(dataarray) 47 53 48 54 def genWriteVar(self,varid, ordinates, times, calunits, caltype, axisorder, fulldata): 55 floatTimes=[] 56 for time in times: 57 time=csml.csmllibs.csmltime.getCDtime(time).torel(calunits) 58 floatTimes.append(time.value) 59 self.addAxis('time',floatTimes,isTime=1,units=calunits,calendar=caltype) 60 for ord in ordinates: 61 vals=[] 62 lon,lat=None,None 63 if ord.coordAxisLabel.CONTENT=='time': 64 continue 65 else: 66 for val in ord.coordAxisValues.coordinateList.CONTENT.split(','): 67 if val != ' ': 68 vals.append(float(val)) 69 if ord.coordAxisLabel.CONTENT=='longitude': 70 lon=1 71 name='longitude' 72 elif ord.coordAxisLabel.CONTENT=='latitude': 73 lat=1 74 name='latitude' 75 else: 76 name=ord.coordAxisLabel.CONTENT 77 self.addAxis(name,vals,isLon=lon,isLat=lat,units='')#to do, units attribute for CF compliance 78 if len(ordinates)==3: 79 axes=['time',axisorder[1],axisorder[2]] 80 elif len(ordinates)==2: 81 axes=['time',axisorder[1]] 82 self.addVariable(fulldata,varid, axes,units='') #to do, units attribute for CF compliance 83 84 49 85 def closeFinishedFile(self): 50 86 #returns finished (hopefully) NetCDF file
Note: See TracChangeset
for help on using the changeset viewer.