1 | ''' ops_GridSeriesFeature contains operations for GridSeriesFeatures''' |
---|
2 | |
---|
3 | import csml.parser |
---|
4 | import csml.csmllibs.csmltime |
---|
5 | import csml.csmllibs.csmldocument |
---|
6 | import csml.API.ops_AbstractFeature |
---|
7 | import csml.API.genSubset |
---|
8 | import csml.csmllibs.netCDFWriter |
---|
9 | import csmlutils |
---|
10 | |
---|
11 | |
---|
12 | import sys #remove later |
---|
13 | |
---|
14 | def testmethod(self): |
---|
15 | #print 'testmethod for gridseries feature' |
---|
16 | return 'testmethod - gridseries' |
---|
17 | |
---|
18 | def getAllowedSubsettings(self): |
---|
19 | return ['subsetToGridSeries'] #other operations |
---|
20 | |
---|
21 | def 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]=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 | |
---|
36 | |
---|
37 | def subsetToGridSeries(self, csmlpath=None, ncpath=None,**kwargs): |
---|
38 | #set self.domain: |
---|
39 | self.getDomain() |
---|
40 | |
---|
41 | #get the CRS from a the catalogue |
---|
42 | cat=csml.csmllibs.csmlcrs.CRSCatalogue() |
---|
43 | crs=cat.getCRS(self.value.gridSeriesDomain.srsName) |
---|
44 | |
---|
45 | #non-feature specific setup code, mainly handles the time dimension/calendar |
---|
46 | pathToSubsetNetCDF, kwargs, timeName, calunits, caltype, times=csml.API.genSubset.genericSubset(self, csmlpath, ncpath, self.domain, kwargs) |
---|
47 | |
---|
48 | #Find the original time name (timeAxis) for the file. |
---|
49 | for gridOrd in self.value.gridSeriesDomain.coordTransformTable.gridOrdinates: |
---|
50 | if gridOrd.coordAxisLabel.CONTENT==timeName: |
---|
51 | timeAxis = gridOrd.gridAxesSpanned.CONTENT #only works for regular grids atm |
---|
52 | else: |
---|
53 | print 'crs not supported' |
---|
54 | |
---|
55 | ##Get names of variables in file and relate them to the subset selection |
---|
56 | selection={} |
---|
57 | for gridOrd in self.value.gridSeriesDomain.coordTransformTable.gridOrdinates: |
---|
58 | selection[gridOrd.gridAxesSpanned.CONTENT]=kwargs[gridOrd.coordAxisLabel.CONTENT] |
---|
59 | |
---|
60 | |
---|
61 | strTimes, axisorder, fulldata=csml.API.genSubset.getTheData(self, selection, times, timeName) |
---|
62 | |
---|
63 | |
---|
64 | #Okay, got the data now. Need to write CSML feature and NetCDF files. |
---|
65 | #Writing out the CSML feature |
---|
66 | |
---|
67 | # define domain/coverage to use in 'value' attribute |
---|
68 | domain=csml.parser.GridSeriesDomain() |
---|
69 | domainSubset, totalArraySize=csml.API.genSubset.subsetDomain(timeName,strTimes,self.domain, **kwargs) |
---|
70 | cTT=csml.API.genSubset.getCoordTransformTable(domainSubset, crs) |
---|
71 | domain.coordTransformTable=cTT |
---|
72 | rangeSet=csml.parser.RangeSet() |
---|
73 | rangeSet.arrayDescriptor=csml.parser.NetCDFExtract(id=self.id,fileName=csml.parser.csString(pathToSubsetNetCDF),variableName=csml.parser.csString(self.id),arraySize=csml.parser.csString(totalArraySize)) |
---|
74 | |
---|
75 | #gridseries coverage |
---|
76 | cvg=csml.parser.GridSeriesCoverage() |
---|
77 | cvg.rangeSet=rangeSet |
---|
78 | cvg.gridSeriesDomain=domain |
---|
79 | csmlWrap=csml.csmllibs.csmlfeaturewrap.CSMLWrapper() |
---|
80 | |
---|
81 | #create a stand alone gridseries feature containing this coverage |
---|
82 | subsettedFeature=csmlWrap.createGridSeriesFeature(value=cvg,featureID=self.id,description=self.description) |
---|
83 | |
---|
84 | |
---|
85 | ### write netcdf using NCWriter class (wraps cdms) ### |
---|
86 | nc=csml.csmllibs.netCDFWriter.NCwriter(pathToSubsetNetCDF) |
---|
87 | ords=cTT.gridOrdinates |
---|
88 | nc.genWriteVar(self.id,ords, times, calunits, caltype, axisorder, fulldata) |
---|
89 | nc.closeFinishedFile() |
---|
90 | |
---|
91 | return subsettedFeature, pathToSubsetNetCDF |
---|
92 | |
---|