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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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 
Note: See TracChangeset for help on using the changeset viewer.