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

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

more on pointseries and qxf

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