def get_dir_files(directory): """ Make dictionary of filenames in given directory files["filename"]["data"] = fits data files["filename"]["lbl"] = lbl info files["filename"]["header"] = fits header info returns: dictionary of files """ files = {} for filename in os.listdir(directory): filepath = directory + "/" + filename name = filename.split('.')[0] ext = '.' + '.'.join(filename.split('.')[1:]) if name not in files: files[name] = {} if ext == ".fit.fz" or ext == ".fit": with fits.open(filepath) as hdul: files[name]["data"] = hdul[1].data files[name]["header"] = hdul[1].header elif ext == ".lbl": files[name]["lbl"] = lblparser.lbl_parse((filepath)) # check for files missing their fit or lbl missing = [name for name in files.keys() if len(files[name]) != 3] for m in missing: print("Missing corresponding fits or lbl for file: " + m) del files[m] return files # nested dictionary -> files[filename][lbl or data]
def preprocessSampleData(light_idx, FITSFiles, longid): """Use provided correction methods to subtract out dark images and use flats to correct for vignetting. Write the processed file to the temporary preprocessed directory. """ dark_idx, flat_idx, ttd, ttf = odf_mapper(FITSFiles, light_idx) print(ttd, ttf) if dark_idx < 0 and flat_idx < 0: print("NON-COMPLIANT") return lights_lbl = lbl_parse(FITSFiles['lights_lbl'][light_idx]) light = CCDData.read(FITSFiles['lights'][light_idx], unit='adu') dark = CCDData.read(FITSFiles['darks'][dark_idx], unit='adu') flat = CCDData.read(FITSFiles['flats'][flat_idx], unit='adu') corr = flat.data corr1 = light.data - dark.data light.data = corr1 / corr flat_corrected = light path_plan = processed_volume + "/" + sample + "/" try: print("Attempting to build path...") os.makedirs(path_plan) print("Built!") print("Writing to file " + str(longid.split('.')[0]) + ".fits") flat_corrected.write(path_plan + str(longid.split('.')[0]) + '.fits') except Exception as e: print("Directory already exists. Writing to file " + str(longid.split('.')[0]) + ".fits") flat_corrected.write(path_plan + str(longid.split('.')[0]) + '.fits') return flat_corrected
def getConeParams(lbl_filepath): lab = lbl_parse(lbl_filepath) world_ra = float(lab['RIGHT_ASCENSION'].partition('<')[0]) world_dec = float(lab['DECLINATION'].partition('<')[0]) hor_fov_arcsec = float(lab['HORIZONTAL_PIXEL_FOV'].partition('<')[0]) radius = 0.15 return (world_ra, world_dec, radius)
def odf_mapper(FITSFiles, light_idx): """Transforms light index into closest dark and flat indexes""" lab = lbl_parse(FITSFiles['lights'][light_idx][:-3] + "lbl") ttd = lab["START_TIME"].partition('T') best_didx = 0 best_fidx = 0 min_t_d_delta = 99999999 min_t_f_delta = 99999999 for didx in range(len(FITSFiles['darks'])): dta = lbl_parse(FITSFiles['darks'][didx][:-3] + "lbl") ttd_dta = dta["STOP_TIME"].partition('T') if ttd_dta[0] == ttd[0]: tdelta = abs((datetime.strptime(ttd_dta[2], FMT) - datetime.strptime(ttd[2], FMT)).total_seconds()) if tdelta < min_t_d_delta: min_t_d_delta = tdelta best_didx = didx for fidx in range(len(FITSFiles['flats'])): fta = lbl_parse(FITSFiles['flats'][fidx][:-3] + "lbl") ttf_fta = fta["STOP_TIME"].partition('T') if (ttf_fta[0] == ttd[0]): tdelta = abs((datetime.strptime(ttf_fta[2], FMT) - datetime.strptime(ttd[2], FMT)).total_seconds()) if tdelta < min_t_f_delta: min_t_f_delta = tdelta best_fidx = fidx light_lbl = lbl_parse(FITSFiles['lights'][light_idx][:-3] + "lbl") dark_lbl = lbl_parse(FITSFiles['darks'][best_didx][:-3] + "lbl") flat_lbl = lbl_parse(FITSFiles['flats'][best_fidx][:-3] + "lbl") if (flat_lbl['TARGET_NAME'] == '"FLAT FIELD"' and dark_lbl['TARGET_NAME'] == '"DARK"' and float(flat_lbl['EXPOSURE_DURATION'].partition('<')[0]) == float(dark_lbl['EXPOSURE_DURATION'].partition('<')[0]) and light_lbl['TARGET_NAME'] == '"ASTEROID"' and light_lbl['FILTER_NAME'] == '"NONE"' and float(light_lbl['EXPOSURE_DURATION'].partition('<')[0]) == float(dark_lbl['EXPOSURE_DURATION'].partition('<')[0])): return (best_didx, best_fidx, min_t_d_delta, min_t_f_delta) else: return (-1, -1, 0, 0)