-
Notifications
You must be signed in to change notification settings - Fork 6
/
searchParams.py
executable file
·129 lines (117 loc) · 5.69 KB
/
searchParams.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/env python
"""
Author : Peter Slaughter
Date : Feb. 2013
Purpose: Maintain database of RHESsys runtime parameters
"""
import commands, csv, getopt, os, re, string, sys, time
import rhessys.constants as rpc
from rhessys.params import paramDB
paramFileRegex = re.compile('^\s*([\w.-]+)\s*([\w.-]+)#*.*$')
csvFileRegex = re.compile('^\s*(\w+)\s*,(\w+).*$')
def usage():
print''
print 'Program: %s' % sys.argv[0]
print 'Purpose: Search the RHESsys parameter database and output the results'
print ''
print 'Syntax:'
print ' %s --name=<class name> [--comment=<comment string>] [--endDatetime=<datetime>] --format-<format type> [--genus=<genus name>] --location=<location name> --reference=<user comment>] [--searchType=<hierarchical | constrained>] [--species=<species name>] [--startDatetime=<datetime>] [--user=<user name>] --verbose' % sys.argv[0]
print ''
print ' where:'
print ' --name=<name>: any name that can uniquely identify a set of parameters'
print ' - example class names might be "Western Hemlock", "Ponderosa Pine, eastern Sierra"'
print ' --comment=<comment string>: this can be any comment that will be used to search for matching records in the parameter database.'
print ' --endDatetime=<datetime>: select parameters that were inserted into the database before the specified datetime. Datetime can'
print ' be specified in one of the formats: "YYYY-MM-DD", "YYYY-MM-DD HH:MM" or "YYYY-MM-DD HH:MM:SS", for example "2013-04-15 10:25:00"'
print ' --format=<format type>: valid formats are "csv" and "param"'
print ' - if "csv" is specified, all fields from the database are written to the output file, separated by commas'
print ' - if "param" is specified, then name, value, comment fields are output with a format that is suitable for use as a RHESsys parameter file'
print ' --genus=<genus name>: the name of a genus that will be used for searching'
print ' --location=<location name>: the location to search for, e.g. "Oregon"'
print ' --type=<class type>: one of: %s' % str(rpc.VALID_TYPES)
print ' --reference=<reference string>: this can be any reference (citation) that will be used to search for matching records in the parameter database.'
print ' --searchType=<hierarchical | constrained>: perform a hierarchical or constrained search'
print ' --species=<species name">: the name of a genus that will be used for searching'
print ' --startDatetime=<datetime>: select parameters that were inserted into the database after the specified datetime. Datetime can'
print ' be specified in one of the formats: "YYYY-MM-DD", "YYYY-MM-DD HH:MM" or "YYYY-MM-DD HH:MM:SS", for example "2013-04-15 10:25:00"'
print ' --user=<user name>: the name of the person that will be used for searching'
print ' --userdef=<user defined string>: a user defined string that will be used for searching'
print ''
print ' for example:'
print ''
print ' %s --searchType=hierarchical --name="Red Alder" --location="Oregon" --startDatetime="2013-04-16" --format=csv --output="red_alder_orgeon.def" ' % sys.argv[0]
print ''
if __name__ == '__main__':
comment = None
className = None
classType = None
endDatetimeStr = None
startDatetimeStr = None
location = None
outputFormat = "csv"
param = None
genus = None
outputPath = None
reference = None
searchType = None
species = None
user = None
verbose = False
# Parse command line
try:
opts, args = getopt.getopt(sys.argv[1:], "hv", ["comment=", "endDatetime=", "startDatetime=", "format=", "genus=", "location=", "output=", "param=", "reference=", "species=", "type=", "name=", "searchType=", "user=", "verbose"])
except getopt.GetoptError:
# print help information and exit:
print 'Error in command line:\n %s' % sys.argv
print 'exception type= ', sys.exc_type
print 'exception value= ', sys.exc_value
usage()
sys.exit(1)
if len(opts) == 0:
usage()
sys.exit()
for o, a in opts:
if o in ("-h", "--help"):
usage()
sys.exit()
elif o in ("--startDatetime"):
startDatetimeStr = a
elif o in ("--endDatetime"):
endDatetimeStr = a
elif o in ("--comment"):
comment = a
elif o in ("--format"):
outputFormat = a
elif o in ("--genus"):
genus = a
elif o in ("--species"):
species = a
elif o in ("--type"):
classType = a
elif o in ("--location"):
location = a
elif o in ("--name"):
className = a
elif o in ("--output"):
outputPath = a
elif o in ("--param"):
param = a
elif o in ("--reference"):
reference = a
elif o in ("--searchType"):
searchType = a
if (searchType not in rpc.SEARCH_TYPES):
msg = "Invalid search type %s" % searchType
usage()
raise RuntimeError, msg
elif o in ("--user"):
user = a
elif o in ("-v", "--verbose"):
verbose = True
if searchType == None: searchType = rpc.SEARCH_TYPE_CONSTRAINED
if (not outputFormat in rpc.VALID_FORMATS):
msg = "Output format must be one of the following: ", rpc.VALID_FORMATS
raise RuntimeError, msg
DBobj = paramDB()
DBobj.search(searchType, classType, className, location, param, genus, species, startDatetimeStr, endDatetimeStr, user, reference)
DBobj.write(outputPath, outputFormat)