source: TI02-CSML/trunk/csml/csmllibs/csmlcrs.py @ 2196

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI02-CSML/trunk/csml/csmllibs/csmlcrs.py@2196
Revision 2196, 7.2 KB checked in by domlowe, 13 years ago (diff)

ProfileSeriesFeature? mostly working in scanner - location attribute needs finishing

Line 
1import string
2import cdms # just while testing
3
4class CRSystem(object):
5    def __init__(self, srsName, axes):
6        self.srsName=srsName
7        self.axes=axes
8        self.srsDimension=len(self.axes)
9        self.axisLabels=''
10        for axis in self.axes:
11            self.axisLabels=self.axisLabels + axis + ' '
12        self.axisLabels=self.axisLabels[:-1]
13        self.timeAxis=None
14        self.lonAxis=None
15        self.latAxis=None
16
17class CRSCatalogue(object):
18    def __init__(self):
19        # dictionary to hold known CRSystems:
20        self.systems={}
21       
22        # define lon lat pressure time CRS:       
23        crs=CRSystem(srsName='ndg:crs:xypt', axes =['longitude', 'latitude','pressure','time'])
24        crs.lonAxis=0
25        crs.latAxis=1
26        crs.timeAxis=3
27        self.systems['ndg:crs:xypt']=crs
28       
29        # define lon lat height time CRS:       
30        crs=CRSystem(srsName='ndg:crs:xyht', axes =['longitude', 'latitude','height','time'])
31        crs.lonAxis=0
32        crs.latAxis=1
33        crs.timeAxis=3
34        self.systems['ndg:crs:xyht']=crs
35       
36        # define lon lat time CRS:
37        crs=CRSystem(srsName='ndg:crs:xyt', axes =['longitude', 'latitude','time'])
38        self.systems['ndg:crs:xyt']=crs
39        crs.lonAxis=0
40        crs.latAxis=1
41        crs.timeAxis=2
42       
43        # define height time CRS:
44        crs=CRSystem(srsName='ndg:crs:ht', axes =['height', 'time'])
45        self.systems['ndg:crs:ht']=crs
46        crs.lonAxis=None
47        crs.latAxis=None
48        crs.timeAxis=1
49       
50        #define unknown 1D CRS:
51        crs=CRSystem(srsName='ndg:crs:unknown', axes=['unknown'])
52        self.systems['ndg:crs:unknown']=crs
53       
54        #define unknown 3D CRS:
55        crs=CRSystem(srsName='ndg:crs:unknown2d', axes=['unknown','unknown'])
56        self.systems['ndg:crs:unknown2d']=crs
57       
58        #define unknown 3D CRS:
59        crs=CRSystem(srsName='ndg:crs:unknown3d', axes=['unknown','unknown','unknown'])
60        self.systems['ndg:crs:unknown3d']=crs
61       
62        #define unknown 4D CRS:
63        crs=CRSystem(srsName='ndg:crs:unknown4d', axes=['unknown','unknown','unknown','unknown'])
64        self.systems['ndg:crs:unknown4d']=crs
65       
66           
67    def getCRS(self, srsName):
68        #given the name of a CRS e.g. 'ndg:crs:xypt' return the CRSystem object
69        try:
70            return self.systems[srsName]
71        except:
72            return self.systems['ndg:crs:unknown']
73   
74    def determineCRS(self, axes, units):
75        '''given any list of axis names and a list of units for these axes attempt to determine the CRS and return the CRSystem object and an axis order list.
76        e.g passing in: (axes=['t', 'ht', 'latitude', 'longitude'],units=['days since 1991-09-01 00:00:00', 'm', 'degrees_north', 'degrees_east'])
77        returns self.systems['ndg:crs:xypt'], [3,2,1,0]
78        Assumes the units are ordered to correspond with the axes!
79        '''
80        axisorder=[]
81        if len(axes)==2:
82            crs=self.systems['ndg:crs:unknown2d']
83        if len(axes)==3:
84            crs=self.systems['ndg:crs:unknown3d']
85        elif len(axes)==4:
86            crs=self.systems['ndg:crs:unknown4d']
87        else :
88            crs=self.systems['ndg:crs:unknown']
89        crsMap=[]
90        #this can be extended to accomodate more units
91        for axis in axes:
92            print axis
93            unit=units[axes.index(axis)]
94            unittype='unknown'
95            if string.lower(unit) in ['second', 'seconds', 's', 'mins','minute','minutes','hour','hours','h','hr','hrs','day','days']:
96                unittype='time'
97            elif string.lower(unit)[:10] in ['days since', 'seconds si', 'minutes si', 'hours sinc','months sin', 'years sinc']:
98                unittype='time'
99            elif string.lower(unit) in ['mbar', 'pa','level']:
100                unittype='pressure'
101            elif string.lower(unit) in ['m', 'km']:
102                unittype='height'
103            elif string.lower(unit) in ['degrees_north', 'degrees_south']:
104                unittype='latitude'
105            elif string.lower(unit) in ['degrees_east','degrees_west']:
106                unittype='longitude'
107            crsMap.append(unittype)
108       
109        #now try and match up crsMap with known crsystems.       
110        #axisorder stores the order of the supplied axes against the known crs.
111        #so if the known crs is 'Lat' 'Lon' 'Time' and the supplied axes are 'Time' 'Lat' 'Lon' then
112        #axisorder would be [2, 0, 1]         
113        for system in self.systems:
114            if len(self.systems[system].axes) != len(crsMap):
115                #wrong dimensionality, skip this crs
116                continue
117            match=0
118            axisorder={}
119            for ax in self.systems[system].axes:
120                if ax in crsMap:
121                    axisorder[ax]=crsMap.index(ax)
122                else:
123                    match = 1
124                    break
125            if match == 0: 
126                crs=self.systems[system]
127                break
128                       
129        return crs, axisorder
130       
131def main():
132    import sys
133    sys.stdout = open("/home/dom/Desktop/SVN/trunk/csml/testfiles/gridseries/crsout.txt", "w")
134    cat=CRSCatalogue()
135 
136    #test every crs in this netcdf file
137    crsdictionary={}
138    f=cdms.open('/home/dom/Desktop/SVN/trunk/csml/testfiles/gridseries/xaaqda@pxs19c1.nc')
139    for var in f.variables:
140        v=f.variables[var]
141        axes=v.getAxisIds()
142        units =[]
143        for axis in axes:
144            ax=f.getAxis(axis)
145            units.append(ax.attributes['units'])
146        crsdictionary[str(axes)]=units
147    n=2
148    #test each definition in dictionary
149    for key in crsdictionary:
150        n=n+1
151        print '\n TEST %d'%n
152        print '*************************************'
153       #make list from string.
154        axs=[]
155        for item in key.split():
156            axs.append(item)
157        uns=crsdictionary[key]
158        print 'AXES: %s'%axs
159        print 'UNITS: %s'%uns
160        crs, axisorder= cat.determineCRS(axes=axs, units=uns)
161        print 'CRS: %s'%crs.srsName
162        print 'ORDER:  %s'%axisorder
163        #print 'axis order =%s'%axisorder
164        labels=''
165       
166        #print axes
167        # ['t', 'ht', 'latitude_1', 'longitude_1']
168        #print crs.axes
169         # ['Lon', 'Lat', 'Pressure', 'Time']
170        #print axisorder
171        # [3, 2, 1, 0]
172       
173        for axis in axes:
174            labels=labels + axis + ' '
175       
176        #print '\n Pseudo CSML:'
177        #print '<GridSeriesDomain srsName=%s srsDimension=%s axisLabels=%s>'%(crs.srsName, crs.srsDimension,crs.axisLabels)
178        #print '<axisLabels>%s</axisLabels>'%labels
179        #for axis in axes:
180            #print '<GridOrdinate>'
181 
182            #unit=units[axes.index(axis)]
183           # print '     <coordAxisLabel>%s</coordAxisLabel>'%crs.axes[axisorder[axes.index(axis)]]
184          #  print '     <gridAxesSpanned>%s</gridAxesSpanned>'%axis
185         #   print '</GridOrdinate>'
186        #print '</GridSeriesDomain>'
187       
188        print '**************************************'
189   #test getting CRS by name:
190    #print 'getting ndg:crs:xypt by name'
191    #crs=cat.getCRS('ndg:crs:xypt')
192    #print 'returns %s'%crs.srsName
193       
194   
195if __name__=="__main__":
196    main()
Note: See TracBrowser for help on using the repository browser.