source: nappy/trunk/naFile2010.py @ 349

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/nappy/trunk/naFile2010.py@349
Revision 349, 5.4 KB checked in by selatham, 16 years ago (diff)

Inserted license information.

  • 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"""
6naFile2010.py
7=============
8
9Container module for NAFile2010 class.
10
11"""
12
13# Imports from python standard library
14
15# Imports from local package
16from textParser import *
17import naFile
18import listManipulator
19
20class NAFile2010(naFile.NAFile):
21    """
22    Class to read, write and interact with NASA Ames files conforming to the
23    File Format Index (FFI) 2010.
24    """
25
26    def readHeader(self):
27        """
28        Reads FFI-specifc header section.
29        """       
30        self._normalizedX="no"
31        self._readCommonHeader()
32        self.DX=readItemsFromLine(self.file.readline(), self.NIV, float)
33        self.DX.reverse()  # Reverse because C-type array is least-changing first
34        self.NX=readItemsFromLine(self.file.readline(), self.NIV-1, int)
35        self.NX.reverse()  # Reverse because C-type array is least-changing first
36        self.NXDEF=readItemsFromLine(self.file.readline(), self.NIV-1, int)
37        self.NXDEF.reverse()  # Reverse because C-type array is least-changing first
38        self.X=[]
39        for i in range(self.NIV-1):
40            self.X.append(readItemsFromUnknownLines(self.file, self.NXDEF[i], float))
41        # Unbounded Independent variable should be first so insert empty list at start
42        self.X.insert(0, [])
43        self.XNAME=readItemsFromLines(self._readLines(self.NIV), self.NIV, str)
44        self.XNAME.reverse()  # Reverse because C-type array is least-changing first
45        self._readVariablesHeaderSection()
46        self._readAuxVariablesHeaderSection()
47        self._readComments()
48
49    def writeHeader(self):
50        """
51        Writes FFI-specifc header section.
52        """       
53        self._writeCommonHeader()
54        self.DX.reverse()
55        self.file.write(("%s "*self.NIV+"\n") % tuple(self.DX))
56        self.NX.reverse()
57        self.file.write(("%s "*(self.NIV-1)+"\n") % tuple(self.NX))
58        self.NXDEF.reverse()
59        self.file.write(("%s "*(self.NIV-1)+"\n") % tuple(self.NXDEF))
60        self.NXDEF.reverse()
61        for i in range(self.NIV-1):
62            #print (self.floatFormat+self.spacer)*self.NXDEF[i]+"\n", self.NXDEF, self.X[i+1][0:self.NXDEF[i]]
63            self.file.write(((self.floatFormat+self.spacer)*self.NXDEF[i]+"\n") % tuple(self.X[i+1][0:self.NXDEF[i]]))           
64        self.XNAME.reverse()
65        self.file.write("%s\n"*self.NIV % tuple(self.XNAME))
66        self._writeVariablesHeaderSection()
67        self._writeAuxVariablesHeaderSection()
68        self._writeComments()         
69
70    def _setupArrays(self):
71        """
72        Sets up FFI-specific arrays to fill with data (lists of lists).
73        """
74        self.V=[]
75        self.A=[]
76        # Create an array size to request using read routines
77        self.arraySize=1
78        for i in self.NX:
79            self.arraySize=self.arraySize*i       
80        for n in range(self.NV):
81            self.V.append([])
82        for a in range(self.NAUXV):
83            self.A.append([])
84           
85    def _readData1(self, datalines, ivar_count):
86        """
87        Reads first line/section of current block of data.
88        """       
89        # Start with independent and Auxilliary vars
90        (x2_and_a, rtlines)=readItemsFromUnknownLines(datalines, 1+self.NAUXV, float)
91        (x, aux)=(x2_and_a[0], x2_and_a[1:])
92        self.X[0].append(x)
93        count=0
94        for a in range(self.NAUXV):
95            self.A[a].append(aux[count])
96            count=count+1
97        return rtlines
98
99    def _readData2(self, datalines, ivar_count):
100        """
101        Reads second line/section (if used) of current block of data.
102        """
103        # Now get the dependent variables         
104        (v, rtlines)=readItemsFromUnknownLines(datalines, self.arraySize, float)
105        for n in range(self.NV):
106            self.V[n].append([])
107            listManipulator.recursiveListPopulator(self.V[n][ivar_count], v, self.NX)
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[0])):
118            # Write Independent variable mark and auxiliary variables
119            var_string="%s    " % self.X[0][m]
120            for a in range(self.NAUXV):
121                var_string=var_string+("%s    " % self.A[a][m])
122            self.file.write("%s\n" % var_string.rstrip())
123            # Write Variables
124            for n in range(self.NV):
125                outlines=listManipulator.recursiveListWriter(self.V[n][m], self.NX, self.floatFormat)
126                for line in outlines:
127                    self.file.write(line)
128
129    def _normalizeIndVars(self):
130        """
131        Normalizes the values in the unbounded independent variable for FFIs
132        that store an abbreviated version of this axis.
133        """
134        for i in range(self.NIV-1):
135            if self.NXDEF[i]==self.NX[i]:
136                pass
137            else:
138                del self.X[i+1][1:]
139                count=0
140                while len(self.X[i+1])<self.NX[i]:
141                    nextx=self.X[i+1][count]+self.DX[i+1]
142                    self.X[i+1].append(nextx)
143                    count=count+1
144        self._normalizedX="yes"
Note: See TracBrowser for help on using the repository browser.