import time import numpy as np import ctypes import re def ID_from_timestamp(): ID = '' gmt = time.gmtime() ID = ''.join( [str(getattr(gmt,key)) for key in ['tm_year', 'tm_mon', 'tm_mday', 'tm_hour', 'tm_min', 'tm_sec'] ] ) return int(ID) def small_round_psf_image(n): x,y = np.mgrid[:n,:n] z2 = (x-n/2.)**2 + (y-n/2.)**2 sigma2=1.0 psf = np.exp(-0.5*z2/2/sigma2) psf/=psf.sum() return psf def double_pointer_from_list(L,pointer_type): """ Utility ctypes function to go from a list of struct instances to a pointer to pointer/array of pointers (these two are compatible)""" n = len(L) struct_arr = pointer_type*n array = struct_arr() for i in xrange(n): try: array[i] = L[i] except TypeError: array[i] = L[i]._struct return array def noise_estimate_edge(image, mask): """ Using all places with non-zero mask around the edge of the image, estimate the noise std dev. """ edge = np.concatenate([image[0,:], image[-1,:],image[1:-1,0],image[1:-1,-1]]) edge_mask = np.concatenate([mask[0,:], mask[-1,:],mask[1:-1,0],mask[1:-1,-1]]) edge = edge[edge_mask>0] return edge.std() def weight_scaling_estimate_edge(image, weight): edge = np.concatenate([image[0,:], image[-1,:],image[1:-1,0],image[1:-1,-1]]) edge_weight = np.concatenate([weight[0,:], weight[-1,:],weight[1:-1,0],weight[1:-1,-1]]) w = edge_weight>0 edge = edge[w] edge_sigma = edge_weight[w]**-0.5 return (edge/edge_sigma).std() # taken from ucl_des_shar/utils, copied so we don't have to be dependent on ucl_des_shear in im3shape def convert_e_linear_to_quadratic(g1, g2): """Convert (a - b)/(a + b) style ellipticities to the (a^2 - b^2)/(a^2 + b^2) convention. Returns the converted g1, g2 pair as a tuple. """ import numpy as np denom = 1. + np.sqrt(1. - g1 * g1 - g2 * g2) return (g1 / denom, g2 / denom) # taken from ucl_des_shar/utils, copied so we don't have to be dependent on ucl_des_shear in im3shape def convert_e_quadratic_to_linear(e1, e2): """Convert (a^2 - b^2)/(a^2 + b^2) style ellipticities to the (a - b)/(a + b) convention. Returns the converted e1, e2 pair as a tuple. """ denom = .5 * (1. + e1 * e1 + e2 * e2) return (e1 / denom, e2 / denom) def rescale_weight_edge(image, weight, options): scaling=utils.weight_scaling_estimate_edge(image, weight) weight *= scaling**-2 if options.verbosity>1: print "Scaling exposure ", scaling def get_unmasked_flux_fraction(model, weight): mask = (weight>0).astype(float) unmask_sum = (mask*abs(model)).sum() sum = abs(model).sum() return unmask_sum/sum #Stolen from Barney Rowe. #For now I'm not going to use this but instead just use a small round gaussian def getPSFExarray(psfex, x_image, y_image, nsidex=32, nsidey=32, upsampling=1, offset=None): """Return an image of the PSFEx model of the PSF as a NumPy array. Arguments --------- psfex A galsim.des.PSFEx instance opened using, for example, `psfex = galsim.des.DES_PSFEx(psfex_file_name)`. x_image Floating point x position on image [pixels] y_image Floating point y position on image [pixels] nsidex Size of PSF image along x [pixels] nsidey Size of PSF image along y [pixels] upsampling Upsampling (see Zuntz et al 2013) Returns a NumPy array with shape (nsidey, nsidex) - note the reversal of y and x to match the NumPy internal [y, x] style array ordering. This is to ensure that `pyfits.writeto()` using the ouput array creates FITS-compliant output. """ import galsim image = galsim.ImageD(nsidex, nsidey) psf = psfex.getPSF(galsim.PositionD(x_image, y_image)) psf.draw(image, scale=1.0/upsampling, offset=offset) return image.array def getShapeletPSFarray(des_shapelet, x_image, y_image, nsidex=32, nsidey=32, upsampling=1, offset=None): """Return an image of the PSFEx model of the PSF as a NumPy array. Arguments --------- des_shapelet A galsim.des.DES_Shapelet instance opened using, for example, `des_shapelet = galsim.des.DES_Shapelet(shapelet_file_name)`. Usually stored as *_fitpsf.fits files. x_image Floating point x position on image [pixels] y_image Floating point y position on image [pixels] nsidex Size of PSF image along x [pixels] nsidey Size of PSF image along y [pixels] upsampling Upsampling (see Zuntz et al 2013) Returns a NumPy array with shape (nsidey, nsidex) - note the reversal of y and x to match the NumPy internal [y, x] style array ordering. This is to ensure that `pyfits.writeto()` using the ouput array creates FITS-compliant output. """ import galsim image = galsim.ImageD(nsidex, nsidey) psf = des_shapelet.getPSF(galsim.PositionD(x_image, y_image), pixel_scale=1.) psf.draw(image, scale=1. / upsampling, offset=offset) return image.array def uberseg(object_id,weight,seg): #Object id in seg map should be #First check that expected object is in seg map if object_id not in seg: print 'ID not in seg...' raise ValueError #First get all indices of all seg map pixels which contain an object i.e. are not equal to zero obj_inds = np.where(seg!=0) #Then loop through pixels in seg map, check which obj ind it is closest to. #If the closest obj ind does not correspond to the target, set this pixel in the weight map to zero. uber_weight = np.ones_like(weight) for i,row in enumerate(seg): for j, element in enumerate(row): obj_dists = (i-obj_inds[0])**2 + (j-obj_inds[1])**2 ind_min=np.argmin(obj_dists) if seg[obj_inds[0][ind_min],obj_inds[1][ind_min]] != object_id: uber_weight[i,j] = 0. weight*=uber_weight return uber_weight tile_pattern = re.compile(r'DES[0-9][0-9][0-9][0-9][+-][0-9][0-9][0-9][0-9]') tile_band_pattern = re.compile(r'DES[0-9][0-9][0-9][0-9][+-][0-9][0-9][0-9][0-9][_-][ugrizy]') run_pattern = re.compile(r'[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]_DES[0-9][0-9][0-9][0-9][+-][0-9][0-9][0-9][0-9]') great_des_pattern=re.compile(r"nbc(.)+\.meds\.([0-9][0-9][0-9])\.g([0-9][0-9])\.fits") def find_tilename(name): m = tile_pattern.search(name) if m is None: return "unknown" return m.group()