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

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/nappy/trunk/nappy/na_file/na_file_2160.py@3414
Revision 3414, 5.2 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"""
6naFile2160.py
7=============
8
9Container module for NAFile2160 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 NAFile2160(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) 2160.
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.LENX = nappy.utils.text_parser.readItemFromLine(self.file.readline(), float)
33        self.XNAME = nappy.utils.text_parser.readItemsFromLines(self._readLines(self.NIV), self.NIV, str)
34        self.XNAME.reverse()  # Reverse because C-type array is least-changing first
35        self._readVariablesHeaderSection()
36        self._readCharAuxVariablesHeaderSection()
37        self._readComments()
38
39    def writeHeader(self):
40        """
41        Writes FFI-specific header section.
42        """
43        self._writeCommonHeader()
44        self.DX.reverse()
45        self.header.write("%s\n" % tuple(self.DX))
46        self.header.write("%s\n" % self.LENX)
47        self.XNAME.reverse()
48        self.header.write("%s\n" * self.NIV % tuple(self.XNAME))
49        self._writeVariablesHeaderSection()
50        self._writeAuxVariablesHeaderSection()
51        self._writeComments()
52        self._fixHeaderLength()
53        self.file.write(self.header.read())
54
55    def _writeAuxVariablesHeaderSection(self):
56        """
57        Writes the auxiliary variables section of the header for FFI 2160.
58        Assumes we are at the right point in the file.
59        """
60        self.header.write("%s\n" % self.NAUXV)
61        self.header.write("%s\n" % self.NAUXC)
62        if self.NAUXV > 0:
63            self.header.write(("%s " * (self.NAUXV - self.NAUXC) + "\n")  % tuple(self.ASCAL))
64            self.header.write(("%s " * (self.NAUXV - self.NAUXC) + "\n")  % tuple(self.AMISS[0:(self.NAUXV - self.NAUXC)]))
65            self.header.write(("%s " * self.NAUXC + "\n") % tuple(self.LENA[(self.NAUXV - self.NAUXC):]))
66            self.header.write(("%s\n" * self.NAUXC) % tuple(self.AMISS[(self.NAUXV - self.NAUXC):]))
67            self.header.write("%s\n" * self.NAUXV % tuple(self.ANAME))
68
69    def _setupArrays(self):
70        """
71        Sets up FFI-specific arrays to fill with data (lists of lists).
72        """
73        self.V = []
74        self.A = []
75        self.X = []
76        self.NX = []
77       
78        for n in range(self.NV):
79            self.V.append([])
80        for i in range(self.NAUXV):
81            self.A.append([])
82
83    def _readData1(self, datalines, ivar_count):
84        """
85        Reads first line/section of current block of data.
86        """     
87        # Start with independent and Auxilliary vars
88        # Get character string independent variable
89        (x1, datalines) = nappy.utils.text_parser.readItemsFromUnknownLines(datalines, 1, str)
90        self.X.append([])
91        self.X[ivar_count].append(x1[0])
92        # Set up list to take second changing independent variable
93        self.X[ivar_count].append([]) 
94
95        # Get NX and Non-character AUX vars
96        (aux, datalines) = nappy.utils.text_parser.readItemsFromUnknownLines(datalines, (self.NAUXV - self.NAUXC), float)
97        self.NX.append(int(aux[0]))
98
99        count = 0
100        for a in range(self.NAUXV - self.NAUXC):
101            self.A[a].append(aux[count])
102            count = count + 1
103
104        # Get character AUX vars
105        (auxc) = nappy.utils.text_parser.readItemsFromLines(datalines[:self.NAUXC], self.NAUXC, str)
106        rtlines = datalines[self.NAUXC:]
107        count = 0
108        for a in range(self.NAUXC):
109            self.A[(self.NAUXV - self.NAUXC) + a].append(auxc[count])
110            count = count + 1
111
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        for m in range(len(self.X)):
121            # Write Independent variable mark and auxiliary variables
122            var_string = "%s    " % self.X[m][0]
123            self.file.write("%s\n" % var_string.rstrip())
124            var_string = ""
125
126            for a in range(self.NAUXV - self.NAUXC):
127                var_string = var_string + ("%s    " % self.A[a][m])
128
129            self.file.write("%s\n" % var_string.rstrip())
130
131            for a in range(self.NAUXC):
132                var_string = ("%s    " % self.A[(self.NAUXV - self.NAUXC) + a][m])
133                self.file.write("%s\n" % var_string.rstrip())
134
135            # Write second independant variable and dependant variables
136            for p in range(self.NX[m]):
137                var_string = "%s%s"  % (self.X[m][1][p], self.delimiter)
138                for n in range(self.NV):
139                    var_string = var_string + ("%s    " % self.V[n][m][p])
140
141                self.file.write("%s    \n" % var_string)
Note: See TracBrowser for help on using the repository browser.