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

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

nearest neighbour working for times, plus separation of subsetting into core functions and feature specific ones

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    csmlWrap=csml.csmllibs.csmlfeaturewrap.CSMLWrapper()
92   
93    #create a stand alone gridseries feature containing this coverage
94    subsettedFeature=csmlWrap.createGridSeriesFeature(value=cvg,featureID=self.id,description=self.description)
95 
96   
97    ### write netcdf using NCWriter class (wraps cdms) ###
98    nc=csml.csmllibs.netCDFWriter.NCwriter(pathToSubsetNetCDF)
99    ords=cTT.gridOrdinates
100    axislist=[]
101    for a in axisorder:
102        axislist.append(self.gridnames[a])
103    nc.genWriteVar(self.id,ords, times, calunits, caltype, axislist, units, fulldata, fillvalue)
104    nc.closeFinishedFile()
105   
106    return subsettedFeature, pathToSubsetNetCDF
107   
Note: See TracBrowser for help on using the repository browser.