source: nappy/trunk/nappy/na_file/na_file_2110.py @ 3414

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

Tidied up _fixHeaderLength in na_file.py.
Changed write interface so self.write(na_dict, delimiter, float_format)
is a separate call to creating the instance.
Fixed unit tests to cope with this.

BUT: broke nappy_api.openNAFile(<file>, "w") with these changes.
Hence need to think about changes being made to openNAFile() so that it
can predict (or not) the FFI.

Only 1001 unit test (worked on by CK) is broken at present.

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"""
6naFile2110.py
7=============
8
9Container module for NAFile2110 class.
10
11"""
12
13# Imports from python standard library
14
15# Imports from local package
16import nappy.utils.text_parser
17import nappy.na_file.na_file_2010
18
19class NAFile2110(nappy.na_file.na_file_2010.NAFile2010):
20    """
21    Class to read, write and interact with NASA Ames files conforming to the
22    File Format Index (FFI) 2110.
23    """
24
25    def readHeader(self):
26        """
27        Reads FFI-specifc header section.
28        """
29        self._readCommonHeader()
30        self.DX = nappy.utils.text_parser.readItemsFromLine(self.file.readline(), self.NIV, float)
31        self.DX.reverse()  # Reverse because C-type array is least-changing first
32        self.XNAME = nappy.utils.text_parser.readItemsFromLines(self._readLines(self.NIV), self.NIV, str)
33        self.XNAME.reverse()  # Reverse because C-type array is least-changing first
34        self._readVariablesHeaderSection()
35        self._readAuxVariablesHeaderSection()
36        self._readComments()
37
38    def writeHeader(self):
39        """
40        Writes FFI-specifc header section.
41        """
42        self._writeCommonHeader()
43        self.DX.reverse()
44        self.header.write(("%s " * self.NIV + "\n") % tuple(self.DX))
45        self.XNAME.reverse()
46        self.header.write("%s\n" * self.NIV % tuple(self.XNAME))
47        self._writeVariablesHeaderSection()
48        self._writeAuxVariablesHeaderSection()
49        self._writeComments()
50        self._fixHeaderLength()
51        self.file.write(self.header.read())
52
53
54    def _setupArrays(self):
55        """
56        Sets up FFI-specific arrays to fill with data (lists of lists).
57        """
58        self.V = []
59        self.A = []
60        self.X = []  # Needs to be a list of sublists each containing [x0n, [x1n, x1n + 1, x1n + 2....]]
61        self.NX = []
62
63        for n in range(self.NV):
64            self.V.append([])
65        for i in range(self.NAUXV):
66           self.A.append([])
67
68    def _readData1(self, datalines, ivar_count): 
69        """
70        Reads first line/section of current block of data.
71        """   
72        # Start with independent and Auxilliary vars
73        (x_and_a, rtlines) = nappy.utils.text_parser.readItemsFromUnknownLines(datalines, self.NAUXV + 1, float)
74        (x, aux) = (x_and_a[0], x_and_a[1:])
75        count = 0
76        for a in range(self.NAUXV):
77            self.A[a].append(aux[count])
78            count = count + 1
79        #for a in range(self.NAUXV):
80            #self.A.append(aux[a])
81        self.X.append([])
82        self.X[ivar_count].append(x)
83        # Set up list to take second changing independent variable
84        self.X[ivar_count].append([]) 
85        self.NX.append(int(aux[0]))
86        return rtlines
87
88    def _readData2(self, datalines, ivar_count):
89        """
90        Reads second line/section (if used) of current block of data.
91        """
92        # Now get the dependent variables
93        for n in range(self.NV):
94            self.V[n].append([])
95
96        for c in range(self.NX[ivar_count]):
97            (x_and_v, datalines) = nappy.utils.text_parser.readItemsFromUnknownLines(datalines, self.NV + 1, float)
98            (x, v) = (x_and_v[0], x_and_v[1:])
99            self.X[ivar_count][1].append(x)
100
101            count = 0
102            for n in range(self.NV):
103                self.V[n][ivar_count].append(v[count])
104                count = count + 1
105
106        rtlines = datalines
107        return rtlines
108
109    def writeData(self):
110        """
111        Writes the data section of the file.
112        This method can be called directly by the user.
113        """
114        # Set up unbounded IV loop
115        # self.NX.reverse()
116        for m in range(len(self.X)):
117            # Write Independent variable mark and auxiliary variables
118            var_string = "%s    " % self.X[m][0]
119
120            for a in range(self.NAUXV):
121                var_string = var_string + ("%s    " % self.A[a][m])
122
123            self.file.write("%s\n" % var_string.rstrip())
124
125            # Write second independant variable and dependant variables
126            for p in range(self.NX[m]):
127                var_string = "%s    " % self.X[m][1][p]
128
129                for n in range(self.NV):
130                    var_string = var_string + ("%s    " %self.V[n][m][p])
131
132                self.file.write("%s    \n" %var_string)
Note: See TracBrowser for help on using the repository browser.