diff --git a/src/hostphot/_constants.py b/src/hostphot/_constants.py index 89c1a0a..be16e2b 100644 --- a/src/hostphot/_constants.py +++ b/src/hostphot/_constants.py @@ -2,6 +2,7 @@ import os from dotenv import load_dotenv + load_dotenv() -workdir = os.getenv('workdir', 'images') +workdir = os.getenv("workdir", "images") diff --git a/src/hostphot/coadd.py b/src/hostphot/coadd.py index 5af936a..08d13b7 100644 --- a/src/hostphot/coadd.py +++ b/src/hostphot/coadd.py @@ -53,6 +53,6 @@ def coadd_images(name, filters="riz", survey="PS1"): ) fits_image[0].data = coadd[0] if isinstance(filters, list): - filters = ''.join(filt for filt in filters) + filters = "".join(filt for filt in filters) outfile = os.path.join(obj_dir, f"{survey}_{filters}.fits") fits_image.writeto(outfile, overwrite=True) diff --git a/src/hostphot/cutouts.py b/src/hostphot/cutouts.py index 536d163..1d3066c 100644 --- a/src/hostphot/cutouts.py +++ b/src/hostphot/cutouts.py @@ -338,7 +338,7 @@ def get_SDSS_images(ra, dec, size=3, filters=None): if hdu_id == 0: hdu_list = SDSS.get_images(matches=ids, band=filters) else: - sub_id = ids[hdu_id - 1:hdu_id] # get the one in the middle + sub_id = ids[hdu_id - 1 : hdu_id] # get the one in the middle hdu_list = SDSS.get_images(matches=sub_id, band=filters) # SDSS images are large so need to be trimmed @@ -398,14 +398,14 @@ def get_GALEX_images(ra, dec, size=3, filters=None): ) obs_table = Observations.query_criteria( - coordinates=coords, radius=size_arcsec, obs_collection=['GALEX'] + coordinates=coords, radius=size_arcsec, obs_collection=["GALEX"] ) obs_df = obs_table.to_pandas() - obs_df = obs_df[obs_df.project == 'AIS'] # only all sky survey + obs_df = obs_df[obs_df.project == "AIS"] # only all sky survey # find the URL of the "intensity" image to be downloaded for url in obs_df.dataURL: - if '-int.fits' in url: + if "-int.fits" in url: dataURL = url break else: @@ -414,30 +414,30 @@ def get_GALEX_images(ra, dec, size=3, filters=None): if dataURL is None: return None - filters_dict = {'NUV':'nd', 'FUV':'fd'} + filters_dict = {"NUV": "nd", "FUV": "fd"} ext_list = [filters_dict[filt] for filt in filters] hdu_list = [] for ext in ext_list: - if f'-{ext}-' in dataURL: + if f"-{ext}-" in dataURL: pass else: - if ext == 'nd': - dataURL = dataURL.replace('-fd-', '-nd-') - elif ext == 'fd': - dataURL = dataURL.replace('-nd-', '-fd-') + if ext == "nd": + dataURL = dataURL.replace("-fd-", "-nd-") + elif ext == "fd": + dataURL = dataURL.replace("-nd-", "-fd-") response = requests.get(dataURL, stream=True) target_file = os.path.basename(dataURL) # current directory if response.status_code == 200: - with open(target_file, 'wb') as f_out: + with open(target_file, "wb") as f_out: f_out.write(response.raw.read()) else: continue # skip this filter - with gzip.open(target_file, 'rb') as f_in: - output_file = target_file.removesuffix('.gz') - with open(output_file, 'wb') as f_out: + with gzip.open(target_file, "rb") as f_in: + output_file = target_file.removesuffix(".gz") + with open(output_file, "wb") as f_out: shutil.copyfileobj(f_in, f_out) hdu = fits.open(output_file) os.remove(output_file) @@ -476,6 +476,7 @@ def get_used_image(header): return used_image + def get_WISE_images(ra, dec, size=3, filters=None): """Downloads a set of WISE fits images for a given set of coordinates and filters. @@ -549,6 +550,7 @@ def get_WISE_images(ra, dec, size=3, filters=None): return hdu_list + def get_unWISE_images(ra, dec, size=3, filters=None, version="allwise"): """Downloads a set of unWISE fits images for a given set of coordinates and filters. @@ -593,17 +595,19 @@ def get_unWISE_images(ra, dec, size=3, filters=None, version="allwise"): size_pixels = int(size_arcsec.value / pixel_scale) assert size_pixels <= 1024, "Maximum cutout size for unWISE is 1024 pixels" - bands = ''.join(filt[-1] for filt in filters) # e.g. 1234 + bands = "".join(filt[-1] for filt in filters) # e.g. 1234 # for more info: http://unwise.me/imgsearch/ base_url = "http://unwise.me/cutout_fits?" - params_url = f"version={version}&ra={ra}&dec={dec}&size={size_pixels}&bands={bands}" + params_url = ( + f"version={version}&ra={ra}&dec={dec}&size={size_pixels}&bands={bands}" + ) master_url = base_url + params_url response = requests.get(master_url, stream=True) - target_file = 'unWISE_images.tar.gz' # current directory + target_file = "unWISE_images.tar.gz" # current directory if response.status_code == 200: - with open(target_file, 'wb') as f: + with open(target_file, "wb") as f: f.write(response.raw.read()) hdu_list = [] @@ -611,8 +615,8 @@ def get_unWISE_images(ra, dec, size=3, filters=None, version="allwise"): files_list = tar_file.getnames() for fits_file in files_list: for filt in filters: - if f'{filt.lower()}-img-m.fits' in fits_file: - tar_file.extract(fits_file, '.') + if f"{filt.lower()}-img-m.fits" in fits_file: + tar_file.extract(fits_file, ".") hdu = fits.open(fits_file) hdu_list.append(hdu) os.remove(fits_file) @@ -677,7 +681,7 @@ def get_2MASS_images(ra, dec, size=3, filters=None): hdu_list = [] for i, filt in enumerate(filters): band_df = twomass_df[twomass_df.band == filt] - if len(band_df)==0: + if len(band_df) == 0: # no data for this band: hdu_list.append(None) continue @@ -790,7 +794,8 @@ def get_HST_images(ra, dec, size=3, filters=None): Table.from_pandas(dp_df), productType="SCIENCE", extension="fits" ) - #return hdu_list + # return hdu_list + # Legacy Survey def get_LegacySurvey_images(ra, dec, size=3, filters=None): @@ -825,8 +830,8 @@ def get_LegacySurvey_images(ra, dec, size=3, filters=None): size_arcsec = size.to(u.arcsec).value size_pixels = int(size_arcsec / pixel_scale) - base_url = 'https://www.legacysurvey.org/viewer/fits-cutout?' - params = f'ra={ra}&dec={dec}&layer=ls-dr9&pixscale={pixel_scale}&bands={filters}&size={size_pixels}' + base_url = "https://www.legacysurvey.org/viewer/fits-cutout?" + params = f"ra={ra}&dec={dec}&layer=ls-dr9&pixscale={pixel_scale}&bands={filters}&size={size_pixels}" url = base_url + params master_hdu = fits.open(url) header = master_hdu[0].header @@ -840,6 +845,7 @@ def get_LegacySurvey_images(ra, dec, size=3, filters=None): return hdu_list + # Spitzer def get_Spitzer_images(ra, dec, size=3, filters=None): """Gets Spitzer fits images for the given coordinates and @@ -866,36 +872,43 @@ def get_Spitzer_images(ra, dec, size=3, filters=None): filters = get_survey_filters(survey) check_filters_validity(filters, survey) - pixel_scale = survey_pixel_scale(survey, 'IRAC.1') # IRAC resolution + pixel_scale = survey_pixel_scale(survey, "IRAC.1") # IRAC resolution if isinstance(size, (float, int)): size_arcsec = (size * u.arcmin).to(u.arcsec).value else: size_arcsec = size.to(u.arcsec).value size_pixels = int(size_arcsec / pixel_scale) - base_url = 'https://irsa.ipac.caltech.edu/cgi-bin/Cutouts/nph-cutouts?' - locstr = f'{str(ra)}+{str(dec)}+eq' - - mission_dict = {'mission': 'SEIP', - 'min_size': '18', - 'max_size': '1800', - 'units': 'arcsec', - 'locstr': locstr, - 'sizeX': size_pixels, - 'ntable_cutouts': '1', - 'cutouttbl1': 'science', - 'mode': 'PI', # XML output file - } + base_url = "https://irsa.ipac.caltech.edu/cgi-bin/Cutouts/nph-cutouts?" + locstr = f"{str(ra)}+{str(dec)}+eq" + + mission_dict = { + "mission": "SEIP", + "min_size": "18", + "max_size": "1800", + "units": "arcsec", + "locstr": locstr, + "sizeX": size_pixels, + "ntable_cutouts": "1", + "cutouttbl1": "science", + "mode": "PI", # XML output file + } # check image size - if size_pixels < int(mission_dict['min_size']): - mission_dict['sizeX'] = int(mission_dict['min_size']) - print(f'Image cannot be smaller than {mission_dict["min_size"]} arcsec (using this value)') - elif size_pixels > int(mission_dict['max_size']): - mission_dict['sizeX'] = int(mission_dict['max_size']) - print(f'Image cannot be larger than {mission_dict["max_size"]} arcsec (using this value)') - - mission_url = '&'.join([f'{key}={val}' for key, val in mission_dict.items()]) + if size_pixels < int(mission_dict["min_size"]): + mission_dict["sizeX"] = int(mission_dict["min_size"]) + print( + f'Image cannot be smaller than {mission_dict["min_size"]} arcsec (using this value)' + ) + elif size_pixels > int(mission_dict["max_size"]): + mission_dict["sizeX"] = int(mission_dict["max_size"]) + print( + f'Image cannot be larger than {mission_dict["max_size"]} arcsec (using this value)' + ) + + mission_url = "&".join( + [f"{key}={val}" for key, val in mission_dict.items()] + ) url = base_url + mission_url response = requests.get(url) @@ -905,7 +918,7 @@ def get_Spitzer_images(ra, dec, size=3, filters=None): files_dict = {filt: None for filt in filters} for filt in filters: for line in list_text: - if filt in line and line.endswith('.mosaic.fits'): + if filt in line and line.endswith(".mosaic.fits"): files_dict[filt] = line # pick the first image and move to the next filter break @@ -914,15 +927,16 @@ def get_Spitzer_images(ra, dec, size=3, filters=None): for filt, file in files_dict.items(): if file is not None: hdu = fits.open(line) - hdu[0].header['MAGZP'] = hdu[0].header['ZPAB'] + hdu[0].header["MAGZP"] = hdu[0].header["ZPAB"] hdu_list.append(hdu) else: hdu_list.append(None) return hdu_list + # VISTA -def get_VISTA_images(ra, dec, size=3, filters=None, version='VHS'): +def get_VISTA_images(ra, dec, size=3, filters=None, version="VHS"): """Gets VISTA fits images for the given coordinates and filters. @@ -960,36 +974,40 @@ def get_VISTA_images(ra, dec, size=3, filters=None, version='VHS'): # VHSDR6: https://b2find.eudat.eu/dataset/0b10d3a0-1cfe-5e67-8a5c-0949db9d19cb # VIDEODR5: https://www.eso.org/sci/publications/announcements/sciann17491.html # VIKINGDR4: https://www.eso.org/sci/publications/announcements/sciann17289.html - database_dict = {'VHS': 'VHSDR6', - 'VIDEO': 'VIDEODR5', - 'VIKING': 'VIKINGDR4', - } + database_dict = { + "VHS": "VHSDR6", + "VIDEO": "VIDEODR5", + "VIKING": "VIKINGDR4", + } valid_surveys = list(database_dict.keys()) - assert version in valid_surveys, f'Not a valid VISTA survey: choose from {valid_surveys}' + assert ( + version in valid_surveys + ), f"Not a valid VISTA survey: choose from {valid_surveys}" database = database_dict[version] - base_url = 'http://horus.roe.ac.uk:8080/vdfs/GetImage?archive=VSA&' - survey_dict = {'database': database, - 'ra': ra, - 'dec': dec, - 'sys': 'J', - 'filterID': 'all', - 'size': size, # in arcmin - 'obsType': 'object', - 'frameType': 'tilestack', - } - survey_url = '&'.join([f'{key}={val}' for key, val in survey_dict.items()]) + base_url = "http://horus.roe.ac.uk:8080/vdfs/GetImage?archive=VSA&" + survey_dict = { + "database": database, + "ra": ra, + "dec": dec, + "sys": "J", + "filterID": "all", + "size": size, # in arcmin + "obsType": "object", + "frameType": "tilestack", + } + survey_url = "&".join([f"{key}={val}" for key, val in survey_dict.items()]) url = base_url + survey_url results = urllib.request.urlopen(url).read() - links = re.findall('href="(http://.*?)"', results.decode('utf-8')) + links = re.findall('href="(http://.*?)"', results.decode("utf-8")) # find url for each filter (None if not found) urls_dict = {filt: None for filt in filters} for filt in filters: for link in links: url = link.replace("getImage", "getFImage", 1) - if f'band={filt}' in url: + if f"band={filt}" in url: urls_dict[filt] = url break @@ -998,13 +1016,14 @@ def get_VISTA_images(ra, dec, size=3, filters=None, version='VHS'): if url is not None: hdu = fits.open(url) # to be consistent with the naming of other surveys - hdu[1].header['MAGZP'] = hdu[1].header['MAGZPT'] + hdu[1].header["MAGZP"] = hdu[1].header["MAGZPT"] hdu_list.append(hdu[1]) # extension 1 else: hdu_list.append(None) return hdu_list + # Check orientation # ------------------ def match_wcs(fits_files): @@ -1039,7 +1058,14 @@ def match_wcs(fits_files): # Master Function # ---------------------------------------- def download_images( - name, ra, dec, size=3, filters=None, overwrite=False, survey="PS1", version=None, + name, + ra, + dec, + size=3, + filters=None, + overwrite=False, + survey="PS1", + version=None, ): """Download images for a given object in the given filters of a given survey. @@ -1097,8 +1123,7 @@ def download_images( hdu_list = get_WISE_images(ra, dec, size, filters) else: wise_version = survey.split("WISE")[-1] - hdu_list = get_unWISE_images(ra, dec, size, - filters, wise_version) + hdu_list = get_unWISE_images(ra, dec, size, filters, wise_version) elif survey == "2MASS": hdu_list = get_2MASS_images(ra, dec, size, filters) elif survey == "LegacySurvey": diff --git a/src/hostphot/dust.py b/src/hostphot/dust.py index d2e8e6b..1b2bf4b 100644 --- a/src/hostphot/dust.py +++ b/src/hostphot/dust.py @@ -157,14 +157,16 @@ def calc_extinction( """ # extract transmission function for the given filter+survey filters = get_survey_filters(survey) - if survey == 'LegacySurvey': + if survey == "LegacySurvey": # https://datalab.noirlab.edu/ls/bass.php # declination above ~32 and above the galactic plane - gal_coords = SkyCoord(ra=ra * u.degree, dec=dec * u.degree, frame='icrs') + gal_coords = SkyCoord( + ra=ra * u.degree, dec=dec * u.degree, frame="icrs" + ) if (dec > 32.375) and (gal_coords.b.value > 0): - version = 'BASS+MzLS' + version = "BASS+MzLS" else: - version = 'DECam' + version = "DECam" else: version = None diff --git a/src/hostphot/global_photometry.py b/src/hostphot/global_photometry.py index 7bf80d5..26ffcad 100644 --- a/src/hostphot/global_photometry.py +++ b/src/hostphot/global_photometry.py @@ -29,7 +29,7 @@ get_image_gain, pixel2pixel, check_work_dir, - magnitude_calc + magnitude_calc, ) from hostphot.objects_detect import extract_objects, plot_detected_objects from hostphot.image_cleaning import remove_nan @@ -236,7 +236,7 @@ def extract_kronparams( else: suffix = "" if isinstance(filt, list): - filt = ''.join(f for f in filt) + filt = "".join(f for f in filt) fits_file = os.path.join(obj_dir, f"{suffix}{survey}_{filt}.fits") img = fits.open(fits_file) @@ -421,14 +421,15 @@ def photometry( flux, flux_err = flux[0], flux_err[0] ap_area = np.pi * gal_obj["a"][0] * gal_obj["b"][0] - mag, mag_err = magnitude_calc(flux, - flux_err, - survey, - filt, - ap_area, - header, - bkg_rms, - ) + mag, mag_err = magnitude_calc( + flux, + flux_err, + survey, + filt, + ap_area, + header, + bkg_rms, + ) # extinction correction is optional if correct_extinction: diff --git a/src/hostphot/image_masking.py b/src/hostphot/image_masking.py index 27f9278..f6a7210 100644 --- a/src/hostphot/image_masking.py +++ b/src/hostphot/image_masking.py @@ -142,7 +142,7 @@ def create_mask( """ check_survey_validity(survey) if isinstance(filt, list): - filt = ''.join(f for f in filt) + filt = "".join(f for f in filt) obj_dir = os.path.join(workdir, name) fits_file = os.path.join(obj_dir, f"{survey}_{filt}.fits") diff --git a/src/hostphot/local_photometry.py b/src/hostphot/local_photometry.py index 81eb7c6..ad746c9 100644 --- a/src/hostphot/local_photometry.py +++ b/src/hostphot/local_photometry.py @@ -264,15 +264,16 @@ def photometry( data_sub, error, px, py, radius_pix ) - ap_area = 2 * np.pi * (radius_pix ** 2) - mag, mag_err = magnitude_calc(flux, - flux_err, - survey, - filt, - ap_area, - header, - bkg_rms, - ) + ap_area = 2 * np.pi * (radius_pix**2) + mag, mag_err = magnitude_calc( + flux, + flux_err, + survey, + filt, + ap_area, + header, + bkg_rms, + ) # extinction correction is optional if correct_extinction: diff --git a/src/hostphot/utils.py b/src/hostphot/utils.py index f6630c8..9284859 100644 --- a/src/hostphot/utils.py +++ b/src/hostphot/utils.py @@ -158,11 +158,11 @@ def get_image_gain(header, survey): gain = 30 # assumed similar to DES elif survey == "Spitzer": # also in the "EXPGAIN" keyword - if header['INSTRUME'] == 'IRAC': + if header["INSTRUME"] == "IRAC": # Table 2.4 of IRAC Instrument Handbook gain = 3.8 # all filters have similar gain - gain *= header['EFCONV'] # convert DN/s to MJy/sr - elif header['INSTRUME'] == 'MIPS': + gain *= header["EFCONV"] # convert DN/s to MJy/sr + elif header["INSTRUME"] == "MIPS": # Table 2.4 of MIPS Instrument Handbook gain = 5.0 elif survey == "VISTA": @@ -208,13 +208,13 @@ def get_image_readnoise(header, survey): elif survey == "LegacySurvey": readnoise = 7.0 # assumed similar to DES elif survey == "Spitzer": - if header['INSTRUME'] == 'IRAC': + if header["INSTRUME"] == "IRAC": # Table 2.3 of IRAC Instrument Handbook # very rough average - readnoise_dict = {1:16.0, 2:12.0, 3:10.0, 4:8.0} - channel = header['CHNLNUM'] + readnoise_dict = {1: 16.0, 2: 12.0, 3: 10.0, 4: 8.0} + channel = header["CHNLNUM"] readnoise = readnoise_dict[channel] - elif header['INSTRUME'] == 'MIPS': + elif header["INSTRUME"] == "MIPS": # Table 2.4 of MIPS Instrument Handbook readnoise = 40.0 elif survey == "VISTA": @@ -262,6 +262,7 @@ def get_image_exptime(header, survey): return exptime + def magnitude_calc( flux, flux_err, @@ -318,18 +319,13 @@ def magnitude_calc( header, bkg_rms, ) - mag_err = np.sqrt(mag_err ** 2 + extra_err ** 2) + mag_err = np.sqrt(mag_err**2 + extra_err**2) return mag, mag_err + def uncertainty_calc( - flux, - flux_err, - survey, - filt=None, - ap_area=0.0, - header=None, - bkg_rms=0.0 + flux, flux_err, survey, filt=None, ap_area=0.0, header=None, bkg_rms=0.0 ): """Calculates the uncertainty propagation. @@ -360,64 +356,90 @@ def uncertainty_calc( mag_err = 0.0 if survey in ["PS1", "DES", "LegacySurvey", "Spitzer", "VISTA"]: if survey == "Spitzer": - flux /= header['EFCONV'] # conv. factor (MJy/sr)/(DN/s) + flux /= header["EFCONV"] # conv. factor (MJy/sr)/(DN/s) # 1.0857 = 2.5/ln(10) extra_err = ( 1.0857 * np.sqrt(ap_area * (readnoise**2) + flux / gain) / flux ) mag_err = np.sqrt(mag_err**2 + extra_err**2) - if survey=="DES": + if survey == "DES": # see https://des.ncsa.illinois.edu/releases/dr1/dr1-docs/quality # Photometry section - unc_dict = {'g':2.6e-3, 'r':2.9e-3, 'i':3.4e-3, - 'z':2.5e-3, 'Y':4.5e-3} + unc_dict = { + "g": 2.6e-3, + "r": 2.9e-3, + "i": 3.4e-3, + "z": 2.5e-3, + "Y": 4.5e-3, + } extra_err = unc_dict[filt] - mag_err = np.sqrt(mag_err ** 2 + extra_err ** 2) + mag_err = np.sqrt(mag_err**2 + extra_err**2) - elif survey=='PS1': + elif survey == "PS1": # just as a check to make sure all surveys are included pass - elif survey=="SDSS": + elif survey == "SDSS": # https://data.sdss.org/datamodel/files/BOSS_PHOTOOBJ/frames/RERUN/RUN/CAMCOL/frame.html - camcol = header['CAMCOL'] - run = header['RUN'] - - gain_dict = {'u':{1:1.62, 2:1.595, 3:1.59, 4:1.6, 5:1.47, 6:2.17}, - 'g': {1: 3.32, 2: 3.855, 3: 3.845, 4: 3.995, 5: 4.05, 6: 4.035}, - 'r': {1: 4.71, 2: 4.6, 3: 4.72, 4: 4.76, 5: 4.725, 6: 4.895}, - 'i': {1: 5.165, 2: 6.565, 3: 4.86, 4: 4.885, 5: 4.64, 6: 4.76}, - 'z': {1: 4.745, 2: 5.155, 3: 4.885, 4: 4.775, 5: 3.48, 6: 4.69}, - } + camcol = header["CAMCOL"] + run = header["RUN"] + + gain_dict = { + "u": {1: 1.62, 2: 1.595, 3: 1.59, 4: 1.6, 5: 1.47, 6: 2.17}, + "g": {1: 3.32, 2: 3.855, 3: 3.845, 4: 3.995, 5: 4.05, 6: 4.035}, + "r": {1: 4.71, 2: 4.6, 3: 4.72, 4: 4.76, 5: 4.725, 6: 4.895}, + "i": {1: 5.165, 2: 6.565, 3: 4.86, 4: 4.885, 5: 4.64, 6: 4.76}, + "z": {1: 4.745, 2: 5.155, 3: 4.885, 4: 4.775, 5: 3.48, 6: 4.69}, + } # dark variance - dv_dict = {'u':{1:9.61, 2:12.6025, 3:8.7025, 4:12.6025, 5:9.3025, 6:7.0225}, - 'g': {1: 15.6025, 2: 1.44, 3: 1.3225, 4: 1.96, 5: 1.1025, 6: 1.8225}, - 'r': {1: 1.8225, 2: 1.00, 3: 1.3225, 4: 1.3225, 5: 0.81, 6: 0.9025}, - 'i': {1: 7.84, 2: 5.76, 3: 4.6225, 4: 6.25, 5: 7.84, 6: 5.0625}, - 'z': {1: 0.81, 2: 1.0, 3: 1.0, 4: 9.61, 5: 1.8225, 6: 1.21}, - } + dv_dict = { + "u": { + 1: 9.61, + 2: 12.6025, + 3: 8.7025, + 4: 12.6025, + 5: 9.3025, + 6: 7.0225, + }, + "g": { + 1: 15.6025, + 2: 1.44, + 3: 1.3225, + 4: 1.96, + 5: 1.1025, + 6: 1.8225, + }, + "r": { + 1: 1.8225, + 2: 1.00, + 3: 1.3225, + 4: 1.3225, + 5: 0.81, + 6: 0.9025, + }, + "i": {1: 7.84, 2: 5.76, 3: 4.6225, 4: 6.25, 5: 7.84, 6: 5.0625}, + "z": {1: 0.81, 2: 1.0, 3: 1.0, 4: 9.61, 5: 1.8225, 6: 1.21}, + } gain = gain_dict[filt][camcol] dark_variance = dv_dict[filt][camcol] - if filt=='u' and camcol==2 and run > 1100: + if filt == "u" and camcol == 2 and run > 1100: gain = 1.825 - if filt=='i' and run > 1500: - if camcol==2: + if filt == "i" and run > 1500: + if camcol == 2: dark_variance = 6.25 - if camcol==4: + if camcol == 4: dark_variance = 7.5625 - if filt=='z' and run > 1500: - if camcol==4: + if filt == "z" and run > 1500: + if camcol == 4: dark_variance = 12.6025 - if camcol==5: + if camcol == 5: dark_variance = 2.1025 - extra_err = ( - 1.0857 * np.sqrt(dark_variance + flux / gain) / flux - ) - mag_err = np.sqrt(mag_err ** 2 + extra_err ** 2) - mag_err = np.sqrt(mag_err ** 2 + extra_err ** 2) + extra_err = 1.0857 * np.sqrt(dark_variance + flux / gain) / flux + mag_err = np.sqrt(mag_err**2 + extra_err**2) + mag_err = np.sqrt(mag_err**2 + extra_err**2) elif survey == "GALEX": CPS = flux @@ -488,21 +510,23 @@ def uncertainty_calc( # add uncertainty from the ZP zp_unc = header["MAGZPUNC"] - mag_err = np.sqrt(mag_err ** 2 + zp_unc ** 2) + mag_err = np.sqrt(mag_err**2 + zp_unc**2) - elif survey=='LegacySurvey': + elif survey == "LegacySurvey": # already added at the beginning pass - elif survey == 'Spitzer': + elif survey == "Spitzer": # already added at the beginning pass - elif survey == 'VISTA': + elif survey == "VISTA": # add uncertainty from the ZP zp_unc = header["MAGZRR"] - mag_err = np.sqrt(mag_err ** 2 + zp_unc ** 2) + mag_err = np.sqrt(mag_err**2 + zp_unc**2) else: - raise Exception(f"Survey {survey} has not been added for error propagation.") + raise Exception( + f"Survey {survey} has not been added for error propagation." + ) return mag_err @@ -529,17 +553,18 @@ def survey_pixel_scale(survey, filt=None): survey_df = config_df[config_df.survey == survey] pixel_scale = survey_df.pixel_scale.values[0] - if len(pixel_scale.split(','))>1: + if len(pixel_scale.split(",")) > 1: filters = get_survey_filters(survey) - pixel_scale_dict = {f:float(ps) for f, ps - in zip(filters, pixel_scale.split(','))} + pixel_scale_dict = { + f: float(ps) for f, ps in zip(filters, pixel_scale.split(",")) + } if filt in pixel_scale_dict.keys(): pixel_scale = pixel_scale_dict[filt] else: - print(f'No pixel scale found for filter {filt}.') + print(f"No pixel scale found for filter {filt}.") filt_used = list(pixel_scale_dict.keys())[0] - print(f'Using the pixel scale of filter {filt_used} ({survey}).') + print(f"Using the pixel scale of filter {filt_used} ({survey}).") pixel_scale = list(pixel_scale_dict.values())[0] return pixel_scale @@ -599,21 +624,29 @@ def extract_filters(filters, survey, version=None): # Assume DECaLS filters below 32 degrees and BASS+MzLS above # https://www.legacysurvey.org/status/ - if survey=='LegacySurvey': - filters_path = os.path.join(hostphot.__path__[0], "filters", "LegacySurvey") - if version == 'BASS+MzLS': + if survey == "LegacySurvey": + filters_path = os.path.join( + hostphot.__path__[0], "filters", "LegacySurvey" + ) + if version == "BASS+MzLS": for filt in filters: - if filt != 'z': + if filt != "z": filt_file = os.path.join(filters_path, f"BASS_{filt}.dat") else: filt_file = os.path.join(filters_path, f"MzLS_z.dat") wave, transmission = np.loadtxt(filt_file).T - filters_dict[filt] = {"wave": wave, "transmission": transmission} - elif version == 'DECam': + filters_dict[filt] = { + "wave": wave, + "transmission": transmission, + } + elif version == "DECam": for filt in filters: filt_file = os.path.join(filters_path, f"DECAM_{filt}.dat") wave, transmission = np.loadtxt(filt_file).T - filters_dict[filt] = {"wave": wave, "transmission": transmission} + filters_dict[filt] = { + "wave": wave, + "transmission": transmission, + } return filters_dict diff --git a/tests/test_cutouts.py b/tests/test_cutouts.py index c7149f4..0146a21 100644 --- a/tests/test_cutouts.py +++ b/tests/test_cutouts.py @@ -3,7 +3,6 @@ class TestHostPhot(unittest.TestCase): - def __init__(self, *args, **kwargs): super(TestHostPhot, self).__init__(*args, **kwargs) # object used for most surveys @@ -13,60 +12,53 @@ def __init__(self, *args, **kwargs): def test_cutouts_PS1(self): download_images( - self.sn_name, self.ra, self.dec, - overwrite=True, survey="PS1" + self.sn_name, self.ra, self.dec, overwrite=True, survey="PS1" ) def test_cutouts_DES(self): download_images( - self.sn_name, self.ra, self.dec, - overwrite=True, survey="DES" + self.sn_name, self.ra, self.dec, overwrite=True, survey="DES" ) def test_cutouts_SDSS(self): download_images( - self.sn_name, self.ra, self.dec, - overwrite=True, survey="SDSS" + self.sn_name, self.ra, self.dec, overwrite=True, survey="SDSS" ) def test_cutouts_GALEX(self): download_images( - self.sn_name, self.ra, self.dec, - overwrite=True, survey="GALEX" + self.sn_name, self.ra, self.dec, overwrite=True, survey="GALEX" ) def test_cutouts_WISE(self): download_images( - self.sn_name, self.ra, self.dec, - overwrite=True, survey="WISE" + self.sn_name, self.ra, self.dec, overwrite=True, survey="WISE" ) def test_cutouts_2MASS(self): download_images( - self.sn_name, self.ra, self.dec, - overwrite=True, survey="2MASS" + self.sn_name, self.ra, self.dec, overwrite=True, survey="2MASS" ) def test_cutouts_unWISE(self): for survey in ["unWISEneo1", "unWISEneo2", "unWISEallwise"]: download_images( - self.sn_name, self.ra, self.dec, - overwrite=True, survey=survey + self.sn_name, self.ra, self.dec, overwrite=True, survey=survey ) def test_cutouts_LegacySurvey(self): download_images( - self.sn_name, self.ra, self.dec, - overwrite=True, survey="LegacySurvey" + self.sn_name, + self.ra, + self.dec, + overwrite=True, + survey="LegacySurvey", ) def test_cutouts_Spitzer(self): name = "Spitzer_test" ra, dec = 52.158591, -27.891113 - download_images( - name, ra, dec, - overwrite=True, survey="Spitzer" - ) + download_images(name, ra, dec, overwrite=True, survey="Spitzer") """ def test_cutouts_VISTA(self): @@ -84,5 +76,6 @@ def test_cutouts_VISTA(self): ) """ + if __name__ == "__main__": unittest.main()