示例#1
0
    config = cr.parse(".config")

    # Get the list of FR bin files (fireball detections) in the given directory
    fr_list = [
        fr for fr in os.listdir(dir_path)
        if fr[0:2] == "FR" and fr.endswith('bin')
    ]
    fr_list = sorted(fr_list)

    if not fr_list:

        print("No files found!")
        sys.exit()

    # Get the list of FF bin files (compressed video frames)
    ff_list = [ff for ff in os.listdir(dir_path) if FFfile.validFFName(ff)]
    ff_list = sorted(ff_list)

    i = 0

    while True:

        # Break the loop if at the end
        if i >= len(fr_list):
            break

        fr = fr_list[i]

        ff_match = None

        # Strip extensions
示例#2
0
if __name__ == "__main__":

    time_start = time.clock()

    # Load config file
    config = cr.parse(".config")

    if not len(sys.argv) == 2:
        print("Usage: python -m RMS.ExtractStars /path/to/FF/files/")
        sys.exit()

    # Get paths to every FF bin file in a directory
    ff_dir = os.path.abspath(sys.argv[1])
    ff_list = [
        ff_name for ff_name in os.listdir(ff_dir)
        if FFfile.validFFName(ff_name)
    ]

    # Check if there are any file in the directory
    if (len(ff_list) == None):
        print("No files found!")
        sys.exit()

    # Try loading a flat field image
    flat_struct = None

    if config.use_flat:

        # Check if there is flat in the data directory
        if os.path.exists(os.path.join(ff_dir, config.flat_file)):
            flat_struct = Image.loadFlat(ff_dir, config.flat_file)
示例#3
0
def generateThumbnails(dir_path, config, mosaic_type, file_list=None):
    """ Generates a mosaic of thumbnails from all FF files in the given folder and saves it as a JPG image.
    
    Arguments:
        dir_path: [str] Path of the night directory.
        config: [Conf object] Configuration.
        mosaic_type: [str] Type of the mosaic (e.g. "Captured" or "Detected")

    Keyword arguments:
        file_list: [list] A list of file names (without full path) which will be searched for FF files. This
            is used when generating separate thumbnails for captured and detected files.

    Return:
        file_name: [str] Name of the thumbnail file.

    """

    if file_list is None:
        file_list = sorted(os.listdir(dir_path))


    # Make a list of all FF files in the night directory
    ff_list = []

    for file_name in file_list:
        if FFfile.validFFName(file_name):
            ff_list.append(file_name)


    # Calculate the dimensions of the binned image
    bin_w = int(config.width/config.thumb_bin)
    bin_h = int(config.height/config.thumb_bin)


    ### RESIZE AND STACK THUMBNAILS ###
    ##########################################################################################################

    timestamps = []
    stacked_imgs = []

    for i in range(0, len(ff_list), config.thumb_stack):

        img_stack = np.zeros((bin_h, bin_w))

        # Stack thumb_stack images using the 'if lighter' method
        for j in range(config.thumb_stack):

            if (i + j) < len(ff_list):

                # Read maxpixel image
                img = FFfile.read(dir_path, ff_list[i + j]).maxpixel

                # Resize the image
                img = cv2.resize(img, (bin_w, bin_h))

                # Stack the image
                img_stack = stackIfLighter(img_stack, img)

            else:
                break


        # Save the timestamp of the first image in the stack
        timestamps.append(FFfile.filenameToDatetime(ff_list[i]))

        # Save the stacked image
        stacked_imgs.append(img_stack)

        # cv2.imshow('test', img_stack)
        # cv2.waitKey(0)
        # cv2.destroyAllWindows()



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

    ### ADD THUMBS TO ONE MOSAIC IMAGE ###
    ##########################################################################################################

    header_height = 20
    timestamp_height = 10

    # Calculate the number of rows for the thumbnail image
    n_rows = int(np.ceil(float(len(ff_list))/config.thumb_stack/config.thumb_n_width))

    # Calculate the size of the mosaic
    mosaic_w = int(config.thumb_n_width*bin_w)
    mosaic_h = int((bin_h + timestamp_height)*n_rows + header_height)

    mosaic_img = np.zeros((mosaic_h, mosaic_w), dtype=np.uint8)

    # Write header text
    header_text = 'Station: ' + str(config.stationID) + ' Night: ' + os.path.basename(dir_path) \
        + ' Type: ' + mosaic_type
    cv2.putText(mosaic_img, header_text, (0, header_height//2), \
                    cv2.FONT_HERSHEY_SIMPLEX, 0.4, (255,255,255), 1)

    for row in range(n_rows):

        for col in range(config.thumb_n_width):

            # Calculate image index
            indx = row*config.thumb_n_width + col

            if indx < len(stacked_imgs):

                # Calculate position of the text
                text_x = col*bin_w
                text_y = row*bin_h + (row + 1)*timestamp_height - 1 + header_height

                # Add timestamp text
                cv2.putText(mosaic_img, timestamps[indx].strftime('%H:%M:%S'), (text_x, text_y), \
                    cv2.FONT_HERSHEY_SIMPLEX, 0.4, (255, 255, 255), 1)

                # Add the image to the mosaic
                img_pos_x = col*bin_w
                img_pos_y = row*bin_h + (row + 1)*timestamp_height + header_height

                mosaic_img[img_pos_y : img_pos_y + bin_h, img_pos_x : img_pos_x + bin_w] = stacked_imgs[indx]


            else:
                break

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

    thumb_name = "{:s}_{:s}_{:s}_thumbs.jpg".format(str(config.stationID), os.path.basename(dir_path), \
        mosaic_type)

    # Save the mosaic
    cv2.imwrite(os.path.join(dir_path, thumb_name), mosaic_img, [int(cv2.IMWRITE_JPEG_QUALITY), 80])


    return thumb_name
示例#4
0
        action="store_true",
        help="""Show a histogram of stddevs of PSFs of all detected stars. """)

    # Parse the command line arguments
    cml_args = arg_parser.parse_args()

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

    # Load the config file
    config = cr.loadConfigFromDirectory(cml_args.config, cml_args.dir_path)

    # Get paths to every FF bin file in a directory
    ff_dir = os.path.abspath(cml_args.dir_path[0])
    ff_list = [
        ff_name for ff_name in os.listdir(ff_dir)
        if FFfile.validFFName(ff_name)
    ]

    # Check if there are any file in the directory
    if (len(ff_list) == None):
        print("No files found!")
        sys.exit()

    # Run extraction and save the resulting CALSTARS file
    star_list = extractStarsAndSave(config, ff_dir)

    fwhm_list = []
    intensity_list = []
    x_list = []
    y_list = []
示例#5
0
def extractStarsAndSave(config, ff_dir):
    """ Extract stars in the given folder and save the CALSTARS file. 
    
    Arguments:
        config: [config object] configuration object (loaded from the .config file)
        ff_dir: [str] Path to directory where FF files are.

    Return:
        star_list: [list] A list of [ff_name, star_data] entries, where star_data contains a list of 
            (column, row, amplitude, intensity, fwhm) values for every star.

    """

    time_start = time.time()

    # Load mask, dark, flat
    mask, dark, flat_struct = loadImageCalibration(ff_dir, config)

    extraction_list = []

    # Go through all files in the directory and add them to the detection list
    for ff_name in sorted(os.listdir(ff_dir)):

        # Check if the given file is a valid FF file
        if not FFfile.validFFName(ff_name):
            continue

        extraction_list.append(ff_name)

    # Run the QueuedPool for detection
    workpool = QueuedPool(extractStars, cores=-1, backup_dir=ff_dir)

    # Add jobs for the pool
    for ff_name in extraction_list:
        print('Adding for extraction:', ff_name)
        workpool.addJob([
            ff_dir, ff_name, config, None, None, None, None, flat_struct, dark,
            mask
        ])

    print('Starting pool...')

    # Start the detection
    workpool.startPool()

    print('Waiting for the detection to finish...')

    # Wait for the detector to finish and close it
    workpool.closePool()

    # Get extraction results
    star_list = []
    for result in workpool.getResults():

        ff_name, x2, y2, amplitude, intensity, fwhm_data = result

        # Skip if no stars were found
        if not x2:
            continue

        # Construct the table of the star parameters
        star_data = list(zip(x2, y2, amplitude, intensity, fwhm_data))

        # Add star info to the star list
        star_list.append([ff_name, star_data])

    dir_name = os.path.basename(os.path.abspath(ff_dir))
    if dir_name.startswith(config.stationID):
        prefix = dir_name
    else:
        prefix = "{:s}_{:s}".format(config.stationID, dir_name)

    # Generate the name for the CALSTARS file
    calstars_name = 'CALSTARS_' + prefix + '.txt'

    # Write detected stars to the CALSTARS file
    CALSTARS.writeCALSTARS(star_list, ff_dir, calstars_name, config.stationID,
                           config.height, config.width)

    # Delete QueudPool backed up files
    workpool.deleteBackupFiles()

    print('Total time taken: {:.2f} s'.format(time.time() - time_start))

    return star_list
示例#6
0
def generateThumbnails(dir_path,
                       config,
                       mosaic_type,
                       file_list=None,
                       no_stack=False):
    """ Generates a mosaic of thumbnails from all FF files in the given folder and saves it as a JPG image.
    
    Arguments:
        dir_path: [str] Path of the night directory.
        config: [Conf object] Configuration.
        mosaic_type: [str] Type of the mosaic (e.g. "Captured" or "Detected")

    Keyword arguments:
        file_list: [list] A list of file names (without full path) which will be searched for FF files. This
            is used when generating separate thumbnails for captured and detected files.

    Return:
        file_name: [str] Name of the thumbnail file.
        no_stack: [bool] Don't stack the images using the config.thumb_stack option. A max of 1000 images
            are supported with this option. If there are more, stacks will be done according to the 
            config.thumb_stack option.

    """

    if file_list is None:
        file_list = sorted(os.listdir(dir_path))

    # Make a list of all FF files in the night directory
    ff_list = []

    for file_name in file_list:
        if FFfile.validFFName(file_name):
            ff_list.append(file_name)

    # Calculate the dimensions of the binned image
    bin_w = int(config.width / config.thumb_bin)
    bin_h = int(config.height / config.thumb_bin)

    ### RESIZE AND STACK THUMBNAILS ###
    ##########################################################################################################

    timestamps = []
    stacked_imgs = []

    thumb_stack = config.thumb_stack

    # Check if no stacks should be done (max 1000 images for no stack)
    if no_stack and (len(ff_list) < 1000):
        thumb_stack = 1

    for i in range(0, len(ff_list), thumb_stack):

        img_stack = np.zeros((bin_h, bin_w))

        # Stack thumb_stack images using the 'if lighter' method
        for j in range(thumb_stack):

            if (i + j) < len(ff_list):

                tmp_file_name = ff_list[i + j]

                # Read the FF file
                ff = FFfile.read(dir_path, tmp_file_name)

                # Skip the FF if it is corruped
                if ff is None:
                    continue

                img = ff.maxpixel

                # Resize the image
                img = cv2.resize(img, (bin_w, bin_h))

                # Stack the image
                img_stack = stackIfLighter(img_stack, img)

            else:
                break

        # Save the timestamp of the first image in the stack
        timestamps.append(FFfile.filenameToDatetime(ff_list[i]))

        # Save the stacked image
        stacked_imgs.append(img_stack)

        # cv2.imshow('test', img_stack)
        # cv2.waitKey(0)
        # cv2.destroyAllWindows()

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

    ### ADD THUMBS TO ONE MOSAIC IMAGE ###
    ##########################################################################################################

    header_height = 20
    timestamp_height = 10

    # Calculate the number of rows for the thumbnail image
    n_rows = int(
        np.ceil(float(len(ff_list)) / thumb_stack / config.thumb_n_width))

    # Calculate the size of the mosaic
    mosaic_w = int(config.thumb_n_width * bin_w)
    mosaic_h = int((bin_h + timestamp_height) * n_rows + header_height)

    mosaic_img = np.zeros((mosaic_h, mosaic_w), dtype=np.uint8)

    # Write header text
    header_text = 'Station: ' + str(config.stationID) + ' Night: ' + os.path.basename(dir_path) \
        + ' Type: ' + mosaic_type
    cv2.putText(mosaic_img, header_text, (0, header_height//2), \
                    cv2.FONT_HERSHEY_SIMPLEX, 0.4, (255,255,255), 1)

    for row in range(n_rows):

        for col in range(config.thumb_n_width):

            # Calculate image index
            indx = row * config.thumb_n_width + col

            if indx < len(stacked_imgs):

                # Calculate position of the text
                text_x = col * bin_w
                text_y = row * bin_h + (
                    row + 1) * timestamp_height - 1 + header_height

                # Add timestamp text
                cv2.putText(mosaic_img, timestamps[indx].strftime('%H:%M:%S'), (text_x, text_y), \
                    cv2.FONT_HERSHEY_SIMPLEX, 0.4, (255, 255, 255), 1)

                # Add the image to the mosaic
                img_pos_x = col * bin_w
                img_pos_y = row * bin_h + (
                    row + 1) * timestamp_height + header_height

                mosaic_img[img_pos_y:img_pos_y + bin_h,
                           img_pos_x:img_pos_x + bin_w] = stacked_imgs[indx]

            else:
                break

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

    # Only add the station ID if the dir name already doesn't start with it
    dir_name = os.path.basename(os.path.abspath(dir_path))
    if dir_name.startswith(config.stationID):
        prefix = dir_name
    else:
        prefix = "{:s}_{:s}".format(config.stationID, dir_name)

    thumb_name = "{:s}_{:s}_thumbs.jpg".format(prefix, mosaic_type)

    # Save the mosaic
    if USING_IMAGEIO:
        # Use imageio to write the image
        imwrite(os.path.join(dir_path, thumb_name), mosaic_img, quality=80)
    else:
        # Use OpenCV to save the image
        imwrite(os.path.join(dir_path, thumb_name), mosaic_img,
                [int(cv2.IMWRITE_JPEG_QUALITY), 80])

    return thumb_name
def generateThumbnails(dir_path, config, mosaic_type, file_list=None):
    """ Generates a mosaic of thumbnails from all FF files in the given folder and saves it as a JPG image.
    
    Arguments:
        dir_path: [str] Path of the night directory.
        config: [Conf object] Configuration.
        mosaic_type: [str] Type of the mosaic (e.g. "Captured" or "Detected")

    Keyword arguments:
        file_list: [list] A list of file names (without full path) which will be searched for FF files. This
            is used when generating separate thumbnails for captured and detected files.

    Return:
        file_name: [str] Name of the thumbnail file.

    """

    if file_list is None:
        file_list = sorted(os.listdir(dir_path))


    # Make a list of all FF files in the night directory
    ff_list = []

    for file_name in file_list:
        if FFfile.validFFName(file_name):
            ff_list.append(file_name)


    # Calculate the dimensions of the binned image
    bin_w = int(config.width/config.thumb_bin)
    bin_h = int(config.height/config.thumb_bin)


    ### RESIZE AND STACK THUMBNAILS ###
    ##########################################################################################################

    timestamps = []
    stacked_imgs = []

    for i in range(0, len(ff_list), config.thumb_stack):

        img_stack = np.zeros((bin_h, bin_w))

        # Stack thumb_stack images using the 'if lighter' method
        for j in range(config.thumb_stack):

            if (i + j) < len(ff_list):

                tmp_file_name = ff_list[i + j]

                    
                # Read the FF file
                ff = FFfile.read(dir_path, tmp_file_name)

                # Skip the FF if it is corruped
                if ff is None:
                    continue

                img = ff.maxpixel

                # Resize the image
                img = cv2.resize(img, (bin_w, bin_h))

                # Stack the image
                img_stack = stackIfLighter(img_stack, img)

            else:
                break


        # Save the timestamp of the first image in the stack
        timestamps.append(FFfile.filenameToDatetime(ff_list[i]))

        # Save the stacked image
        stacked_imgs.append(img_stack)

        # cv2.imshow('test', img_stack)
        # cv2.waitKey(0)
        # cv2.destroyAllWindows()



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

    ### ADD THUMBS TO ONE MOSAIC IMAGE ###
    ##########################################################################################################

    header_height = 20
    timestamp_height = 10

    # Calculate the number of rows for the thumbnail image
    n_rows = int(np.ceil(float(len(ff_list))/config.thumb_stack/config.thumb_n_width))

    # Calculate the size of the mosaic
    mosaic_w = int(config.thumb_n_width*bin_w)
    mosaic_h = int((bin_h + timestamp_height)*n_rows + header_height)

    mosaic_img = np.zeros((mosaic_h, mosaic_w), dtype=np.uint8)

    # Write header text
    header_text = 'Station: ' + str(config.stationID) + ' Night: ' + os.path.basename(dir_path) \
        + ' Type: ' + mosaic_type
    cv2.putText(mosaic_img, header_text, (0, header_height//2), \
                    cv2.FONT_HERSHEY_SIMPLEX, 0.4, (255,255,255), 1)

    for row in range(n_rows):

        for col in range(config.thumb_n_width):

            # Calculate image index
            indx = row*config.thumb_n_width + col

            if indx < len(stacked_imgs):

                # Calculate position of the text
                text_x = col*bin_w
                text_y = row*bin_h + (row + 1)*timestamp_height - 1 + header_height

                # Add timestamp text
                cv2.putText(mosaic_img, timestamps[indx].strftime('%H:%M:%S'), (text_x, text_y), \
                    cv2.FONT_HERSHEY_SIMPLEX, 0.4, (255, 255, 255), 1)

                # Add the image to the mosaic
                img_pos_x = col*bin_w
                img_pos_y = row*bin_h + (row + 1)*timestamp_height + header_height

                mosaic_img[img_pos_y : img_pos_y + bin_h, img_pos_x : img_pos_x + bin_w] = stacked_imgs[indx]


            else:
                break

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

    thumb_name = "{:s}_{:s}_{:s}_thumbs.jpg".format(str(config.stationID), os.path.basename(dir_path), \
        mosaic_type)

    # Save the mosaic
    cv2.imwrite(os.path.join(dir_path, thumb_name), mosaic_img, [int(cv2.IMWRITE_JPEG_QUALITY), 80])


    return thumb_name
示例#8
0
    # Load the configuration file
    config = cr.parse(".config")

    

    # Get the list of FR bin files (fireball detections) in the given directory
    fr_list = [fr for fr in os.listdir(dir_path) if fr[0:2]=="FR" and fr.endswith('bin')]
    fr_list = sorted(fr_list)

    if not fr_list:

        print("No files found!")
        sys.exit()

    # Get the list of FF bin files (compressed video frames)
    ff_list = [ff for ff in os.listdir(dir_path) if FFfile.validFFName(ff)]
    ff_list = sorted(ff_list)


    i = 0

    while True:

        # Break the loop if at the end
        if i >= len(fr_list):
            break

        fr = fr_list[i]

        ff_match = None
示例#9
0

if __name__ == "__main__":

    time_start = time.clock()

    # Load config file
    config = cr.parse(".config")

    if not len(sys.argv) == 2:
        print("Usage: python -m RMS.ExtractStars /path/to/FF/files/")
        sys.exit()
    
    # Get paths to every FF bin file in a directory 
    ff_dir = os.path.abspath(sys.argv[1])
    ff_list = [ff_name for ff_name in os.listdir(ff_dir) if FFfile.validFFName(ff_name)]

    # Check if there are any file in the directory
    if(len(ff_list) == None):
        print("No files found!")
        sys.exit()



    # Try loading a flat field image
    flat_struct = None

    if config.use_flat:
        
        # Check if there is flat in the data directory
        if os.path.exists(os.path.join(ff_dir, config.flat_file)):
示例#10
0
        help="Path to a config file which will be used instead of the default one.")

    arg_parser.add_argument('-s', '--showstd', action="store_true", help="""Show a histogram of stddevs of PSFs of all detected stars. """)

    # Parse the command line arguments
    cml_args = arg_parser.parse_args()

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

    # Load the config file
    config = cr.loadConfigFromDirectory(cml_args.config, cml_args.dir_path)

    
    # Get paths to every FF bin file in a directory 
    ff_dir = os.path.abspath(cml_args.dir_path[0])
    ff_list = [ff_name for ff_name in os.listdir(ff_dir) if FFfile.validFFName(ff_name)]

    # Check if there are any file in the directory
    if(len(ff_list) == None):
        print("No files found!")
        sys.exit()



    # Try loading a flat field image
    flat_struct = None

    if config.use_flat:
        
        # Check if there is flat in the data directory
        if os.path.exists(os.path.join(ff_dir, config.flat_file)):