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

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

Annotations and isolating reverses on write

RevLine 
[3321]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
[3356]16import nappy.utils.text_parser
[3321]17import nappy.na_file.na_file_2010
[3521]18import nappy.utils.common_utils
19wrapLine = nappy.utils.common_utils.annotateLine
20wrapLines = nappy.utils.common_utils.annotateLines
[3321]21
22class NAFile2110(nappy.na_file.na_file_2010.NAFile2010):
23    """
24    Class to read, write and interact with NASA Ames files conforming to the
25    File Format Index (FFI) 2110.
26    """
27
28    def readHeader(self):
29        """
30        Reads FFI-specifc header section.
31        """
32        self._readCommonHeader()
[3356]33        self.DX = nappy.utils.text_parser.readItemsFromLine(self.file.readline(), self.NIV, float)
[3321]34        self.DX.reverse()  # Reverse because C-type array is least-changing first
[3356]35        self.XNAME = nappy.utils.text_parser.readItemsFromLines(self._readLines(self.NIV), self.NIV, str)
[3321]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        """
[3521]43        Writes FFI-specific header section.
[3321]44        """
45        self._writeCommonHeader()
[3521]46        DX = self.DX
47        DX.reverse()
48        self.header.write(wrapLine("DX", self.annotation, self.delimiter, (("%s" + self.delimiter) * (self.NIV - 1) + "%s\n") % tuple(DX)))
49        XNAME = self.XNAME
50        XNAME.reverse()
51        self.header.write(wrapLines("XNAME", self.annotation, self.delimiter, "%s\n" * self.NIV % tuple(XNAME)))
[3321]52        self._writeVariablesHeaderSection()
53        self._writeAuxVariablesHeaderSection()
54        self._writeComments()
[3414]55        self._fixHeaderLength()
56        self.file.write(self.header.read())
[3321]57
[3414]58
[3321]59    def _setupArrays(self):
60        """
61        Sets up FFI-specific arrays to fill with data (lists of lists).
62        """
63        self.V = []
64        self.A = []
65        self.X = []  # Needs to be a list of sublists each containing [x0n, [x1n, x1n + 1, x1n + 2....]]
66        self.NX = []
67
68        for n in range(self.NV):
69            self.V.append([])
70        for i in range(self.NAUXV):
71           self.A.append([])
72
73    def _readData1(self, datalines, ivar_count): 
74        """
75        Reads first line/section of current block of data.
76        """   
77        # Start with independent and Auxilliary vars
[3356]78        (x_and_a, rtlines) = nappy.utils.text_parser.readItemsFromUnknownLines(datalines, self.NAUXV + 1, float)
[3321]79        (x, aux) = (x_and_a[0], x_and_a[1:])
80        count = 0
81        for a in range(self.NAUXV):
82            self.A[a].append(aux[count])
83            count = count + 1
84        #for a in range(self.NAUXV):
85            #self.A.append(aux[a])
86        self.X.append([])
87        self.X[ivar_count].append(x)
88        # Set up list to take second changing independent variable
89        self.X[ivar_count].append([]) 
90        self.NX.append(int(aux[0]))
91        return rtlines
92
93    def _readData2(self, datalines, ivar_count):
94        """
95        Reads second line/section (if used) of current block of data.
96        """
97        # Now get the dependent variables
98        for n in range(self.NV):
99            self.V[n].append([])
100
101        for c in range(self.NX[ivar_count]):
[3356]102            (x_and_v, datalines) = nappy.utils.text_parser.readItemsFromUnknownLines(datalines, self.NV + 1, float)
[3321]103            (x, v) = (x_and_v[0], x_and_v[1:])
104            self.X[ivar_count][1].append(x)
105
106            count = 0
107            for n in range(self.NV):
108                self.V[n][ivar_count].append(v[count])
109                count = count + 1
110
111        rtlines = datalines
112        return rtlines
113
114    def writeData(self):
115        """
116        Writes the data section of the file.
117        This method can be called directly by the user.
118        """
119        # Set up unbounded IV loop
[3461]120
[3321]121        for m in range(len(self.X)):
122
[3461]123            # Write unbounded independent variable mark and auxiliary variables
124            var_string = self.format % self.X[m][0]
125
[3491]126            # Loop through aux vars which includes NX as first aux var
[3489]127            for a in range(self.NAUXV): 
[3461]128                var_string = var_string + (self.format % self.A[a][m])
[3521]129            self.file.write(wrapLine("Data", self.annotation, self.delimiter, "%s\n" % var_string.rstrip(" ,")))
[3321]130
131            # Write second independant variable and dependant variables
132            for p in range(self.NX[m]):
[3461]133                var_string = self.format % self.X[m][1][p]
[3321]134
135                for n in range(self.NV):
[3461]136                    var_string = var_string + (self.format %self.V[n][m][p])
[3321]137
[3521]138                self.file.write(wrapLine("Data", self.annotation, self.delimiter, "%s\n" %var_string.rstrip(" ,")))
Note: See TracBrowser for help on using the repository browser.