source: nappy/trunk/general.py @ 339

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/nappy/trunk/general.py@342
Revision 339, 2.7 KB checked in by astephen, 16 years ago (diff)

Initial revision

  • 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# Import required modules
19import os
20import textParser
21from naFile import *
22
23# Set up global variables
24classMap={1001:NAFile1001, 1010:NAFile1010, 1020:NAFile1020,
25          2010:NAFile2010, 2110:NAFile2110, 2160:NAFile2160,
26          2310:NAFile2310, 3010:NAFile3010, 4010:NAFile4010}
27
28def readFFI(filename):
29    topline=open(filename).readline()
30    return int(topline.split()[-1])
31
32
33def chooseFFI(naDict):
34    d=naDict
35    if d["NIV"]>4:        # More than 4 independent variables not allowed
36        raise "NASA Ames cannot write more than 4 independent variables."
37    elif d["NIV"]==4:     # 4 independent variables
38        return 4010
39    elif d["NIV"]==3:     # 3 independent variables
40        return 3010
41    elif d["NIV"]==2:     # 2 independent variables
42        # Three choices here:   2010 - Straightforward 2-D variables
43        #                       2110 - one independent variable changes length
44        #                       2160 - some auxiliary variables are character strings
45        for aux_var in d["A"]:
46            if type(aux_var[0])==str:
47                return 2160
48        if type(d["X"][0][0])==list:
49            return 2110
50        else:
51            return 2010
52    elif d["NIV"]==1:     # 1 independent variable
53        # Three choices here:   1001 - No auxiliary variables
54        #                       1010 - Auxiliary variables included
55        #                       1020 - Implied values for independent variable
56        if not d.has_key("NAUXV"):
57            return 1001
58        elif d.has_key("NVPM"):
59            return 1020
60        else:
61            return 1010
62    else:
63        raise """Could not resolve the dictionary object to create a suitable NASA
64        Ames File Format Index (FFI). Please modify the contents and try again."""
65
66
67def openNAFile(filename, mode="r", naDict=None):
68    """
69    Works out the correct FFI and maps to the appropriate NASA Ames File
70    class.
71    """
72    if mode=="r":
73        FFI=readFFI(filename)
74        return apply(classMap[FFI], (filename, mode))
75    elif mode=="w":
76        if naDict.has_key('FFI') and type(naDict['FFI'])==int:
77            FFI=naDict['FFI']
78        else:
79            FFI=chooseFFI(naDict)
80            naDict['FFI']=FFI
81            print "\nFormat identified as:", FFI
82        return apply(classMap[FFI], (filename, mode, naDict))
83    else:
84        raise "File mode not recognised '%s'." % mode
85   
86
87
Note: See TracBrowser for help on using the repository browser.