From 0f1e6c2d80a2fba3143eded2de7a3c80b459f032 Mon Sep 17 00:00:00 2001 From: Adam Date: Tue, 26 Jun 2012 20:07:42 +0200 Subject: [PATCH 1/5] added magpis query tool --- astrodata/magpis/magpis.py | 108 +++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 astrodata/magpis/magpis.py diff --git a/astrodata/magpis/magpis.py b/astrodata/magpis/magpis.py new file mode 100644 index 0000000000..833dc0acf3 --- /dev/null +++ b/astrodata/magpis/magpis.py @@ -0,0 +1,108 @@ +""" +MAGPIS Image and Catalog Query Tool +----------------------------------- + +:Author: Thomas Robitalle (thomas.robitaille@gmail.com) +:Author: Adam Ginsburg (adam.g.ginsburg@gmail.com) +""" +import cookielib +import urllib2 +import urllib +import htmllib +import formatter +import os +import sys +try: + import astropy.io.fits as pyfits +except ImportError: + import pyfits +from math import cos, radians +import multiprocessing as mp +import time +import tempfile +import StringIO + +url_gpscutout = "http://third.ucllnl.org/cgi-bin/gpscutout" + + +surveys = ["gps6epoch3", + "gps6epoch4", + "gps20", + "gps20new", + "gps90", + "gpsmsx", + "gpsmsx2", + "gpsglimpse36", + "gpsglimpse45", + "gpsglimpse58", + "gpsglimpse80", + "mipsgal", + "bolocam"] + + +def get_image_gal(glon, glat, survey='bolocam', size=1.0, + verbose=False, savename=None, save=True, + overwrite=False): + """ + Get an image at a specified glon/glat. Size can be specified + WARNING: MAGPIS has a maxmimum image size of about 2048x2048 + + Parameters + ---------- + glon : float + glat : float + Galactic latitude and longitude at the center + survey : string + Which MAGPIS survey do you want to cut out? + frametype : ['stack','normal','interleave','deep%stack','confidence','difference','all'] + The type of image + savename : None or string + filename to save fits file as. If None, will become G###.###p###.###_(survey).fits + size : float + Size of cutout (symmetric) in arcminutes + verbose : bool + Print out extra error messages? + save : bool + Save FITS file? + overwrite : bool + Overwrite if file already exists? + + Examples + -------- + >>> R = Request() + >>> fitsfile = R.get_image_gal(10.5,0.0) + """ + + if survey not in surveys: + raise ValueError("Invalide survey. Valid surveys are: %s" % surveys) + + # Construct request + request = {} + request["Survey"] = survey + # NOTE: RA is passed as a 2-part string, DEC is not used. Whoops! + request["RA"] = "%s %s" % (glon,glat) + # request["Dec"] = + request["Equinox"] = "Galactic" + request["ImageSize"] = size + request["ImageType"] = "FITS" + + # these options are not used + # optional request["MaxInt"] = 10 + # optional request["Epochs"] = + # optional request["Fieldname"] = + + # create the request header data + request = urllib.urlencode(request) + # load the URL as text + U = urllib.urlopen(url_gpscutout, request) + # turn the text into a StringIO object for FITS reading + S = StringIO.StringIO(U.read()) + fitsfile = pyfits.open(S) + + if save: + if savename is None: + savename = "G%08.4f%+09.4f_%s.fits" % (glon,glat,survey) + fitsfile.writeto(savename, clobber=overwrite) + + return fitsfile + From 3c60e124357726eeee3dedaf9d6fa12cc88d981b Mon Sep 17 00:00:00 2001 From: Adam Date: Tue, 26 Jun 2012 20:58:06 +0200 Subject: [PATCH 2/5] fixed magpis __init__ / import added magpis docs --- astrodata/magpis/__init__.py | 1 + astrodata/magpis/magpis.py | 4 ++-- docs/astrodata/magpis.rst | 21 +++++++++++++++++++++ 3 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 astrodata/magpis/__init__.py create mode 100644 docs/astrodata/magpis.rst diff --git a/astrodata/magpis/__init__.py b/astrodata/magpis/__init__.py new file mode 100644 index 0000000000..d2909380c4 --- /dev/null +++ b/astrodata/magpis/__init__.py @@ -0,0 +1 @@ +from .magpis import * diff --git a/astrodata/magpis/magpis.py b/astrodata/magpis/magpis.py index 833dc0acf3..ad8cf3e0df 100644 --- a/astrodata/magpis/magpis.py +++ b/astrodata/magpis/magpis.py @@ -24,6 +24,7 @@ url_gpscutout = "http://third.ucllnl.org/cgi-bin/gpscutout" +__all__ = ['get_image_gal'] surveys = ["gps6epoch3", "gps6epoch4", @@ -69,8 +70,7 @@ def get_image_gal(glon, glat, survey='bolocam', size=1.0, Examples -------- - >>> R = Request() - >>> fitsfile = R.get_image_gal(10.5,0.0) + >>> fitsfile = get_image_gal(10.5,0.0) """ if survey not in surveys: diff --git a/docs/astrodata/magpis.rst b/docs/astrodata/magpis.rst new file mode 100644 index 0000000000..a1f5b15ec3 --- /dev/null +++ b/docs/astrodata/magpis.rst @@ -0,0 +1,21 @@ +.. _astrodata.magpis: + +***************************************** +MAGPIS Queries (`astrodata.magpis`) +***************************************** + +Getting started +=============== + +The following example illustrates a MAGPIS image query:: + + >>> from astrodata import magpis + >>> fitsfile = magpis.get_image_gal(10.5,0.0) + >>> fitsfile = magpis.get_image_gal(10.5,0.0,survey='gpsmsx') + + +Reference/API +============= + +.. automodapi:: astrodata.magpis + :no-inheritance-diagram: From f2443380f58d19b19675f7fcee0be02601325e2a Mon Sep 17 00:00:00 2001 From: Adam Date: Tue, 26 Jun 2012 21:05:17 +0200 Subject: [PATCH 3/5] astropy-friendliness --- astrodata/magpis/magpis.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/astrodata/magpis/magpis.py b/astrodata/magpis/magpis.py index ad8cf3e0df..a1684ba739 100644 --- a/astrodata/magpis/magpis.py +++ b/astrodata/magpis/magpis.py @@ -12,10 +12,7 @@ import formatter import os import sys -try: - import astropy.io.fits as pyfits -except ImportError: - import pyfits +import astropy.io.fits as pyfits from math import cos, radians import multiprocessing as mp import time @@ -24,7 +21,7 @@ url_gpscutout = "http://third.ucllnl.org/cgi-bin/gpscutout" -__all__ = ['get_image_gal'] +__all__ = ['get_magpis_image_gal'] surveys = ["gps6epoch3", "gps6epoch4", @@ -41,7 +38,7 @@ "bolocam"] -def get_image_gal(glon, glat, survey='bolocam', size=1.0, +def get_magpis_image_gal(glon, glat, survey='bolocam', size=1.0, verbose=False, savename=None, save=True, overwrite=False): """ From 9b2e9d54569a0d3091879a9d0bde0d218a479fac Mon Sep 17 00:00:00 2001 From: Adam Date: Tue, 26 Jun 2012 21:05:57 +0200 Subject: [PATCH 4/5] fixed docs, code to agree with get_magpis_image_gal --- astrodata/magpis/magpis.py | 2 +- docs/astrodata/magpis.rst | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/astrodata/magpis/magpis.py b/astrodata/magpis/magpis.py index a1684ba739..74e24e8a32 100644 --- a/astrodata/magpis/magpis.py +++ b/astrodata/magpis/magpis.py @@ -67,7 +67,7 @@ def get_magpis_image_gal(glon, glat, survey='bolocam', size=1.0, Examples -------- - >>> fitsfile = get_image_gal(10.5,0.0) + >>> fitsfile = get_magpis_image_gal(10.5,0.0) """ if survey not in surveys: diff --git a/docs/astrodata/magpis.rst b/docs/astrodata/magpis.rst index a1f5b15ec3..29faf23b5a 100644 --- a/docs/astrodata/magpis.rst +++ b/docs/astrodata/magpis.rst @@ -10,8 +10,8 @@ Getting started The following example illustrates a MAGPIS image query:: >>> from astrodata import magpis - >>> fitsfile = magpis.get_image_gal(10.5,0.0) - >>> fitsfile = magpis.get_image_gal(10.5,0.0,survey='gpsmsx') + >>> fitsfile = magpis.get_magpis_image_gal(10.5,0.0) + >>> fitsfile = magpis.get_magpis_image_gal(10.5,0.0,survey='gpsmsx') Reference/API From f7f76e0f64a8bdb9617e1fd67076f2d06d884ccd Mon Sep 17 00:00:00 2001 From: Adam Date: Fri, 29 Jun 2012 11:14:17 +0200 Subject: [PATCH 5/5] minor change to reading method --- astrodata/magpis/magpis.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/astrodata/magpis/magpis.py b/astrodata/magpis/magpis.py index 74e24e8a32..0533271abc 100644 --- a/astrodata/magpis/magpis.py +++ b/astrodata/magpis/magpis.py @@ -2,7 +2,6 @@ MAGPIS Image and Catalog Query Tool ----------------------------------- -:Author: Thomas Robitalle (thomas.robitaille@gmail.com) :Author: Adam Ginsburg (adam.g.ginsburg@gmail.com) """ import cookielib @@ -18,6 +17,7 @@ import time import tempfile import StringIO +from astrodata.utils import progressbar url_gpscutout = "http://third.ucllnl.org/cgi-bin/gpscutout" @@ -92,8 +92,10 @@ def get_magpis_image_gal(glon, glat, survey='bolocam', size=1.0, request = urllib.urlencode(request) # load the URL as text U = urllib.urlopen(url_gpscutout, request) + # read results with progressbar + results = progressbar.chunk_read(U, report_hook=progressbar.chunk_report) # turn the text into a StringIO object for FITS reading - S = StringIO.StringIO(U.read()) + S = StringIO.StringIO(results) fitsfile = pyfits.open(S) if save: