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

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

Put all the old code in the old_stuff branch.

  • Property svn:executable set to *
Line 
1"""
2
3common.py
4=========
5
6Holds common functions and classes used in the Data Extractor software.
7
8"""
9
10# Import python modules
11import os
12
13# Import package modules
14from config import *
15
16# Set up commmon variables. These are CAPITALISED to
17# improve visibility in other modules.
18HTTP_HEADER="Content-Type: text/html\n\n"
19
20STAGES=["DatasetGroupPage", "DatasetPage", "VariablesPage", "DomainPage", "ResultsPage"]
21
22DOMAIN_PARAMS=("start_time", "end_time", "time_interval", "horiz_domain", 
23                      "resolution", "vertical_units", "vertical_domain", 
24                       "output_type")
25
26TIME_KEYS=("year", "month", "day", "hour", "min", "sec")
27
28HORIZ_KEYS=("northernExtent", "easternExtent", "southernExtent", "westernExtent")
29
30CF_METADATA_GLOBAL_ATTRIBUTES="Conventions title source institution history references comment".split()
31
32# Set up common functions
33
34def getValuesInRange(start, end, array):
35    """
36    getValuesInRange function - takes a start and end value and returns the
37    values in the array that are between them.
38    """
39    # check all are floats
40    array=map(lambda x: float(x), list(array))
41    if array[0]>array[-1]: array.reverse()
42    if start>end:  (start, end)=(end, start)
43    (start, end)=(float(start), float(end))
44    rtarray=[]
45    for i in array:
46        if i>=start and i<=end:
47            rtarray.append(i)
48    return rtarray
49
50
51def overlap(list1, list2):
52    """
53    overlap function - returns a list of overlapping items in list1 and list2.
54    Otherwise returns None.
55    """ 
56    rtlist=[]
57    for i in list1:
58       if i in list2: rtlist.append(i)
59
60    if len(rtlist)>0:
61        return rtlist
62    else:
63        return None
64
65
66def makeDirsAndPerms(basedir, dirs, permissions, owner, verbose="no"):
67    """
68    A function for making directories recursively and setting permissions/ownership.
69    """
70    if type(dirs)==str: dirs=[dirs]
71    dirs=[basedir]+list(dirs)
72    dir=None
73
74    while len(dirs)>0:
75
76        if dir:
77            dir=os.path.join(dir, dirs[0])
78        else:
79            dir=dirs[0]
80        dirs=dirs[1:]
81
82        if not os.path.exists(dir):
83            if verbose=="yes":   print "Making directory:", dir
84            os.mkdir(dir)
85            os.chmod(dir, permissions)
86            os.system('/bin/chown %s %s' % (owner, dir))
87
88    return
89   
90def checkSubDirectory(user=None):
91    """
92    checkSubDirectory method - checks if the required sub-directory exists
93    to write the output and if not it creates it.
94    """ 
95    if user==None or user=="None":   user="anonymous"
96    outputDir=os.path.join(OUTPUT_DIR, user, "dx_output")
97    if not os.path.isdir(outputDir):
98        makeDirsAndPerms(OUTPUT_DIR, (user, "dx_output"), OUTPUT_DIR_PERMISSION, "%s.%s" %
99                                           (OUTPUT_FILE_USER, OUTPUT_FILE_GROUP))
100    # Local rule follows
101    if LOCAL_RULES==1:  makeAccessControlFile(user)     
102    return outputDir
103   
104# Set up common classes
105class RedirectStdout:
106    """
107    RedirectStdout class - used to direct standard output away from
108    the screen in CGI scripts.
109    """
110
111    def write(self, item):
112         """
113         write method - allows dummy standard out to work.
114         """
115         pass
116
Note: See TracBrowser for help on using the repository browser.