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

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

workaround for masked array bug

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    ### write netcdf using NCWriter class (wraps cdms) ###
100    nc=csml.csmllibs.netCDFWriter.NCwriter(pathToSubsetNetCDF)
101    ords=cTT.gridOrdinates
102    axislist=[]
103    for a in axisorder:
104        axislist.append(self.gridnames[a])
105    nc.genWriteVar(self.id,ords, times, calunits, caltype, axislist, units, fulldata, fillvalue)
106    nc.closeFinishedFile()
107    print 'NetCDF file written to %s'%pathToSubsetNetCDF
108    return subsettedFeature, pathToSubsetNetCDF
109   
110
111def subsetToProfileSeries(self, csmlpath=None, ncpath=None,**kwargs):
112    #TODO   !!!!!!!!! Need to perform some sort of testing on the kwargs to check it is a profileseries request.
113   
114   
115    #perform the subset (note this included nearest neighbour searching, so may return a different set of kwargs
116    pathToSubsetNetCDF, crs, timeName, times, strTimes, calunits, caltype, axisorder,units, fulldata, fillvalue, kwargs=self._subsetGrid(csmlpath, ncpath,**kwargs) 
117   
118    #Okay, got the data now. Need to write CSML feature and NetCDF files.
119    #Writing out the CSML feature
120   
121    # define domain/coverage  to use in 'value' attribute   
122    newdomain=csml.parser.ProfileSeriesDomain()
123    domainSubset, totalArraySize=csml.API.genSubset.subsetDomain(timeName,strTimes,self.domain, **kwargs)
124    cTT=csml.API.genSubset.getCoordTransformTable(domainSubset, crs)
125    newdomain.coordTransformTable=cTT
126    rangeSet=csml.parser.RangeSet()
127    rangeSet.arrayDescriptor=csml.parser.NetCDFExtract(id=self.id,fileName=csml.parser.csString(pathToSubsetNetCDF),variableName=csml.parser.csString(self.id),arraySize=csml.parser.csString(totalArraySize))
128   
129    #gridseries coverage
130    cvg=csml.parser.ProfileSeriesCoverage()
131    cvg.rangeSet=rangeSet
132    cvg.profileSeriesDomain=newdomain   
133    csmlWrap=csml.csmllibs.csmlfeaturewrap.CSMLWrapper()
134   
135    #parameter, as before subsetting.
136    param = self.parameter
137   
138    #create 'location' attribute:
139    latName=crs.axes[crs.latAxis]
140    lonName=crs.axes[crs.lonAxis]
141    if (latName, lonName) is not (None, None):
142        locStr ='%s %s'%(kwargs[latName], kwargs[lonName])
143        loc=csml.parser.csString(locStr)
144    else:
145        loc = csml.parser.csString('unknown location')
146       
147   
148    #create a stand alone gridseries feature containing this coverage
149    subsettedFeature=csmlWrap.createProfileSeriesFeature(value=cvg,parameter=param, location=loc, featureID=self.id,description=self.description)
150 
151   
152    ### write netcdf using NCWriter class (wraps cdms) ###
153    nc=csml.csmllibs.netCDFWriter.NCwriter(pathToSubsetNetCDF)
154    ords=cTT.gridOrdinates
155    axislist=[]
156    for a in axisorder:
157        axislist.append(self.gridnames[a])
158    nc.genWriteVar(self.id,ords, times, calunits, caltype, axislist, units, fulldata, fillvalue)
159    nc.closeFinishedFile()
160   
161    return subsettedFeature, pathToSubsetNetCDF
162   
163   
Note: See TracBrowser for help on using the repository browser.