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

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