source: TI02-CSML/trunk/csml/API/ops_ProfileSeriesFeature.py @ 2224

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI02-CSML/trunk/csml/API/ops_ProfileSeriesFeature.py@2224
Revision 2224, 4.4 KB checked in by domlowe, 14 years ago (diff)

subsetting ProfileSeries? to Profile working more or less

Line 
1''' ops_ProfileSeriesFeature  contains operations for ProfileSeriesFeatures'''
2import csml
3
4def testmethod(self):
5    print 'testmethod for profileSeries feature'
6    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
Note: See TracBrowser for help on using the repository browser.