source: TI03-DataExtractor/branches/old_stuff/dx-webservice/extractMetadata.py @ 793

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI03-DataExtractor/branches/old_stuff/dx-webservice/extractMetadata.py@793
Revision 793, 3.9 KB checked in by astephen, 13 years ago (diff)

Put all the old code in the old_stuff branch.

  • Property svn:executable set to *
Line 
1"""
2extractMetadata.py
3==================
4
5Contains the base class MetadataExtractor that is used to extract the
6appropriate data out of a metadata/data file to view the variables,
7axes etc. It converts also extracts the equivalent information from
8the request so it can be checked against the dataset metadata.
9
10"""
11
12# Import library modules
13import os, sys
14
15# Import package modules
16from times import *
17from config import *
18from common import *
19from localRules import *
20from requestDict import RequestDict
21
22# Import external modules
23import cdms
24
25class MetadataExtractor:
26    """
27    MetadataExtractor class - used to extract the appropriate data
28    from a metadata/data file to view the variables, axes etc. It
29    converts also extracts the equivalent information from the
30    request so it can be checked against the dataset metadata.
31    """
32   
33    def __init__(self, filenames, request):
34        """
35        __init__ method - requires one or two filenames and a request object
36        and sets up instance objects.
37        """
38        self.files=[]
39        self.request=RequestDict(request)
40        self.variables={}
41        self.user=request["user"]
42             
43        for n in range(1, self._getNumDatasets()+1):
44            self.files.append(cdms.open(filenames[n-1]))
45
46
47    def _getNumDatasets(self):
48        """
49        Method wrapping RequestDict.getNumDatasets method.
50        """
51        return self.request.getNumDatasets()
52
53       
54    def getFileList(self):
55        """
56        Method that returns the list of files associated with a particular
57        request. If more than one variable has been selected then the list
58        contains a concatenation of allVar1Files+allVar2Files.....+allVarNFiles.
59        """
60        ###NEED THE CODE THAT I USED THE GRIB STUFF FOR...
61       
62    def _getValuesInRange(self, start, end, array):
63        """
64        Wrapper method for common.getValuesInRange function.
65        """
66        return getValuesInRange(start, end, array)
67       
68    def _defineTimeDicts(self, dset_num):
69        """
70        Method to define the startTime and endTime dictionaries from the request.
71        """
72        (self.startList, self.endList, self.timeInterval)=self.request.getTemporalDimension(dset_num)
73        return
74       
75    def getTimeSteps(self, dset_num=1):
76        """
77        Method to return a list of tuples for all timesteps requested.
78        """
79        self._defineTimeDicts(dset_num)
80        timeSteps=createList(self.startList, self.endList, self.timeInterval)
81        return timeSteps
82       
83    def getTimeStepCount(self):
84        """
85        Method to return the number of time steps requested.
86        """
87        return len(self.getTimeSteps())
88       
89    def _getNumberOfSpatialPoints(self, dset_num=1):
90        """
91        Method to return the number of spatial points (in 1,2 or 3 dimensions)
92        per time step.
93        """
94
95        npoints=1
96        var_metadata=self.files[dset_num-1][self.request.getVariableID(dset_num)]
97        # Get list of axes
98        axis_list=var_metadata.getAxisList()
99        # Get the horizontal bounds of the request
100        (northernExtent, westernExtent, southernExtent, easternExtent) = self.request.getHorizontalBounds()
101
102        # Work through all axes capturing the number of points within the interval
103        for axis in axis_list:
104
105            if axis.isTime():   # we already have the number of time steps
106                npoints=npoints*self.getTimeStepCount()
107            else:
108                if axis.isLongitude(): 
109                    npoints=npoints*len(getValuesInRange(westernExtent, easternExtent, axis))
110
111                if axis.isLatitude():
112                    npoints=npoints*len(getValuesInRange(southernExtent, northernExtent, axis))
113
114                if axis.isLevel():
115                    if type(levels)!=type([]) and type(levels)!=type((0,)):
116                        templevels=[levels]
117                    else:
118                        templevels=levels
119                    npoints=npoints*len(getValuesInRange(templevels[0], templevels[-1], axis))
120
121        # Now we have the number of points we can multiply that by 4 bytes per float
122        size=4.*npoints
123        return size
124       
125    def getRequestSizeInBytes(self):
126        """
127        Method to get the size of the request in bytes.
128        """
129        return self.getTimeStepCount()*self._getNumberOfSpatialPoints()
130       
Note: See TracBrowser for help on using the repository browser.