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