source: nappy/trunk/naFile2010.py @ 366

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/nappy/trunk/naFile2010.py@3321
Revision 366, 5.6 KB checked in by astephen, 14 years ago (diff)

Latest version - for compatibility with web service. Includes multiple file
output on conversion from NetCDF.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
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"""
6naFile2010.py
7=============
8
9Container module for NAFile2010 class.
10
11"""
12
13# Imports from python standard library
14
15# Imports from local package
16from textParser import *
17import naFile
18import listManipulator
19
20class NAFile2010(naFile.NAFile):
21    """
22    Class to read, write and interact with NASA Ames files conforming to the
23    File Format Index (FFI) 2010.
24    """
25
26    def readHeader(self):
27        """
28        Reads FFI-specifc header section.
29        """       
30        self._normalizedX="no"
31        self._readCommonHeader()
32        self.DX=readItemsFromLine(self.file.readline(), self.NIV, float)
33        self.DX.reverse()  # Reverse because C-type array is least-changing first
34        self.NX=readItemsFromLine(self.file.readline(), self.NIV-1, int)
35        self.NX.reverse()  # Reverse because C-type array is least-changing first
36        self.NXDEF=readItemsFromLine(self.file.readline(), self.NIV-1, int)
37        self.NXDEF.reverse()  # Reverse because C-type array is least-changing first
38        self.X=[]
39        for i in range(self.NIV-1):
40            self.X.append(readItemsFromUnknownLines(self.file, self.NXDEF[i], float))
41        # Unbounded Independent variable should be first so insert empty list at start
42        self.X.reverse()                   # Bug fix - Ag Stephens - 21/06/05
43        self.X.insert(0, [])
44        self.XNAME=readItemsFromLines(self._readLines(self.NIV), self.NIV, str)
45        self.XNAME.reverse()  # Reverse because C-type array is least-changing first
46        self._readVariablesHeaderSection()
47        self._readAuxVariablesHeaderSection()
48        self._readComments()
49
50    def writeHeader(self):
51        """
52        Writes FFI-specifc header section.
53        """       
54        self._writeCommonHeader()
55        self.DX.reverse()
56        self.header.write(("%s "*self.NIV+"\n") % tuple(self.DX))
57        self.NX.reverse()
58        self.header.write(("%s "*(self.NIV-1)+"\n") % tuple(self.NX))
59        self.NXDEF.reverse()
60        self.header.write(("%s "*(self.NIV-1)+"\n") % tuple(self.NXDEF))
61        self.NXDEF.reverse()
62        X_lines=[]
63        for i in range(self.NIV-1):
64            X_lines.append(((self.floatFormat+self.spacer)*self.NXDEF[i]+"\n") % tuple(self.X[i+1][0:self.NXDEF[i]]))
65        X_lines.reverse()
66        for line in X_lines:
67            self.header.write(line.lstrip())
68        self.XNAME.reverse()
69        self.header.write("%s\n"*self.NIV % tuple(self.XNAME))
70        self._writeVariablesHeaderSection()
71        self._writeAuxVariablesHeaderSection()
72        self._writeComments()         
73        lines=self._fixHeaderLength(self.header)
74        self.file.writelines(lines)
75
76    def _setupArrays(self):
77        """
78        Sets up FFI-specific arrays to fill with data (lists of lists).
79        """
80        self.V=[]
81        self.A=[]
82        # Create an array size to request using read routines
83        self.arraySize=1
84        for i in self.NX:
85                self.arraySize=self.arraySize*i
86        for n in range(self.NV):
87            self.V.append([])
88        for a in range(self.NAUXV):
89            self.A.append([])
90           
91    def _readData1(self, datalines, ivar_count):
92        """
93        Reads first line/section of current block of data.
94        """       
95        # Start with independent and Auxilliary vars
96        (x2_and_a, rtlines)=readItemsFromUnknownLines(datalines, 1+self.NAUXV, float)
97        (x, aux)=(x2_and_a[0], x2_and_a[1:])
98        self.X[0].append(x)
99        count=0
100        for a in range(self.NAUXV):
101            self.A[a].append(aux[count])
102            count=count+1
103        return rtlines
104
105    def _readData2(self, datalines, ivar_count):
106        """
107        Reads second line/section (if used) of current block of data.
108        """
109        # Now get the dependent variables
110        for n in range(self.NV):
111            (v, rtlines)=readItemsFromUnknownLines(datalines, self.arraySize, float)
112            self.V[n].append([])
113            listManipulator.recursiveListPopulator(self.V[n][ivar_count], v, self.NX)
114            datalines=rtlines
115        return rtlines
116
117    def writeData(self):
118        """
119        Writes the data section of the file.
120        This method can be called directly by the user.
121        """       
122        # Set up unbounded IV loop
123        self.NX.reverse()
124        for m in range(len(self.X[0])):
125            # Write Independent variable mark and auxiliary variables
126            var_string=(self.floatFormat+self.spacer) % self.X[0][m]
127            for a in range(self.NAUXV):
128                var_string=var_string+((self.floatFormat+self.spacer) % self.A[a][m])
129            self.file.write("%s\n" % var_string.rstrip())
130            # Write Variables
131            for n in range(self.NV):
132                outlines=listManipulator.recursiveListWriter(self.V[n][m], self.NX, spacer=self.spacer, floatFormat=self.floatFormat)
133                for line in outlines:
134                    self.file.write(line)
135
136    def _normalizeIndVars(self):
137        """
138        Normalizes the values in the unbounded independent variable for FFIs
139        that store an abbreviated version of this axis.
140        """
141        for i in range(self.NIV-1):
142            if self.NXDEF[i]==self.NX[i]:
143                pass
144            else:
145                del self.X[i+1][1:]
146                count=0
147                while len(self.X[i+1])<self.NX[i]:
148                    nextx=self.X[i+1][count]+self.DX[i+1]
149                    self.X[i+1].append(nextx)
150                    count=count+1
151        self._normalizedX="yes"
Note: See TracBrowser for help on using the repository browser.