source: nappy/trunk/nappy/na_file/na_file_2010.py @ 3383

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/nappy/trunk/nappy/na_file/na_file_2010.py@3383
Revision 3383, 6.0 KB checked in by astephen, 12 years ago (diff)

Changed spacer to delimiter.

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