source: nappy/trunk/naFile2110.py @ 366

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/nappy/trunk/naFile2110.py@366
Revision 366, 4.2 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"""
6naFile2110.py
7=============
8
9Container module for NAFile2110 class.
10
11"""
12
13# Imports from python standard library
14
15# Imports from local package
16from textParser import *
17import naFile2010
18
19class NAFile2110(naFile2010.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=readItemsFromLine(self.file.readline(), self.NIV, float)
31        self.DX.reverse()  # Reverse because C-type array is least-changing first
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()
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.NIV+"\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        lines=self._fixHeaderLength(self.header)
51        self.file.writelines(lines)
52
53    def _setupArrays(self):
54        """
55        Sets up FFI-specific arrays to fill with data (lists of lists).
56        """
57        self.V=[]
58        self.A=[]
59        self.X=[]  # Needs to be a list of sublists each containing [x0n, [x1n, x1n+1, x1n+2....]]
60        self.NX=[]
61
62        for n in range(self.NV):
63            self.V.append([])
64        for i in range(self.NAUXV):
65           self.A.append([])
66
67    def _readData1(self, datalines, ivar_count): 
68        """
69        Reads first line/section of current block of data.
70        """   
71        # Start with independent and Auxilliary vars
72        (x_and_a, rtlines)=readItemsFromUnknownLines(datalines, self.NAUXV+1, float)
73        (x, aux)=(x_and_a[0], x_and_a[1:])
74        count=0
75        for a in range(self.NAUXV):
76            self.A[a].append(aux[count])
77            count=count+1
78        #for a in range(self.NAUXV):
79            #self.A.append(aux[a])
80        self.X.append([])
81        self.X[ivar_count].append(x)
82        # Set up list to take second changing independent variable
83        self.X[ivar_count].append([]) 
84        self.NX.append(int(aux[0]))
85        return rtlines
86
87    def _readData2(self, datalines, ivar_count):
88        """
89        Reads second line/section (if used) of current block of data.
90        """
91        # Now get the dependent variables
92        for n in range(self.NV):
93            self.V[n].append([])
94        for c in range(self.NX[ivar_count]):
95            (x_and_v, datalines)=readItemsFromUnknownLines(datalines, self.NV+1, float)
96            (x, v)=(x_and_v[0], x_and_v[1:])
97            self.X[ivar_count][1].append(x)
98            count=0
99            for n in range(self.NV):
100                self.V[n][ivar_count].append(v[count])
101                count=count+1
102        rtlines=datalines
103        return rtlines
104
105    def writeData(self):
106        """
107        Writes the data section of the file.
108        This method can be called directly by the user.
109        """
110        # Set up unbounded IV loop
111       # self.NX.reverse()
112        for m in range(len(self.X)):
113             # Write Independent variable mark and auxiliary variables
114             var_string="%s    " % self.X[m][0]
115             for a in range(self.NAUXV):
116                 var_string=var_string+("%s    " % self.A[a][m])
117             self.file.write("%s\n" % var_string.rstrip())
118             # Write second independant variable and dependant variables
119             for p in range(self.NX[m]):
120                var_string="%s    " % self.X[m][1][p]
121                for n in range(self.NV):
122                        var_string=var_string+("%s    " %self.V[n][m][p])
123                self.file.write("%s    \n" %var_string)
Note: See TracBrowser for help on using the repository browser.