source: TI03-DataExtractor/branches/old_stuff/latest_dx/dx/pydxs/MetadataExtractor.py @ 793

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

Put all the old code in the old_stuff branch.

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