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

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

Fixed config file DEFAULT to main and changed error in na_core.py.

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