def make_dark(self, savedir=None, do_bias=True, mbiaspath=None, dtype='float32', delimiter='-', comb_kwargs=MEDCOMB_KEYS): """ Makes and saves dark (bias subtracted) images. Parameters ---------- savedir: path-like, optional The directory where the frames will be saved. do_bias : bool, optional If ``True``, subtracts bias from dark frames using self.biaspahts. You can also specify ``mbiaspath`` to ignore that in ``self.``. mbiaspath : None, path-like, optional If you want to force a certain bias to be used, then you can specify its path here. delimiter : str, optional. The delimiter for the renaming. dtype : str or numpy.dtype object, optional. The data type you want for the final master bias frame. It is recommended to use ``float32`` or ``int16`` if there is no specific reason. comb_kwargs : dict or None, optional The parameters for ``combine_ccd``. """ # Initial settings self.initialize_self() if savedir is None: savedir = self.topdir yfu.mkdir(Path(savedir)) darkpaths = {} # For simplicity, crop the original data by type_key and # type_val first. st = self.summary_raw.copy() for k, v in zip(self.dark_type_key, self.dark_type_val): st = st[st[k] == v] # For grouping, use _key (i.e., type_key + group_key). This is # because (1) it is not harmful cuz type_key will have unique # column values as ``st`` has already been cropped in above for # loop (2) by doing this we get more information from combining # process because, e.g., "images with ["OBJECT", "EXPTIME"] = # ["dark", 1.0] are loaded" will be printed rather than just # "images with ["EXPTIME"] = [1.0] are loaded". gs = st.groupby(self.dark_key) # Do dark combine: for dark_val, dark_group in gs: if not isinstance(dark_val, tuple): dark_val = tuple([dark_val]) fname = delimiter.join([str(x) for x in dark_val]) + ".fits" fpath = Path(savedir) / fname mdark = yfu.combine_ccd(dark_group["file"].tolist(), output=None, dtype=dtype, **comb_kwargs, type_key=self.dark_key, type_val=dark_val) # set path to master bias if mbiaspath is not None: biaspath = mbiaspath elif do_bias: # corresponding key for biaspaths: corr_bias = tuple(self.bias_type_val) # if _group_key not empty, add appropriate ``group_val``: if self.bias_group_key: corr_bias += tuple(dark_group[self.bias_group_key].iloc[0]) # else: empty. path is fully specified by _type_val. try: biaspath = self.biaspaths[corr_bias] except KeyError: biaspath = None warn(f"Bias not available for {corr_bias}. " + "Processing without bias.") mdark = yfu.bdf_process(mdark, mbiaspath=biaspath, dtype=dtype, unit=None) mdark.write(fpath, output_verify='fix', overwrite=True) darkpaths[tuple(dark_val)] = fpath # Save list of file paths for future use. # It doesn't take much storage and easy to erase if you want. with open(self.listdir / 'darkpaths.list', 'w+') as ll: for p in list(darkpaths.values()): ll.write(f"{str(p)}\n") with open(self.listdir / 'darkpaths.pkl', 'wb') as pkl: pickle.dump(darkpaths, pkl) self.darkpaths = darkpaths
def make_flat(self, savedir=None, do_bias=True, do_dark=True, mbiaspath=None, mdarkpath=None, comb_kwargs=MEDCOMB_KEYS, delimiter='-', dtype='float32'): '''Makes and saves flat images. Parameters ---------- savedir: path-like, optional The directory where the frames will be saved. do_bias, do_dark : bool, optional If ``True``, subtracts bias and dark frames using ``self.biaspahts`` and ``self.darkpaths``. You can also specify ``mbiaspath`` and/or ``mdarkpath`` to ignore those in ``self.``. mbiaspath, mdarkpath : None, path-like, optional If you want to force a certain bias or dark to be used, then you can specify its path here. comb_kwargs: dict or None, optional The parameters for ``combine_ccd``. delimiter : str, optional. The delimiter for the renaming. dtype : str or numpy.dtype object, optional. The data type you want for the final master bias frame. It is recommended to use ``float32`` or ``int16`` if there is no specific reason. ''' # Initial settings self.initialize_self() if savedir is None: savedir = self.topdir yfu.mkdir(savedir) flatpaths = {} # For simplicity, crop the original data by type_key and # type_val first. st = self.summary_raw.copy() for k, v in zip(self.flat_type_key, self.flat_type_val): st = st[st[k] == v] # For grouping, use type_key + group_key. This is because (1) it # is not harmful cuz type_key will have unique column values as # ``st`` has already been cropped in above for loop (2) by doing # this we get more information from combining process because, # e.g., "images with ["OBJECT", "EXPTIME"] = ["dark", 1.0] are # loaded" will be printed rather than just "images with # ["EXPTIME"] = [1.0] are loaded". gs = st.groupby(self.flat_key) # Do flat combine: for flat_val, flat_group in gs: # set path to master bias if mbiaspath is not None: biaspath = mbiaspath elif do_bias: # corresponding key for biaspaths: corr_bias = tuple(self.bias_type_val) # if _group_key not empty, add appropriate ``group_val``: if self.bias_group_key: corr_bias += tuple(flat_group[self.bias_group_key].iloc[0]) # else: empty. path is fully specified by _type_val. biaspath = self.biaspaths[corr_bias] try: biaspath = self.biaspaths[corr_bias] except KeyError: biaspath = None warn(f"Bias not available for {corr_bias}. " + "Processing without bias.") # set path to master dark if mdarkpath is not None: darkpath = mdarkpath elif do_dark: # corresponding key for darkpaths: corr_dark = tuple(self.dark_type_val) # if _group_key not empty, add appropriate ``group_val``: if self.dark_group_key: corr_dark += tuple(flat_group[self.dark_group_key].iloc[0]) # else: empty. path is fully specified by _type_val. try: darkpath = self.darkpaths[corr_dark] except (KeyError): darkpath = None warn(f"Dark not available for {corr_dark}. " + "Processing without dark.") # Do BD preproc before combine flat_bd_paths = [] for i, flat_row in flat_group.iterrows(): flat_orig_path = Path(flat_row["file"]) flat_bd_path = (flat_orig_path.parent / (flat_orig_path.stem + "_BD.fits")) ccd = yfu.load_ccd(flat_orig_path, unit='adu') _ = yfu.bdf_process(ccd, output=flat_bd_path, mbiaspath=biaspath, mdarkpath=darkpath, dtype="int16", overwrite=True, unit=None) flat_bd_paths.append(flat_bd_path) if not isinstance(flat_val, tuple): flat_val = tuple([flat_val]) fname = delimiter.join([str(x) for x in flat_val]) + ".fits" fpath = Path(savedir) / fname _ = yfu.combine_ccd( flat_bd_paths, output=fpath, dtype=dtype, **comb_kwargs, normalize_average=True, # Since skyflat!! type_key=self.flat_key, type_val=flat_val) flatpaths[tuple(flat_val)] = fpath # Save list of file paths for future use. # It doesn't take much storage and easy to erase if you want. with open(self.listdir / 'flatpaths.list', 'w+') as ll: for p in list(flatpaths.values()): ll.write(f"{str(p)}\n") with open(self.listdir / 'flatpaths.pkl', 'wb') as pkl: pickle.dump(flatpaths, pkl) self.flatpaths = flatpaths
def make_bias(self, savedir=None, delimiter='-', dtype='float32', comb_kwargs=MEDCOMB_KEYS): ''' Finds and make bias frames. Parameters ---------- savedir : path-like, optional. The directory where the frames will be saved. delimiter : str, optional. The delimiter for the renaming. dtype : str or numpy.dtype object, optional. The data type you want for the final master bias frame. It is recommended to use ``float32`` or ``int16`` if there is no specific reason. comb_kwargs: dict or None, optional. The parameters for `~ysfitsutilpy.combine_ccd`. ''' # Initial settings self.initialize_self() if savedir is None: savedir = self.topdir yfu.mkdir(Path(savedir)) biaspaths = {} # For simplicity, crop the original data by type_key and # type_val first. st = self.summary_raw.copy() for k, v in zip(self.bias_type_key, self.bias_type_val): st = st[st[k] == v] # For grouping, use _key (i.e., type_key + group_key). This is # because (1) it is not harmful cuz type_key will have unique # column values as ``st`` has already been cropped in above for # loop (2) by doing this we get more information from combining # process because, e.g., "images with ["OBJECT", "EXPTIME"] = # ["dark", 1.0] are loaded" will be printed rather than just # "images with ["EXPTIME"] = [1.0] are loaded". gs = st.groupby(self.bias_key) # Do bias combine: for bias_val, bias_group in gs: if not isinstance(bias_val, tuple): bias_val = tuple([str(bias_val)]) fname = delimiter.join([str(x) for x in bias_val]) + ".fits" fpath = Path(savedir) / fname _ = yfu.combine_ccd(bias_group["file"].tolist(), output=fpath, dtype=dtype, **comb_kwargs, type_key=self.bias_key, type_val=bias_val) biaspaths[tuple(bias_val)] = fpath # Save list of file paths for future use. # It doesn't take much storage and easy to erase if you want. with open(self.listdir / 'biaspaths.list', 'w+') as ll: for p in list(biaspaths.values()): ll.write(f"{str(p)}\n") with open(self.listdir / 'biaspaths.pkl', 'wb') as pkl: pickle.dump(biaspaths, pkl) self.biaspaths = biaspaths