source: nappy/trunk/nappy/na_file/na_file_2310.py @ 3521

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/nappy/trunk/nappy/na_file/na_file_2310.py
Revision 3521, 4.2 KB checked in by ckilburn, 12 years ago (diff)

Annotations and isolating reverses on write

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"""
6naFile2310.py
7=============
8
9Container module for NAFile2310 class.
10
11"""
12
13# Imports from python standard library
14
15# Imports from local package
16import nappy.utils.text_parser
17import nappy.na_file.na_file_2110
18import nappy.utils.common_utils
19wrapLine = nappy.utils.common_utils.annotateLine
20wrapLines = nappy.utils.common_utils.annotateLines
21
22class NAFile2310(nappy.na_file.na_file_2110.NAFile2110):
23    """
24    Class to read, write and interact with NASA Ames files conforming to the
25    File Format Index (FFI) 2310.
26    """
27
28    def readHeader(self):
29        """
30        Reads FFI-specifc header section.
31        """       
32        self._normalized_X = False
33        self._readCommonHeader()
34        self.DX = nappy.utils.text_parser.readItemsFromLine(self.file.readline(), 1, float)
35        self.XNAME = nappy.utils.text_parser.readItemsFromLines(self._readLines(self.NIV), self.NIV, str)
36        self.XNAME.reverse()  # Reverse because C-type array is least-changing first         
37        self._readVariablesHeaderSection()
38        self._readAuxVariablesHeaderSection()
39        self._readComments()
40
41    def writeHeader(self):
42        """
43        Writes FFI-specifc header section.
44        """
45        self._writeCommonHeader()
46        self.header.write(wrapLine("DX", self.annotation, self.delimiter, "%s\n" % self.DX[0]))
47        XNAME = self.XNAME
48        XNAME.reverse()
49        self.header.write(wrapLines("XNAME", self.annotation, self.delimiter, "%s\n" * self.NIV % tuple(self.XNAME)))
50        self._writeVariablesHeaderSection()
51        self._writeAuxVariablesHeaderSection()
52        self._writeComments()
53        self._fixHeaderLength()
54        self.file.write(self.header.read())
55
56    def _readData1(self, datalines, ivar_count):
57        """
58        Reads first line/section of current block of data.
59        """       
60        # Start with independent and Auxilliary vars
61        (x_and_a, rtlines) = nappy.utils.text_parser.readItemsFromUnknownLines(datalines, self.NAUXV + 1, float)
62        (x, aux) = (x_and_a[0], x_and_a[1:])
63
64        count = 0
65        for a in range(self.NAUXV):
66            self.A[a].append(aux[count])
67            count = count + 1
68
69        self.X.append([])
70        self.X[ivar_count].append(x)
71
72        # Set up list to take second changing independent variable
73        self.X[ivar_count].append([aux[1]])
74        self.NX.append(int(aux[0]))
75        self.DX.append(int(aux[2]))
76        return rtlines
77   
78    def _readData2(self, datalines, ivar_count):
79        """
80        Reads second line/section (if used) of current block of data.
81        """
82        # Now get the dependent variables
83        (v, rtlines) = nappy.utils.text_parser.readItemsFromUnknownLines(datalines, self.NV * self.NX[ivar_count], float)
84        count = 0
85        for n in range(self.NV):
86            self.V[n].append([])
87            for i in range(self.NX[ivar_count]):
88                self.V[n][ivar_count].append(v[count])
89                count = count + 1
90        return rtlines
91
92    def writeData(self):
93        """
94        Writes the data section of the file.
95        This method can be called directly by the user.
96        """
97        # Set up unbounded IV loop
98        # self.NX.reverse()
99
100        for m in range(len(self.X)):
101            # Write Independent variable mark and auxiliary variables
102            var_string = self.format % self.X[m][0]
103
104            #  Loop through NAUXV which includes NX, X[0], DX as first 3 aux vars
105            for a in range(self.NAUXV):
106                var_string = var_string + (self.format % self.A[a][m])
107
108            self.file.write(wrapLine("Data", self.annotation, self.delimiter, "%s\n" % var_string.rstrip(" ,")))
109
110            # Write second independant variable and dependant variables
111            var_string = ""
112            for p in range(self.NX[m]):
113                for n in range(self.NV):
114                    var_string = var_string + (self.format %self.V[n][m][p])
115            self.file.write(wrapLine("Data", self.annotation, self.delimiter, "%s\n" % var_string.rstrip(" ,")))
Note: See TracBrowser for help on using the repository browser.