source: TI02-CSML/trunk/csml/API/ops_PointSeriesFeature.py @ 2567

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

Fixed problem with relative paths in CSML documents

Line 
1''' ops_PointSeriesFeature  contains operations for PointSeriesFeatures'''
2import csml,sys
3import cdms, MV, MA
4
5def testmethod(self):
6    print 'testmethod for pointSeries feature'
7    return 'testmethod pointSeries'
8
9def getAllowedSubsettings(self):
10    return ['subsetToPointSeries']
11   
12def getDomain(self):
13    #returns domain as a dictionary of ordinates {name: [values], ...}
14    self.domain={}
15    self.gridnames={}
16    for gridOrd in self.value.pointSeriesDomain.coordTransformTable.gridOrdinates:
17        name=gridOrd.coordAxisLabel.CONTENT
18        self.gridnames[gridOrd.gridAxesSpanned.CONTENT]=name
19        if hasattr(gridOrd.coordAxisValues, 'insertedExtract'):
20            self.domain[name], fill, axisorder, units=gridOrd.coordAxisValues.insertedExtract.getData()
21        else:           
22            valList=[]
23            try:
24                vals=gridOrd.coordAxisValues.coordinateList.CONTENT
25                for val in vals.split(): 
26                    valList.append(eval(val))
27            except:
28                vals=gridOrd.coordAxisValues.timePositionList.CONTENT
29                for val in vals.split(): 
30                    valList.append(val)           
31            self.domain[name]=valList   
32    return self.domain   
33
34
35
36   
37def subsetToPointSeries(self, outputdir, ncname='pointseries.nc' ,times=None):
38    pathToSubsetNetCDF=outputdir + '/' +ncname
39    csml.csmllibs.csmlextra.checkDirExists(outputdir)
40    if type(times) is str:
41        selection = tuple(csml.csmllibs.csmlextra.listify(times))
42    else:
43        selection = times 
44    tlist=[]   
45    for val in self.value.pointSeriesDomain.timePositionList.CONTENT.split():
46        tlist.append(val)
47   
48    indices=[]
49    if  len(selection) ==2:       
50        selectedTimes=[]
51        for domaintime in tlist:
52            if selection[0] == domaintime:
53                minIndex=tlist.index(domaintime)
54                selectedTimes.append(domaintime)
55            if domaintime > selection[0]:
56                if domaintime < selection[1]:
57                    selectedTimes.append(domaintime)         
58            if selection[1] == domaintime:
59                maxIndex=tlist.index(domaintime)
60                selectedTimes.append(domaintime)         
61                break   
62                   
63    elif len(selection)==1:   #point only
64        for domaintime in tlist:
65            if selection[0] == domaintime:
66               
67                minIndex=tlist.index(domaintime)
68                maxIndex=minIndex
69                selectedTimes=selection
70                break
71   
72   
73   
74    fileList=[]
75    for f in self.value.rangeSet.arrayDescriptor.components.fileList.fileNames.CONTENT.split():
76        fileList.append(f)
77   
78    fextract=self.value.rangeSet.arrayDescriptor.components
79    uom = self.value.rangeSet.arrayDescriptor.uom
80    data, fillvalue=fextract.getDataFromChunks(minIndex, maxIndex)
81   
82    units=[uom.CONTENT]
83   
84    #Now write out the CSML Feature:
85    #create a PointSeriesCoverage
86    cvg=csml.parser.PointSeriesCoverage()
87    cvg.id=csml.csmllibs.csmlextra.getRandomID()
88   
89    #create a TimeSeriesDomain (appropriate domain for a PointSeriesFeature
90    ptsd=csml.parser.TimeSeries()
91    ptsd.id=csml.csmllibs.csmlextra.getRandomID()
92    #create (and populate) a TimePositionList. Using keyword arguements for conciseness.
93    ptsd.timePositionList=csml.parser.TimePositionList(CONTENT=csml.csmllibs.csmlextra.stringify(selectedTimes))
94       
95    sdid=csml.csmllibs.csmlextra.getRandomID() # for the storage descriptor
96    #create a RangeSet
97    rs=csml.parser.RangeSet()
98    va=csml.parser.ValueArray()
99    vc=csml.parser.MeasureOrNullList()
100    vc.href='#%s'%sdid
101    vc.arcrole="http://ndg.nerc.ac.uk/xlinkUsage/insert#QuantityList"
102    vc.role="http://ndg.nerc.ac.uk/fileFormat/csmlStorageDescriptor"
103    vc.show='embed'
104    try:
105        vc.uom=self.value.rangeSet.valueArray.valueComponent.uom
106    except:
107        vc.uom = 'unknown'  #TODO,
108    va.valueComponent=vc
109    va.id=csml.csmllibs.csmlextra.getRandomID()
110    rs.valueArray=va
111   
112   
113    #Add the domain and rangeSet as attributes of the coverage
114    cvg.pointSeriesDomain=ptsd
115    cvg.rangeSet=rs
116   
117    totalArraySize=10
118    descriptor=csml.parser.NetCDFExtract(id=sdid,fileName=csml.parser.csString(ncname),variableName=self.name,arraySize=csml.parser.csString(totalArraySize))
119
120   
121    #the parameter of the feature is of type Phenomenon, here href creates "xlink:href=..."
122    param=self.parameter
123   
124     
125    #create a stand alone pointseries feature containing this coverage
126    csmlWrap=csml.csmllibs.csmlfeaturewrap.CSMLWrapper()
127    subsettedFeature=csmlWrap.createPointSeriesFeature(value=cvg,parameter=param,location=self.location,featureID=self.id,description=self.description)
128   
129     
130   
131    #Now write out the NetCDF File:   
132    nc=cdms.open(pathToSubsetNetCDF,'w')
133    var=MV.array(data)
134    var.id=self.name.CONTENT
135    var.name=self.name.CONTENT
136    var.units=units[0] # hopefully there is just one unit in this list..
137    setattr(var, 'missing_value' ,fillvalue)
138   
139    floatTimes=[]
140   
141    tOne=csml.csmllibs.csmltime.getCDtime(selection[0])
142    tbase=csml.csmllibs.csmltime.getBaseUnits(tOne)
143    for time in selectedTimes:
144        time=csml.csmllibs.csmltime.getCDtime(time).torel(tbase)
145        floatTimes.append(time.value)
146    timeAx=cdms.createAxis(floatTimes)
147    timeAx.designateTime()
148    timeAx.id='time'
149    timeAx.units=tbase 
150    var.setAxis(0,timeAx)     
151    nc.write(var)
152    nc.close()   
153    print 'NetCDF file written to %s'%pathToSubsetNetCDF
154    return subsettedFeature, pathToSubsetNetCDF, descriptor
155   
156   
157def subsetToPoint(self, outputdir, ncname='point.nc',time=None):
158    pseriesFeature, pathToSubsetNetCDF, descriptor=self.subsetToPointSeries(outputdir, ncname,time)         
159    #now need to take this point series feature containing one time and rewrite it as a point feature.
160    pointFeature=csml.parser.PointFeature()
161    pointFeature.location=pseriesFeature.location
162    pointFeature.parameter=pseriesFeature.parameter
163    pointFeature.description=pseriesFeature.description
164    cvg=csml.parser.PointCoverage()
165    domain=csml.parser.PointDomain()
166    domain.id=csml.csmllibs.csmlextra.getRandomID()
167    domain.pointMember=csml.parser.csString(pseriesFeature.location.CONTENT)
168    #create (and populate) a TimePositionList. Using keyword arguements for conciseness.
169    cvg.pointDomain=domain
170    pointFeature.value=cvg
171    pointFeature.time=csml.parser.csString(time)
172    return pointFeature, pathToSubsetNetCDF, descriptor
Note: See TracBrowser for help on using the repository browser.