Changeset 1042


Ignore:
Timestamp:
26/05/06 17:23:06 (13 years ago)
Author:
domlowe
Message:

subsetToGridSeries part working

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

Legend:

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

    r1040 r1042  
    7070Parser.GridSeriesFeature.getDomainComplement=ops_GridSeriesFeature.getDomainComplement 
    7171Parser.GridSeriesFeature.getDomain=ops_GridSeriesFeature.getDomain 
     72Parser.GridSeriesFeature.subsetToGridSeries=ops_GridSeriesFeature.subsetToGridSeries 
    7273 
    7374#TrajectoryFeature methods: 
  • TI02-CSML/trunk/parser/API/ops_AbstractFileExtract.py

    r1032 r1042  
    77 
    88 
    9 def getData(self): 
     9def getData(self, fileposition=None, **kwargs): 
    1010    #should handle setAxis or setVariable as cdms netcdf model 
     11    #file position defines the position of the filename if a list of filenames exists 
     12    #**kwargs can hold subsetting request. 
     13    print fileposition 
     14     
     15    #get the right file 
     16    if fileposition: 
     17        file=self.fileName.split()[fileposition] 
     18    else: 
     19        file=self.fileName 
     20     
     21    #open the file 
    1122    DI = csmldataiface.DataInterface() 
    12     DI=DI.getUnknownInterfaceType(self.fileName) 
    13     DI.openFile(self.fileName) 
     23    DI=DI.getUnknownInterfaceType(file) 
     24    DI.openFile(file) 
     25     
     26    #get the data 
    1427    DI.setAxis(self.variableName) 
    1528    data=DI.getDataForAxis() 
  • TI02-CSML/trunk/parser/API/ops_GridSeriesFeature.py

    r1040 r1042  
    4242        domain[key]=dr[key] 
    4343    return domain 
     44     
     45 
     46def subsetToGridSeries(self, timeSubset,  **kwargs): 
     47    pathToSubsetCSML = 'temp.xml' 
     48    pathToSubsetNetCDF='temp.nc' 
     49    domainref = getDomainReference(self)  
     50    self.times=timeSubset 
     51    self.files=[] 
     52    strTimes='' 
     53    fulldata=None 
     54    for time in self.times: 
     55        listPosition=domainref['t'].split().index(time) 
     56        strTimes= strTimes + ' ' + time  
     57        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: 
     62            data=comp.getData(fileposition=listPosition, **kwargs) 
     63            self.files.append(comp.fileName.split()[listPosition]) 
     64            if fulldata is None: 
     65                fulldata = data.tolist() 
     66            else: 
     67                for item in data.tolist(): 
     68                    fulldata.append(item) 
     69     
     70    ### create csml document#### 
     71    domain=GridSeriesDomain() 
     72    domain.domainReference=TimePositionList(timePositions=strTimes)  
     73    grid=Grid() 
     74    dc = self.__getDomainComplement() 
     75    ordinates= [] 
     76    i=0 
     77    valueStore=[]  # use the values again later to generate netcdf 
     78    for key in dc.keys(): 
     79        i=i+1 
     80        god=GridOrdinateDescription() 
     81        god.gridAxesSpanned='dim%s'%i 
     82        god.sequenceRule='+x+y+z' 
     83        god.definesAxis=key 
     84        straxisValues='' 
     85        if key in kwargs: 
     86            for val in dc[key]: 
     87                if val >= kwargs[key][0]: 
     88                    if val <= kwargs[key] [1]: 
     89                        straxisValues=straxisValues+ str(val) + ', ' 
     90        else: # this dimension has not been subsetted 
     91            for val in dc[key]: 
     92                straxisValues=straxisValues+ str(val) + ', ' 
     93        god.axisValues=straxisValues[:-2] 
     94        ordinates.append(god) 
     95    grid.ordinates=ordinates 
     96    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) 
     101 
     102    #### write csml document ##### 
     103    output=open(pathToSubsetCSML,'w') 
     104    output.write(subsetCSML) 
     105    output.close() 
     106 
     107    ### create and write netcdf#### 
     108    ncfile=NetCDFFile(pathToSubsetNetCDF,'w') 
     109    # create the dimensions         
     110    ncfile.createDimension ( 'time', len(timeSubset)) 
     111    time_var = ncfile.createVariable ( 'time', Float, ('time',) ) 
     112    time_var.longname = 'time' 
     113    floatTimes=[] 
     114    for time in timeSubset: 
     115        floatTimes.append(5) # NEED TO CONVERT 'back' from string to standard julian day.. . 
     116    time_var[:] =floatTimes[:] 
     117 
     118    for ordinate in ordinates: 
     119        ncfile.createDimension(ordinate.definesAxis, len(ordinate.axisValues.split()))  
     120        item_var = ncfile.createVariable (ordinate.definesAxis, Float, (ordinate.definesAxis,) ) 
     121        #convert to list 
     122        vals=[] 
     123        for val in ordinate.axisValues.split(','): 
     124            vals.append(float(val)) 
     125        ordinate.axisValues=vals 
     126        item_var[:]=vals[:] 
     127        print ordinate.definesAxis 
     128    #this needs rethinking. 
     129    if len(ordinates)==3: 
     130        feature_var = ncfile.createVariable (self.parsedFeature.id, Float, ('time',ordinates[1].definesAxis,ordinates[0].definesAxis,ordinates[2].definesAxis)) 
     131    elif len(grid.ordinates)==2: 
     132        feature_var = ncfile.createVariable (self.parsedFeature.id, Float, ('time',ordinates[1].definesAxis,ordinates[0].definesAxis)) 
     133    print shape(feature_var) 
     134    print shape(fulldata) 
     135    feature_var[:]=fulldata[:] 
     136    ncfile.close() 
     137 
     138    return pathToSubsetCSML, pathToSubsetNetCDF 
  • TI02-CSML/trunk/parser/apicalls.py

    r1040 r1042  
    3030print '\n The feature has domain:'  
    3131print feature.getDomain() 
     32 
     33#get list of allowed subsettings 
     34print '\n the following feature subsetting operations are allowed:' 
     35print feature.getAllowedSubsettings() 
     36 
     37 
     38#define a selection (based on the domain ref/complement) 
     39timeSelection=['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'] 
     40spatialSubsetDictionary= {} 
     41spatialSubsetDictionary['latitude']=(0.,10.0) 
     42spatialSubsetDictionary['longitude']=(90, 120.0) 
     43 
     44#request subsetted data from feature 
     45subsetCSML, subsetNetCDF=feature.subsetToGridSeries(timeSelection, **spatialSubsetDictionary) 
     46 
     47print subsetCSML #csml document (string) 
     48print subsetNetCDF # netcdf file (file) 
Note: See TracChangeset for help on using the changeset viewer.