source: TI02-CSML/trunk/csml/API/ops_GridSeriesFeature.py @ 2210

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

parameter added to subsetted feature, plus general tidying and removal of obselete csmlwrapper functions

Line 
1''' ops_GridSeriesFeature  contains operations for GridSeriesFeatures'''
2
3import csml.parser
4import csml.csmllibs.csmltime
5import csml.csmllibs.csmldocument
6import csml.API.ops_AbstractFeature
7import csml.API.genSubset
8import csml.csmllibs.netCDFWriter
9import csmlutils
10
11
12import sys  #remove later
13
14def testmethod(self):
15    #print 'testmethod for gridseries feature'
16    return 'testmethod - gridseries'
17
18def getAllowedSubsettings(self):
19    return ['subsetToGridSeries']  #other operations
20
21def getDomain(self):
22    #returns domain as a dictionary of ordinates {name: [values], ...}
23    self.domain={}
24    self.gridnames={}
25    for gridOrd in self.value.gridSeriesDomain.coordTransformTable.gridOrdinates:
26        name=gridOrd.coordAxisLabel.CONTENT
27        self.gridnames[gridOrd.gridAxesSpanned.CONTENT]=name
28        if hasattr(gridOrd.coordAxisValues, '__insertedExtract'):
29            self.domain[name], fill, axisorder, units=gridOrd.coordAxisValues.__insertedExtract.getData()
30        else:
31            vals=gridOrd.coordAxisValues.coordinateList.CONTENT
32            valList=[]
33            for val in vals.split(): 
34                valList.append(val)
35            self.domain[name]=valList
36    return self.domain
37
38def getUom(self):
39    uom=None
40    #returns the uom of the phenomenon.
41    try: 
42        uom=self.value.rangeSet.valueArray.valueComponent.quantityList.__insertedExtract.uom.CONTENT
43    except AttributeError:
44        uom =None
45    return uom
46
47def _subsetGrid(self, csmlpath=None, ncpath=None,**kwargs):
48    '''this takes a selection from a gridseries object (be it a profile, a grid, whatever, it is still just a selection'''
49           
50    #set self.domain:
51    self.getDomain()     
52   
53    #if request doesn't match domain points find nearest neighbours
54    kwargs=csml.API.genSubset.checkNeighbours(self.domain, self.gridnames, **kwargs)
55    #get the CRS from a  the  catalogue
56    cat=csml.csmllibs.csmlcrs.CRSCatalogue()
57    crs=cat.getCRS(self.value.gridSeriesDomain.srsName) 
58   
59    #non-feature specific setup code, mainly handles the time dimension/calendar
60    pathToSubsetNetCDF, kwargs, timeAxis, timeName,calunits, caltype, times=csml.API.genSubset.genericSubset(self, csmlpath, ncpath, self.domain, kwargs)
61     
62    ##Get names of variables in file and relate them to the subset selection
63    selection={}
64    for gridOrd in self.value.gridSeriesDomain.coordTransformTable.gridOrdinates:
65        try:
66            selection[gridOrd.gridAxesSpanned.CONTENT]=kwargs[gridOrd.coordAxisLabel.CONTENT]
67        except KeyError:
68            allValues=tuple(self.domain[gridOrd.coordAxisLabel.CONTENT])
69    strTimes, axisorder, units, fulldata, fillvalue =csml.API.genSubset.getTheData(self, selection, times, timeName)
70    return pathToSubsetNetCDF, crs, timeName, times, strTimes, calunits, caltype, axisorder,units, fulldata, fillvalue, kwargs
71   
72def subsetToGridSeries(self, csmlpath=None, ncpath=None,**kwargs):
73    #perform the subset (note this included nearest neighbour searching, so may return a different set of kwargs
74    pathToSubsetNetCDF, crs, timeName, times, strTimes, calunits, caltype, axisorder,units, fulldata, fillvalue, kwargs=self._subsetGrid(csmlpath, ncpath,**kwargs) 
75   
76    #Okay, got the data now. Need to write CSML feature and NetCDF files.
77    #Writing out the CSML feature
78   
79    # define domain/coverage  to use in 'value' attribute   
80    newdomain=csml.parser.GridSeriesDomain()
81    domainSubset, totalArraySize=csml.API.genSubset.subsetDomain(timeName,strTimes,self.domain, **kwargs)
82    cTT=csml.API.genSubset.getCoordTransformTable(domainSubset, crs)
83    newdomain.coordTransformTable=cTT
84    rangeSet=csml.parser.RangeSet()
85    rangeSet.arrayDescriptor=csml.parser.NetCDFExtract(id=self.id,fileName=csml.parser.csString(pathToSubsetNetCDF),variableName=csml.parser.csString(self.id),arraySize=csml.parser.csString(totalArraySize))
86   
87    #gridseries coverage
88    cvg=csml.parser.GridSeriesCoverage()
89    cvg.rangeSet=rangeSet
90    cvg.gridSeriesDomain=newdomain   
91   
92    #parameter, as before subsetting.
93    param = self.parameter
94       
95    #create a stand alone gridseries feature containing this coverage
96    csmlWrap=csml.csmllibs.csmlfeaturewrap.CSMLWrapper()
97    subsettedFeature=csmlWrap.createGridSeriesFeature(value=cvg,parameter=param,featureID=self.id,description=self.description)
98 
99   
100    ### write netcdf using NCWriter class (wraps cdms) ###
101    nc=csml.csmllibs.netCDFWriter.NCwriter(pathToSubsetNetCDF)
102    ords=cTT.gridOrdinates
103    axislist=[]
104    for a in axisorder:
105        axislist.append(self.gridnames[a])
106    nc.genWriteVar(self.id,ords, times, calunits, caltype, axislist, units, fulldata, fillvalue)
107    nc.closeFinishedFile()
108   
109    return subsettedFeature, pathToSubsetNetCDF
110   
111
112def subsetToProfileSeries(self, csmlpath=None, ncpath=None,**kwargs):
113    #TODO   !!!!!!!!! Need to perform some sort of testing on the kwargs to check it is a profileseries request.
114   
115   
116    #perform the subset (note this included nearest neighbour searching, so may return a different set of kwargs
117    pathToSubsetNetCDF, crs, timeName, times, strTimes, calunits, caltype, axisorder,units, fulldata, fillvalue, kwargs=self._subsetGrid(csmlpath, ncpath,**kwargs) 
118   
119    #Okay, got the data now. Need to write CSML feature and NetCDF files.
120    #Writing out the CSML feature
121   
122    # define domain/coverage  to use in 'value' attribute   
123    newdomain=csml.parser.ProfileSeriesDomain()
124    domainSubset, totalArraySize=csml.API.genSubset.subsetDomain(timeName,strTimes,self.domain, **kwargs)
125    cTT=csml.API.genSubset.getCoordTransformTable(domainSubset, crs)
126    newdomain.coordTransformTable=cTT
127    rangeSet=csml.parser.RangeSet()
128    rangeSet.arrayDescriptor=csml.parser.NetCDFExtract(id=self.id,fileName=csml.parser.csString(pathToSubsetNetCDF),variableName=csml.parser.csString(self.id),arraySize=csml.parser.csString(totalArraySize))
129   
130    #gridseries coverage
131    cvg=csml.parser.ProfileSeriesCoverage()
132    cvg.rangeSet=rangeSet
133    cvg.profileSeriesDomain=newdomain   
134    csmlWrap=csml.csmllibs.csmlfeaturewrap.CSMLWrapper()
135   
136    #parameter, as before subsetting.
137    param = self.parameter
138   
139    #create a stand alone gridseries feature containing this coverage
140    subsettedFeature=csmlWrap.createProfileSeriesFeature(value=cvg,parameter=param, featureID=self.id,description=self.description)
141 
142   
143    ### write netcdf using NCWriter class (wraps cdms) ###
144    nc=csml.csmllibs.netCDFWriter.NCwriter(pathToSubsetNetCDF)
145    ords=cTT.gridOrdinates
146    axislist=[]
147    for a in axisorder:
148        axislist.append(self.gridnames[a])
149    nc.genWriteVar(self.id,ords, times, calunits, caltype, axislist, units, fulldata, fillvalue)
150    nc.closeFinishedFile()
151   
152    return subsettedFeature, pathToSubsetNetCDF
153   
154   
Note: See TracBrowser for help on using the repository browser.