Example #1
0
    def run_sextractor(self, use_dict, out_name, output_params, clean=True):
        param_ascii = asciidata.create(1, len(output_params))
        row_counter = 0
        for param in output_params:
            param_ascii[0][row_counter] = param
            row_counter += 1
        param_fname = self.out_name + ".param"
        param_ascii.writeto(param_fname)
        #Create config newfiles[i] and write out to a file
        config_ascii = asciidata.create(2, 4 + len(use_dict))
        #File-Specific Configurations
        config_ascii[0][0] = 'CATALOG_NAME'
        config_ascii[1][0] = out_name + ".cat"
        config_ascii[0][1] = 'PARAMETERS_NAME'
        config_ascii[1][1] = self.out_name + ".param"
        config_ascii[0][2] = 'WEIGHT_TYPE'
        config_ascii[1][2] = 'MAP_WEIGHT'
        config_ascii[0][3] = 'WEIGHT_IMAGE'
        config_ascii[1][3] = self.weight_file
        row_counter = 4
        for key, value in use_dict.iteritems():
            config_ascii[0][row_counter] = key
            config_ascii[1][row_counter] = value
            row_counter += 1
        config_fname = out_name + ".config"
        config_ascii.writeto(config_fname)

        #Run sextractor and get the catalog
        subprocess.call(["sex", self.file, "-c", config_fname])

        #Optional Clean
        subprocess.call(["rm", config_fname])
        subprocess.call(["rm", param_fname])
def manual_mask(catalog, x_vertices, y_vertices, clean=True):
    orig_name = catalog
    catalog = asciidata.open(catalog)
    new_table = asciidata.create(catalog.ncols,catalog.nrows)
    delete_numbers = []
    for i in range(catalog.nrows):
        if (i+1) % 1000 == 0:
            print "Working on object", i+1, "out of", catalog.nrows
        number = catalog['NUMBER'][i]
        is_star = catalog['IS_STAR'][i]
        if is_star == 1:
            continue
        x_min,y_min = catalog['XMIN_IMAGE'][i], catalog['YMIN_IMAGE'][i]
        x_max,y_max = catalog['XMAX_IMAGE'][i], catalog['YMAX_IMAGE'][i]
        bottom_pixels = [(x,y_min) for x in range(x_min,x_max)]
        left_pixels = [(x_min,y) for y in range(y_min,y_max)]
        top_pixels = [(x, y_max) for x in range(x_min,x_max)]
        right_pixels = [(x_max,y) for y in range(y_min,y_max)]
        pixels = bottom_pixels + left_pixels + top_pixels + right_pixels
    	bools = [inpoly(pixel[0],pixel[1],x_vertices,y_vertices) for pixel in pixels]
        if max(bools) == 1:
            delete_numbers.append(number)
    print "Delete numbers", delete_numbers
    new_table = asciidata.create(catalog.ncols,catalog.nrows)
    for i in range(catalog.nrows):
        if catalog['NUMBER'][i] not in delete_numbers:
             for k in range(catalog.ncols):
                 new_table[k][i] = catalog[k][i]
    #Get rid of empty rows
    row_number = 0
    while True:
        try:
            if new_table[0][row_number] is None:
                new_table.delete(row_number)
            else:
                row_number += 1
        except:
            break
    #Write out to another catalog
    new_table.writeto("manual_filter.cat")
    new_catalog = open("manual_filter.cat")
    old_catalog = open(orig_name)
    final_catalog = open("final_catalog.cat", "w")
    for line in old_catalog.readlines():
        if line[0] == "#":
            final_catalog.write(line)
        else:
            break
    for line in new_catalog:
        final_catalog.write(line)
    old_catalog.close()
    final_catalog.close()
    final = open("final_catalog.cat", "r")
    rewrite = open(orig_name, "w")
    for line in final.readlines():
        rewrite.write(line)
    #Optional clean
    if clean:
        subprocess.call(["rm", "final_catalog.cat"])
        subprocess.call(["rm", "manual_filter.cat"])
 def run_sextractor(self, use_dict, out_name, output_params, clean=True):
     param_ascii = asciidata.create(1,len(output_params))
     row_counter = 0
     for param in output_params:
         param_ascii[0][row_counter] = param
         row_counter += 1
     param_fname = self.out_name + ".param"
     param_ascii.writeto(param_fname)
     #Create config newfiles[i] and write out to a file
     config_ascii = asciidata.create(2,4+len(use_dict))
     #File-Specific Configurations
     config_ascii[0][0] = 'CATALOG_NAME'
     config_ascii[1][0] = out_name + ".cat"
     config_ascii[0][1] = 'PARAMETERS_NAME'
     config_ascii[1][1] = self.out_name + ".param"
     config_ascii[0][2] = 'WEIGHT_TYPE'
     config_ascii[1][2] = 'MAP_WEIGHT'
     config_ascii[0][3] = 'WEIGHT_IMAGE'
     config_ascii[1][3] = self.weight_file
     row_counter = 4
     for key, value in use_dict.iteritems():
         config_ascii[0][row_counter] = key
         config_ascii[1][row_counter] = value
         row_counter += 1
     config_fname = out_name + ".config"
     config_ascii.writeto(config_fname)
             
     #Run sextractor and get the catalog
     subprocess.call(["sex", self.file , "-c", config_fname])
 
     #Optional Clean
     subprocess.call(["rm", config_fname])
     subprocess.call(["rm", param_fname])
def run_sextractor_tt(file,output_params,out_name,clean=True): 
    #Create params_file and write out to a file
    param_ascii = asciidata.create(1,len(output_params))
    row_counter = 0
    for param in output_params:
        param_ascii[0][row_counter] = param 
        row_counter += 1
    param_fname = out_name + ".param"
    param_ascii.writeto(param_fname)
    
    #Create config newfiles[i] and write out to a file
    config_ascii = asciidata.create(2,5)
        
    #File-Specific Configurations
    config_ascii[0][0] = 'CATALOG_NAME'
    config_ascii[1][0] = out_name
    config_ascii[0][1] = 'PARAMETERS_NAME'
    config_ascii[1][1] = out_name + ".param"
    config_ascii[0][2] = 'BACK_TYPE'
    config_ascii[1][2] = 'MANUAL'
    config_ascii[0][3] = 'BACK_VALUE'
    config_ascii[1][3] = 1e-8
    config_ascii[0][4] = 'DETECT_THRESH'
    config_ascii[1][4] = 1e-10
    config_fname = out_name + ".config"
    config_ascii.writeto(config_fname)
        
    #Run sextractor and get the catalog
    subprocess.call(["sex", file , "-c", config_fname])
    
    #Optional Clean
    subprocess.call(["rm", config_fname])
    subprocess.call(["rm", param_fname])
def run_sextractor_tt(file, output_params, out_name, clean=True):
    #Create params_file and write out to a file
    param_ascii = asciidata.create(1, len(output_params))
    row_counter = 0
    for param in output_params:
        param_ascii[0][row_counter] = param
        row_counter += 1
    param_fname = out_name + ".param"
    param_ascii.writeto(param_fname)

    #Create config newfiles[i] and write out to a file
    config_ascii = asciidata.create(2, 5)

    #File-Specific Configurations
    config_ascii[0][0] = 'CATALOG_NAME'
    config_ascii[1][0] = out_name
    config_ascii[0][1] = 'PARAMETERS_NAME'
    config_ascii[1][1] = out_name + ".param"
    config_ascii[0][2] = 'BACK_TYPE'
    config_ascii[1][2] = 'MANUAL'
    config_ascii[0][3] = 'BACK_VALUE'
    config_ascii[1][3] = 1e-8
    config_ascii[0][4] = 'DETECT_THRESH'
    config_ascii[1][4] = 1e-10
    config_fname = out_name + ".config"
    config_ascii.writeto(config_fname)

    #Run sextractor and get the catalog
    subprocess.call(["sex", file, "-c", config_fname])

    #Optional Clean
    subprocess.call(["rm", config_fname])
    subprocess.call(["rm", param_fname])
Example #6
0
 def run_sextractor_dual(self, data_files, wht_files,
                         use_dict, out_dir, out_name, filt):
     """ Runs sextractor in dual image mode"""
     print "Running dual:", out_name
     # Create config newfiles[i] and write out to a file
     config_ascii = asciidata.create(2, 8 + len(use_dict))
     # File-Specific Configurations
     config_ascii[0][0] = 'CATALOG_NAME'
     config_ascii[1][0] = out_dir + '/' + out_name + ".cat"
     config_ascii[0][1] = 'PARAMETERS_NAME'
     config_ascii[1][1] = out_dir + "/sex_out.param"
     config_ascii[0][2] = 'WEIGHT_TYPE'
     config_ascii[1][2] = str(self.params.wht_type + ',' + self.params.det_wht_type)
     config_ascii[0][3] = 'WEIGHT_IMAGE'
     config_ascii[1][3] = str(wht_files[0] + ',' + wht_files[1])
     row_counter = 4
     for key, value in use_dict.iteritems():
         config_ascii[0][row_counter] = key
         config_ascii[1][row_counter] = value
         row_counter += 1
     config_ascii[0][row_counter] = 'CHECKIMAGE_NAME'
     config_ascii[1][row_counter] = out_dir + '/' + out_name + "_seg_map.fits"
     config_ascii[0][row_counter + 1] = 'CHECKIMAGE_TYPE'
     config_ascii[1][row_counter + 1] = 'SEGMENTATION'
     config_ascii[0][row_counter + 2] = 'MAG_ZEROPOINT'
     config_ascii[1][row_counter + 2] = self.params.zero_point_mag[filt]
     config_ascii[0][row_counter + 3] = 'GAIN'
     config_ascii[1][row_counter + 3] = self.params.gain[filt]
     config_fname = out_dir + '/' + out_name + ".config"
     config_ascii.writeto(config_fname)
     # Run sextractor and get the catalog
     subprocess.call(["sex", data_files[0] + "," + data_files[1], "-c", config_fname])
Example #7
0
 def run_sextractor_dual(self, data_files, wht_files, use_dict, out_dir,
                         out_name, filt):
     """ Runs sextractor in dual image mode"""
     print "Running dual:", out_name
     # Create config newfiles[i] and write out to a file
     config_ascii = asciidata.create(2, 8 + len(use_dict))
     # File-Specific Configurations
     config_ascii[0][0] = 'CATALOG_NAME'
     config_ascii[1][0] = out_dir + '/' + out_name + ".cat"
     config_ascii[0][1] = 'PARAMETERS_NAME'
     config_ascii[1][1] = out_dir + "/sex_out.param"
     config_ascii[0][2] = 'WEIGHT_TYPE'
     config_ascii[1][2] = str(self.params.wht_type + ',' +
                              self.params.det_wht_type)
     config_ascii[0][3] = 'WEIGHT_IMAGE'
     config_ascii[1][3] = str(wht_files[0] + ',' + wht_files[1])
     row_counter = 4
     for key, value in use_dict.iteritems():
         config_ascii[0][row_counter] = key
         config_ascii[1][row_counter] = value
         row_counter += 1
     config_ascii[0][row_counter] = 'CHECKIMAGE_NAME'
     config_ascii[1][
         row_counter] = out_dir + '/' + out_name + "_seg_map.fits"
     config_ascii[0][row_counter + 1] = 'CHECKIMAGE_TYPE'
     config_ascii[1][row_counter + 1] = 'SEGMENTATION'
     config_ascii[0][row_counter + 2] = 'MAG_ZEROPOINT'
     config_ascii[1][row_counter + 2] = self.params.zero_point_mag[filt]
     config_ascii[0][row_counter + 3] = 'GAIN'
     config_ascii[1][row_counter + 3] = self.params.gain[filt]
     config_fname = out_dir + '/' + out_name + ".config"
     config_ascii.writeto(config_fname)
     # Run sextractor and get the catalog
     subprocess.call(
         ["sex", data_files[0] + "," + data_files[1], "-c", config_fname])
Example #8
0
def run_sextractor(file,
                   weight_file,
                   use_dict=faint_config_dict,
                   output_params=output_params,
                   clean=True):
    #Create params_file and write out to a file
    param_ascii = asciidata.create(1, len(output_params))
    row_counter = 0
    for param in output_params:
        param_ascii[0][row_counter] = param
        row_counter += 1
    param_fname = file + ".param"
    param_ascii.writeto(param_fname)

    #Create config newfiles[i] and write out to a file
    config_ascii = asciidata.create(2, 6 + len(use_dict))

    #File-Specific Configurations
    config_ascii[0][0] = 'CATALOG_NAME'
    config_ascii[1][0] = file + ".cat"
    config_ascii[0][1] = 'PARAMETERS_NAME'
    config_ascii[1][1] = file + ".param"
    config_ascii[0][2] = 'WEIGHT_TYPE'
    config_ascii[1][2] = 'MAP_WEIGHT'
    config_ascii[0][3] = 'WEIGHT_IMAGE'
    config_ascii[1][3] = weight_file
    config_ascii[0][4] = 'CHECKIMAGE_TYPE'
    config_ascii[1][4] = 'SEGMENTATION'
    config_ascii[0][5] = 'CHECKIMAGE_NAME'
    config_ascii[1][5] = file[:len(file) - 5] + ".mask.fits"
    row_counter = 6
    for key, value in use_dict.iteritems():
        config_ascii[0][row_counter] = key
        config_ascii[1][row_counter] = value
        row_counter += 1
    config_fname = file + ".config"
    config_ascii.writeto(config_fname)

    #Run sextractor and get the catalog
    subprocess.call(["sex", file, "-c", config_fname])
    if clean:
        #Optional Clean
        subprocess.call(["rm", config_fname])
        subprocess.call(["rm", param_fname])
def run_sextractor(file,weight_file,use_dict=faint_config_dict,output_params=output_params,clean=True): 
    #Create params_file and write out to a file
    param_ascii = asciidata.create(1,len(output_params))
    row_counter = 0
    for param in output_params:
        param_ascii[0][row_counter] = param
        row_counter += 1
    param_fname = file + ".param"
    param_ascii.writeto(param_fname)
    
    #Create config newfiles[i] and write out to a file
    config_ascii = asciidata.create(2,6+len(use_dict))
        
    #File-Specific Configurations
    config_ascii[0][0] = 'CATALOG_NAME'
    config_ascii[1][0] = file + ".cat"
    config_ascii[0][1] = 'PARAMETERS_NAME'
    config_ascii[1][1] = file + ".param"
    config_ascii[0][2] = 'WEIGHT_TYPE'
    config_ascii[1][2] = 'MAP_WEIGHT'
    config_ascii[0][3] = 'WEIGHT_IMAGE'
    config_ascii[1][3] = weight_file
    config_ascii[0][4] = 'CHECKIMAGE_TYPE'
    config_ascii[1][4] = 'SEGMENTATION'
    config_ascii[0][5] = 'CHECKIMAGE_NAME'
    config_ascii[1][5] = file[:len(file)-5] + ".mask.fits"
    row_counter = 6
    for key, value in use_dict.iteritems():
        config_ascii[0][row_counter] = key
        config_ascii[1][row_counter] = value
        row_counter += 1
    config_fname = file + ".config"
    config_ascii.writeto(config_fname)
                
    #Run sextractor and get the catalog
    subprocess.call(["sex", file , "-c", config_fname])
    if clean:
        #Optional Clean
        subprocess.call(["rm", config_fname])
        subprocess.call(["rm", param_fname])
Example #10
0
def create_ascii(listObj, saveto=None, header=None, delimiter='\t'):
    '''
    (by Alejandro N |uacute| |ntilde| ez)
    
    Save data from a Python list into an ascii file. It returns an asciidata type instance defined in the *asciidata* module.
    
    *listObj*
        Python list object with data to be saved.
    *saveto*
        String with name for ascii file. If no full path is provided, ascii file is created in the current directory. If no name is provided, ascii file is not created, only asciidata type instance is returned. This file name does not need to include extension (e.g. ".txt").
    *header*
        String or Python list of strings to be included as header information for the ascii file. This string (or list of strings) will appear as comments at the top of the ascii file contents.
    *delimiter*
        String specifying the delimiter desired for the ascii file. The default is *tab delimited*.
    '''
    # Initialize variables
    DELIMITER = '\t'
    
    # Determine important parameters
    numCols = len(listObj)
    numRows = 0
    for col in listObj:
        if len(col) > numRows:
            numRows = len(col)
    
    # Create ascii table
    asciiObj = ad.create(numCols, numRows, delimiter=DELIMITER)
    
    # Populate the asciidata table
    for colIdx in range(asciiObj.ncols):
        for rowIdx in range(len(listObj[colIdx])):
            asciiObj[colIdx][rowIdx] = listObj[colIdx][rowIdx]
    
    # Add header info
    if header is not None:
        if isinstance(header, types.StringTypes):
            asciiObj.header.append(header)
        else:
            for headerRow in header:
                asciiObj.header.append(headerRow)
    
    # Save file
    if saveto is not None:
        fileTp = '.txt'
        try:
            asciiObj.writeto(saveto + fileTp)
        except IOError:
            print 'Invalid name/location to save ascii file.'
    
    return asciiObj
Example #11
0
def run_sextractor(file, use_dict, output_params, out_name, clean=True):
    #Make sure the file opens
    try:
        hdulist = pyfits.open(file)
        hdulist.close()
    except IOError:
        print "IO Error for filename: ", file
    #Create params_file and write out to a file
    param_ascii = asciidata.create(1, len(output_params))
    row_counter = 0
    for param in output_params:
        param_ascii[0][row_counter] = param
        row_counter += 1
    param_fname = out_name + ".param"
    param_ascii.writeto(param_fname)

    #Create config newfiles[i] and write out to a file
    config_ascii = asciidata.create(2, 2 + len(use_dict))

    #File-Specific Configurations
    config_ascii[0][0] = 'CATALOG_NAME'
    config_ascii[1][0] = out_name + ".cat"
    config_ascii[0][1] = 'PARAMETERS_NAME'
    config_ascii[1][1] = out_name + ".param"
    row_counter = 2
    for key, value in use_dict.iteritems():
        config_ascii[0][row_counter] = key
        config_ascii[1][row_counter] = value
        row_counter += 1
    config_fname = out_name + ".config"
    config_ascii.writeto(config_fname)
    #Run sextractor and get the catalog
    subprocess.call(["sex", file, "-c", config_fname])
    #Optional Clean
    if clean:
        subprocess.call(["rm", config_fname])
        subprocess.call(["rm", param_fname])
def run_sextractor(file,use_dict,output_params,out_name,clean=True): 
    #Make sure the file opens
    try:
        hdulist = pyfits.open(file)
        hdulist.close()
    except IOError:
        print "IO Error for filename: ", file
    #Create params_file and write out to a file
    param_ascii = asciidata.create(1,len(output_params))
    row_counter = 0
    for param in output_params:
        param_ascii[0][row_counter] = param
        row_counter += 1
    param_fname = out_name + ".param"
    param_ascii.writeto(param_fname)
    
    #Create config newfiles[i] and write out to a file
    config_ascii = asciidata.create(2,2+len(use_dict))
        
    #File-Specific Configurations
    config_ascii[0][0] = 'CATALOG_NAME'
    config_ascii[1][0] = out_name + ".cat"
    config_ascii[0][1] = 'PARAMETERS_NAME'
    config_ascii[1][1] = out_name + ".param"
    row_counter = 2
    for key, value in use_dict.iteritems():
        config_ascii[0][row_counter] = key
        config_ascii[1][row_counter] = value
        row_counter += 1
    config_fname = out_name + ".config"
    config_ascii.writeto(config_fname)                
    #Run sextractor and get the catalog
    subprocess.call(["sex", file , "-c", config_fname])
    #Optional Clean
    if clean:
        subprocess.call(["rm", config_fname])
        subprocess.call(["rm", param_fname])
Example #13
0
    def work4(self):
        """
        Arguments:
        - `self`:
        """
        import asciidata
        
        newtable = asciidata.create(self.table.ncols, self.table.nrows)
        newtable[0] = self.table[2]
        newtable[1] = self.table[1]
        newtable[2] = self.table[0]
        newtable[3] = self.table[3]
        newtable[4] = self.table[4]

        newtable.sort(4)
        LaTeXTable = newtable.writetolatex('release_solutions.tex')
Example #14
0
def match_to_tt(image_star_table, tt_star_data_file, dist=200.):
    centroid_table = asciidata.open(tt_star_data_file)
    star_table = asciidata.open(image_star_table)
    out_table = asciidata.create(6, star_table.nrows)
    tt_centroids = []
    for i in range(star_table.nrows):
        x0 = star_table[0][i] * (7500. / 4210.)
        y0 = star_table[1][i] * (7500. / 4242.)
        r = star_table[2][i]
        for j in range(centroid_table.nrows):
            x = centroid_table[0][j]
            y = centroid_table[1][j]
            if abs(x0 - x) < dist and abs(y0 - y) < dist:
                tt_centroids.append((x, y, r))
                break
            if j == centroid_table.nrows - 1:
                print "no star found at location", x0, y0
    return tt_centroids
def select_good_stars(catalog, out_name, nstars=10):
    cat = asciidata.open(catalog)
    numbers = []
    for i in range(cat.nrows):
        numbers.append(cat['NUMBER'][i])
    random.shuffle(numbers)
    keep = []
    for i in range(nstars):
        keep.append(numbers[i])
    star_table = asciidata.create(3,nstars)
    for i in range(nstars):
        x = cat['X_IMAGE'][keep[i]]
        y = cat['Y_IMAGE'][keep[i]]
        r = cat['FLUX_RADIUS'][keep[i]]
        star_table[0][i] = x
        star_table[1][i] = y
        star_table[2][i] = r
    star_table.writeto(out_name)
def match_to_tt(image_star_table, tt_star_data_file, dist=200.):
    centroid_table = asciidata.open(tt_star_data_file)
    star_table = asciidata.open(image_star_table)
    out_table = asciidata.create(6, star_table.nrows)
    tt_centroids = []
    for i in range(star_table.nrows):
        x0 = star_table[0][i]
        y0 = star_table[1][i]
        r = star_table[2][i]
        for j in range(centroid_table.nrows):
            x = centroid_table[0][j]
            y = centroid_table[1][j]
            if abs(x0-x) < dist and abs(y0-y) < dist:
                tt_centroids.append((x,y,r))
                break
            if j == centroid_table.nrows-1:
                print "no star found"
    return tt_centroids
Example #17
0
def select_good_stars(catalog, out_name, nstars=10):
    cat = asciidata.open(catalog)
    numbers = []
    for i in range(cat.nrows):
        numbers.append(cat['NUMBER'][i])
    random.shuffle(numbers)
    keep = []
    for i in range(nstars):
        keep.append(numbers[i])
    star_table = asciidata.create(3, nstars)
    for i in range(nstars):
        x = cat['X_IMAGE'][keep[i]]
        y = cat['Y_IMAGE'][keep[i]]
        r = cat['FLUX_RADIUS'][keep[i]]
        star_table[0][i] = x
        star_table[1][i] = y
        star_table[2][i] = r
    star_table.writeto(out_name)
Example #18
0
 def edge_overlap_clean(self, out_name):
     A = (390., 321.)
     B = (498., 6725.)
     C = (6898., 7287.)
     D = (7002., 806.)
     (left_m, left_b) = points_to_line(A, B)
     (top_m, top_b) = points_to_line(B, C)
     (right_m, right_b) = points_to_line(C, D)
     (bottom_m, bottom_b) = points_to_line(D, A)
     #Open the original catalog
     table_catalog = asciidata.open(self.snr_catalog)
     #Make a new ascii table (the new catalog)
     new_table = asciidata.create(table_catalog.ncols, table_catalog.nrows)
     for i in range(table_catalog.nrows):
         x_min = table_catalog['XMIN_IMAGE'][i]
         x_max = table_catalog['XMAX_IMAGE'][i]
         y_min = table_catalog['YMIN_IMAGE'][i]
         y_max = table_catalog['YMAX_IMAGE'][i]
         if (x_min < left_m*x_min+left_b and x_max < right_m*x_max+right_b and \
         y_min > bottom_m*y_min+bottom_b and y_max < top_m*y_max+top_b):
             for k in range(table_catalog.ncols):
                 new_table[k][i] = table_catalog[k][i]
     #Get rid of empty rows
     row_number = 0
     while True:
         try:
             if new_table[0][row_number] is None:
                 new_table.delete(row_number)
             else:
                 row_number += 1
         except:
             break
     #Write out to another catalog
     new_table.writeto("table_edge_clean.cat")
     new_catalog = open("table_edge_clean.cat")
     old_catalog = open(self.snr_catalog)
     final_catalog = open(out_name, "w")
     for line in old_catalog.readlines():
         if line[0] == "#":
             final_catalog.write(line)
         else:
             break
     for line in new_catalog:
         final_catalog.write(line)
 def edge_overlap_clean(self, out_name):
     A = (390.,321.)
     B = (498.,6725.)
     C = (6898.,7287.)
     D = (7002.,806.)
     (left_m, left_b) = points_to_line(A,B)
     (top_m, top_b) = points_to_line(B,C)
     (right_m, right_b) = points_to_line(C,D)
     (bottom_m, bottom_b) = points_to_line(D,A)
     #Open the original catalog
     table_catalog = asciidata.open(self.snr_catalog)
     #Make a new ascii table (the new catalog)
     new_table = asciidata.create(table_catalog.ncols,table_catalog.nrows)
     for i in range(table_catalog.nrows):
         x_min = table_catalog['XMIN_IMAGE'][i]
         x_max = table_catalog['XMAX_IMAGE'][i]
         y_min = table_catalog['YMIN_IMAGE'][i]
         y_max = table_catalog['YMAX_IMAGE'][i]
         if (x_min < left_m*x_min+left_b and x_max < right_m*x_max+right_b and \
         y_min > bottom_m*y_min+bottom_b and y_max < top_m*y_max+top_b):
              for k in range(table_catalog.ncols):
                  new_table[k][i] = table_catalog[k][i]
     #Get rid of empty rows
     row_number = 0
     while True:
         try:
             if new_table[0][row_number] is None:
                 new_table.delete(row_number)
             else:
                 row_number += 1
         except:
             break
     #Write out to another catalog
     new_table.writeto("table_edge_clean.cat")
     new_catalog = open("table_edge_clean.cat")
     old_catalog = open(self.snr_catalog)
     final_catalog = open(out_name, "w")
     for line in old_catalog.readlines():
         if line[0] == "#":
             final_catalog.write(line)
         else:
             break
     for line in new_catalog:
         final_catalog.write(line)
 def filter_cat_with_segmentation_map(self, out_name):
     catalog = asciidata.open(self.faint_catalog)
     new_table = asciidata.create(catalog.ncols,catalog.nrows) 
     segmentation_file = pyfits.open(self.seg_map)
     data = segmentation_file[0].data   
     for j in range(0,catalog.nrows):
         x_center = catalog['X_IMAGE'][j]
         y_center = catalog['Y_IMAGE'][j]
         if data[y_center,x_center] == 0:
 	        for k in range(0,catalog.ncols):
 	            new_table[k][j] = catalog[k][j]
 	#Get rid of empty rows
     row_number = 0
     while True:
         try:
             if new_table[0][row_number] is None:
                 new_table.delete(row_number)
             else:
                 row_number += 1
         except:
             break
     #Write out to another catalog
     new_table.writeto(out_name)  
def select_good_stars(catalog, out_name, nstars=10):
    renumber(catalog)
    cat = asciidata.open(catalog)
    numbers = []
    snrs = []
    for i in range(cat.nrows):
        if cat['IS_STAR'][i] == 1: # and cat['MAG_AUTO'][i]+25 < 25.0:
            numbers.append(cat['NUMBER'][i])
            snrs.append(cat['SNR'][i])
    snr_arr = np.asarray(snrs)
    nstars = len(snrs)
    max_indexes = np.argsort(snr_arr)[-1*nstars:]
    keep = []
    for i in range(nstars):
        keep.append(numbers[max_indexes[i]])
    star_table = asciidata.create(3,nstars)
    for i in range(nstars):
        x = cat['X_IMAGE'][keep[i]]
        y = cat['Y_IMAGE'][keep[i]]
        r = cat['FLUX_RADIUS'][keep[i]]
        star_table[0][i] = x
        star_table[1][i] = y
        star_table[2][i] = r
    star_table.writeto(out_name)
Example #22
0
    def filter_cat_with_segmentation_map(self, out_name):
        catalog = asciidata.open(self.faint_catalog)
        new_table = asciidata.create(catalog.ncols, catalog.nrows)
        segmentation_file = pyfits.open(self.seg_map)
        data = segmentation_file[0].data
        for j in range(0, catalog.nrows):
            x_center = catalog['X_IMAGE'][j]
            y_center = catalog['Y_IMAGE'][j]
            if data[y_center, x_center] == 0:
                for k in range(0, catalog.ncols):
                    new_table[k][j] = catalog[k][j]

    #Get rid of empty rows
        row_number = 0
        while True:
            try:
                if new_table[0][row_number] is None:
                    new_table.delete(row_number)
                else:
                    row_number += 1
            except:
                break
        #Write out to another catalog
        new_table.writeto(out_name)
def select_good_stars(catalog, out_name, nstars=10):
    renumber(catalog)
    cat = asciidata.open(catalog)
    numbers = []
    snrs = []
    for i in range(cat.nrows):
        if cat['IS_STAR'][i] == 1:  # and cat['MAG_AUTO'][i]+25 < 25.0:
            numbers.append(cat['NUMBER'][i])
            snrs.append(cat['SNR'][i])
    snr_arr = np.asarray(snrs)
    nstars = len(snrs)
    max_indexes = np.argsort(snr_arr)[-1 * nstars:]
    keep = []
    for i in range(nstars):
        keep.append(numbers[max_indexes[i]])
    star_table = asciidata.create(3, nstars)
    for i in range(nstars):
        x = cat['X_IMAGE'][keep[i]]
        y = cat['Y_IMAGE'][keep[i]]
        r = cat['FLUX_RADIUS'][keep[i]]
        star_table[0][i] = x
        star_table[1][i] = y
        star_table[2][i] = r
    star_table.writeto(out_name)
Example #24
0
def delete_overlap(catalog, tolerance = 1./18000, clean=True):
    s = time.time()
    orig_name = catalog
    print "Deleting overlaps on file: ", orig_name
    catalog = asciidata.open(catalog)
    new_table = asciidata.create(catalog.ncols,catalog.nrows)
    delete_numbers = []
    for i in range(catalog.nrows-1):
        if (i+1) % 10 == 0:
            print "Working on object", i+1, "out of", catalog.nrows
        number = catalog['NUMBER'][i]
        ra, dec = catalog['ALPHA_SKY'][i], catalog['DELTA_SKY'][i]
        for j in range(i+1, catalog.nrows):
            number0 = catalog['NUMBER'][j]
            ra0, dec0 = catalog['ALPHA_SKY'][j], catalog['DELTA_SKY'][j]
            if abs(ra0-ra) < tolerance and abs(dec0-dec) < tolerance:
                delete_numbers.append(number0)
                delete_numbers.append(number)
    print "Delete numbers", delete_numbers
    new_table = asciidata.create(catalog.ncols,catalog.nrows)
    for i in range(catalog.nrows):
        if catalog['NUMBER'][i] not in delete_numbers:
             for k in range(catalog.ncols):
                 new_table[k][i] = catalog[k][i]
    #Get rid of empty rows
    row_number = 0
    while True:
        try:
            if new_table[0][row_number] is None:
                new_table.delete(row_number)
                if "606" in orig_name:
                    n_overlap606 += 1
                if "814" in orig_name:
                    n_overlap814 += 1
            else:
                row_number += 1
        except:
            break
    #Write out to another catalog
    new_table.writeto("overlap.cat")
    new_catalog = open("overlap.cat")
    old_catalog = open(orig_name)
    final_catalog = open("final_overlap.cat", "w")
    for line in old_catalog.readlines():
        if line[0] == "#":
            final_catalog.write(line)
        else:
            break
    for line in new_catalog:
        final_catalog.write(line)
    old_catalog.close()
    final_catalog.close()
    final = open("final_overlap.cat", "r")
    rewrite = open(orig_name, "w")
    for line in final.readlines():
        rewrite.write(line)
    #Optional clean
    if clean:
        subprocess.call(["rm", "overlap.cat"])
        subprocess.call(["rm", "final_overlap.cat"])
    e = time.time()
    print "Time:", e-s
            file_out.write(str(x) + '\t' + y + '\n')
        file_out.close()

###############################################################################

if False:
    # asciidata   (https://www.stecf.org/software/PYTHONtools/astroasciidata/)
    # dopo averlo estratto: python setup.py install
    import asciidata

    # scrivo
    if 0:
        x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
        y = [pippo * 2 for pippo in x]

        tabella = asciidata.create(2, 10, null=None, delimiter=None)
        #print tabella
        for i in range(tabella.nrows):
            tabella[0][i] = x[i]
            tabella[1][i] = y[i]

        tabella.writeto('tabella_ascii.txt')

    # leggo:
    if 0:
        tab = asciidata.open('tabella_ascii.txt')
        col1 = tab[0]
        col2 = tab[1]
        '''
        col1 = [x_ for x_ in col1]
        col2 = [x_ for x_ in col2]
    def diffraction_mask_cleanup(self, out_name, diff_spike_params, mag_cutoff = 19.0):
        #Get all stars for which we are masking
        #the length of the spike is given by l = m*Flux+b
        #the width of the spike is given by w
        m = diff_spike_params[0] 
        b = diff_spike_params[1]
        w = diff_spike_params[2]*0.5
        theta = diff_spike_params[3]
        star_numbers = []
        star_centroids = []
        star_radii = []
        star_spike_lengths = []
        orig_name = self.edge_catalog
        catalog = asciidata.open(self.edge_catalog)
        for i in range(catalog.nrows):
           if catalog['MAG_AUTO'][i] + 25 < mag_cutoff and catalog['IS_STAR'][i] == 1:
              star_numbers.append(catalog['NUMBER'][i])
              star_centroids.append((catalog['X_IMAGE'][i],catalog['Y_IMAGE'][i]))
              radius = np.mean([catalog['A_IMAGE'][i],catalog['B_IMAGE'][i]])
              star_radii.append(radius)
              flux = catalog['FLUX_AUTO'][i]
              length = m*flux+b
              star_spike_lengths.append(length)
        #Get the vertices of each star's diffraction mask
        x_vertex_sets = []
        y_vertex_sets = []
        for i in range(len(star_numbers)):
            centroid = (star_centroids[i])
            x0 = centroid[0]
            y0 = centroid[1]
            r = star_radii[i]
            l = star_spike_lengths[i]
            x_vertices = [x0-w,x0-w,x0+w,x0+w,x0+r,x0+l,x0+l,x0+r,x0+w,x0+w,x0-w,x0-w,x0-r,x0-l,x0-l,x0-r]
            y_vertices = [y0+r,y0+l,y0+l,y0+r,y0+w,y0+w,y0-w,y0-w,y0-r,y0-l,y0-l,y0-r,y0-w,y0-w,y0+w,y0+w]
            rotated = rotate(x_vertices,y_vertices,x0,y0,theta)
            x_rotated = rotated[0]
            y_rotated = rotated[1]
            x_vertex_sets.append(x_rotated)
            y_vertex_sets.append(y_rotated)
        delete_numbers = []
        print "Applying masks for", len(x_vertex_sets), "stars."
        for i in range(catalog.nrows):
            if (i+1) % 1000 == 0:
                print "Working on object", i+1, "out of", catalog.nrows
            number = catalog['NUMBER'][i]
            is_star = catalog['IS_STAR'][i]
            if is_star == 1:
                continue
            x_min,y_min = int(catalog['XMIN_IMAGE'][i]), int(catalog['YMIN_IMAGE'][i])
            x_max,y_max = int(catalog['XMAX_IMAGE'][i]), int(catalog['YMAX_IMAGE'][i])
            bottom_pixels = [(x,y_min) for x in range(x_min,x_max)]
            left_pixels = [(x_min,y) for y in range(y_min,y_max)]
            top_pixels = [(x, y_max) for x in range(x_min,x_max)]
            right_pixels = [(x_max,y) for y in range(y_min,y_max)]
            pixels = bottom_pixels + left_pixels + top_pixels + right_pixels
            bools = [inpoly(pixel[0],pixel[1],x_vertex_sets[j],y_vertex_sets[j]) for pixel in pixels for j in range(len(x_vertex_sets))]
            if max(bools) == 1:
                delete_numbers.append(number)
        print "Delete numbers", delete_numbers
        #Delete entries for which any pixel is within the mask
        #Make a new ascii table (the new catalog)
        new_table = asciidata.create(catalog.ncols,catalog.nrows)
        for i in range(catalog.nrows):
            if catalog['NUMBER'][i] not in delete_numbers:
                for k in range(catalog.ncols):
                    new_table[k][i] = catalog[k][i]
        #Get rid of empty rows
        row_number = 0
        while True:
            try:
                if new_table[0][row_number] is None:
                     new_table.delete(row_number)
                else:
                     row_number += 1
            except:
                break
        #Write out to another catalog
        new_table.writeto("diffraction_filter.cat")
        new_catalog = open("diffraction_filter.cat")
        old_catalog = open(orig_name)
        final_catalog = open(out_name, "w")
        for line in old_catalog.readlines():
            if line[0] == "#":
                final_catalog.write(line)
            else:
                break
        for line in new_catalog:
            final_catalog.write(line) 
    
	# Runs manual mask on a file where lines are:
	# # 'filename' (must be preceded by # and a space)
	# list of x-vertices for mask 1
	# list of y-vertices for mask 1
	# list of x-vertices for mask 2, etc...
	def manual_mask_catalogs(self, catalog_vertex_file=self.catalog_vertex_file):
		f = open(catalog_vertex_file)
		current_catalog = ""
		x_vertices = []
		y_vertices = []
		for line in f.readlines():
			split = line.split()
			if split[0] == '#':
				current_catalog = split[1]
				x_vertices = []
				y_vertices = []
			else:
				if len(x_vertices) == 0:
					for word in split:
						x_vertices.append(np.float32(word))
				else:
					for word in split:
						y_vertices.append(np.float32(word))
					print "Masking vertices on", current_catalog, "with vertices:"
					print x_vertices
					print y_vertices
					manual_mask(current_catalog, x_vertices, y_vertices)
					x_vertices = []
					y_vertices = []
Example #27
0
    def diffraction_mask_cleanup(self,
                                 out_name,
                                 diff_spike_params,
                                 mag_cutoff=19.0):
        #Get all stars for which we are masking
        #the length of the spike is given by l = m*Flux+b
        #the width of the spike is given by w
        m = diff_spike_params[0]
        b = diff_spike_params[1]
        w = diff_spike_params[2] * 0.5
        theta = diff_spike_params[3]
        star_numbers = []
        star_centroids = []
        star_radii = []
        star_spike_lengths = []
        orig_name = self.edge_catalog
        catalog = asciidata.open(self.edge_catalog)
        for i in range(catalog.nrows):
            if catalog['MAG_AUTO'][i] + 25 < mag_cutoff and catalog['IS_STAR'][
                    i] == 1:
                star_numbers.append(catalog['NUMBER'][i])
                star_centroids.append(
                    (catalog['X_IMAGE'][i], catalog['Y_IMAGE'][i]))
                radius = np.mean(
                    [catalog['A_IMAGE'][i], catalog['B_IMAGE'][i]])
                star_radii.append(radius)
                flux = catalog['FLUX_AUTO'][i]
                length = m * flux + b
                star_spike_lengths.append(length)
        #Get the vertices of each star's diffraction mask
        x_vertex_sets = []
        y_vertex_sets = []
        for i in range(len(star_numbers)):
            centroid = (star_centroids[i])
            x0 = centroid[0]
            y0 = centroid[1]
            r = star_radii[i]
            l = star_spike_lengths[i]
            x_vertices = [
                x0 - w, x0 - w, x0 + w, x0 + w, x0 + r, x0 + l, x0 + l, x0 + r,
                x0 + w, x0 + w, x0 - w, x0 - w, x0 - r, x0 - l, x0 - l, x0 - r
            ]
            y_vertices = [
                y0 + r, y0 + l, y0 + l, y0 + r, y0 + w, y0 + w, y0 - w, y0 - w,
                y0 - r, y0 - l, y0 - l, y0 - r, y0 - w, y0 - w, y0 + w, y0 + w
            ]
            rotated = rotate(x_vertices, y_vertices, x0, y0, theta)
            x_rotated = rotated[0]
            y_rotated = rotated[1]
            x_vertex_sets.append(x_rotated)
            y_vertex_sets.append(y_rotated)
        delete_numbers = []
        print "Applying masks for", len(x_vertex_sets), "stars."
        for i in range(catalog.nrows):
            if (i + 1) % 1000 == 0:
                print "Working on object", i + 1, "out of", catalog.nrows
            number = catalog['NUMBER'][i]
            is_star = catalog['IS_STAR'][i]
            if is_star == 1:
                continue
            x_min, y_min = int(catalog['XMIN_IMAGE'][i]), int(
                catalog['YMIN_IMAGE'][i])
            x_max, y_max = int(catalog['XMAX_IMAGE'][i]), int(
                catalog['YMAX_IMAGE'][i])
            bottom_pixels = [(x, y_min) for x in range(x_min, x_max)]
            left_pixels = [(x_min, y) for y in range(y_min, y_max)]
            top_pixels = [(x, y_max) for x in range(x_min, x_max)]
            right_pixels = [(x_max, y) for y in range(y_min, y_max)]
            pixels = bottom_pixels + left_pixels + top_pixels + right_pixels
            bools = [
                inpoly(pixel[0], pixel[1], x_vertex_sets[j], y_vertex_sets[j])
                for pixel in pixels for j in range(len(x_vertex_sets))
            ]
            if max(bools) == 1:
                delete_numbers.append(number)
        print "Delete numbers", delete_numbers
        #Delete entries for which any pixel is within the mask
        #Make a new ascii table (the new catalog)
        new_table = asciidata.create(catalog.ncols, catalog.nrows)
        for i in range(catalog.nrows):
            if catalog['NUMBER'][i] not in delete_numbers:
                for k in range(catalog.ncols):
                    new_table[k][i] = catalog[k][i]
        #Get rid of empty rows
        row_number = 0
        while True:
            try:
                if new_table[0][row_number] is None:
                    new_table.delete(row_number)
                else:
                    row_number += 1
            except:
                break
        #Write out to another catalog
        new_table.writeto("diffraction_filter.cat")
        new_catalog = open("diffraction_filter.cat")
        old_catalog = open(orig_name)
        final_catalog = open(out_name, "w")
        for line in old_catalog.readlines():
            if line[0] == "#":
                final_catalog.write(line)
            else:
                break
        for line in new_catalog:
            final_catalog.write(line)

# Runs manual mask on a file where lines are:
# # 'filename' (must be preceded by # and a space)
# list of x-vertices for mask 1
# list of y-vertices for mask 1
# list of x-vertices for mask 2, etc...

        def manual_mask_catalogs(self,
                                 catalog_vertex_file=self.catalog_vertex_file):
            f = open(catalog_vertex_file)
            current_catalog = ""
            x_vertices = []
            y_vertices = []
            for line in f.readlines():
                split = line.split()
                if split[0] == '#':
                    current_catalog = split[1]
                    x_vertices = []
                    y_vertices = []
                else:
                    if len(x_vertices) == 0:
                        for word in split:
                            x_vertices.append(np.float32(word))
                    else:
                        for word in split:
                            y_vertices.append(np.float32(word))
                        print "Masking vertices on", current_catalog, "with vertices:"
                        print x_vertices
                        print y_vertices
                        manual_mask(current_catalog, x_vertices, y_vertices)
                        x_vertices = []
                        y_vertices = []
Example #28
0
def manual_mask(catalog, x_vertices, y_vertices, clean=True):
    orig_name = catalog
    catalog = asciidata.open(catalog)
    new_table = asciidata.create(catalog.ncols, catalog.nrows)
    delete_numbers = []
    for i in range(catalog.nrows):
        if (i + 1) % 1000 == 0:
            print "Working on object", i + 1, "out of", catalog.nrows
        number = catalog['NUMBER'][i]
        is_star = catalog['IS_STAR'][i]
        if is_star == 1:
            continue
        x_min, y_min = catalog['XMIN_IMAGE'][i], catalog['YMIN_IMAGE'][i]
        x_max, y_max = catalog['XMAX_IMAGE'][i], catalog['YMAX_IMAGE'][i]
        bottom_pixels = [(x, y_min) for x in range(x_min, x_max)]
        left_pixels = [(x_min, y) for y in range(y_min, y_max)]
        top_pixels = [(x, y_max) for x in range(x_min, x_max)]
        right_pixels = [(x_max, y) for y in range(y_min, y_max)]
        pixels = bottom_pixels + left_pixels + top_pixels + right_pixels
        bools = [
            inpoly(pixel[0], pixel[1], x_vertices, y_vertices)
            for pixel in pixels
        ]
        if max(bools) == 1:
            delete_numbers.append(number)
    print "Delete numbers", delete_numbers
    new_table = asciidata.create(catalog.ncols, catalog.nrows)
    for i in range(catalog.nrows):
        if catalog['NUMBER'][i] not in delete_numbers:
            for k in range(catalog.ncols):
                new_table[k][i] = catalog[k][i]
    #Get rid of empty rows
    row_number = 0
    while True:
        try:
            if new_table[0][row_number] is None:
                new_table.delete(row_number)
            else:
                row_number += 1
        except:
            break
    #Write out to another catalog
    new_table.writeto("manual_filter.cat")
    new_catalog = open("manual_filter.cat")
    old_catalog = open(orig_name)
    final_catalog = open("final_catalog.cat", "w")
    for line in old_catalog.readlines():
        if line[0] == "#":
            final_catalog.write(line)
        else:
            break
    for line in new_catalog:
        final_catalog.write(line)
    old_catalog.close()
    final_catalog.close()
    final = open("final_catalog.cat", "r")
    rewrite = open(orig_name, "w")
    for line in final.readlines():
        rewrite.write(line)
    #Optional clean
    if clean:
        subprocess.call(["rm", "final_catalog.cat"])
        subprocess.call(["rm", "manual_filter.cat"])
Example #29
0
def delete_overlap(catalog, tolerance=1. / 18000, clean=True):
    s = time.time()
    orig_name = catalog
    print "Deleting overlaps on file: ", orig_name
    catalog = asciidata.open(catalog)
    new_table = asciidata.create(catalog.ncols, catalog.nrows)
    delete_numbers = []
    for i in range(catalog.nrows - 1):
        if (i + 1) % 10 == 0:
            print "Working on object", i + 1, "out of", catalog.nrows
        number = catalog['NUMBER'][i]
        ra, dec = catalog['ALPHA_SKY'][i], catalog['DELTA_SKY'][i]
        for j in range(i + 1, catalog.nrows):
            number0 = catalog['NUMBER'][j]
            ra0, dec0 = catalog['ALPHA_SKY'][j], catalog['DELTA_SKY'][j]
            if abs(ra0 - ra) < tolerance and abs(dec0 - dec) < tolerance:
                delete_numbers.append(number0)
                delete_numbers.append(number)
    print "Delete numbers", delete_numbers
    new_table = asciidata.create(catalog.ncols, catalog.nrows)
    for i in range(catalog.nrows):
        if catalog['NUMBER'][i] not in delete_numbers:
            for k in range(catalog.ncols):
                new_table[k][i] = catalog[k][i]
    #Get rid of empty rows
    row_number = 0
    while True:
        try:
            if new_table[0][row_number] is None:
                new_table.delete(row_number)
                if "606" in orig_name:
                    n_overlap606 += 1
                if "814" in orig_name:
                    n_overlap814 += 1
            else:
                row_number += 1
        except:
            break
    #Write out to another catalog
    new_table.writeto("overlap.cat")
    new_catalog = open("overlap.cat")
    old_catalog = open(orig_name)
    final_catalog = open("final_overlap.cat", "w")
    for line in old_catalog.readlines():
        if line[0] == "#":
            final_catalog.write(line)
        else:
            break
    for line in new_catalog:
        final_catalog.write(line)
    old_catalog.close()
    final_catalog.close()
    final = open("final_overlap.cat", "r")
    rewrite = open(orig_name, "w")
    for line in final.readlines():
        rewrite.write(line)
    #Optional clean
    if clean:
        subprocess.call(["rm", "overlap.cat"])
        subprocess.call(["rm", "final_overlap.cat"])
    e = time.time()
    print "Time:", e - s