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

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

Replace print with logging

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