-
Notifications
You must be signed in to change notification settings - Fork 0
/
sample.py
140 lines (114 loc) · 4.25 KB
/
sample.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
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/env python
import getopt, sys, urllib, time
def main():
status = 0
# input arguments
try:
opts, args = getopt.getopt(sys.argv[1:],"h:iq",
["help","invid=","quarter="])
except getopt.GetoptError:
usage()
tree = False
for o, a in opts:
if o in ("-h", "--help"):
usage()
if o in ("-i", "--invid"):
invid = str(a)
if o in ("-q", "--quarter"):
quarter = int(a)
kepid, invid, kepmag, mode, start, stop, release = GetMetaData(invid,quarter)
# convert Gregorian date to Julian date
def Greg2JD(year, month, day):
if (month < 3):
y = float(year) - 1.0
m = float(month) + 12.0
else:
y = float(year)
m = float(month)
a = 0; b = 0
if (y + m / 12 + float(day) / 365 > 1582.87166):
a = int(y / 100)
b = 2 - a + int(float(a / 4))
c = 0
if (y < 0.0):
c = int(365.25 * y - 0.75)
else:
c = int(365.25 * y)
d = int(30.6001 * (m + 1))
jd = float(b + c + d + day + 1720994.5);
return jd
# start and stop Julian dates for Kepler quarters
def QuarterDates(quarter):
Qstart = [2454953.5,2454964.5,2454998.5]
Qstop = [2454962.5,2454997.5,2455100.5]
if (quarter < len(Qstart)):
return Qstart[quarter] - 10, Qstop[quarter] + 10
else:
message = 'No spacecraft roll dates recorded for quarter ' + str(quarter) + '.\n'
message += 'Find an updated script at http://keplergo.arc.nasa.gov'
sys.exit(message)
def GetMetaData(invid,quarter):
# get start and stop dates for quarter
Qstart, Qstop = QuarterDates(quarter)
# URL for MAST data access
url = 'http://archive.stsci.edu/kepler/data_search/search.php?'
url += 'action=Search'
url += '&max_records=100000'
url += '&verb=3'
url += '&ktc_investigation_id=' + invid
url += '&ktc_target_type[]=LC'
url += '&ktc_target_type[]=SC'
url += '&outputformat=CSV'
# retrieve results from MAST
lines = urllib.urlopen(url)
# extract metadata from CSV
print '\n%4s %9s %7s %5s %2s %10s %10s %10s' % \
('#', 'KepID', 'InvID', 'KpMag', 'Md', 'Start', 'Stop', 'Release')
kepid = []; invid = []; mode = []
ra = []; dec = []; kepmag = []
start = []; stop = []; release = []
for line in lines:
line = line.strip().split(',')
if (len(line[0]) > 0 and
'Kepler' not in line[0] and
'integer' not in line[0] and
'no rows found' not in line[0]):
GregStart = line[7][:10].split('-')
GregStop = line[8][:10].split('-')
JDstart = Greg2JD(int(GregStart[0]),int(GregStart[1]),int(GregStart[2]))
JDstop = Greg2JD(int(GregStop[0]),int(GregStop[1]),int(GregStop[2]))
if (JDstart > Qstart and JDstop < Qstop):
kepid.append(line[0])
invid.append(line[1])
kepmag.append(float(line[22]))
mode.append(line[6])
ra.append(line[4])
dec.append(line[5])
start.append(line[7])
stop.append(line[8])
release.append(line[9])
print '%4d %9s %7s %5.2f %2s %10s %10s %10s' % \
(len(kepid), kepid[-1], invid[-1], kepmag[-1], mode[-1],
start[-1][:10], stop[-1][:10], release[-1][:10])
if (len(kepid) == 0):
print '\nNo data available as of ' + time.asctime(time.localtime())
else:
print '\n' + time.asctime(time.localtime())
return kepid, invid, kepmag, mode, start, stop, release
def usage():
print ' -------------------------------------------------------------------------'
print ' Martin Still (martin.d.still@nasa.gov) NASA Ames Nov 11, 2009'
print ' '
print ' Find all quarter data associated with an investigation ID within the MAST'
print ' archive.'
print ' '
print ' Typical usage:'
print ' KepInvestigationAtMAST.py --invid=GO10003 --quarter=1'
print ' '
print ' --invid Investigation ID number of GO program'
print ' --quarter Kepler quarter (integer number)'
print ' -------------------------------------------------------------------------'
sys.exit(' ')
#-------------------------------
if __name__ == "__main__":
main()