source: nappy/trunk/nappy/na_file/na_file_1001.py @ 3519

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

Annotation

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"""
6naFile1001.py
7=============
8
9Container module for NAFile1001 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
18import nappy.utils.common_utils
19getAnnotation = nappy.utils.common_utils.getAnnotation
20
21
22class NAFile1001(nappy.na_file.na_file.NAFile):
23    """
24    Class to read, write and interact with NASA Ames files conforming to the
25    File Format Index (FFI) 1001.
26    """
27
28    def readHeader(self):
29        """
30        Reads FFI-specific header section.
31        """
32        self._readCommonHeader()
33        self.DX = nappy.utils.text_parser.readItemsFromLine(self.file.readline(), self.NIV, float)
34        self.XNAME = nappy.utils.text_parser.readItemsFromLines(self._readLines(self.NIV), self.NIV, str)
35        self._readVariablesHeaderSection()
36        self._readComments()
37
38    def writeHeader(self):
39        """
40        Writes FFI-specific header section.
41        """       
42        self._writeCommonHeader()
43        annotation = getAnnotation("DX", self.annotation, delimiter = self.delimiter)
44        self.header.write((annotation + "%s" * self.NIV + "\n") % tuple(self.DX))
45        annotation = getAnnotation("XNAME", self.annotation, delimiter = self.delimiter)
46        self.header.write((annotation + "%s\n") * self.NIV % tuple(self.XNAME))
47        self._writeVariablesHeaderSection()
48        self._writeComments()
49        self._fixHeaderLength()
50        self.file.write(self.header.read())
51
52    def _setupArrays(self):
53        """
54        Sets up FFI-specific arrays to fill with data (lists of lists).
55        """
56        self.X = []
57        self.V = []
58        # Set up the variables list
59        for n in range(self.NV):
60            self.V.append([])
61
62    def _readData1(self, datalines, ivar_count):
63        """
64        Reads first line/section of current block of data.
65        """
66        (x_and_v, rtlines) = nappy.utils.text_parser.readItemsFromUnknownLines(datalines, 1 + self.NV, float)
67        (x, v) = (x_and_v[0], x_and_v[1:])
68        self.X.append(x)
69        count = 0
70        # Set up mth list in self.V
71        for n in range(self.NV):
72            self.V[n].append(v[count])
73            count = count + 1
74        return rtlines   
75
76    def _readData2(self, datalines, ivar_count):
77        """
78        Reads second line/section (if used) of current block of data.
79        """
80        return datalines 
81
82    def writeData(self):
83        """
84        Writes the data section of the file.
85        This method can be called directly by the user.
86        """
87        for m in range(len(self.X)):
88
89            var_string = var_string + (self.format % self.X[m])
90
91            for n in range(self.NV):
92                var_string = var_string + (self.format % self.V[n][m])
93
94            self.file.write(wrapLine("Data", self.annotation, self.delimiter, "%s\n" % var_string.rstrip(" ,")))
Note: See TracBrowser for help on using the repository browser.