source: nappy/trunk/naFile2160.py @ 366

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/nappy/trunk/naFile2160.py@366
Revision 366, 4.8 KB checked in by astephen, 14 years ago (diff)

Latest version - for compatibility with web service. Includes multiple file
output on conversion from NetCDF.

  • 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.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)=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        # Get NX and Non-character AUX vars
95        (aux, datalines)=readItemsFromUnknownLines(datalines, (self.NAUXV-self.NAUXC), float)
96        self.NX.append(int(aux[0]))
97        count=0
98        for a in range(self.NAUXV-self.NAUXC):
99            self.A[a].append(aux[count])
100            count=count+1
101        # Get character AUX vars
102        (auxc)=readItemsFromLines(datalines[:self.NAUXC], self.NAUXC, str)
103        rtlines=datalines[self.NAUXC:]
104        count=0
105        for a in range(self.NAUXC):
106            self.A[(self.NAUXV-self.NAUXC)+a].append(auxc[count])
107            count=count+1
108        return rtlines
109   
110    def writeData(self):
111        """
112        Writes the data section of the file.
113        This method can be called directly by the user.
114        """
115        # Set up unbounded IV loop
116       # self.NX.reverse()
117        for m in range(len(self.X)):
118             # Write Independent variable mark and auxiliary variables
119             var_string="%s    " % self.X[m][0]
120             self.file.write("%s\n" % var_string.rstrip())
121             var_string=""
122             for a in range(self.NAUXV-self.NAUXC):
123                 var_string=var_string+("%s    " % self.A[a][m])
124             self.file.write("%s\n" % var_string.rstrip())
125
126             for a in range(self.NAUXC):
127                 var_string=("%s    " % self.A[(self.NAUXV-self.NAUXC)+a][m])
128                 self.file.write("%s\n" % var_string.rstrip())
129
130             # Write second independant variable and dependant variables
131             for p in range(self.NX[m]):
132                var_string="%s    " % self.X[m][1][p]
133                for n in range(self.NV):
134                        var_string=var_string+("%s    " %self.V[n][m][p])
135                self.file.write("%s    \n" %var_string)
Note: See TracBrowser for help on using the repository browser.