Changeset 2224 for TI02-CSML/trunk/csml


Ignore:
Timestamp:
02/03/07 11:38:31 (13 years ago)
Author:
domlowe
Message:

subsetting ProfileSeries? to Profile working more or less

Location:
TI02-CSML/trunk/csml
Files:
7 edited

Legend:

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

    r2207 r2224  
    4545 
    4646##ProfileSeriesFeature methods: 
    47 #addMethods(csml.parser.ProfileSeriesFeature,[ops_ProfileSeriesFeature.testmethod]) 
     47addMethods(csml.parser.ProfileSeriesFeature,[ops_ProfileSeriesFeature.testmethod, ops_ProfileSeriesFeature.getDomain, ops_ProfileSeriesFeature.subsetToProfile,ops_ProfileSeriesFeature._subsetProfileSeries]) 
    4848 
    4949##GridFeature methods: 
  • TI02-CSML/trunk/csml/API/genSubset.py

    r2220 r2224  
    7979    return subsetDomain, totalArraySize 
    8080     
    81 def _setCalendar(feature, timeName): 
     81def _setCalendar(feature, timeName, ords): 
    8282    '''sets the cdtime calendar needed for conversion from CSML to NetCDF''' 
    8383    calset=False 
    84     for gridOrd in feature.value.gridSeriesDomain.coordTransformTable.gridOrdinates: 
     84    for gridOrd in ords: 
    8585        if gridOrd.coordAxisLabel.CONTENT==timeName: 
    8686            try: 
     
    197197    #get the name of the time axis in the coordinate reference system 
    198198    cat=csml.csmllibs.csmlcrs.CRSCatalogue() 
    199     crs=cat.getCRS(feature.value.gridSeriesDomain.srsName) 
     199    if type(feature) == csml.parser.GridSeriesFeature: 
     200        crs=cat.getCRS(feature.value.gridSeriesDomain.srsName) 
     201        gridordinates = feature.value.gridSeriesDomain.coordTransformTable.gridOrdinates 
     202    elif type(feature) == csml.parser.ProfileSeriesFeature: 
     203        crs=cat.getCRS(feature.value.profileSeriesDomain.srsName) 
     204        gridordinates = feature.value.profileSeriesDomain.coordTransformTable.gridOrdinates 
    200205    timeName=crs.axes[crs.timeAxis] 
    201206    #Find the original time name (timeAxis) for the file. 
    202     for gridOrd in feature.value.gridSeriesDomain.coordTransformTable.gridOrdinates: 
     207    for gridOrd in gridordinates: 
    203208        if gridOrd.coordAxisLabel.CONTENT==timeName: 
    204209            timeAxis = gridOrd.gridAxesSpanned.CONTENT  #only works for regular grids atm 
    205210            break 
    206211    #set the reference system for the time axis 
    207     calunits, caltype=_setCalendar(feature, timeName) 
     212    calunits, caltype=_setCalendar(feature, timeName, gridordinates) 
    208213     
    209214    #selection by time - get explcit time values, not just a range mint, maxt 
  • TI02-CSML/trunk/csml/API/ops_ProfileSeriesFeature.py

    r1025 r2224  
    11''' ops_ProfileSeriesFeature  contains operations for ProfileSeriesFeatures''' 
     2import csml 
    23 
    34def testmethod(self): 
    45    print 'testmethod for profileSeries feature' 
    56    return 'testmethod profileseries' 
     7 
     8 
     9def getDomain(self): 
     10    #returns domain as a dictionary of ordinates {name: [values], ...} 
     11    self.domain={} 
     12    self.gridnames={} 
     13    for gridOrd in self.value.profileSeriesDomain.coordTransformTable.gridOrdinates: 
     14        name=gridOrd.coordAxisLabel.CONTENT 
     15        self.gridnames[gridOrd.gridAxesSpanned.CONTENT]=name 
     16        if hasattr(gridOrd.coordAxisValues, '__insertedExtract'): 
     17            self.domain[name], fill, axisorder, units=gridOrd.coordAxisValues.__insertedExtract.getData() 
     18        else: 
     19            vals=gridOrd.coordAxisValues.coordinateList.CONTENT 
     20            valList=[] 
     21            for val in vals.split():   
     22                valList.append(val) 
     23            self.domain[name]=valList 
     24    return self.domain 
     25 
     26def _subsetProfileSeries(self, csmlpath=None, ncpath=None,**kwargs): 
     27    '''this takes a selection from a ProfileSeries object ''' 
     28            
     29    #set self.domain: 
     30    self.getDomain()       
     31     
     32    #if request doesn't match domain points find nearest neighbours 
     33    kwargs=csml.API.genSubset.checkNeighbours(self.domain, self.gridnames, **kwargs) 
     34    #get the CRS from a  the  catalogue 
     35    cat=csml.csmllibs.csmlcrs.CRSCatalogue() 
     36    crs=cat.getCRS(self.value.profileSeriesDomain.srsName)   
     37     
     38    #non-feature specific setup code, mainly handles the time dimension/calendar 
     39    pathToSubsetNetCDF, kwargs, timeAxis, timeName,calunits, caltype, times=csml.API.genSubset.genericSubset(self, csmlpath, ncpath, self.domain, kwargs) 
     40       
     41    ##Get names of variables in file and relate them to the subset selection  
     42    selection={} 
     43    for gridOrd in self.value.profileSeriesDomain.coordTransformTable.gridOrdinates: 
     44        try: 
     45            selection[gridOrd.gridAxesSpanned.CONTENT]=kwargs[gridOrd.coordAxisLabel.CONTENT] 
     46        except KeyError: 
     47            allValues=tuple(self.domain[gridOrd.coordAxisLabel.CONTENT]) 
     48    strTimes, axisorder, units, fulldata, fillvalue =csml.API.genSubset.getTheData(self, selection, times, timeName) 
     49    return pathToSubsetNetCDF, crs, timeName, times, strTimes, calunits, caltype, axisorder,units, fulldata, fillvalue, kwargs 
     50             
     51             
     52def subsetToProfile(self, csmlpath=None, ncpath=None,**kwargs): 
     53    #perform the subset  
     54    pathToSubsetNetCDF, crs, timeName, times, strTimes, calunits, caltype, axisorder,units, fulldata, fillvalue, kwargs=self._subsetProfileSeries(csmlpath, ncpath,**kwargs)  
     55     
     56    #Okay, got the data now. Need to write CSML feature and NetCDF files. 
     57    #Writing out the CSML feature 
     58    # define domain/coverage  to use in 'value' attribute    
     59    newdomain=csml.parser.ProfileSeriesDomain() 
     60    domainSubset, totalArraySize=csml.API.genSubset.subsetDomain(timeName,strTimes,self.domain, **kwargs) 
     61    cTT=csml.API.genSubset.getCoordTransformTable(domainSubset, crs) 
     62    newdomain.coordTransformTable=cTT 
     63    rangeSet=csml.parser.RangeSet() 
     64    rangeSet.arrayDescriptor=csml.parser.NetCDFExtract(id=self.id,fileName=csml.parser.csString(pathToSubsetNetCDF),variableName=csml.parser.csString(self.id),arraySize=csml.parser.csString(totalArraySize)) 
     65     
     66    #gridseries coverage 
     67    cvg=csml.parser.ProfileSeriesCoverage() 
     68    cvg.rangeSet=rangeSet 
     69    cvg.profileSeriesDomain=newdomain     
     70     
     71    #parameter, as before subsetting. 
     72    param = self.parameter 
     73         
     74    #create 'location' attribute: 
     75    loc=self.location  #locataion attribute of profile is same as that of parent profileseries 
     76     
     77    #create 'time' attribute 
     78    tName=crs.axes[crs.timeAxis] 
     79    try: 
     80        t=kwargs[tName] 
     81    except: 
     82        t=csml.parser.csString('unknown') 
     83                
     84    #create a stand alone profile feature containing this coverage 
     85    csmlWrap=csml.csmllibs.csmlfeaturewrap.CSMLWrapper() 
     86    subsettedFeature=csmlWrap.createProfileFeature(value=cvg,parameter=param,location=loc, time=t, featureID=self.id,description=self.description) 
     87  
     88    ### write netcdf using NCWriter class (wraps cdms) ### 
     89    nc=csml.csmllibs.netCDFWriter.NCwriter(pathToSubsetNetCDF) 
     90    ords=cTT.gridOrdinates 
     91    axislist=[] 
     92    for a in axisorder: 
     93        axislist.append(self.gridnames[a]) 
     94    nc.genWriteVar(self.id,ords, times, calunits, caltype, axislist, units, fulldata, fillvalue) 
     95    nc.closeFinishedFile() 
     96    print 'NetCDF file written to %s'%pathToSubsetNetCDF 
     97    return subsettedFeature, pathToSubsetNetCDF 
  • TI02-CSML/trunk/csml/csmllibs/csmlfeaturewrap.py

    r2211 r2224  
    2828        return feature 
    2929 
     30    def createProfileFeature(self,value=None, parameter=None, location=None, time=None, featureID=None,description=None): 
     31        feature=csml.parser.ProfileFeature() 
     32        feature=self._createAbstractFeature(feature, value, parameter,featureID,description) 
     33        #feature.location =location 
     34        return feature 
     35 
    3036    def createGridSeriesFeature(self,value, parameter=None,featureID=None,description=None): 
    3137        feature=csml.parser.GridSeriesFeature() 
  • TI02-CSML/trunk/csml/parser_extra.py

    r2112 r2224  
    137137        for feature in csml.csmllibs.csmlextra.listify(self.dataset.featureCollection.featureMembers): 
    138138            cvg = feature.value 
    139             for att in ['gridSeriesDomain', 'pointDomain', 'blah blah']: 
     139            for att in ['gridSeriesDomain', 'pointDomain', 'profileSeriesDomain','blah blah']: 
    140140                if hasattr(cvg, att): 
    141141                    domain=getattr(cvg,att) 
  • TI02-CSML/trunk/csml/testfiles/apicalls.py

    r2216 r2224  
    2222print '\n Here are all the features in %s:' %f 
    2323print flist 
     24for feature in  flist: 
     25    print feature 
     26    print flist.index(feature) 
    2427 
    2528#select a feature by name (gml:id) 
    26 print '\n Selecting feature with gml:id = %s' %flist[4] 
    27 feature=csmldoc.getFeature(flist[4]) 
     29print '\n Selecting feature with gml:id = %s' %flist[47] 
     30feature=csmldoc.getFeature(flist[47]) 
    2831 
    2932#feature 'bestname' 
     
    4346#define a selection (based on the domain) 
    4447subsetDictionary={} 
    45 subsetDictionary['t']=('2794-12-1T0:0:0.0','2795-12-1T0:0:0.0') 
    46 #subsetDictionary['t']=() 
    47 subsetDictionary['latitude']=(-90,60) 
    48 subsetDictionary['longitude']=(-31.265,30.258) 
     48#subsetDictionary['t']=('2794-12-1T0:0:0.0','2795-12-1T0:0:0.0') 
     49##subsetDictionary['t']=() 
     50#subsetDictionary['latitude']=(-90,60) 
     51#subsetDictionary['longitude']=(-31.265,30.258) 
    4952 
    5053#request subsetted data from features (can set paths here) and add to container 
    5154#subset a feature 
    52 csmlContainer.add(feature.subsetToGridSeries(ncpath='myfile.nc',**subsetDictionary)) 
     55#csmlContainer.add(feature.subsetToGridSeries(ncpath='myfile.nc',**subsetDictionary)) 
    5356#subset another feature (in fact it's the same feature again, but this time subsetting to a profileseries) 
    54 subsetDictionary['t']=('2794-12-1T0:0:0.0','2795-12-1T0:0:0.0') 
    55 subsetDictionary['latitude']=(75.) 
    56 subsetDictionary['longitude']=(48.8) 
     57#subsetDictionary['t']=('2794-12-1T0:0:0.0','2795-12-1T0:0:0.0') 
     58subsetDictionary['latitude']=(75) 
     59subsetDictionary['longitude']=(45) 
    5760csmlContainer.add(feature.subsetToProfileSeries(ncpath='myfile2.nc',**subsetDictionary)) 
    5861 
  • TI02-CSML/trunk/csml/webparser/parseTest.py

    r2218 r2224  
    9898    print "<p>Please note this is experimental, any questions please contact d.lowe@rl.ac.uk</p>" 
    9999    print "<p>Parser version - 28 February 2007 - includes NDG security elements, fixed unicode encoding</p>"     
    100     print "<h2>Load a CSML file totest:</h2>"     
     100    print "<h2>Load a CSML file to test:</h2>"     
    101101    form= cgi.FieldStorage() 
    102102    if not form: 
     
    126126            except: 
    127127                traceback.print_exc() 
    128                 print "<p>Could not parse CSML, problem reading from file</p>" 
     128                print "<p>Could not parse CSML, problem reading from file.</p>" 
    129129                sys.exit() 
    130130            dataset = Dataset() 
     
    135135            except: 
    136136                traceback.print_exc() 
    137                 print "<p>Could not parse CSM, problem reading XML into objects</p>" 
     137                print "<p>Could not parse CSM, problem reading XML into objects.</p>" 
    138138                sys.exit() 
    139139             
     
    147147            except: 
    148148                traceback.print_exc() 
    149                 print "<p>Could not parse CSML, problem writing XML from objects</p>" 
     149                print "<p>Could not parse CSML, problem writing XML from objects.</p>" 
    150150                sys.exit() 
    151151             
Note: See TracChangeset for help on using the changeset viewer.