source: nappy/trunk/naFile2010.py @ 343

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

Latest version with new files in test directory.

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