def makeCat(self, imgfile, instdet, weightfile=None, extref=False): """Makes a catalog of objects to be used for input to superalign and creates a DS9 region file of objects""" imgfile_cat = '%s.cat' % imgfile.replace('.fits', '') imgfile_reg = '%s.reg' % imgfile.replace('.fits', '') o_radec = [] ext = 0 objectlist = self.findSources('%s[%s]' % (imgfile, ext), imgfile_cat, instdet, weightfile, extref=extref) cleanobjectlist = self.removeCloseSources(objectlist) print 'Found %s sources' % len(cleanobjectlist) wcs = HSTWCS(str(imgfile)) for obj in cleanobjectlist: sky = wcs.all_pix2world(np.array([[obj.x, obj.y]]), 1) o_radec.append([obj.ra[0], obj.dec[0]]) obj.ra = sky[0][0] obj.dec = sky[0][1] # Write out a ds9 region file of object selected for alignment regout = open(imgfile_reg, 'w') regout.write('global color=green font="helvetica 8 normal" edit=1 move=1 delete=1 include=1 fixed=0\nfk5\n') for i,rd in enumerate(o_radec): oid = i+1 regout.write('circle(%s,%s,%s") # color=%s text={%s}\n' % (rd[0], rd[1], 0.5, 'red', oid)) regout.close() # Now we need to write out the catalog in the reference image coords in arcseconds with respect to center of the image catout = open(imgfile_cat, 'w') for i,obj in enumerate(cleanobjectlist): oid = i+1 catout.write('%i %.9f %.9f %.4f %.4f %.4f\n' % (oid, obj.ra, obj.dec, obj.x, obj.y, obj.mag)) catout.close() return
def stars2cat(drzfile, refwcs): """ Makes catalog file from a superalign .stars file """ wcs = HSTWCS(fits.open(drzfile)) starsfile = drzfile.replace('.fits', '_sa.cat.stars') catfile = '%s.cat' % starsfile rcat = np.genfromtxt(starsfile, usecols=(1, 2)) rrdcat = wcs.all_pix2world( (rcat / wcs.pscale) + [wcs.naxis1 / 2, wcs.naxis2 / 2], 1).tolist() xy = refwcs.all_world2pix(rrdcat, 1).tolist() with open(catfile, 'w') as catout: for i, rd in enumerate(rrdcat): catout.write('%d %.8f %.8f %.3f %.3f %d\n' % (i, rd[0], rd[1], xy[i][0], xy[i][1], i))
def makeSAin(visit, imgs, refwcs, refcat_sa): sa_file = '%s_superalign.in' % visit print('Creating %s' % sa_file) sa_in = open(sa_file, 'w') sa_in.write('%s 1\n' % str(len(imgs) + 1)) sa_in.write('%s 0.000 0.000 0.000\n' % refcat_sa) for drz in imgs: f = os.path.basename(drz) wcs = HSTWCS(fits.open(drz)) sky = wcs.all_pix2world([[wcs.naxis1 / 2, wcs.naxis2 / 2]], 1) arc = (refwcs.all_world2pix(sky, 1) - [refwcs.naxis1 / 2, refwcs.naxis2 / 2]) * refwcs.pscale rot = round(refwcs.orientat - wcs.orientat, 3) sa_in.write('%s %s %s %s\n' % (f.replace('.fits', '_sa.cat'), round( arc[0][0], 3), round(arc[0][1], 3), rot)) sa_in.close()
def makeCat(self, imgfile, instdet, weightfile=None, extref=False): """Makes a catalog of objects to be used for input to superalign and creates a DS9 region file of objects""" imgfile_cat = '%s_all.cat' % imgfile.replace('.fits', '') imgfile_reg = '%s_all.reg' % imgfile.replace('.fits', '') o_radec = [] ext = 0 objectlist = self.findSources('%s[%s]' % (imgfile, ext), imgfile_cat, instdet, weightfile, extref=extref) cleanobjectlist = self.removeCloseSources(objectlist) print('Found %s sources' % len(cleanobjectlist)) wcs = HSTWCS(str(imgfile)) for obj in cleanobjectlist: sky = wcs.all_pix2world(np.array([[obj.x, obj.y]]), 1) o_radec.append([obj.ra[0], obj.dec[0]]) obj.ra = sky[0][0] obj.dec = sky[0][1] # Write out a ds9 region file of object selected for alignment regout = open(imgfile_reg, 'w') regout.write( 'global color=green font="helvetica 8 normal" edit=1 move=1 delete=1 include=1 fixed=0\nfk5\n' ) for i, rd in enumerate(o_radec): oid = i + 1 regout.write('circle(%s,%s,%s") # color=%s text={%s}\n' % (rd[0], rd[1], 0.5, 'red', oid)) regout.close() # Now we need to write out the catalog in the reference image coords in arcseconds with respect to center of the image catout = open(imgfile_cat, 'w') for i, obj in enumerate(cleanobjectlist): oid = i + 1 catout.write('%i %.9f %.9f %.4f %.4f %.4f\n' % (oid, obj.ra, obj.dec, obj.x, obj.y, obj.mag)) catout.close() return
def make_grismcat(self, data_name, data_angle, grism_cat, mdrizzle_image, odd_signs=None): """ Input: data_name - filename of the position data data_angle - filename of the displaced positions grism_cat - refernece to the axecat object mdrizzle_image - name of the multidrizzled image Return: - Description: The method creates a new input object list. The positional information on objects in a multidrizzled image are projected back into the coordinate system of one nput image. A selection is done on the basis of the projected coordinates, and the selected objects are stored to a new IOL file """ import os import os.path import math from . import awtran print('') print(' >>>> Working on Input Object List: ', self.iol_name, '>>>>') print('') #------------------------------------------------------------------------ # newer faster version from aXe-1.7 on: # if (self.useMdriz): print("Using multidrizzle coeffs for coordinate transformation\n") dir_pts = awtran.b(mdrizzle_image + "[SCI]", self.header_name, List=data_name) ang_pts = awtran.b(mdrizzle_image + "[SCI]", self.header_name, List=data_angle) else: #use HSTWCS instead for the astrodrizzle image #read in the data_name file # awtran gives back this format: all_out.append("%10.3f %10.3f %10.3f %10.3f" % (xin,yin,xout,yout)) hstimage = HSTWCS(mdrizzle_image, ext=1) datanamefile = open(data_name, 'r') datapoints = datanamefile.readlines() data = [list(map(float, line.split())) for line in datapoints] skypoints = hstimage.all_pix2world( data, 1) #trans points are now in ra and dec from mdrizzle_image #now translate to new image pixel points newhstimage = HSTWCS(self.header_name) trans_pts = newhstimage.all_world2pix(skypoints[:, 0], skypoints[:, 1], 1) #pix2sky returns (2,len) array of points, reshape for what awtran returned dir_pts = list() for i in range(0, len(trans_pts[0])): dir_pts.append("%10.8g %10.8g %10.8g %10.8g" % (trans_pts[0][i], trans_pts[1][i], skypoints[i, 0], skypoints[i, 1])) #now the same for the ang file points angfile = open(data_angle, 'r') angpoints = angfile.readlines() data = [list(map(float, line.split())) for line in datapoints] ang_pts = list() for i in range(0, len(trans_pts[0])): ang_pts.append( "%10.8g %10.8g %10.8g %10.8g" % (data[i][0], data[i][1], trans_pts[0][i], trans_pts[1][i])) # delete the first element, # which contains a description only # but only for the multidrizzle version #------------------------------------------------------------------------- if self.useMdriz: del (dir_pts[0]) del (ang_pts[0]) # start the reverse index; # iterate over all objects r_index = len(dir_pts) - 1 for index in range(grism_cat.nrows): # extract the projected object positions x_ori = float(dir_pts[r_index].split()[0]) y_ori = float(dir_pts[r_index].split()[1]) # check whether the object position is # range to be stored if x_ori >= self.dim_info[0] and x_ori <= self.dim_info[1] and \ y_ori >= self.dim_info[2] and y_ori <= self.dim_info[3]: # extract the displaced projected position x_ang = float(ang_pts[r_index].split()[0]) y_ang = float(ang_pts[r_index].split()[1]) # compute the new object angle dx = x_ang - x_ori dy = y_ang - y_ori angle = math.atan2(dy, dx) / math.pi * 180.0 # Note: this correction is only necessary # as long as there are two different # flavors in pydrizzle and iraf.drizzle if (self.useMdriz): if odd_signs != None: if odd_signs[0]: x_ori -= 0.5 if odd_signs[1]: y_ori -= 0.5 # fill in the new position and angle grism_cat['X_IMAGE'][r_index] = x_ori grism_cat['Y_IMAGE'][r_index] = y_ori grism_cat['THETA_IMAGE'][r_index] = angle # delete outside entries else: grism_cat.delete(r_index) # decrease the reverse index r_index -= 1 # save the new IOL grism_cat.writeto(self.iol_name) print('') print(' >>>> Catalog: ', self.iol_name, 'written with ', grism_cat.nrows, 'entries.>>>>') print('')