source: nappy/trunk/naFile2010.py @ 359

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

Fixed a bug so that X values in the header are now ordered correctly.

  • 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.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()
61        X_lines=[]
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]]
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())
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
81        self.arraySize=1
82        for i in self.NX:
83            self.arraySize=self.arraySize*i       
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        """
107        # Now get the dependent variables         
108        (v, rtlines)=readItemsFromUnknownLines(datalines, self.arraySize, float)
109        for n in range(self.NV):
110            self.V[n].append([])
111            listManipulator.recursiveListPopulator(self.V[n][ivar_count], v, self.NX)
112        return rtlines   
113
114    def writeData(self):
115        """
116        Writes the data section of the file.
117        This method can be called directly by the user.
118        """       
119        # Set up unbounded IV loop
120        self.NX.reverse()
121        for m in range(len(self.X[0])):
122            # Write Independent variable mark and auxiliary variables
123            var_string="%s    " % self.X[0][m]
124            for a in range(self.NAUXV):
125                var_string=var_string+("%s    " % self.A[a][m])
126            self.file.write("%s\n" % var_string.rstrip())
127            # Write Variables
128            for n in range(self.NV):
129                outlines=listManipulator.recursiveListWriter(self.V[n][m], self.NX, self.floatFormat)
130                for line in outlines:
131                    self.file.write(line)
132
133    def _normalizeIndVars(self):
134        """
135        Normalizes the values in the unbounded independent variable for FFIs
136        that store an abbreviated version of this axis.
137        """
138        for i in range(self.NIV-1):
139            if self.NXDEF[i]==self.NX[i]:
140                pass
141            else:
142                del self.X[i+1][1:]
143                count=0
144                while len(self.X[i+1])<self.NX[i]:
145                    nextx=self.X[i+1][count]+self.DX[i+1]
146                    self.X[i+1].append(nextx)
147                    count=count+1
148        self._normalizedX="yes"
Note: See TracBrowser for help on using the repository browser.