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

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

getUom() method added to GridSeriesFeature?

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    for gridOrd in self.value.gridSeriesDomain.coordTransformTable.gridOrdinates:
25        name=gridOrd.coordAxisLabel.CONTENT
26        if hasattr(gridOrd.coordAxisValues, '__insertedExtract'):
27            self.domain[name], fill, axisorder, units=gridOrd.coordAxisValues.__insertedExtract.getData()
28        else:
29            vals=gridOrd.coordAxisValues.coordinateList.CONTENT
30            valList=[]
31            for val in vals.split(): 
32                valList.append(val)
33            self.domain[name]=valList
34    return self.domain
35
36def getUom(self):
37    uom=None
38    #returns the uom of the phenomenon.
39    try: 
40        uom=self.value.rangeSet.valueArray.valueComponent.quantityList.__insertedExtract.uom.CONTENT
41    except AttributeError:
42        uom =None
43    return uom
44       
45   
46def subsetToGridSeries(self, csmlpath=None, ncpath=None,**kwargs):
47       
48    #set self.domain:
49    self.getDomain()     
50   
51    #if request doesn't match domain points find nearest neighbours
52    kwargs=csml.API.genSubset.checkNeighbours(self.domain, **kwargs)
53   
54    #get the CRS from a  the  catalogue
55    cat=csml.csmllibs.csmlcrs.CRSCatalogue()
56    crs=cat.getCRS(self.value.gridSeriesDomain.srsName) 
57   
58    #non-feature specific setup code, mainly handles the time dimension/calendar
59    pathToSubsetNetCDF, kwargs, timeName, calunits, caltype, times=csml.API.genSubset.genericSubset(self, csmlpath, ncpath, self.domain, kwargs)
60     
61    #Find the original time name (timeAxis) for the file.
62    for gridOrd in self.value.gridSeriesDomain.coordTransformTable.gridOrdinates:
63        if gridOrd.coordAxisLabel.CONTENT==timeName:
64            timeAxis = gridOrd.gridAxesSpanned.CONTENT  #only works for regular grids atm
65        else: 
66            print 'crs not supported'
67 
68    ##Get names of variables in file and relate them to the subset selection
69    selection={}
70    for gridOrd in self.value.gridSeriesDomain.coordTransformTable.gridOrdinates:
71        try:
72            selection[gridOrd.gridAxesSpanned.CONTENT]=kwargs[gridOrd.coordAxisLabel.CONTENT]
73        except KeyError:
74            allValues=tuple(self.domain[gridOrd.coordAxisLabel.CONTENT])
75    strTimes, axisorder, units, fulldata, fillvalue =csml.API.genSubset.getTheData(self, selection, times, timeName)
76   
77   
78    #Okay, got the data now. Need to write CSML feature and NetCDF files.
79    #Writing out the CSML feature
80   
81    # define domain/coverage  to use in 'value' attribute   
82    newdomain=csml.parser.GridSeriesDomain()
83    domainSubset, totalArraySize=csml.API.genSubset.subsetDomain(timeName,strTimes,self.domain, **kwargs)
84    cTT=csml.API.genSubset.getCoordTransformTable(domainSubset, crs)
85    newdomain.coordTransformTable=cTT
86    rangeSet=csml.parser.RangeSet()
87    rangeSet.arrayDescriptor=csml.parser.NetCDFExtract(id=self.id,fileName=csml.parser.csString(pathToSubsetNetCDF),variableName=csml.parser.csString(self.id),arraySize=csml.parser.csString(totalArraySize))
88   
89    #gridseries coverage
90    cvg=csml.parser.GridSeriesCoverage()
91    cvg.rangeSet=rangeSet
92    cvg.gridSeriesDomain=newdomain   
93    csmlWrap=csml.csmllibs.csmlfeaturewrap.CSMLWrapper()
94   
95    #create a stand alone gridseries feature containing this coverage
96    subsettedFeature=csmlWrap.createGridSeriesFeature(value=cvg,featureID=self.id,description=self.description)
97 
98   
99    ### write netcdf using NCWriter class (wraps cdms) ###
100    nc=csml.csmllibs.netCDFWriter.NCwriter(pathToSubsetNetCDF)
101    ords=cTT.gridOrdinates
102   
103    nc.genWriteVar(self.id,ords, times, calunits, caltype, axisorder, units, fulldata, fillvalue)
104    nc.closeFinishedFile()
105   
106    return subsettedFeature, pathToSubsetNetCDF
107   
Note: See TracBrowser for help on using the repository browser.