source: nappy/trunk/naFile2110.py @ 361

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/nappy/trunk/naFile2110.py@361
Revision 361, 4.1 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"""
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.file.write(("%s "*self.NIV+"\n") % tuple(self.DX))
45        self.XNAME.reverse()
46        self.file.write("%s\n"*self.NIV % tuple(self.XNAME))
47        self._writeVariablesHeaderSection()
48        self._writeAuxVariablesHeaderSection()
49        self._writeComments()
50
51    def _setupArrays(self):
52        """
53        Sets up FFI-specific arrays to fill with data (lists of lists).
54        """
55        self.V=[]
56        self.A=[]
57        self.X=[]  # Needs to be a list of sublists each containing [x0n, [x1n, x1n+1, x1n+2....]]
58        self.NX=[]
59
60        for n in range(self.NV):
61            self.V.append([])
62        for i in range(self.NAUXV):
63           self.A.append([])
64
65    def _readData1(self, datalines, ivar_count): 
66        """
67        Reads first line/section of current block of data.
68        """   
69        # Start with independent and Auxilliary vars
70        (x_and_a, rtlines)=readItemsFromUnknownLines(datalines, self.NAUXV+1, float)
71        (x, aux)=(x_and_a[0], x_and_a[1:])
72        count=0
73        for a in range(self.NAUXV):
74            self.A[a].append(aux[count])
75            count=count+1
76        #for a in range(self.NAUXV):
77            #self.A.append(aux[a])
78        self.X.append([])
79        self.X[ivar_count].append(x)
80        # Set up list to take second changing independent variable
81        self.X[ivar_count].append([]) 
82        self.NX.append(int(aux[0]))
83        return rtlines
84
85    def _readData2(self, datalines, ivar_count):
86        """
87        Reads second line/section (if used) of current block of data.
88        """
89        # Now get the dependent variables
90        for n in range(self.NV):
91            self.V[n].append([])
92        for c in range(self.NX[ivar_count]):
93            (x_and_v, datalines)=readItemsFromUnknownLines(datalines, self.NV+1, float)
94            (x, v)=(x_and_v[0], x_and_v[1:])
95            self.X[ivar_count][1].append(x)
96            count=0
97            for n in range(self.NV):
98                self.V[n][ivar_count].append(v[count])
99                count=count+1
100        rtlines=datalines
101        return rtlines
102
103    def writeData(self):
104        """
105        Writes the data section of the file.
106        This method can be called directly by the user.
107        """
108        # Set up unbounded IV loop
109       # self.NX.reverse()
110        for m in range(len(self.X)):
111             # Write Independent variable mark and auxiliary variables
112             var_string="%s    " % self.X[m][0]
113             for a in range(self.NAUXV):
114                 var_string=var_string+("%s    " % self.A[a][m])
115             self.file.write("%s\n" % var_string.rstrip())
116             # Write second independant variable and dependant variables
117             for p in range(self.NX[m]):
118                var_string="%s    " % self.X[m][1][p]
119                for n in range(self.NV):
120                        var_string=var_string+("%s    " %self.V[n][m][p])
121                self.file.write("%s    \n" %var_string)
Note: See TracBrowser for help on using the repository browser.