source: nappy/trunk/nappy/na_file/na_core.py @ 3615

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/nappy/trunk/nappy/na_file/na_core.py@3615
Revision 3615, 7.2 KB checked in by astephen, 11 years ago (diff)

Fixing some bugs and inconsistencies.

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"""
6naCore.py
7=========
8
9Holds the NACore class that holds the None type version
10of all the possible NASA Ames variables described in the Gaines
11and Hipskind (1998)document. It also holds a number of useful methods
12for accessing metadata within the file.
13
14"""
15
16# Imports from standard python library
17import copy
18import re
19
20class NACore:
21    """
22    Abstract class to hold the empty NASA Ames contents and
23    a number of methods to access information in files. This
24    class is sub-classed by all NAFile classes.
25    """
26   
27    var_and_units_pattern = re.compile("^\s*(.*)\((.+?)\)(.*)\s*$")
28    na_dictionary_keys = ("A", "AMISS", "ANAME", "ASCAL", "DATE", "DX",
29                     "FFI", "IVOL", "LENA", "LENX", "MNAME", "NAUXC",
30                     "NAUXV", "NCOM", "NIV", "NLHEAD", "NNCOML",
31                     "NSCOML", "NV", "NVOL", "NVPM", "NX", "NXDEF",
32                     "ONAME", "ORG", "RDATE", "SCOM", "SNAME", "V",
33                     "VMISS", "VNAME", "VSCAL", "X", "XNAME")
34
35    def __init__(self):
36        """
37        Creates an instance variable of every type used in all
38        NA formats. All are set to None until/unless defined.
39        """
40        # Set up attributes for all possible NASA Ames dictionary items
41        for key in NACore.na_dictionary_keys:
42            setattr(self, key, None)
43
44    def getNADict(self):
45        """
46        Returns a dictionary of the contents of a NASA Ames file.
47        """
48        dct = {}
49        for key in NACore.na_dictionary_keys:
50            dct[key] = getattr(self, key)
51
52        self.na_dict={}
53        for i in dct.keys():
54            if dct[i] != None:
55                self.na_dict[i] = dct[i]
56        return self.na_dict
57
58    def setNADict(self, na_dict):
59        """
60        Dynamic setting of the na_dict dictionary.
61        """
62        self.na_dict = copy.deepcopy(na_dict)
63
64    def __getitem__(self, item):
65        """
66        Dictionary item access to NASA Ames contents, called NAFileObj['NIV']
67        will return NAFileObj.NIV.
68       
69        Note: In future this might return whatever user wants and to translate
70        NASA Ames variables such as 'NIV' to explanatory strings
71        such as 'number_of_independent_variables'. Need a map for
72        this defined at top of the nasaAmesData.py module
73        """
74        if hasattr(self, item):
75            return getattr(self, item)
76        else:
77            return "Item '%s' not found." % item
78
79    def _attemptVarAndUnitsMatch(self, item):
80        """
81        If it can match variable name and units from the name it does and returns
82        (var_name, units). Otherwise returns (item, None).
83        """
84        match = NACore.var_and_units_pattern.match(item)
85
86        if match:
87            (v1, units, v2) = match.groups()
88            var_name = v1 + " " + v2
89        else:
90            (var_name, units) = (item, None)   
91 
92        return (var_name.strip(), units)
93
94    def getVariable(self, var_number): 
95        """
96        Returns variable metadata corresponding to the var_number argument in the
97        list of varibles. Tuple of (variable_name, units, missing_value, scale_factor)
98        is returned.
99        """
100        (variable, units) = self._attemptVarAndUnitsMatch(self.VNAME[var_number])
101        miss = self.getMissingValue(var_number)
102        scale = self.getScaleFactor(var_number)
103        return (variable, units, miss, scale)
104
105    def getIndependentVariable(self, ivar_number):
106        """
107        Returns an independent variable name and units in a tuple corresponding to
108        the ivar_number index in the list.
109        """
110        (variable, units) = self._attemptVarAndUnitsMatch(self.XNAME[ivar_number])
111        return (variable, units)
112
113    def getAuxVariable(self, avar_number):       
114        """
115        Returns an auxiliary variable name and units in a tuple corresponding to
116        the ivar_number index in the list.
117        """
118        (variable, units) = self._attemptVarAndUnitsMatch(self.ANAME[avar_number])
119        miss = self.getAuxMissingValue(avar_number)
120        scale = self.getAuxScaleFactor(avar_number)
121        return (variable, units, miss, scale)   
122
123    def getVariables(self):
124        """
125        Returns metadata for all main (non-auxiliary or independent) variables.
126        """
127        vars=[]
128        for i in range(self.NV):
129            vars.append(self.getVariable(i))
130        return vars
131
132    def getIndependentVariables(self):
133        """
134        Returns metadata for all independent variables.
135        """
136        ivars=[]
137        for i in range(self.NIV):
138            ivars.append(self.getIndependentVariable(i))
139        return ivars
140
141    def getAuxVariables(self):
142        """
143        Returns metadata for all auxiliary variables.
144        """
145        avars = []
146        if not hasattr(self, "NAUXV"):
147            for i in range(self.NAUXV):
148                avars.append(self.getAuxVariable(i))
149        else:
150            avars = []
151        return avars
152
153    def getMissingValue(self, var_number):
154        """
155        Returns a missing value for a given variable.
156        """
157        return self.VMISS[var_number]
158
159
160    def getScaleFactor(self, var_number):
161        """
162        Returns a scale factor for a given variable.
163        """
164        return self.VSCAL[var_number]
165       
166    def getAuxMissingValue(self, avar_number):
167        """
168        Returns the missing value of an auxiliary variable.
169        """
170        return self.AMISS[avar_number]
171
172    def getAuxScaleFactor(self, avar_number):
173        """
174        Returns the scale factor of an auxiliary variable.
175        """ 
176        return self.ASCAL[avar_number]
177
178    def getNumHeaderLines(self):
179        """
180        Returns the number of header lines.
181        """
182        return self.NLHEAD
183
184    def getFFI(self):
185        """
186        Returns the File Format Index for the file.
187        """
188        return self.FFI
189
190    def getOriginator(self):
191        """
192        Returns the Originator (ONAME) string.
193        """
194        return self.ONAME
195
196    def getOrganisation(self):
197        """
198        Returns the Organisation (ORG) string.
199        """
200        return self.ORG
201
202    def getOrg(self):
203        """
204        Returns the Organisation (ORG) string.
205        """
206        return self.getOrganisation()
207
208    def getSource(self):
209        """
210        Returns the Source (SOURCE) string.
211        """
212        return self.SNAME
213
214    def getMission(self):
215        """
216        Returns the mission (MNAME) string.     
217        """
218        return self.MNAME
219
220    def getVolumes(self):
221        """
222        Returns the volume numbers (IVOL and NVOL).
223        """
224        return (self.IVOL, self.NVOL)
225
226    def getFileDates(self):
227        """
228        Returns the first valid date in the data (DATE) and the
229        Revision Date (RDATE).
230        """
231        return (self.DATE, self.RDATE)
232
233    def getNormalComments(self):
234        """
235        Returns the Normal Comment (NCOM) lines.
236        """
237        return self.NCOM
238
239    def getSpecialComments(self):
240        """
241        Returns the Special Comments (SCOM) lines.
242        """
243        return self.SCOM
244
Note: See TracBrowser for help on using the repository browser.