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

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

fixing ID/naming issues and paths

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=None, ncname='pointseries.nc' ,times=None):
38    if outputdir is not None:
39        self.outputdir=outputdir
40    elif csml.API.csmlContainer.globalContainer.OUTPUTDIR is not None:
41        self.outputdir=csml.API.csmlContainer.globalContainer.OUTPUTDIR
42     
43    if self.outputdir is not None:
44        pathToSubsetNetCDF=self.outputdir + '/' +ncname
45    else: 
46        pathToSubsetNetCDF=ncname
47    if self.outputdir is not None:
48        csml.csmllibs.csmlextra.checkDirExists(self.outputdir)
49 
50    if type(times) is str:
51        selection = tuple(csml.csmllibs.csmlextra.listify(times))
52    else:
53        selection = times 
54    tlist=[]   
55    for val in self.value.pointSeriesDomain.timePositionList.CONTENT.split():
56        tlist.append(val)
57   
58    indices=[]
59    if  len(selection) ==2:       
60        selectedTimes=[]
61        for domaintime in tlist:
62            if selection[0] == domaintime:
63                minIndex=tlist.index(domaintime)
64                selectedTimes.append(domaintime)
65            if domaintime > selection[0]:
66                if domaintime < selection[1]:
67                    selectedTimes.append(domaintime)         
68            if selection[1] == domaintime:
69                maxIndex=tlist.index(domaintime)
70                selectedTimes.append(domaintime)         
71                break   
72                   
73    elif len(selection)==1:   #point only
74        for domaintime in tlist:
75            if selection[0] == domaintime:
76               
77                minIndex=tlist.index(domaintime)
78                maxIndex=minIndex
79                selectedTimes=selection
80                break
81   
82   
83   
84    fileList=[]
85    for f in self.value.rangeSet.arrayDescriptor.components.fileList.fileNames.CONTENT.split():
86        fileList.append(f)
87   
88    fextract=self.value.rangeSet.arrayDescriptor.components
89    uom = self.value.rangeSet.arrayDescriptor.uom
90    data, fillvalue=fextract.getDataFromChunks(minIndex, maxIndex)
91   
92    units=[uom.CONTENT]
93   
94    #Now write out the CSML Feature:
95    #create a PointSeriesCoverage
96    cvg=csml.parser.PointSeriesCoverage()
97    cvg.id=csml.csmllibs.csmlextra.getRandomID()
98   
99    #create a TimeSeriesDomain (appropriate domain for a PointSeriesFeature
100    ptsd=csml.parser.TimeSeries()
101    ptsd.id=csml.csmllibs.csmlextra.getRandomID()
102    #create (and populate) a TimePositionList. Using keyword arguements for conciseness.
103    ptsd.timePositionList=csml.parser.TimePositionList(CONTENT=csml.csmllibs.csmlextra.stringify(selectedTimes))
104       
105    sdid=csml.csmllibs.csmlextra.getRandomID() # for the storage descriptor
106    #create a RangeSet
107    rs=csml.parser.RangeSet()
108    va=csml.parser.ValueArray()
109    vc=csml.parser.MeasureOrNullList()
110    vc.href='#%s'%sdid
111    vc.arcrole="http://ndg.nerc.ac.uk/xlinkUsage/insert#QuantityList"
112    vc.role="http://ndg.nerc.ac.uk/fileFormat/csmlStorageDescriptor"
113    vc.show='embed'
114    try:
115        vc.uom=self.value.rangeSet.valueArray.valueComponent.uom
116    except:
117        vc.uom = 'unknown'  #TODO,
118    va.valueComponent=vc
119    va.id=csml.csmllibs.csmlextra.getRandomID()
120    rs.valueArray=va
121   
122   
123    #Add the domain and rangeSet as attributes of the coverage
124    cvg.pointSeriesDomain=ptsd
125    cvg.rangeSet=rs
126   
127    totalArraySize=10
128    descriptor=csml.parser.NetCDFExtract(id=sdid,fileName=csml.parser.csString(ncname),variableName=self.name,arraySize=csml.parser.csString(totalArraySize))
129
130   
131    #the parameter of the feature is of type Phenomenon, here href creates "xlink:href=..."
132    param=self.parameter
133   
134     
135    #create a stand alone pointseries feature containing this coverage
136    csmlWrap=csml.csmllibs.csmlfeaturewrap.CSMLWrapper()
137    subsettedFeature=csmlWrap.createPointSeriesFeature(value=cvg,parameter=param,location=self.location,featureID=self.id,description=self.description)
138   
139     
140   
141    #Now write out the NetCDF File:   
142    nc=cdms.open(pathToSubsetNetCDF,'w')
143    var=MV.array(data)
144    var.id=self.name.CONTENT
145    var.name=self.name.CONTENT
146    var.units=units[0] # hopefully there is just one unit in this list..
147    setattr(var, 'missing_value' ,fillvalue)
148   
149    floatTimes=[]
150   
151    tOne=csml.csmllibs.csmltime.getCDtime(selection[0])
152    tbase=csml.csmllibs.csmltime.getBaseUnits(tOne)
153    for time in selectedTimes:
154        time=csml.csmllibs.csmltime.getCDtime(time).torel(tbase)
155        floatTimes.append(time.value)
156    timeAx=cdms.createAxis(floatTimes)
157    timeAx.designateTime()
158    timeAx.id='time'
159    timeAx.units=tbase 
160    var.setAxis(0,timeAx)     
161    nc.write(var)
162    nc.close()   
163    print 'NetCDF file written to %s'%pathToSubsetNetCDF
164    return subsettedFeature, pathToSubsetNetCDF, descriptor
165   
166   
167def subsetToPoint(self, outputdir=None, ncname='point.nc',time=None):
168    if outputdir is not None:
169        self.outputdir=outputdir
170    elif csml.API.csmlContainer.globalContainer.OUTPUTDIR is not None:
171        self.outputdir=csml.API.csmlContainer.globalContainer.OUTPUTDIR
172    pseriesFeature, pathToSubsetNetCDF, descriptor=self.subsetToPointSeries(self.outputdir, ncname,time)         
173    #now need to take this point series feature containing one time and rewrite it as a point feature.
174    pointFeature=csml.parser.PointFeature()
175    pointFeature.location=pseriesFeature.location
176    pointFeature.parameter=pseriesFeature.parameter
177    pointFeature.description=pseriesFeature.description
178    cvg=csml.parser.PointCoverage()
179    domain=csml.parser.PointDomain()
180    domain.id=csml.csmllibs.csmlextra.getRandomID()
181    domain.pointMember=csml.parser.csString(pseriesFeature.location.CONTENT)
182    #create (and populate) a TimePositionList. Using keyword arguements for conciseness.
183    cvg.pointDomain=domain
184    pointFeature.value=cvg
185    pointFeature.time=csml.parser.csString(time)
186    return pointFeature, pathToSubsetNetCDF, descriptor
Note: See TracBrowser for help on using the repository browser.