Skip to content

Commit 52da66d

Browse files
committed
Still debugging query, but latest updates for Friday
1 parent 66013dd commit 52da66d

1 file changed

Lines changed: 64 additions & 47 deletions

File tree

‎astroquery/astrometry_net/core.py‎

Lines changed: 64 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Licensed under a 3-clause BSD style license - see LICENSE.rst
2-
from __future__ import print_function
2+
#from __future__ import print_function
33

44
# put all imports organized as shown below
55
# 1. standard library imports
@@ -19,7 +19,7 @@
1919
from ..utils import prepend_docstr_noreturns # automatically generate docs for similar functions
2020
from ..utils import async_to_sync # all class methods must be callable as static as well as instance methods.
2121
from . import SERVER, TIMEOUT # import configurable items declared in __init__.py
22-
22+
import time
2323

2424
# export all the public classes and methods
2525
__all__ = ['Astrometry', 'AstrometryClass']
@@ -29,6 +29,8 @@
2929
# Now begin your main class
3030
# should be decorated with the async_to_sync imported previously
3131

32+
apiurl = 'http://nova.astrometry.net/api/'
33+
3234
@async_to_sync
3335
class AstrometryClass(BaseQuery):
3436

@@ -92,7 +94,13 @@ def build_request(self, catalog, settings={}, x_colname='x', y_colname='y'):
9294
result: str
9395
String used to send request to astrometry.net
9496
"""
95-
apiurl = 'http://nova.astrometry.net/api/'
97+
from urllib import urlencode
98+
from urllib2 import urlopen, Request
99+
from email.mime.multipart import MIMEMultipart
100+
from email.mime.base import MIMEBase
101+
from email.mime.application import MIMEApplication
102+
from email.encoders import encode_noop
103+
import simplejson
96104

97105
# Login to the service
98106
login_url = apiurl + "login"
@@ -105,10 +113,7 @@ def build_request(self, catalog, settings={}, x_colname='x', y_colname='y'):
105113
result = simplejson.loads(txt)
106114
stat = result.get('status')
107115
if stat == 'error':
108-
print("Login error, exiting.")
109-
os.system("rm " + anetcat_file)
110-
os.system("rm " + sexcat_file)
111-
sys.exit()
116+
raise Exception("Login error, exiting")
112117
session_string = result["session"]
113118

114119
# Upload the text file to request a WCS solution
@@ -124,18 +129,24 @@ def build_request(self, catalog, settings={}, x_colname='x', y_colname='y'):
124129
'parity': 0,
125130
'session': session_string
126131
}
127-
upload_args = upload_args.update(settings)
132+
upload_args.update(settings)
128133
upload_json = simplejson.dumps(upload_args)
129134

130135
# Format the list in the appropriate format
131136
# Currently this is just using the code from astrometry.net's client.py
132137
# Ideally this could probably be re-written and improved
133-
src_list = '\n'.join(['{0:.3f}\t{1:.3f}'.format(row[x_colname], row[y_colname])
134-
for row in catalog])
138+
#src_list = '\n'.join(['{0:.3f}\t{1:.3f}'.format(row[x_colname], row[y_colname])
139+
# for row in catalog])
140+
#print 'src_list:', src_list
141+
135142
temp_file = 'temp.cat'
136-
f = open(tempfile, 'w')
137-
f.write(src_list)
138-
f = open(upload_filename, 'rb')
143+
f = open(temp_file, 'w')
144+
#f.write(src_list)
145+
for source in catalog:
146+
f.write('{0:.3f}\t{1:.3f}\n'.format(source[x_colname], source[y_colname]))
147+
f.close()
148+
149+
f = open(temp_file, 'rb')
139150
file_args=(temp_file, f.read())
140151

141152
m1 = MIMEBase('text', 'plain')
@@ -207,7 +218,22 @@ def submit(self, url, headers, data):
207218
data: str
208219
data packet to send in request
209220
"""
210-
request = Request(url=url, header=headers, data=data)
221+
from urllib2 import urlopen, Request
222+
import simplejson
223+
224+
print 'header:\n', headers
225+
print 'data:\n',data
226+
request = Request(url=url, headers=headers, data=data)
227+
f = urlopen(request)
228+
txt = f.read()
229+
print txt
230+
result = simplejson.loads(txt)
231+
stat = result.get('status')
232+
if stat == 'error':
233+
raise Exception("Upload error, exiting.")
234+
subid = result["subid"]
235+
236+
return subid
211237

212238
def get_submit_status(self, subid, timeout=30):
213239
"""
@@ -227,6 +253,9 @@ def get_submit_status(self, subid, timeout=30):
227253
jobid astrometry.net has assigned to the submitted list
228254
"""
229255
import math
256+
from urllib2 import urlopen, Request
257+
import simplejson
258+
230259
# Check submission status
231260
subcheck_url = apiurl + "submissions/" + str(subid)
232261

@@ -272,6 +301,12 @@ def get_wcs_file(self, jobs, timeout=90):
272301
header: `astropy.io.fits.Header`
273302
Header returned by astrometry.net
274303
"""
304+
import math
305+
from urllib2 import urlopen, Request
306+
import simplejson
307+
308+
print 'jobs', jobs
309+
275310
# Attempt to load wcs from astrometry.net
276311
n_jobs = len(jobs)
277312
still_processing = True
@@ -280,7 +315,7 @@ def get_wcs_file(self, jobs, timeout=90):
280315
max_attempts = math.ceil(timeout/5)
281316
while still_processing and n_failed_attempts < max_attempts and n_failed_jobs < n_jobs:
282317
time.sleep(5)
283-
for job_id in job_id_list:
318+
for job_id in jobs:
284319
jobcheck_url = apiurl + "jobs/" + str(job_id)
285320
print(jobcheck_url)
286321
request = Request(url=jobcheck_url)
@@ -291,7 +326,7 @@ def get_wcs_file(self, jobs, timeout=90):
291326
if result["status"] == "failure":
292327
print('failed')
293328
n_failed_jobs += 1
294-
job_id_list.remove(job_id)
329+
jobs.remove(job_id)
295330
if result["status"] == "success":
296331
print('success')
297332
solved_job_id = job_id
@@ -300,9 +335,8 @@ def get_wcs_file(self, jobs, timeout=90):
300335
n_failed_attempts += 1
301336

302337
if still_processing == True:
303-
print("Astrometry.net took too long to process, so we're exiting. Try checking "+
304-
"astrometry.net again later")
305-
return
338+
raise Exception("Astrometry.net took too long to process, so we're exiting. " +
339+
"Try checking astrometry.net again later")
306340

307341
if still_processing == False:
308342
import wget
@@ -363,7 +397,7 @@ def get_wcs_file(self, jobs, timeout=90):
363397
return wcs_header
364398

365399
def solve(self, sources, settings, x_colname="x", y_colname="y", fwhm_colname=None,
366-
fwhm_std_cut=1, flags_colname=None, flux=None, timeout=30):
400+
flag_colname=None, flux_colname=None, fwhm_std_cut=1, timeout=30):
367401
"""
368402
First draft of function to send a catalog or image to astrometry.net and
369403
return the astrometric solution.
@@ -403,19 +437,10 @@ def solve(self, sources, settings, x_colname="x", y_colname="y", fwhm_colname=No
403437
# Temporarily import all of the modules used by astromery.net's client to get the
404438
# astrometric solution. In the future this can be cleaned up and done in a more
405439
# standard way
406-
import os
407-
import sys
408-
from numpy import median, std
409440
import astropy.io.fits as pyfits
410441
import simplejson
411-
from urllib import urlencode
442+
from numpy import median, std
412443
from urllib2 import urlopen, Request
413-
from email.mime.multipart import MIMEMultipart
414-
from email.mime.base import MIMEBase
415-
from email.mime.application import MIMEApplication
416-
from email.encoders import encode_noop
417-
import time
418-
import subprocess
419444

420445
# Remove flagged sources
421446
if flag_colname is not None:
@@ -431,37 +456,29 @@ def solve(self, sources, settings, x_colname="x", y_colname="y", fwhm_colname=No
431456
fwhm_upper = fwhm_med+fwhm_std_cut * fwhm_std
432457
catalog = catalog[(catalog[fwhm_colname]<fwhm_upper) &
433458
(catalog[fwhm_colname]>fwhm_lower)]
434-
459+
#catalog = catalog.group_by(flux_colname)
460+
catalog.sort(flux_colname)
461+
catalog.reverse()
435462
# Only choose the top 50 sources
436463
if len(catalog)>50:
437464
catalog = catalog[:50]
438-
catalog = catalog.group_by(flux_colname)
465+
466+
print 'catalog', catalog
439467

440468
# Create a list of coordinates in a format that astrometry.net recognizes
441-
upload_kwargs = self.build_request(settings, catalog, x_colname, y_colname)
469+
upload_kwargs = self.build_request(catalog, settings, x_colname, y_colname)
442470

443-
self.submit(**upload_kwargs)
471+
print upload_kwargs['data']
444472

445-
f = urlopen(request)
446-
txt = f.read()
447-
result = simplejson.loads(txt)
448-
stat = result.get('status')
449-
if stat == 'error':
450-
print("Upload error, exiting.")
451-
os.system("rm " + temp_file)
452-
sys.exit()
453-
subid = result["subid"]
454-
455-
print('stat', stat)
456-
print('id', result['subid'])
473+
subid = self.submit(**upload_kwargs)
457474

458475
time.sleep(5)
459476

460477
jobs = self.get_submit_status(subid, 30)
461478

462479
time.sleep(5)
463480

464-
wcs_fits = self.get_wcs_file(job_id, timeout)
481+
wcs_fits = self.get_wcs_file(jobs, timeout)
465482

466483
return result
467484

0 commit comments

Comments
 (0)