-
Notifications
You must be signed in to change notification settings - Fork 1
/
getgsc2.py
148 lines (118 loc) · 4.74 KB
/
getgsc2.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
141
142
143
144
145
146
147
148
#! /usr/bin/env python
# A Python script to retrieve lists of sources from the HST
# Guide Star Catalog (currently at version 2.3)
# specific target (astronomical object or coordinates).
#
# Requires Python 2.3 or higher (due to use of optparse and booleans)
#
# HISTORY:
# 29 May 2007: Created.
# Import various standard modules:
import sys, optparse
# Import our libraries:
import utils
import getcoords
import gsc2_archive
VERSION_STRING = "0.1"
NO_COORDS = "NO COORDS"
DEFAULT_ROOTNAME = "gsc2"
DEFAULT_COORDLIST = ['12 00 00', '-70 00 00']
DEFAULT_SAVE_FILE = "gsc2_sources.dat"
DEFAULT_RADIUS = 2.0 # radius in arc minutes
def FetchCoordinates( objectName ):
"""Wrapper function which calls getcoords.GetCoordinates() to fetch
coordinates of objectName from SIMBAD. Returns a two-element list
of strings (first string = RA, second string = Declination)."""
success, coordinates = getcoords.GetCoordinates(objectName)
if ( success >= 1 ):
print "Found object coordinates: RA = %s, Dec = %s" % (coordinates[1],
coordinates[2])
return coordinates[1:]
else:
return None
def QueryArchive( archive, debug=False ):
"""Handles submission of request and evaluation of results for a single archive.
Requires a BasicArchive object [see basic_archive.py] or derived object.
Returns a tuple of (messageString, nDataFound); for SDSS queries, nDataFound
is 1 if data exist, 0 if not, and -1 if there was an I/O error.
If debug = 1, then *all* returned HTML is saved to a file."""
# Query archive and save resulting HTML in a list of lines:
try:
textReceived = archive.QueryServer()
nDataFound = 1
except IOError, e:
messageString = "I/O Error -- " + str(e.args[0]) + ": " + \
str(e.args[1]) + "\n"
if ( str(e.args[1]) == "(7, 'No address associated with nodename')" ):
messageString += 20*" " + "(DNS server is down,"
messageString += " or we're not connected to the Internet?)"
else:
messageString += " (try again later?)"
textReceived = messageString
nDataFound = -1
return (textReceived, nDataFound)
def main(argv):
# Set up target, etc, by parsing command-line:
coordsList = DEFAULT_COORDLIST
dataFound = 0
searchRadius = DEFAULT_RADIUS
usageString = "%prog \"target_name\" [options]\n"
usageString += " %prog --coords=\"hh mm ss dd mm ss\" [filter-list] [options]\n"
usageString += "\n\nExamples: %prog \"NGC 936\""
usageString += "\n %prog \"NGC 936\""
usageString += "\n\nRequested data is stored in an file named \"sdss.tar.gz\" or \"sdss.jpeg\";\n"
usageString += "use -o/--output to specify a different root name instead of \"sdss\"."
parser = optparse.OptionParser(usage=usageString, version="%prog " + VERSION_STRING)
parser.add_option("-o", "--output", dest="filename", default=DEFAULT_ROOTNAME,
help="name for saved data file")
parser.add_option("--coords", dest="coordinates", default=NO_COORDS,
help="RA and Dec as string (\"hh mm ss dd mm ss\" or \"hh:mm:ss dd:mm:ss\")")
(options, args) = parser.parse_args(argv)
if (options.coordinates == NO_COORDS):
# User is evidently giving us a target name
if (len(args) < 2):
print "\nYou must supply a target name (or else use the --coords options).\n"
return 1
targetName = args[1]
if (len(args) > 2):
searchRadius = float(args[2])
print "\nDoing coordinate lookup for \"%s\"..." % targetName
coordsList = FetchCoordinates(targetName)
if (coordsList == getcoords.NO_COORDS_FOUND) or (coordsList is None):
msg = "*** Unable to get coordinates for \"%s\". *** \n" % targetName
msg += "Cannot check Guide Star Catalog -- terminating search.\n"
print msg
return 2
p1 = coordsList[0].split()
p2 = coordsList[1].split()
if ( len(p1) < 3 ) or ( len(p2) < 3 ):
print "*** Warning: coordinates appear to be truncated or low-resolution! ***"
print "Try entering full coordinates (including seconds) by hand with --coords option.\n"
return 2
else:
if (len(args) > 1):
searchRadius = float(args[1])
# Convert user-supplied coordinates to decimal degrees
try:
coordsList = utils.ProcessCoords(options.coordinates)
except utils.CoordinateError, e:
newmsg = "*** Problem with coordinate string \"%s\"\n" % str(e) + "\n"
print newmsg
return 2
# Create an instance of gsc2_archive and prep it for queries
gscServer = gsc2_archive.MakeArchive()
gscServer.InsertCoordinates(coordsList)
print "\nQuerying Guide Star Catalog 2.3 server..."
(responseText, dataFound) = QueryArchive(gscServer)
if (dataFound > 0):
saveFileName = DEFAULT_SAVE_FILE
saveFile = open(responseText, 'w')
saveFile.write(textReceived)
saveFile.close()
else:
print "Problems contacting Guide Star Catalog server:"
print responseText
print "Done!\n"
return 0
if __name__ == '__main__':
main(sys.argv)