idx = diff.index(min(diff)) return stepra[idx],ralabel[idx] if __name__ == "__main__": arg = ReadCmd(spec) image = arg.getstr('in',exist=True) rangex = arg.getlistfloat('xrange',length=2) rangey = arg.getlistfloat('yrange',length=2) tmplength = arg.getfloat('length') tmpra = arg.getlistfloat('ra',length=2) tmpdec = arg.getlistfloat('dec',length=2) outfile = arg.getstr('out',exist=False) import worldpos wcs = worldpos.wcs(image) if wcs.header['rot'] != 0: error('WIP cannot handle rotations!') if len(rangex) == 0: xmin = 0.5 xmax = naxis1 + 0.5 else: xmin = rangex[0] - 0.5 xmax = rangex[1] + 0.5 if len(rangey) == 0: ymin = 0.5 ymax = naxis2 + 0.5 else: ymin = rangey[0] - 0.5 ymax = rangey[1] + 0.5
return stepra[idx], ralabel[idx] if __name__ == "__main__": arg = ReadCmd(spec) image = arg.getstr('in', exist=True) rangex = arg.getlistfloat('xrange', length=2) rangey = arg.getlistfloat('yrange', length=2) tmplength = arg.getfloat('length') tmpra = arg.getlistfloat('ra', length=2) tmpdec = arg.getlistfloat('dec', length=2) outfile = arg.getstr('out', exist=False) import worldpos wcs = worldpos.wcs(image) if wcs.header['rot'] != 0: error('WIP cannot handle rotations!') if len(rangex) == 0: xmin = 0.5 xmax = naxis1 + 0.5 else: xmin = rangex[0] - 0.5 xmax = rangex[1] + 0.5 if len(rangey) == 0: ymin = 0.5 ymax = naxis2 + 0.5 else: ymin = rangey[0] - 0.5 ymax = rangey[1] + 0.5
# read CDELT from FITS header to pick an appropriate distance for scaling fp = pyfits.open(image,mode='readonly') if cdFlag is True: cdelt = abs(float(fp[ext].header['cd1_1'])) # cdelt in degrees else: cdelt = abs(float(fp[ext].header['cdelt2'])) # cdelt in degrees scale = 5*cdelt fp.close() data0 = loadtxt(infile,usecols=cols,comments='#') if len(data0.shape) == 1: # only 1 line in file, so upgrade to 2-d array b/c data0.shape = (1,data0.size) # numpy doesn't seem to do this by default data0[:,3] = pi/180.*data0[:,3] # convert angles to radians wcs = worldpos.wcs(image,ext=ext,cd=cdFlag) data1 = wcs.sky2xy(data0[:,0],data0[:,1]) data0[:,1] = data0[:,1] + scale # move dec's north by length amounts data2 = wcs.sky2xy(data0[:,0],data0[:,1]) # convert to numpy array with x,y in different columns data1 = column_stack(data1) data2 = column_stack(data2) # make positions relative to initial position, so we can rotate around those # points. data2 = data2 - data1 # rotate x,y sintmp = sin(data0[:,3]) costmp = cos(data0[:,3])
fits_file = 'YOURFITS.fits' out_file = 'cutout1.fits' fits_data = pyfits.getdata(fits_file, 0) header = pyfits.getheader(fits_file) image_data = fits_data[0][0] ID, RA, Dec = np.loadtxt('/INSERT/YOUR/DIRECTORY/WITH/coordinates.txt', unpack=True, usecols=[0, 1, 2], dtype=[('ID', '|S30'), ('RA', 'f8'), ('dec', 'f8')]) # Convert RA and Dec to x and y wcs = worldpos.wcs( fits_file, ext=0, rot='CROTA2', cd=False) # crota2 assumed to be zero (i.e. image has no rotation wrt sky) x, y = wcs.sky2xy(RA, Dec) # POS_X, POS_Y # Pixel numbers pixel_x = np.rint(x).astype( 'i' ) # Rounding to the nearest integer, then ensuring type is changed from float to integer pixel_y = np.rint(y).astype('i') def cutout(file, xc, yc, name, xw=100,