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

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

subsetting grid to profileseries to profile 99 percent 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    #print crs
74    #print timeName
75    #print times
76    #print strTimes
77    #print calunits
78    #print caltype
79    #print axisorder
80    #print units
81    #print fillvalue
82    #print kwargs 
83    #import sys
84   
85   
86    (time, lat, lon)=(crs.axes[crs.timeAxis],crs.axes[crs.latAxis], crs.axes[crs.lonAxis])
87    print axisorder
88    for axis in axisorder:
89        if axis not in [time, lat, lon]:
90            #this should be the 'profile' axis
91            prof=self.domain[axis]
92   
93   
94    #Okay, got the data now. Need to write CSML feature and NetCDF files.
95    #Writing out the CSML feature
96    # define domain/coverage  to use in 'value' attribute   
97   
98    newdomain=csml.parser.csString(prof)
99   
100    rangeSet=csml.parser.RangeSet()
101    #rangeSet.arrayDescriptor=csml.parser.NetCDFExtract(id=self.id,fileName=csml.parser.csString(pathToSubsetNetCDF),variableName=csml.parser.csString(self.id),arraySize=csml.parser.csString(totalArraySize))
102   
103    #gridseries coverage
104    cvg=csml.parser.ProfileSeriesCoverage()
105    cvg.rangeSet=rangeSet
106    cvg.profileSeriesDomain=newdomain   
107   
108    #parameter, as before subsetting.
109    param = self.parameter
110       
111    #create 'location' attribute:
112    loc=self.location  #locataion attribute of profile is same as that of parent profileseries
113   
114    #create 'time' attribute
115    tName=crs.axes[crs.timeAxis]
116    try:
117        t=kwargs[tName]
118    except:
119        t=csml.parser.csString('unknown')
120               
121    #create a stand alone profile feature containing this coverage
122    csmlWrap=csml.csmllibs.csmlfeaturewrap.CSMLWrapper()
123    subsettedFeature=csmlWrap.createProfileFeature(value=cvg,parameter=param,location=loc, time=t, featureID=self.id,description=self.description)
124 
125    ### write netcdf using NCWriter class (wraps cdms) ###
126    #nc=csml.csmllibs.netCDFWriter.NCwriter(pathToSubsetNetCDF)
127    #ords=None
128    #axislist=[]
129    #for a in axisorder:
130        #axislist.append(self.gridnames[a])
131    #nc.genWriteVar(self.id,ords, times, calunits, caltype, axislist, units, fulldata, fillvalue)
132    #nc.closeFinishedFile()
133    print 'NetCDF file written to %s'%pathToSubsetNetCDF
134    return subsettedFeature, pathToSubsetNetCDF
Note: See TracBrowser for help on using the repository browser.