Changeset 1086 for TI02-CSML/trunk


Ignore:
Timestamp:
02/06/06 16:52:45 (14 years ago)
Author:
domlowe
Message:

time and calendar working in netcdf output. Time subsetting by min/max or explicit values. Ready for attempt at data extractor integration.

Location:
TI02-CSML/trunk/parser
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • TI02-CSML/trunk/parser/API/CSMLDocument.py

    r1041 r1086  
    11''' CSMLDocument.py - simple implementation to return a CSML document containing a single type of feature''' 
     2from API import * 
    23 
    34class CSMLDocument: 
    45    def makeGridSeries(self,domain,rangeSet,datasetID=None,featureID=None): 
    56        fms=[] #empty featureMembers list 
    6         dset=Dataset() 
    7         feat=GridSeriesFeature() 
     7        feat=Parser.GridSeriesFeature() 
    88        if featureID: 
    99            feat.id= featureID 
    1010        else: 
    1111            feat.id='testfeature' 
    12         feat.description=Description('???') 
     12        feat.description=Parser.Description('???') 
    1313        feat.domain=domain 
    1414        feat.rangeSet=rangeSet 
    1515        fms.append(feat) 
    16         fc=FeatureCollection(members=fms) 
     16        fc=Parser.FeatureCollection(members=fms) 
    1717        #Create an Empty Dataset 
    18         ds = Dataset() 
     18        ds = Parser.Dataset() 
    1919        #Set objects as attributes of dataset 
    2020        if datasetID: 
  • TI02-CSML/trunk/parser/API/__init__.py

    r1042 r1086  
    1010except: 
    1111    print 'Could not import CSML data interface. ' 
     12    sys.exit() 
     13     
     14try: 
     15    import csmllibs.csmltime as csmltime 
     16except: 
     17    print 'Could not import csmltime. ' 
    1218    sys.exit() 
    1319 
     
    2733import ops_GridSeriesFeature 
    2834import ops_TrajectoryFeature 
     35import CSMLDocument 
     36 
     37 
     38 
     39 
     40 
    2941 
    3042 
  • TI02-CSML/trunk/parser/API/ops_AbstractFeature.py

    r1028 r1086  
    11''' ops_AbstractFeature  contains operations for AbstractFeatures''' 
    22from API import * 
     3import cdtime 
    34 
    45def testmethod(self): 
     
    89def getAllowedSubsettings(self): 
    910    return ['none'] 
     11 
     12def __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 
     25def __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  
    11''' ops_AbstractFileExtract  contains operations for AbstractFileExtracts''' 
    22from API import * 
     3import sys 
    34 
    45def testmethod(self): 
     
    1112    #file position defines the position of the filename if a list of filenames exists 
    1213    #**kwargs can hold subsetting request. 
    13     print fileposition 
     14    #print fileposition 
     15    #print self.fileName 
    1416     
    1517    #get the right file 
    16     if fileposition: 
     18    if fileposition is not None: 
    1719        file=self.fileName.split()[fileposition] 
    1820    else: 
     
    2527     
    2628    #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() 
    2940    return data 
     41         
     42def __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  
    11''' ops_GridSeriesFeature  contains operations for GridSeriesFeatures''' 
    22from API import * 
     3from CSMLDocument import * 
     4from Scientific.IO.NetCDF import *  #use this instead of cdms for now for it's simple write interface.. 
     5from Numeric import * 
     6 
    37 
    48def testmethod(self): 
     
    4246        domain[key]=dr[key] 
    4347    return domain 
    44      
     48 
     49 
     50         
    4551 
    4652def subsetToGridSeries(self, timeSubset,  **kwargs): 
     
    5157    self.files=[] 
    5258    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) 
    5469    for time in self.times: 
    5570        listPosition=domainref['t'].split().index(time) 
    5671        strTimes= strTimes + ' ' + time  
    5772        for comp in self.rangeSet.aggregatedArray.component: 
    58             #comp should be a file extract 
    59             #var = comp.variableName 
    60             #f=CSMLAnyFile(comp.fileName.split()[listPosition]) 
    61 #TODO - get this bit working properly: 
    6273            data=comp.getData(fileposition=listPosition, **kwargs) 
    6374            self.files.append(comp.fileName.split()[listPosition]) 
    64             if fulldata is None: 
     75            if fulldata ==[]: 
    6576                fulldata = data.tolist() 
    6677            else: 
    6778                for item in data.tolist(): 
    6879                    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() 
    7591    ordinates= [] 
    7692    i=0 
    7793    valueStore=[]  # use the values again later to generate netcdf 
     94    arraySize=0 
     95    totalArraySize=1 
    7896    for key in dc.keys(): 
     97        arraySize=0 
    7998        i=i+1 
    80         god=GridOrdinateDescription() 
     99        god=Parser.GridOrdinateDescription() 
    81100        god.gridAxesSpanned='dim%s'%i 
    82101        god.sequenceRule='+x+y+z' 
     
    87106                if val >= kwargs[key][0]: 
    88107                    if val <= kwargs[key] [1]: 
     108                        arraySize=arraySize+1 
    89109                        straxisValues=straxisValues+ str(val) + ', ' 
    90110        else: # this dimension has not been subsetted 
    91111            for val in dc[key]: 
     112                arraySize=arraySize+1 
    92113                straxisValues=straxisValues+ str(val) + ', ' 
     114        totalArraySize=totalArraySize*arraySize 
    93115        god.axisValues=straxisValues[:-2] 
    94116        ordinates.append(god) 
     117    totalArraySize=totalArraySize*len(self.times) 
    95118    grid.ordinates=ordinates 
    96119    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]) 
    101122 
    102     #### write csml document ##### 
     123    #### write csml document ##### -move this to the csmldocument module? 
     124    subsetCSML=CSMLDocument() 
     125    subsetCSML=subsetCSML.makeGridSeries(domain,rangeSet) 
    103126    output=open(pathToSubsetCSML,'w') 
    104127    output.write(subsetCSML) 
    105128    output.close() 
    106129 
    107     ### create and write netcdf#### 
     130    ### create and write netcdf - uses scientific python#### 
    108131    ncfile=NetCDFFile(pathToSubsetNetCDF,'w') 
    109132    # create the dimensions         
    110     ncfile.createDimension ( 'time', len(timeSubset)) 
     133    ncfile.createDimension ( 'time', len(self.times)) 
    111134    time_var = ncfile.createVariable ( 'time', Float, ('time',) ) 
    112135    time_var.longname = 'time' 
    113136    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) 
    116141    time_var[:] =floatTimes[:] 
     142    time_var.units=calunits 
     143    time_var.calendar=caltype 
    117144 
    118145    for ordinate in ordinates: 
     
    126153        item_var[:]=vals[:] 
    127154        print ordinate.definesAxis 
    128     #this needs rethinking. 
     155    #this needs reconsidering - do the shapes always match up?? 
    129156    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)) 
    131158    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)) 
    133160    print shape(feature_var) 
    134161    print shape(fulldata) 
     
    136163    ncfile.close() 
    137164 
    138     return pathToSubsetCSML, pathToSubsetNetCDF 
     165    return pathToSubsetCSML, pathToSubsetNetCDF, totalArraySize 
  • TI02-CSML/trunk/parser/apicalls.py

    r1042 r1086  
    2121#get the domain of the feature 
    2222print '\n The feature has domain reference:'  
    23 print feature.getDomainReference() 
     23#print feature.getDomainReference() 
    2424 
    2525#get the domain complement of the feature 
     
    2929#get combined domain 
    3030print '\n The feature has domain:'  
    31 print feature.getDomain() 
     31#print feature.getDomain() 
    3232 
    3333#get list of allowed subsettings 
     
    3737 
    3838#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'] 
     40timeSelection=['2881-3-16T0:0:0.0', '2881-9-16T0:0:0.0'] 
    4041spatialSubsetDictionary= {} 
    41 spatialSubsetDictionary['latitude']=(0.,10.0) 
     42spatialSubsetDictionary['latitude']=(0.,30.0) 
    4243spatialSubsetDictionary['longitude']=(90, 120.0) 
    4344 
    4445#request subsetted data from feature 
    45 subsetCSML, subsetNetCDF=feature.subsetToGridSeries(timeSelection, **spatialSubsetDictionary) 
     46subsetCSML, subsetNetCDF, arraySize=feature.subsetToGridSeries(timeSelection, **spatialSubsetDictionary) 
    4647 
    4748print subsetCSML #csml document (string) 
    4849print subsetNetCDF # netcdf file (file) 
     50print 'arraySize: %s' %arraySize 
Note: See TracChangeset for help on using the changeset viewer.