source: nappy/trunk/general.py @ 347

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/nappy/trunk/general.py@347
Revision 347, 3.3 KB checked in by selatham, 15 years ago (diff)

Incorporates changes by selatham and Ag Stephens. as of 5/11/04.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1"""
2general.py
3=========
4
5Functions and classes general to the NASAAmesIO package.
6
7Functions are:
8
9
10readFFI()   - reads the NASA Ames file format from first line.
11openNAFile()  - opens a NASA Ames File for read or write.
12                    - it does this by mapping to the appropriate class instance.
13
14chooseFFI() - decides which FFI to open for writing.
15
16"""
17
18# Imports from local package
19import naFile, naFile1001, naFile1010, naFile1020, naFile2010
20import naFile2110, naFile2110, naFile2160, naFile2310
21import naFile3010, naFile4010
22
23# Set up global variables
24classMap={1001:naFile1001.NAFile1001, 1010:naFile1010.NAFile1010,
25          1020:naFile1020.NAFile1020, 2010:naFile2010.NAFile2010,
26          2110:naFile2110.NAFile2110, 2160:naFile2160.NAFile2160,
27          2310:naFile2310.NAFile2310, 3010:naFile3010.NAFile3010,
28          4010:naFile4010.NAFile4010}
29
30def readFFI(filename):
31    """
32    Function to read the top line of a NASA Ames file to extract
33    the File Format Index (FFI) and return it as an integer.
34    """
35    topline=open(filename).readline()
36    return int(topline.split()[-1])
37
38def chooseFFI(naDict):
39    """
40    Function to choose the appropriate FFI based on the contents of the
41    'naDict' dictionary object that holds NASA Ames internal variables.
42    """
43    d=naDict
44    if d["NIV"]>4:        # More than 4 independent variables not allowed
45        raise "NASA Ames cannot write more than 4 independent variables."
46    elif d["NIV"]==4:     # 4 independent variables
47        return 4010
48    elif d["NIV"]==3:     # 3 independent variables
49        return 3010
50    elif d["NIV"]==2:     # 2 independent variables
51        # Three choices here:   2010 - Straightforward 2-D variables
52        #                       2110 - one independent variable changes length
53        #                       2160 - some auxiliary variables are character strings
54        for aux_var in d["A"]:
55            if type(aux_var[0])==str:
56                return 2160
57        if type(d["X"][0][0])==list:
58            return 2110
59        else:
60            return 2010
61    elif d["NIV"]==1:     # 1 independent variable
62        # Three choices here:   1001 - No auxiliary variables
63        #                       1010 - Auxiliary variables included
64        #                       1020 - Implied values for independent variable
65        if not d.has_key("NAUXV"):
66            return 1001
67        elif d.has_key("NVPM"):
68            return 1020
69        else:
70            return 1010
71    else:
72        raise """Could not resolve the dictionary object to create a suitable NASA
73        Ames File Format Index (FFI). Please modify the contents and try again."""
74
75
76def openNAFile(filename, mode="r", naDict=None):
77    """
78    Function wrapper around the NASA Ames File classes. Any NASA Ames
79    file can be opened through this function and the appropriate read or
80    write NASA Ames File class instance is returned.
81    """
82    if mode=="r":
83        FFI=readFFI(filename)
84        return apply(classMap[FFI], (filename, mode))
85    elif mode=="w":
86        if naDict.has_key('FFI') and type(naDict['FFI'])==int:
87            FFI=naDict['FFI']
88        else:
89            FFI=chooseFFI(naDict)
90            naDict['FFI']=FFI
91            print "\nFormat identified as:", FFI
92        return apply(classMap[FFI], (filename, mode, naDict))
93    else:
94        raise "File mode not recognised '%s'." % mode
95   
96
97
Note: See TracBrowser for help on using the repository browser.