source: nappy/trunk/naFile2010.py @ 360

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

Bug fixing and write methods

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
RevLine 
[349]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
[343]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.insert(0, [])
43        self.XNAME=readItemsFromLines(self._readLines(self.NIV), self.NIV, str)
44        self.XNAME.reverse()  # Reverse because C-type array is least-changing first
45        self._readVariablesHeaderSection()
46        self._readAuxVariablesHeaderSection()
47        self._readComments()
48
49    def writeHeader(self):
50        """
51        Writes FFI-specifc header section.
52        """       
53        self._writeCommonHeader()
54        self.DX.reverse()
55        self.file.write(("%s "*self.NIV+"\n") % tuple(self.DX))
56        self.NX.reverse()
57        self.file.write(("%s "*(self.NIV-1)+"\n") % tuple(self.NX))
58        self.NXDEF.reverse()
59        self.file.write(("%s "*(self.NIV-1)+"\n") % tuple(self.NXDEF))
60        self.NXDEF.reverse()
[359]61        X_lines=[]
[343]62        for i in range(self.NIV-1):
63            #print (self.floatFormat+self.spacer)*self.NXDEF[i]+"\n", self.NXDEF, self.X[i+1][0:self.NXDEF[i]]
[359]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.file.write(line.lstrip())
[343]68        self.XNAME.reverse()
69        self.file.write("%s\n"*self.NIV % tuple(self.XNAME))
70        self._writeVariablesHeaderSection()
71        self._writeAuxVariablesHeaderSection()
72        self._writeComments()         
73
74    def _setupArrays(self):
75        """
76        Sets up FFI-specific arrays to fill with data (lists of lists).
77        """
78        self.V=[]
79        self.A=[]
80        # Create an array size to request using read routines
[360]81        self.arraySize=1
82        for i in self.NX:
83                self.arraySize=self.arraySize*i
[343]84        for n in range(self.NV):
85            self.V.append([])
86        for a in range(self.NAUXV):
87            self.A.append([])
88           
89    def _readData1(self, datalines, ivar_count):
90        """
91        Reads first line/section of current block of data.
92        """       
93        # Start with independent and Auxilliary vars
94        (x2_and_a, rtlines)=readItemsFromUnknownLines(datalines, 1+self.NAUXV, float)
95        (x, aux)=(x2_and_a[0], x2_and_a[1:])
96        self.X[0].append(x)
97        count=0
98        for a in range(self.NAUXV):
99            self.A[a].append(aux[count])
100            count=count+1
101        return rtlines
102
103    def _readData2(self, datalines, ivar_count):
104        """
105        Reads second line/section (if used) of current block of data.
106        """
[360]107        # Now get the dependent variables
[343]108        for n in range(self.NV):
[360]109            (v, rtlines)=readItemsFromUnknownLines(datalines, self.arraySize, float)
[343]110            self.V[n].append([])
111            listManipulator.recursiveListPopulator(self.V[n][ivar_count], v, self.NX)
[360]112            datalines=rtlines
113        return rtlines
[343]114
115    def writeData(self):
116        """
117        Writes the data section of the file.
118        This method can be called directly by the user.
119        """       
120        # Set up unbounded IV loop
121        self.NX.reverse()
122        for m in range(len(self.X[0])):
123            # Write Independent variable mark and auxiliary variables
124            var_string="%s    " % self.X[0][m]
125            for a in range(self.NAUXV):
126                var_string=var_string+("%s    " % self.A[a][m])
127            self.file.write("%s\n" % var_string.rstrip())
128            # Write Variables
129            for n in range(self.NV):
130                outlines=listManipulator.recursiveListWriter(self.V[n][m], self.NX, self.floatFormat)
131                for line in outlines:
132                    self.file.write(line)
133
134    def _normalizeIndVars(self):
135        """
136        Normalizes the values in the unbounded independent variable for FFIs
137        that store an abbreviated version of this axis.
138        """
139        for i in range(self.NIV-1):
140            if self.NXDEF[i]==self.NX[i]:
141                pass
142            else:
143                del self.X[i+1][1:]
144                count=0
145                while len(self.X[i+1])<self.NX[i]:
146                    nextx=self.X[i+1][count]+self.DX[i+1]
147                    self.X[i+1].append(nextx)
148                    count=count+1
149        self._normalizedX="yes"
Note: See TracBrowser for help on using the repository browser.