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

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/nappy/trunk/nappy/na_file/na_file_2310.py@3414
Revision 3414, 3.8 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"""
6naFile2310.py
7=============
8
9Container module for NAFile2310 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_2110
18
19class NAFile2310(nappy.na_file.na_file_2110.NAFile2110):
20    """
21    Class to read, write and interact with NASA Ames files conforming to the
22    File Format Index (FFI) 2310.
23    """
24
25    def readHeader(self):
26        """
27        Reads FFI-specifc header section.
28        """       
29        self._normalized_X = False
30        self._readCommonHeader()
31        self.DX = nappy.utils.text_parser.readItemsFromLine(self.file.readline(), 1, float)
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\n") % self.DX[0])
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    def _readData1(self, datalines, ivar_count):
54        """
55        Reads first line/section of current block of data.
56        """       
57        # Start with independent and Auxilliary vars
58        (x_and_a, rtlines) = nappy.utils.text_parser.readItemsFromUnknownLines(datalines, self.NAUXV + 1, float)
59        (x, aux) = (x_and_a[0], x_and_a[1:])
60
61        count = 0
62        for a in range(self.NAUXV):
63            self.A[a].append(aux[count])
64            count = count + 1
65
66        self.X.append([])
67        self.X[ivar_count].append(x)
68
69        # Set up list to take second changing independent variable
70        self.X[ivar_count].append([aux[1]])
71        self.NX.append(int(aux[0]))
72        self.DX.append(int(aux[2]))
73        return rtlines
74   
75    def _readData2(self, datalines, ivar_count):
76        """
77        Reads second line/section (if used) of current block of data.
78        """
79        # Now get the dependent variables
80        (v, rtlines) = nappy.utils.text_parser.readItemsFromUnknownLines(datalines, self.NV * self.NX[ivar_count], float)
81        count = 0
82        for n in range(self.NV):
83            self.V[n].append([])
84            for i in range(self.NX[ivar_count]):
85                self.V[n][ivar_count].append(v[count])
86                count = count + 1
87        return rtlines
88
89    def writeData(self):
90        """
91        Writes the data section of the file.
92        This method can be called directly by the user.
93        """
94        # Set up unbounded IV loop
95        # self.NX.reverse()
96
97        for m in range(len(self.X)):
98            # Write Independent variable mark and auxiliary variables
99            var_string = "%s    " % self.X[m][0]
100            for a in range(self.NAUXV):
101                var_string = var_string + ("%s    " % self.A[a][m])
102            self.file.write("%s\n" % var_string.rstrip())
103
104            # Write second independant variable and dependant variables
105            var_string = ""
106            for p in range(self.NX[m]):
107                for n in range(self.NV):
108                    var_string = var_string + ("%s    " %self.V[n][m][p])
109
110            self.file.write("%s    \n" %var_string)
Note: See TracBrowser for help on using the repository browser.