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

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

pointseries subsetting workign in WCS, ragged section part working

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