source: nappy/trunk/general.py @ 366

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

Latest version - for compatibility with web service. Includes multiple file
output on conversion from NetCDF.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
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"""
6general.py
7=========
8
9Functions and classes general to the NASAAmesIO package.
10
11Functions are:
12
13
14readFFI()   - reads the NASA Ames file format from first line.
15openNAFile()  - opens a NASA Ames File for read or write.
16                    - it does this by mapping to the appropriate class instance.
17
18chooseFFI() - decides which FFI to open for writing.
19
20"""
21
22# Imports from local package
23import naFile, naFile1001, naFile1010, naFile1020, naFile2010
24import 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    # 2 independent variables
55        # Four choices here:   2010 - Straightforward 2-D variables
56        #                       2110 - one independent variable changes length and the values are specified
57        #                       2310 - one indepenent variable changes length but only the first value is specifically stated
58        #                       2160 - the  independent unbounded variable is a character string
59        #                                 (not "some auxiliary variables are character strings" as previously coded)
60
61    elif d["NIV"]==2 and type(d["X"][0][0])==str:
62        return 2160
63    elif d["NIV"]==2 and type(d["X"][0][1])==list and len(d["X"][0][1])>1:
64        return 2110
65    elif d["NIV"]==2 and type(d["X"][0][1])==list and len(d["X"][0][1])==1:
66        return 2310
67    elif d["NIV"]==2:
68        return 2010
69        #for aux_var in d["A"]:
70         #   if type(aux_var[0])==str:
71          #      return 2160
72        #print d["X"][0][0]
73        #if type(d["X"][0][0])==str:
74         #   return 2160
75        #elif type(d["X"][0][1])==list:
76          #  return 2110
77       # else:
78         #   return 2010
79    elif d["NIV"]==1:     # 1 independent variable
80        # Three choices here:   1001 - No auxiliary variables
81        #                       1010 - Auxiliary variables included
82        #                       1020 - Implied values for independent variable
83        if not d.has_key("NAUXV"):
84            return 1001
85        elif d.has_key("NVPM"):
86            return 1020
87        else:
88            return 1010
89    else:
90        raise "Could not resolve the dictionary object to create a suitable NASA Ames File Format Index (FFI). Please modify the contents and try again."
91
92
93def openNAFile(filename, mode="r", naDict=None, spacer="    ", floatFormat="%8.3f"):
94    """
95    Function wrapper around the NASA Ames File classes. Any NASA Ames
96    file can be opened through this function and the appropriate read or
97    write NASA Ames File class instance is returned.
98    """
99    if mode=="r":
100        FFI=readFFI(filename)
101        return apply(classMap[FFI], (filename, mode))
102    elif mode=="w":
103        if naDict.has_key('FFI') and type(naDict['FFI'])==int:
104            FFI=naDict['FFI']
105        else:
106            FFI=chooseFFI(naDict)
107            naDict['FFI']=FFI
108            print "\nFormat identified as:", FFI   
109        return apply(classMap[FFI], (filename, mode, naDict, spacer, floatFormat))
110    else:
111        raise "File mode not recognised '%s'." % mode
112   
113
114
Note: See TracBrowser for help on using the repository browser.