def get_exp_files(pipeline, night, obj, filtr, exptime, proctype='InstCal', prep_files=False): """ Get all of the exposures for a given night, object, and filter. This funpacks the relevant exposure fits image, weight maps, and data quality masks and copies them into the pipelines temp_path directory. """ from astropyp import index sql = "select * from decam_obs where cal_date='{0}' and object like'{1}%'".format(night, obj) sql += "and object not like '%%SLEW' and filter like '{0}%' and exptime='{1}'".format( filtr, exptime ) exposures = index.query(sql, pipeline.idx_connect_str).sort(['expnum']) expnums = exposures['expnum'].unique() # Get the filenames for the image. dqmask, and wtmap products for each exposure exp_files = {expnum:{} for expnum in expnums} all_exposures = [] for idx, row in exposures.iterrows(): sql = "select * from decam_files where EXPNUM={0} and proctype='{1}'".format( row['expnum'], proctype) files = index.query(sql, pipeline.idx_connect_str) for fidx, file in files.iterrows(): fits_name = get_fits_name(file['EXPNUM'], file['PRODTYPE']) fits_path = os.path.join(pipeline.temp_path, fits_name) # Funpack the file if prep_files: funpack_file(file['filename'], fits_path) exp_files[file['EXPNUM']][file['PRODTYPE']] = fits_path if file['PRODTYPE']=='image': all_exposures.append(fits_path) # Create ahead files if prep_files: create_ahead(file['EXPNUM'], pipeline.temp_path) return (exposures, expnums, exp_files, all_exposures)
def get_header_info(pipeline, cat_name, frame): """ Get the exposure time, airmass, and gain from a DECam fits header Parameters ---------- pipeline: :class:`.Pipeline` Object that contains parameters about a decam pipeline cat_name: str Name of a catalog that contains a list of sources found using decamtoyz frame: int CCD number of the header to load Returns ------- exptime: float Exposure time of the image airmass: float Airmass (sec(zenith distance)) gain: float Gain in electrons/adu """ from astropyp import index from astropy.io import fits import numpy as np expnum = os.path.basename(cat_name).split('-')[0] sql = "select * from decam_files where EXPNUM={0} and PRODTYPE='image'".format(int(expnum)) files = index.query(sql, pipeline.idx_connect_str) header = fits.getheader(os.path.join(pipeline.paths['decam'], files.iloc[0].filename), ext=0) exptime = header['exptime'] airmass = 1/np.cos(np.radians(header['ZD'])) header = fits.getheader(os.path.join(pipeline.paths['decam'], files.iloc[0].filename), ext=frame) gain = header['arawgain'] return exptime, airmass, gain
def funpack_exposures(idx_connect_str, exposures, temp_path, proctype='InstCal'): """ Run funpack to unpack compressed fits images (fits.fz) so that the AstrOmatic suite can read them. This also creates an ahead file for each image that can be used by SCAMP containing the appropriate header information Parameters ---------- idx_connect_str: str Connection to database containing decam observation index exposures: `pandas.DataFrame` DataFrame with information about the observations temp_path: str Temporary directory where files are stored for pipeline processing proctype: str PROCTYPE from DECam image header (processing type of product from DECam community pipeline) """ from astropyp import index expnums = exposures['expnum'].unique() # Get the filenames for the image, dqmask, and wtmap products for each exposure for idx, row in exposures.iterrows(): sql = "select * from decam_files where EXPNUM={0} and proctype='{1}'".format( row['expnum'], proctype) files = index.query(sql, idx_connect_str) for fidx, file_info in files.iterrows(): fits_name = get_fits_name(file_info['EXPNUM'], file_info['PRODTYPE']) fits_path = os.path.join(temp_path, fits_name) funpack_file(file_info['filename'], fits_path) if file_info['PRODTYPE']=='image': create_ahead(file_info['EXPNUM'], temp_path)
def get_phot_params(catalog, mag_name, img_filename=None, expnum=None, proctype='InstCal', connection=None, frame_colname='frame', flux_name='FLUX_PSF'): """ Get necessary parameters like airmass and exposure time from the FITS image headers """ import numpy as np from astropyp import index from astropy.io import fits # Get fields from FITS header if img_filename is None: sql = "select * from decam_obs where EXPNUM={0} and PRODTYPE='image'".format(expnum) sql += " and PROCTYPE='{0}'".format(proctype) files = index.query(sql, connection) # Make sure there is only one image with the correct expnum if len(files)==0: raise DecamPhotError("No files found with EXPNUM {0}".format(expnum)) elif len(files)>1: raise DecamPhotError("Multiple files found with EXPNUM {0}".format(expnum)) img_filename = files[0]['filename'] hdulist = fits.open(img_filename, memmap=True) header = hdulist[0].header exptime = header['EXPTIME'] catalog['airmass'] = 1/np.cos(np.radians(header['ZD'])) catalog[mag_name] = np.nan #catalog['filename'] = catalog_filename frames = np.unique(catalog[frame_colname]) # add fields to catalog frame by frame for frame in frames: gain = hdulist[frame].header['arawgain'] logger.debug('frame {0}: gain={1}, exptime={2}'.format(frame, gain, exptime)) frame_condition = catalog[frame_colname]==frame counts = np.array(catalog[flux_name]) * gain/exptime catalog[flux_name][frame_condition & (counts==0)] = np.nan catalog[mag_name][frame_condition] = -2.5*np.log10(counts[frame_condition]) catalog['ccd'] = catalog['frame'] return catalog