source: nappy/trunk/naFile2310.py @ 366

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/nappy/trunk/naFile2310.py@366
Revision 366, 3.6 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
RevLine 
[349]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
[343]5"""
6naFile2310.py
7=============
8
9Container module for NAFile2310 class.
10
11"""
12
13# Imports from python standard library
14
15# Imports from local package
16from textParser import *
17import naFile2110
18
19class NAFile2310(naFile2110.NAFile2110):
20    """
21    Class to read, write and interact with NASA Ames files conforming to the
22    File Format Index (FFI) 2310.
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.XNAME=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()
[361]37
38    def writeHeader(self):
[343]39        """
[361]40        Writes FFI-specifc header section.
41        """
42        self._writeCommonHeader()
43        #self.DX.reverse()
[366]44        self.header.write(("%s\n") % self.DX[0])
[361]45        self.XNAME.reverse()
[366]46        self.header.write("%s\n"*self.NIV % tuple(self.XNAME))
[361]47        self._writeVariablesHeaderSection()
48        self._writeAuxVariablesHeaderSection()
49        self._writeComments()
[366]50        lines=self._fixHeaderLength(self.header)
51        self.file.writelines(lines)
[361]52
53    def _readData1(self, datalines, ivar_count):
54        """
[343]55        Reads first line/section of current block of data.
56        """       
57        # Start with independent and Auxilliary vars
58        (x_and_a, rtlines)=readItemsFromUnknownLines(datalines, self.NAUXV+1, float)
59        (x, aux)=(x_and_a[0], x_and_a[1:])
[361]60        count=0
[343]61        for a in range(self.NAUXV):
[361]62            self.A[a].append(aux[count])
63            count=count+1
[343]64        self.X.append([])
65        self.X[ivar_count].append(x)
66        # Set up list to take second changing independent variable
[361]67        self.X[ivar_count].append([aux[1]])
[343]68        self.NX.append(int(aux[0]))
69        self.DX.append(int(aux[2]))
70        return rtlines
71   
72    def _readData2(self, datalines, ivar_count):
73        """
74        Reads second line/section (if used) of current block of data.
75        """
76        # Now get the dependent variables
77        (v, rtlines)=readItemsFromUnknownLines(datalines, self.NV*self.NX[ivar_count], float)
78        count=0
79        for n in range(self.NV):
80            self.V[n].append([])
81            for i in range(self.NX[ivar_count]):
82                self.V[n][ivar_count].append(v[count])
83                count=count+1
84        return rtlines
[361]85
86    def writeData(self):
87        """
88        Writes the data section of the file.
89        This method can be called directly by the user.
90        """
91        # Set up unbounded IV loop
92       # self.NX.reverse()
93        for m in range(len(self.X)):
94             # Write Independent variable mark and auxiliary variables
95             var_string="%s    " % self.X[m][0]
96             for a in range(self.NAUXV):
97                 var_string=var_string+("%s    " % self.A[a][m])
98             self.file.write("%s\n" % var_string.rstrip())
99             # Write second independant variable and dependant variables
100             var_string=""
101             for p in range(self.NX[m]):
102                #var_string="%s    " % self.X[m][1][p]
103                for n in range(self.NV):
104                        var_string=var_string+("%s    " %self.V[n][m][p])
[366]105             self.file.write("%s    \n" %var_string)
Note: See TracBrowser for help on using the repository browser.