source: nappy/trunk/naFile2160.py @ 361

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/nappy/trunk/naFile2160.py@361
Revision 361, 4.7 KB checked in by selatham, 15 years ago (diff)

bug fixes, write classes and other new classes as required.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
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
16from textParser import *
17import naFile2110
18
19class NAFile2160(naFile2110.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._normalizedX="no"
30        self._readCommonHeader()
31        self.DX=readItemsFromLine(self.file.readline(), 1, float)
32        self.LENX=readItemFromLine(self.file.readline(), float)
33        self.XNAME=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.file.write("%s\n" % tuple(self.DX))
46        self.file.write("%s\n" % self.LENX)
47        self.XNAME.reverse()
48        self.file.write("%s\n"*self.NIV % tuple(self.XNAME))
49        self._writeVariablesHeaderSection()
50        self._writeAuxVariablesHeaderSection()
51        self._writeComments()
52
53    def _writeAuxVariablesHeaderSection(self):
54        """
55        Writes the auxiliary variables section of the header for FFI 2160.
56        Assumes we are at the right point in the file.
57        """
58        self.file.write("%s\n" % self.NAUXV)
59        self.file.write("%s\n" % self.NAUXC)
60        if self.NAUXV>0:
61            self.file.write(("%s "*(self.NAUXV-self.NAUXC)+"\n")  % tuple(self.ASCAL))
62            self.file.write(("%s "*(self.NAUXV-self.NAUXC)+"\n")  % tuple(self.AMISS[0:(self.NAUXV-self.NAUXC)]))
63            self.file.write(("%s "*self.NAUXC+"\n") % tuple(self.LENA[(self.NAUXV-self.NAUXC):]))
64            self.file.write(("%s\n"*self.NAUXC) % tuple(self.AMISS[(self.NAUXV-self.NAUXC):]))
65            self.file.write("%s\n"*self.NAUXV % tuple(self.ANAME))
66
67    def _setupArrays(self):
68        """
69        Sets up FFI-specific arrays to fill with data (lists of lists).
70        """
71        self.V=[]
72        self.A=[]
73        self.X=[]
74        self.NX=[]
75       
76        for n in range(self.NV):
77            self.V.append([])
78        for i in range(self.NAUXV):
79            self.A.append([])
80
81    def _readData1(self, datalines, ivar_count):
82        """
83        Reads first line/section of current block of data.
84        """     
85        # Start with independent and Auxilliary vars
86        # Get character string independent variable
87        (x1, datalines)=readItemsFromUnknownLines(datalines, 1, str)
88        self.X.append([])
89        self.X[ivar_count].append(x1[0])
90        # Set up list to take second changing independent variable
91        self.X[ivar_count].append([]) 
92        # Get NX and Non-character AUX vars
93        (aux, datalines)=readItemsFromUnknownLines(datalines, (self.NAUXV-self.NAUXC), float)
94        self.NX.append(int(aux[0]))
95        count=0
96        for a in range(self.NAUXV-self.NAUXC):
97            self.A[a].append(aux[count])
98            count=count+1
99        # Get character AUX vars
100        (auxc)=readItemsFromLines(datalines[:self.NAUXC], self.NAUXC, str)
101        rtlines=datalines[self.NAUXC:]
102        count=0
103        for a in range(self.NAUXC):
104            self.A[(self.NAUXV-self.NAUXC)+a].append(auxc[count])
105            count=count+1
106        return rtlines
107   
108    def writeData(self):
109        """
110        Writes the data section of the file.
111        This method can be called directly by the user.
112        """
113        # Set up unbounded IV loop
114       # self.NX.reverse()
115        for m in range(len(self.X)):
116             # Write Independent variable mark and auxiliary variables
117             var_string="%s    " % self.X[m][0]
118             self.file.write("%s\n" % var_string.rstrip())
119             var_string=""
120             for a in range(self.NAUXV-self.NAUXC):
121                 var_string=var_string+("%s    " % self.A[a][m])
122             self.file.write("%s\n" % var_string.rstrip())
123
124             for a in range(self.NAUXC):
125                 var_string=("%s    " % self.A[(self.NAUXV-self.NAUXC)+a][m])
126                 self.file.write("%s\n" % var_string.rstrip())
127
128             # Write second independant variable and dependant variables
129             for p in range(self.NX[m]):
130                var_string="%s    " % self.X[m][1][p]
131                for n in range(self.NV):
132                        var_string=var_string+("%s    " %self.V[n][m][p])
133                self.file.write("%s    \n" %var_string)
Note: See TracBrowser for help on using the repository browser.