source: TI03-DataExtractor/branches/old_stuff/latest_dx/dx/pydxs/common.py @ 793

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI03-DataExtractor/branches/old_stuff/latest_dx/dx/pydxs/common.py@793
Revision 793, 6.0 KB checked in by astephen, 13 years ago (diff)

Put all the old code in the old_stuff branch.

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"""
6common.py
7=========
8
9Holds common functions and classes used in the package.
10
11"""
12
13# Import python modules
14import os
15
16# Import package modules
17from serverConfig import *
18
19# Set up commmon variables. These are CAPITALISED to
20# improve visibility in other modules.
21
22STAGES=["DatasetGroupPage", "DatasetPage", "VariablesPage", "DomainPage", "ResultsPage"]
23
24DOMAIN_PARAMS=("start_time", "end_time", "time_interval", "horiz_domain", 
25                      "resolution", "vertical_units", "vertical_domain", 
26                       "outputFormat")
27
28TIME_KEYS=["Year", "Month", "Day", "Hour", "Minute", "Second"]
29
30HORIZ_KEYS=["northernExtent", "easternExtent", "southernExtent", "westernExtent"]
31
32CF_METADATA_GLOBAL_ATTRIBUTE_KEYS="Conventions title source institution history references comment".split()
33
34# Set up common functions
35
36def createTimeKeyList():
37    """
38    Returns all the separate date and time component arguments required as a list.
39    """
40    allTimeKeys=[]
41    for ts in ["start", "end"]:
42        for i in range(1, MAX_NUM_DATASETS+1):
43            for key in TIME_KEYS:
44                allTimeKeys.append("%s%s_%s" % (ts, key, i))
45    return allTimeKeys
46
47def isUndefined(dict, item): 
48    """
49    Function that returns 1 if item is not a key in dict or
50    is defined as None in dict.
51    """
52    if dict.has_key(item):
53        if dict[item]!=None:
54            return 0
55    return 1
56
57def keyPatternMatch(dct, pattern):
58    """
59    Returns 1 if one or more keys in the dictionary 'dct' match the
60    pattern provided using string.find(). Returns 0 otherwise.
61    """
62    for key in dct.keys():
63        if key.find(pattern)>-1:
64            return 1
65    return 0
66
67
68def getValuesInRange(start, end, array):
69    """
70    Takes a start and end value and returns the values in the array that are between them.
71    If not in range and are the same value then returns [start].
72    """
73    # check all are floats
74    array=map(lambda x: float(x), list(array))
75    if array[0]>array[-1]: array.reverse()
76    if start>end:  (start, end)=(end, start)
77    (start, end)=(float(start), float(end))
78    rtarray=[]
79    for i in array:
80        if i>=start and i<=end:
81            rtarray.append(i)
82    if rtarray==[] and start==end:
83        rtarray=[start]     
84    return rtarray
85
86
87def deUnicodeObject(obj):
88    """
89    Returns the object identical except unicode strings are all
90    converted to normal strings.
91    """
92    tupleFound=None
93    if type(obj)==type((1,2)): 
94        tupleFound=1
95        obj=list(obj)
96
97    if type(obj)==type([]):
98        rtobj=[]
99        for i in obj:
100            if type(i) in (type([]), type((1,2))):
101                rtobj.append(deUnicodeObject(i)) 
102            elif type(i)==type(u""):
103                rtobj.append(str(i))
104            else:
105                rtobj.append(i)
106        if tupleFound==1: rtobj=tuple(rtobj)
107
108    elif type(obj)==type(u""):
109        rtobj=str(obj)
110    else: 
111        rtobj=obj
112    return rtobj
113
114def overlap(list1, list2):
115    """
116    overlap function - returns a list of overlapping items in list1 and list2.
117    Otherwise returns None.
118    """ 
119    rtlist=[]
120    for i in list1:
121       if i in list2: rtlist.append(i)
122
123    if len(rtlist)>0:
124        return rtlist
125    else:
126        return None
127
128def compareAxes(ax1, ax2):
129    """Takes 2 cdms axis objects returning 1 if they are essentially
130    the same and 0 if not."""
131    import cdms
132    for axtype in ("time", "level", "latitude", "longitude"):
133        if cdms.axisMatches(ax1, axtype) and not cdms.axisMatches(ax2, axtype):
134            return 0
135
136    # Check ids
137    if ax1.id!=ax2.id: return 0
138    # Check lengths
139    if len(ax1)!=len(ax2): return 0
140    # Check values
141    if ax1._data_!=ax2._data_: return 0
142    # Check units
143    if ax1.units!=ax2.units: return 0
144    # OK, I think they are the same axis!
145    return 1
146
147def compareGrids(grid1, grid2):
148    """Takes 2 cdms grid objects returning 1 if they are essentially
149    the same and 0 if not."""
150    import cdms
151    if compareAxes(grid1.getLatitude(), grid2.getLatitude())==0: return 0
152    if compareAxes(grid1.getLongitude(), grid2.getLongitude())==0: return 0
153    return 1
154
155def makeDirsAndPerms(basedir, dirs, permissions, owner, verbose="no"):
156    """
157    A function for making directories recursively and setting permissions/ownership.
158    """
159    if type(dirs)==str: dirs=[dirs]
160    dirs=[basedir]+list(dirs)
161    dir=None
162
163    while len(dirs)>0:
164
165        if dir:
166            dir=os.path.join(dir, dirs[0])
167        else:
168            dir=dirs[0]
169        dirs=dirs[1:]
170
171        if not os.path.exists(dir):
172            if verbose=="yes":   print "Making directory:", dir
173            os.mkdir(dir)
174            os.chmod(dir, permissions)
175            os.system('/bin/chown %s %s' % (owner, dir))
176
177    return
178   
179def checkSubDirectory(user=None):
180    """
181    checkSubDirectory method - checks if the required sub-directory exists
182    to write the output and if not it creates it.
183    """ 
184    if user==None or user=="None":   user="anonymous"
185    outputDir=os.path.join(OUTPUT_DIR, user, "dx_output")
186    if not os.path.isdir(outputDir):
187        makeDirsAndPerms(OUTPUT_DIR, (user, "dx_output"), OUTPUT_DIR_PERMISSION, "%s.%s" %
188                                           (OUTPUT_FILE_USER, OUTPUT_FILE_GROUP))
189    # Local rule follows
190    if LOCAL_RULES==1:  makeAccessControlFile(user)     
191    return outputDir
192   
193# Set up common classes
194class RedirectStdout:
195    """
196    RedirectStdout class - used to direct standard output away from
197    the screen in CGI scripts.
198    """
199
200    def write(self, item):
201        """
202        write method - allows dummy standard out to work.
203        """
204        pass
205
206    def flush(self):
207        """
208        Method to do nothing, again!
209        """
210        pass
Note: See TracBrowser for help on using the repository browser.