source: TI02-CSML/trunk/csml/API/ops_ProfileSeriesFeature.py @ 2290

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

subset ProfileSeries to Profile, netcdf working

Line 
1''' ops_ProfileSeriesFeature  contains operations for ProfileSeriesFeatures'''
2import csml
3
4def testmethod(self):
5    print 'testmethod for profileSeries feature'
6    return 'testmethod profileseries'
7
8
9def getDomain(self):
10    #returns domain as a dictionary of ordinates {name: [values], ...}
11    #For ProfileSeries this is a combination of the location attribute and the time ordinate.
12    self.domain={}
13    self.gridnames={}
14    for gridOrd in self.value.profileSeriesDomain.coordTransformTable.gridOrdinates:
15        name=gridOrd.coordAxisLabel.CONTENT
16        self.gridnames[gridOrd.gridAxesSpanned.CONTENT]=name
17        #handle time axis
18        if hasattr(gridOrd.coordAxisValues, 'timePositionList'):  #..it should do..
19            if hasattr(gridOrd.coordAxisValues, '__insertedExtract'):
20                self.domain[name], fill, axisorder, units=gridOrd.timePositionList.__insertedExtract.getData()
21            else:
22                vals=gridOrd.coordAxisValues.timePositionList.CONTENT             
23                valList=[]
24                for val in vals.split(): 
25                    valList.append(val)
26                self.domain[name]=valList
27        #handle other axes , eg pressure levels, but ignore location
28        #TODO should use lonAxis at latAxis from CRS rather than latitude, longitude.
29        if hasattr(gridOrd.coordAxisValues, 'coordinateList'): 
30            if gridOrd.coordAxisLabel not in ['latitude', 'longitude']:
31                if hasattr(gridOrd.coordAxisValues, '__insertedExtract'):
32                    self.domain[name], fill, axisorder, units=gridOrd.coordinateList.__insertedExtract.getData()
33                else:
34                    vals=gridOrd.coordAxisValues.coordinateList.CONTENT             
35                    valList=[]
36                    for val in vals.split(): 
37                        valList.append(val)
38                    self.domain[name]=valList
39    return self.domain
40
41def _subsetProfileSeries(self, csmlpath=None, ncpath=None,**kwargs):
42    '''this takes a temporal selection from a ProfileSeries object  - i.e. returns one or more 'profiles', can be used to make another ProfileSeriesFeature or a Profile feature in the case of a single selection'''
43           
44    #set self.domain:
45    self.getDomain()     
46   
47    #if request doesn't match domain points find nearest neighbours
48    kwargs=csml.API.genSubset.checkNeighbours(self.domain, self.gridnames, **kwargs)
49    #get the CRS from a  the  catalogue
50    cat=csml.csmllibs.csmlcrs.CRSCatalogue()
51    crs=cat.getCRS(self.value.profileSeriesDomain.srsName) 
52   
53    #non-feature specific setup code, mainly handles the time dimension/calendar
54    pathToSubsetNetCDF, kwargs, timeAxis, timeName,calunits, caltype, times=csml.API.genSubset.genericSubset(self, csmlpath, ncpath, self.domain, kwargs)
55     
56    ##Get names of variables in file and relate them to the subset selection
57    selection={}
58   
59   
60    for gridOrd in self.value.profileSeriesDomain.coordTransformTable.gridOrdinates:
61        try:
62            selection[gridOrd.gridAxesSpanned.CONTENT]=kwargs[gridOrd.coordAxisLabel.CONTENT]
63        except KeyError:
64            allValues=tuple(self.domain[gridOrd.coordAxisLabel.CONTENT])
65    strTimes, axisorder, units, fulldata, fillvalue =csml.API.genSubset.getTheData(self, selection, times, timeName)
66    return pathToSubsetNetCDF, crs, timeName, times, strTimes, calunits, caltype, axisorder,units, fulldata, fillvalue, kwargs
67           
68           
69def subsetToProfile(self, csmlpath=None, ncpath=None,**kwargs):
70    #perform the subset
71    pathToSubsetNetCDF, crs, timeName, times, strTimes, calunits, caltype, axisorder,units, fulldata, fillvalue, kwargs=self._subsetProfileSeries(csmlpath, ncpath,**kwargs) 
72   
73    (time, lat, lon)=(crs.axes[crs.timeAxis],crs.axes[crs.latAxis], crs.axes[crs.lonAxis])
74    print axisorder
75    for axis in axisorder:
76        if axis not in [time, lat, lon]:
77            #this should be the 'profile' axis
78            prof=self.domain[axis]
79   
80   
81    #Okay, got the data now. Need to write CSML feature and NetCDF files.
82    #Writing out the CSML feature
83    # define domain/coverage  to use in 'value' attribute   
84   
85    newdomain=csml.parser.csString(prof)
86   
87    rangeSet=csml.parser.RangeSet()
88    #rangeSet.arrayDescriptor=csml.parser.NetCDFExtract(id=self.id,fileName=csml.parser.csString(pathToSubsetNetCDF),variableName=csml.parser.csString(self.id),arraySize=csml.parser.csString(totalArraySize))
89   
90    #gridseries coverage
91    cvg=csml.parser.ProfileSeriesCoverage()
92    cvg.rangeSet=rangeSet
93    cvg.profileSeriesDomain=newdomain   
94   
95    #parameter, as before subsetting.
96    param = self.parameter
97       
98    #create 'location' attribute:
99    loc=self.location  #locataion attribute of profile is same as that of parent profileseries
100   
101    #create 'time' attribute
102    tName=crs.axes[crs.timeAxis]
103    try:
104        t=kwargs[tName]
105    except:
106        t=csml.parser.csString('unknown')
107               
108    #create a stand alone profile feature containing this coverage
109    csmlWrap=csml.csmllibs.csmlfeaturewrap.CSMLWrapper()
110    subsettedFeature=csmlWrap.createProfileFeature(value=cvg,parameter=param,location=loc, time=t, featureID=self.id,description=self.description)
111 
112    ### write netcdf using NCWriter class (wraps cdms) ###
113    nc=csml.csmllibs.netCDFWriter.NCwriter(pathToSubsetNetCDF)
114    axislist=[]
115    for a in axisorder:
116        if self.gridnames.has_key(a):
117            axislist.append(self.gridnames[a])
118        else:
119            axislist.append(a)
120    ords=None
121    nc.genWriteVar(self.id,ords, times, calunits, caltype, axislist, units, fulldata, fillvalue)
122    nc.closeFinishedFile()
123    print 'NetCDF file written to %s'%pathToSubsetNetCDF
124    return subsettedFeature, pathToSubsetNetCDF
Note: See TracBrowser for help on using the repository browser.