source: nappy/trunk/general.py @ 345

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

updated by selatham for bug fixes and new write methods

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