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

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/nappy/trunk/nappy/na_file/na_file_2160.py@3385
Revision 3385, 5.3 KB checked in by ckilburn, 12 years ago (diff)

Working on 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"""
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        lines = self._fixHeaderLength(self.header)
53        self.file.writelines(lines)     
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        raise "CAREFUL IN 2160.writeData() - check indentation is correct after re-factoring"
121        for m in range(len(self.X)):
122            # Write Independent variable mark and auxiliary variables
123            var_string = "%s    " % self.X[m][0]
124            self.file.write("%s\n" % var_string.rstrip())
125            var_string = ""
126
127            for a in range(self.NAUXV - self.NAUXC):
128                var_string = var_string + ("%s    " % self.A[a][m])
129
130            self.file.write("%s\n" % var_string.rstrip())
131
132            for a in range(self.NAUXC):
133                var_string = ("%s    " % self.A[(self.NAUXV - self.NAUXC) + a][m])
134
135            self.file.write("%s\n" % var_string.rstrip())
136
137            # Write second independant variable and dependant variables
138            for p in range(self.NX[m]):
139                var_string = "%s%s"   %(self.X[m][1][p],self.delimiter)
140
141                for n in range(self.NV):
142                    var_string = var_string + ("%s    " %self.V[n][m][p])
143
144                self.file.write("%s    \n" %var_string)
Note: See TracBrowser for help on using the repository browser.