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

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

Reverted to previous version of 2110 and 2310 where writes all work from A and NAUXV so same as read methods.

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.delimiter) * (self.NIV - 1) + "%s\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
116        for m in range(len(self.X)):
117
118            # Write unbounded independent variable mark and auxiliary variables
119            var_string = self.format % self.X[m][0]
120            #############var_string = var_string + (self.format % self.NX[m])
121
122            ################ Loop through aux vars minus 1 because NX is used as first aux var
123            for a in range(self.NAUXV): 
124                var_string = var_string + (self.format % self.A[a][m])
125            self.file.write("%s\n" % var_string.rstrip(" ,"))
126
127            # Write second independant variable and dependant variables
128            for p in range(self.NX[m]):
129                var_string = self.format % self.X[m][1][p]
130
131                for n in range(self.NV):
132                    var_string = var_string + (self.format %self.V[n][m][p])
133
134                self.file.write("%s\n" %var_string.rstrip(" ,"))
Note: See TracBrowser for help on using the repository browser.