From 62583f9ee7150bab024087b67d79bfc7780df57e Mon Sep 17 00:00:00 2001 From: Simon Walker Date: Mon, 27 Apr 2015 12:13:13 +0100 Subject: [PATCH 1/2] Try importing pyfits then try astropy --- blind/anet.py | 8 +++++++- blind/centroid-noise.py | 8 +++++++- blind/image2xy.py | 8 +++++++- blind/spoof.py | 8 +++++++- blind/test_plotstuff.py | 8 +++++++- blind/test_plotstuff2.py | 8 +++++++- blind/ver.py | 8 +++++++- catalogs/ngc2000tofits.py | 8 +++++++- net/process_submissions.py | 8 +++++++- sdss/common.py | 8 +++++++- sdss/dr10.py | 8 +++++++- sdss/dr8.py | 8 +++++++- sdss/dr9.py | 8 +++++++- util/2mass_catalog.py | 8 +++++++- util/fits.py | 28 +++++++++++++++++++++------- util/fits2fits.py | 16 ++++++++++++++-- util/fix_sdss_idr.py | 8 +++++++- util/hpimage.py | 8 +++++++- util/matchfile_to_wcs.py | 8 +++++++- util/removelines.py | 8 +++++++- util/removelines_general.py | 8 +++++++- util/removelines_rotate.py | 18 ++++++++++++++++-- util/sdss_astrom.py | 8 +++++++- util/sdss_cutout.py | 9 +++++++-- util/sdss_photom.py | 8 +++++++- util/tstimg.py | 8 +++++++- util/uniformize.py | 16 ++++++++++++++-- util/usnob_catalog.py | 8 +++++++- util/usnob_trim.py | 8 +++++++- 29 files changed, 240 insertions(+), 39 deletions(-) diff --git a/blind/anet.py b/blind/anet.py index e787e2826..2d214e544 100644 --- a/blind/anet.py +++ b/blind/anet.py @@ -33,7 +33,13 @@ import mimetools, mimetypes import os, stat from cStringIO import StringIO -import pyfits +try: + import pyfits +except ImportError: + try: + from astropy.io import fits as pyfits + except ImportError: + raise ImportError("Cannot import either pyfits or astropy.io.fits") import time __author__ = "J. S. Bloom" diff --git a/blind/centroid-noise.py b/blind/centroid-noise.py index ff374e524..a13b93d8b 100644 --- a/blind/centroid-noise.py +++ b/blind/centroid-noise.py @@ -7,7 +7,13 @@ from pylab import * from numpy import * from numpy.random import * -import pyfits +try: + import pyfits +except ImportError: + try: + from astropy.io import fits as pyfits + except ImportError: + raise ImportError("Cannot import either pyfits or astropy.io.fits") # Given an image and an xylist (including estimated image sigma), # look at a cutout around each source position, add noise, and recompute diff --git a/blind/image2xy.py b/blind/image2xy.py index 5781fcf6b..db88bac4f 100644 --- a/blind/image2xy.py +++ b/blind/image2xy.py @@ -42,7 +42,13 @@ # You need ctypes and a recent (1.0) numpy for this to work. I've included # pyfits so you don't have to. -import pyfits +try: + import pyfits +except ImportError: + try: + from astropy.io import fits as pyfits + except ImportError: + raise ImportError("Cannot import either pyfits or astropy.io.fits") import sys import scipy import os diff --git a/blind/spoof.py b/blind/spoof.py index cc27e758e..0f4ef80cf 100644 --- a/blind/spoof.py +++ b/blind/spoof.py @@ -1,4 +1,10 @@ -import pyfits +try: + import pyfits +except ImportError: + try: + from astropy.io import fits as pyfits + except ImportError: + raise ImportError("Cannot import either pyfits or astropy.io.fits") import math from math import exp from matplotlib.pylab import imread diff --git a/blind/test_plotstuff.py b/blind/test_plotstuff.py index 916c7f5de..d1d6effc1 100644 --- a/blind/test_plotstuff.py +++ b/blind/test_plotstuff.py @@ -3,7 +3,13 @@ matplotlib.use('Agg') import unittest -import pyfits +try: + import pyfits +except ImportError: + try: + from astropy.io import fits as pyfits + except ImportError: + raise ImportError("Cannot import either pyfits or astropy.io.fits") import numpy as np import pylab as plt from math import pi,sqrt diff --git a/blind/test_plotstuff2.py b/blind/test_plotstuff2.py index c1ce07128..77db85bdd 100644 --- a/blind/test_plotstuff2.py +++ b/blind/test_plotstuff2.py @@ -2,7 +2,13 @@ if __name__ == '__main__': matplotlib.use('Agg') import unittest -import pyfits +try: + import pyfits +except ImportError: + try: + from astropy.io import fits as pyfits + except ImportError: + raise ImportError("Cannot import either pyfits or astropy.io.fits") from astrometry.util.fits import * from astrometry.blind.plotstuff import * import numpy as np diff --git a/blind/ver.py b/blind/ver.py index e2f81ef7f..ed6ae5e86 100644 --- a/blind/ver.py +++ b/blind/ver.py @@ -1,5 +1,11 @@ import math -import pyfits +try: + import pyfits +except ImportError: + try: + from astropy.io import fits as pyfits + except ImportError: + raise ImportError("Cannot import either pyfits or astropy.io.fits") from numpy import * from matplotlib.pylab import figure, plot, xlabel, ylabel, loglog, clf from matplotlib.pylab import semilogy, show, find, legend, hist, axis diff --git a/catalogs/ngc2000tofits.py b/catalogs/ngc2000tofits.py index 0cbc2c1df..8e099b6e4 100644 --- a/catalogs/ngc2000tofits.py +++ b/catalogs/ngc2000tofits.py @@ -1,4 +1,10 @@ -import pyfits +try: + import pyfits +except ImportError: + try: + from astropy.io import fits as pyfits + except ImportError: + raise ImportError("Cannot import either pyfits or astropy.io.fits") from numpy import * from ngc2000 import ngc2000, ngc2000accurate from astrometry.util.fits import * diff --git a/net/process_submissions.py b/net/process_submissions.py index 795b978fa..50c08b126 100644 --- a/net/process_submissions.py +++ b/net/process_submissions.py @@ -29,7 +29,13 @@ django.setup() -import pyfits +try: + import pyfits +except ImportError: + try: + from astropy.io import fits as pyfits + except ImportError: + raise ImportError("Cannot import either pyfits or astropy.io.fits") import tempfile diff --git a/sdss/common.py b/sdss/common.py index cd9a19e47..fad7793b5 100644 --- a/sdss/common.py +++ b/sdss/common.py @@ -2,7 +2,13 @@ from astrometry.util.fits import fits_table from astrometry.util.miscutils import get_overlapping_region import numpy as np -import pyfits +try: + import pyfits +except ImportError: + try: + from astropy.io import fits as pyfits + except ImportError: + raise ImportError("Cannot import either pyfits or astropy.io.fits") try: import cutils diff --git a/sdss/dr10.py b/sdss/dr10.py index 0c920959b..4d0b56247 100644 --- a/sdss/dr10.py +++ b/sdss/dr10.py @@ -1,5 +1,11 @@ import os -import pyfits +try: + import pyfits +except ImportError: + try: + from astropy.io import fits as pyfits + except ImportError: + raise ImportError("Cannot import either pyfits or astropy.io.fits") from astrometry.util.fits import fits_table import numpy as np diff --git a/sdss/dr8.py b/sdss/dr8.py index dd37ecf97..07ee757e7 100644 --- a/sdss/dr8.py +++ b/sdss/dr8.py @@ -9,7 +9,13 @@ try: import fitsio except: - import pyfits + try: + import pyfits + except ImportError: + try: + from astropy.io import fits as pyfits + except ImportError: + raise ImportError("Cannot import either pyfits or astropy.io.fits") from common import * from dr7 import * diff --git a/sdss/dr9.py b/sdss/dr9.py index 1ec32daf0..5d4daa3e9 100644 --- a/sdss/dr9.py +++ b/sdss/dr9.py @@ -1,5 +1,11 @@ import os -import pyfits +try: + import pyfits +except ImportError: + try: + from astropy.io import fits as pyfits + except ImportError: + raise ImportError("Cannot import either pyfits or astropy.io.fits") from astrometry.util.fits import fits_table import numpy as np diff --git a/util/2mass_catalog.py b/util/2mass_catalog.py index 098600c0b..b9a03faf4 100755 --- a/util/2mass_catalog.py +++ b/util/2mass_catalog.py @@ -2,7 +2,13 @@ import sys from optparse import OptionParser -import pyfits +try: + import pyfits +except ImportError: + try: + from astropy.io import fits as pyfits + except ImportError: + raise ImportError("Cannot import either pyfits or astropy.io.fits") from numpy import * from astrometry.util.fits import * diff --git a/util/fits.py b/util/fits.py index 98164cffa..915f1149f 100644 --- a/util/fits.py +++ b/util/fits.py @@ -7,7 +7,13 @@ class NoPyfits(object): pyfits = NoPyfits() except: try: - import pyfits + try: + import pyfits + except ImportError: + try: + from astropy.io import fits as pyfits + except ImportError: + raise ImportError("Cannot import either pyfits or astropy.io.fits") except: pyfits = NoPyfits() import numpy as np @@ -686,17 +692,25 @@ def fits_table(dataorfn=None, rows=None, hdunum=1, hdu=None, ext=None, # in a try/catch in case pyfits isn't available isrecarray = (type(data) == pyfits.core.FITS_rec) except: - import traceback - traceback.print_exc() - pass - if not isrecarray: try: - import pyfits.fitsrec - isrecarray = (type(data) == pyfits.fitsrec.FITS_rec) + from astropy.io import fits as pyfits + isrecarray = (type(data) == pyfits.core.FITS_rec) except: import traceback traceback.print_exc() pass + if not isrecarray: + try: + import pyfits.fitsrec + isrecarray = (type(data) == pyfits.fitsrec.FITS_rec) + except: + try: + from astropy.io import fits as pyfits + isrecarray = (type(data) == pyfits.fitsrec.FITS_rec) + except: + import traceback + traceback.print_exc() + pass #if not isrecarray: # if type(data) == np.recarray: # isrecarray = True diff --git a/util/fits2fits.py b/util/fits2fits.py index 7937f8957..40e3cba6a 100755 --- a/util/fits2fits.py +++ b/util/fits2fits.py @@ -16,9 +16,21 @@ # of the path: astrometry to pick up pyfits, and .. to pick up astrometry itself. sys.path.insert(1, andir) sys.path.insert(2, rootdir) - import pyfits + try: + import pyfits + except ImportError: + try: + from astropy.io import fits as pyfits + except ImportError: + raise ImportError("Cannot import either pyfits or astropy.io.fits") -import pyfits +try: + import pyfits +except ImportError: + try: + from astropy.io import fits as pyfits + except ImportError: + raise ImportError("Cannot import either pyfits or astropy.io.fits") from astrometry.util.fits import pyfits_writeto def fits2fits(infile, outfile, verbose=False, fix_idr=False): diff --git a/util/fix_sdss_idr.py b/util/fix_sdss_idr.py index 9cf3341eb..130a838ec 100644 --- a/util/fix_sdss_idr.py +++ b/util/fix_sdss_idr.py @@ -1,5 +1,11 @@ import sys -import pyfits +try: + import pyfits +except ImportError: + try: + from astropy.io import fits as pyfits + except ImportError: + raise ImportError("Cannot import either pyfits or astropy.io.fits") import numpy from astrometry.util.fits import pyfits_writeto diff --git a/util/hpimage.py b/util/hpimage.py index 428e588a2..199062976 100644 --- a/util/hpimage.py +++ b/util/hpimage.py @@ -1,7 +1,13 @@ import sys from pylab import * -import pyfits +try: + import pyfits +except ImportError: + try: + from astropy.io import fits as pyfits + except ImportError: + raise ImportError("Cannot import either pyfits or astropy.io.fits") from numpy import * from astrometry.util.healpix import * diff --git a/util/matchfile_to_wcs.py b/util/matchfile_to_wcs.py index 32da2f838..2e37411d7 100644 --- a/util/matchfile_to_wcs.py +++ b/util/matchfile_to_wcs.py @@ -1,4 +1,10 @@ -import pyfits +try: + import pyfits +except ImportError: + try: + from astropy.io import fits as pyfits + except ImportError: + raise ImportError("Cannot import either pyfits or astropy.io.fits") from optparse import OptionParser from astrometry.util.fits import * diff --git a/util/removelines.py b/util/removelines.py index e95ce8acd..3d5210af6 100755 --- a/util/removelines.py +++ b/util/removelines.py @@ -9,7 +9,13 @@ addpath.addpath() import numpy -import pyfits +try: + import pyfits +except ImportError: + try: + from astropy.io import fits as pyfits + except ImportError: + raise ImportError("Cannot import either pyfits or astropy.io.fits") from numpy import * from numpy.random import rand from astrometry.util.fits import pyfits_writeto diff --git a/util/removelines_general.py b/util/removelines_general.py index e9b2210ac..83bdc2fb6 100755 --- a/util/removelines_general.py +++ b/util/removelines_general.py @@ -1,7 +1,13 @@ #! /usr/bin/env python import sys -import pyfits +try: + import pyfits +except ImportError: + try: + from astropy.io import fits as pyfits + except ImportError: + raise ImportError("Cannot import either pyfits or astropy.io.fits") from math import * from numpy import * diff --git a/util/removelines_rotate.py b/util/removelines_rotate.py index a61853015..643714cd2 100755 --- a/util/removelines_rotate.py +++ b/util/removelines_rotate.py @@ -13,7 +13,15 @@ if __name__ == '__main__': try: - import pyfits + try: + import pyfits + except ImportError: + try: + from astropy.io import fits as pyfits + except ImportError: + raise ImportError( + "Cannot import either pyfits or astropy.io.fits" + ) import astrometry from astrometry.util.shell import shell_escape from astrometry.util.filetype import filetype_short @@ -33,7 +41,13 @@ sys.path.insert(2, rootdir) import numpy -import pyfits +try: + import pyfits +except ImportError: + try: + from astropy.io import fits as pyfits + except ImportError: + raise ImportError("Cannot import either pyfits or astropy.io.fits") from numpy import * from numpy.random import rand diff --git a/util/sdss_astrom.py b/util/sdss_astrom.py index 1a256563f..14ffb21ba 100644 --- a/util/sdss_astrom.py +++ b/util/sdss_astrom.py @@ -8,7 +8,13 @@ from astrometry.util.fits import * from numpy import * -import pyfits +try: + import pyfits +except ImportError: + try: + from astropy.io import fits as pyfits + except ImportError: + raise ImportError("Cannot import either pyfits or astropy.io.fits") from glob import glob def tsfield_get_node_incl(tsfield): diff --git a/util/sdss_cutout.py b/util/sdss_cutout.py index f7efb06b9..bb2c24198 100644 --- a/util/sdss_cutout.py +++ b/util/sdss_cutout.py @@ -2,9 +2,14 @@ import os import sys -import pyfits +try: + import pyfits +except ImportError: + try: + from astropy.io import fits as pyfits + except ImportError: + raise ImportError("Cannot import either pyfits or astropy.io.fits") from astrometry.util.util import * -import pyfits from astrometry.util.sdss_noise import * from astrometry.util.sdss_psfield import * diff --git a/util/sdss_photom.py b/util/sdss_photom.py index f8cfb7442..3d31ace76 100644 --- a/util/sdss_photom.py +++ b/util/sdss_photom.py @@ -8,7 +8,13 @@ from astrometry.util.fits import * from numpy import * import numpy as np -import pyfits +try: + import pyfits +except ImportError: + try: + from astropy.io import fits as pyfits + except ImportError: + raise ImportError("Cannot import either pyfits or astropy.io.fits") class PhotometricCalib(object): def __init__(self, tsfieldfn): diff --git a/util/tstimg.py b/util/tstimg.py index 8559d1843..60d82833c 100644 --- a/util/tstimg.py +++ b/util/tstimg.py @@ -1,4 +1,10 @@ -import pyfits +try: + import pyfits +except ImportError: + try: + from astropy.io import fits as pyfits + except ImportError: + raise ImportError("Cannot import either pyfits or astropy.io.fits") from numpy import * if __name__ == '__main__': diff --git a/util/uniformize.py b/util/uniformize.py index 86d054b12..a0f04251d 100755 --- a/util/uniformize.py +++ b/util/uniformize.py @@ -6,7 +6,13 @@ if __name__ == '__main__': try: - import pyfits + try: + import pyfits + except ImportError: + try: + from astropy.io import fits as pyfits + except ImportError: + raise ImportError("Cannot import either pyfits or astropy.io.fits") import astrometry from astrometry.util.shell import shell_escape from astrometry.util.filetype import filetype_short @@ -24,7 +30,13 @@ from numpy import * from numpy.random import rand from astrometry.util.fits import * -import pyfits +try: + import pyfits +except ImportError: + try: + from astropy.io import fits as pyfits + except ImportError: + raise ImportError("Cannot import either pyfits or astropy.io.fits") def uniformize(infile, outfile, n, xcol='X', ycol='Y', ext=1, **kwargs): p = pyfits.open(infile) diff --git a/util/usnob_catalog.py b/util/usnob_catalog.py index e967019cd..8eaaa98cb 100755 --- a/util/usnob_catalog.py +++ b/util/usnob_catalog.py @@ -2,7 +2,13 @@ import sys from optparse import OptionParser -import pyfits +try: + import pyfits +except ImportError: + try: + from astropy.io import fits as pyfits + except ImportError: + raise ImportError("Cannot import either pyfits or astropy.io.fits") from numpy import * from astrometry.util.fits import * diff --git a/util/usnob_trim.py b/util/usnob_trim.py index dce08f9a6..68b9df6f6 100644 --- a/util/usnob_trim.py +++ b/util/usnob_trim.py @@ -6,7 +6,13 @@ import sys from optparse import OptionParser -import pyfits +try: + import pyfits +except ImportError: + try: + from astropy.io import fits as pyfits + except ImportError: + raise ImportError("Cannot import either pyfits or astropy.io.fits") from numpy import * from astrometry.util.fits import * From c851e97efe4b58d62336f377668afa32718befa8 Mon Sep 17 00:00:00 2001 From: Dustin Lang Date: Mon, 4 May 2015 09:30:34 -0700 Subject: [PATCH 2/2] resample: linspace wants inclusive bounds --- util/fits.py | 11 ++++++++--- util/resample.py | 4 ++-- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/util/fits.py b/util/fits.py index 98164cffa..1a5f2f0c7 100644 --- a/util/fits.py +++ b/util/fits.py @@ -849,7 +849,7 @@ def streaming_text_table(forfn, skiplines=0, split=None, maxcols=None, t1 = time.clock() floattypes = [float,np.float32,np.float64] - inttypes = [int, np.int32, np.int64] + inttypes = [int, np.int16, np.int32, np.int64] for dat,typ in zip(data, coltypes): if typ in floattypes: @@ -876,8 +876,13 @@ def streaming_text_table(forfn, skiplines=0, split=None, maxcols=None, # trim to valid rows data = [dat[:goodrows] for dat in data] # convert - data = [np.array(dat).astype(typ) for dat,typ in zip(data, coltypes)] - + try: + data = [np.array(dat).astype(typ) for dat,typ in zip(data, coltypes)] + except: + for name,dat,typ in zip(colnames, data, coltypes): + print 'Column', name + np.array(dat).astype(typ) + raise t3 = time.clock() #print 'Reading & splitting:', t1-t0 diff --git a/util/resample.py b/util/resample.py index 85af2ed66..68723e6a6 100644 --- a/util/resample.py +++ b/util/resample.py @@ -96,11 +96,11 @@ def resample_with_wcs(targetwcs, wcs, Limages, L, spline=True, margin = splineMargin step = splineStep xlo = max(0, x0-margin) - xhi = min(W, x1+margin+1) + xhi = min(W-1, x1+margin) nx = np.ceil(float(xhi - xlo) / step) + 1 xx = np.linspace(xlo, xhi, nx) ylo = max(0, y0-margin) - yhi = min(H, y1+margin+1) + yhi = min(H-1, y1+margin) ny = np.ceil(float(yhi - ylo) / step) + 1 yy = np.linspace(ylo, yhi, ny)