Skip to content

Commit

Permalink
Fix style errors on the 01 nb
Browse files Browse the repository at this point in the history
  • Loading branch information
haticekaratay committed Oct 17, 2023
1 parent ed09067 commit 434401e
Showing 1 changed file with 71 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,8 @@
"from urllib.request import urlretrieve\n",
"import tarfile\n",
"\n",
"from astropy.convolution import Gaussian2DKernel\n",
"from astropy.io import fits\n",
"from astropy.stats import gaussian_fwhm_to_sigma\n",
"from astropy.table import QTable\n",
"import astropy.units as u\n",
"from astropy.visualization import make_lupton_rgb, SqrtStretch, ImageNormalize, simple_norm\n",
"import astropy.wcs as wcs\n",
"from astropy.io import ascii\n",
"\n",
"import astropy\n",
Expand Down Expand Up @@ -93,11 +88,10 @@
"outputs": [],
"source": [
"import specutils\n",
"from specutils.fitting import continuum #, find_lines_threshold, find_lines_derivative\n",
"from specutils.spectra.spectrum1d import Spectrum1D\n",
"from astropy.nddata import StdDevUncertainty\n",
"\n",
"print(\"Specutils: \",specutils.__version__)"
"print(\"Specutils: \", specutils.__version__)"
]
},
{
Expand Down Expand Up @@ -136,7 +130,7 @@
"DIR_OUT = './output/'\n",
"filt = 'f200w'\n",
"grism = 'G150C'\n",
"id = '00004'\n",
"id = '00004'\n",
"ndither = 2 # Number of dithering positions."
]
},
Expand All @@ -153,23 +147,23 @@
"metadata": {},
"outputs": [],
"source": [
"dithers = np.arange(0,ndither,1)\n",
"dithers = np.arange(0, ndither, 1)\n",
"for dither in dithers:\n",
" file_1d = '%sl3_nis_%s_%s_s%s_ndither%d_1d_opt.fits'%(DIR_OUT, filt, grism, id, dither)\n",
" file_1d = f'{DIR_OUT}l3_nis_{filt}_{grism}_s{id}_ndither{dither}_1d_opt.fits'\n",
" fd = fits.open(file_1d)[1].data\n",
" if dither == 0:\n",
" wave = fd['wavelength']\n",
" flux = np.zeros((ndither,len(wave)),'float')\n",
" flux_err = np.zeros((ndither,len(wave)),'float')\n",
" flux[dither,:] = fd['flux']\n",
" flux_err[dither,:] = fd['uncertainty']\n",
" flux = np.zeros((ndither, len(wave)), 'float')\n",
" flux_err = np.zeros((ndither, len(wave)), 'float')\n",
" flux[dither, :] = fd['flux']\n",
" flux_err[dither, :] = fd['uncertainty']\n",
" else:\n",
" wave_tmp = fd['wavelength']\n",
" flux_tmp = fd['flux']\n",
" flux_err_tmp = fd['uncertainty']\n",
" \n",
" flux[dither,:] = np.interp(wave, wave_tmp, flux_tmp)\n",
" flux_err[dither,:] = np.sqrt(np.interp(wave, wave_tmp, flux_err_tmp[:]**2))"
" flux[dither, :] = np.interp(wave, wave_tmp, flux_tmp)\n",
" flux_err[dither, :] = np.sqrt(np.interp(wave, wave_tmp, flux_err_tmp[:]**2))"
]
},
{
Expand All @@ -178,8 +172,8 @@
"metadata": {},
"outputs": [],
"source": [
"plt.plot(wave, flux[0,:], color='r', label='dither1')\n",
"plt.plot(wave, flux[1,:], color='b', label='dither2')\n",
"plt.plot(wave, flux[0, :], color='r', label='dither1')\n",
"plt.plot(wave, flux[1, :], color='b', label='dither2')\n",
"plt.legend(loc=2)"
]
},
Expand All @@ -196,12 +190,12 @@
"metadata": {},
"outputs": [],
"source": [
"flux_combine = np.zeros(len(wave),'float')\n",
"flux_err_combine = np.zeros(len(wave),'float')\n",
"flux_combine = np.zeros(len(wave), 'float')\n",
"flux_err_combine = np.zeros(len(wave), 'float')\n",
"\n",
"for ii in range(len(wave)):\n",
" flux_combine[ii] = np.sum(flux[:,ii] * 1 / flux_err[:,ii]**2) / np.sum(1 / flux_err[:,ii]**2) \n",
" flux_err_combine[ii] = np.sqrt(np.sum(flux_err[:,ii]**2)) / len(flux_err[:,ii])\n",
" flux_combine[ii] = np.sum(flux[:, ii] * 1 / flux_err[:, ii]**2) / np.sum(1 / flux_err[:, ii]**2) \n",
" flux_err_combine[ii] = np.sqrt(np.sum(flux_err[:, ii]**2)) / len(flux_err[:, ii])\n",
"\n",
"plt.plot(wave, flux[0,:], color='gray', label='dither1')\n",
"plt.plot(wave, flux[1,:], color='gray', label='dither2')\n",
Expand All @@ -226,7 +220,7 @@
"# Open broadband flux catalog from Notebook 01a;\n",
"\n",
"# Flux are already in Fnu, with magzp = 25.0;\n",
"file = DIR_OUT+'l3_nis_flux.cat'\n",
"file = DIR_OUT + 'l3_nis_flux.cat'\n",
"\n",
"fd_cat = ascii.read(file)\n",
"id_cat = fd_cat['id']\n",
Expand Down Expand Up @@ -263,12 +257,13 @@
"eazy_filt = 311\n",
"\n",
"# Read transmission data;\n",
"filt_data = ascii.read('%s/NIRISS_%s.txt'%(DIR_FIL, filt.upper()))\n",
"filt_data = ascii.read(f'{DIR_FIL}/NIRISS_{filt.upper()}.txt')\n",
"\n",
"print(filt_data)\n",
"wave_filt = filt_data['Wavelength']\n",
"flux_filt = filt_data['FilterTrans']\n",
"\n",
"plt.plot(wave_filt, flux_filt, ls='-', label='NIRISS %s'%filt.upper())\n",
"plt.plot(wave_filt, flux_filt, ls='-', label=f'NIRISS {filt.upper()}')\n",
"plt.xlabel('wavelength')\n",
"plt.ylabel('response')\n",
"plt.legend(loc=1)"
Expand All @@ -291,23 +286,21 @@
"\n",
" '''\n",
"\n",
" lmin = np.min(lfil)\n",
" lmax = np.max(lfil)\n",
" imin = 0\n",
" imax = 0\n",
" lmin = np.min(lfil)\n",
" lmax = np.max(lfil)\n",
" imax = 0\n",
"\n",
" fhalf = np.max(ffil)/2.0\n",
" con = (ffil>fhalf)\n",
" con = (ffil > fhalf)\n",
" lfwhml = np.min(lfil[con])\n",
" lfwhmr = np.max(lfil[con])\n",
" lcen = (lfwhmr+lfwhml)/2.\n",
" lcen = (lfwhmr + lfwhml)/2.\n",
"\n",
" lamS,spec = l0, f0 # Two columns with wavelength and flux density\n",
" lamF,filt = lfil, ffil # Two columns with wavelength and response in the range [0,1]\n",
" lamS, spec = l0, f0 # Two columns with wavelength and flux density\n",
" lamF, filt = lfil, ffil # Two columns with wavelength and response in the range [0,1]\n",
" filt_int = np.interp(lamS, lamF, filt) # Interpolate Filter to common(spectra) wavelength axis\n",
" wht = 1.\n",
"\n",
" if len(lamS)>0:\n",
" if len(lamS) > 0:\n",
" I1 = simps(spec / lamS**2 * c * filt_int * lamS, lamS) # Denominator for Fnu\n",
" I2 = simps(filt_int/lamS, lamS) # Numerator\n",
" fnu = I1/I2/c # Average flux density\n",
Expand Down Expand Up @@ -342,7 +335,7 @@
"source": [
"# Get normalization factor;\n",
"iix = np.where(id_cat[:] == int(id))\n",
"Cnorm = fd_cat['F%d'%(eazy_filt)][iix] / fnu\n",
"Cnorm = fd_cat[f'F{eazy_filt}'][iix] / fnu \n",
"Cnorm"
]
},
Expand All @@ -354,7 +347,7 @@
"source": [
"# Wirte the normalized spectrum:\n",
"# Make it into a Spectrum1D instance.\n",
"file_1d = '%sl3_nis_%s_%s_s%s_combine_1d_opt.fits'%(DIR_OUT, filt, grism, id)\n",
"file_1d = f'{DIR_OUT}l3_nis_{filt}_{grism}_s{id}_ndither{dither}_1d_opt.fits'\n",
"\n",
"obs = Spectrum1D(spectral_axis=wave*u.um,\n",
" flux=flux_combine * Cnorm * u.dimensionless_unscaled,\n",
Expand All @@ -376,59 +369,59 @@
"outputs": [],
"source": [
"DIR_FIL = './NIRISS_Filters/DATA/'\n",
"filts = ['f115w', 'f150w', 'f200w']\n",
"filts = ['f115w', 'f150w', 'f200w']\n",
"# The number corresponds to NIRISS filters in EAZY filter response;\n",
"eazy_filts = [309, 310, 311]\n",
"\n",
"# Masks for problematic flux at edge;\n",
"mask_lw = [1.05, 1.35, 1.75]\n",
"mask_uw = [1.25, 1.65, 2.23]\n",
"\n",
"for ff,filt in enumerate(filts):\n",
"for ff, filt in enumerate(filts):\n",
" for dither in dithers:\n",
" file_1d = '%sl3_nis_%s_%s_s%s_ndither%d_1d_opt.fits'%(DIR_OUT, filt, grism, id, dither)\n",
" file_1d = f'{DIR_OUT}l3_nis_{filt}_{grism}_s{id}_ndither{dither}_1d_opt.fits'\n",
" fd = fits.open(file_1d)[1].data\n",
" if dither == 0:\n",
" wave = fd['wavelength']\n",
" flux = np.zeros((ndither, len(wave)), 'float')\n",
" flux_err = np.zeros((ndither, len(wave)), 'float')\n",
"\n",
" flux[dither,:] = fd['flux']\n",
" flux_err[dither,:] = fd['uncertainty']\n",
" flux[dither, :] = fd['flux']\n",
" flux_err[dither, :] = fd['uncertainty']\n",
" else:\n",
" wave_tmp = fd['wavelength']\n",
" flux_tmp = fd['flux']\n",
" flux_err_tmp = fd['uncertainty']\n",
"\n",
" flux[dither,:] = np.interp(wave, wave_tmp, flux_tmp)\n",
" flux_err[dither,:] = np.sqrt(np.interp(wave, wave_tmp, flux_err_tmp[:]**2))\n",
" flux[dither, :] = np.interp(wave, wave_tmp, flux_tmp)\n",
" flux_err[dither, :] = np.sqrt(np.interp(wave, wave_tmp, flux_err_tmp[:]**2))\n",
"\n",
" # Combine;\n",
" flux_combine = np.zeros(len(wave), 'float')\n",
" flux_err_combine = np.zeros(len(wave), 'float')\n",
"\n",
" for ii in range(len(wave)):\n",
" flux_combine[ii] = np.sum(flux[:,ii] * 1 / flux_err[:,ii]**2) / np.sum(1 / flux_err[:,ii]**2) \n",
" flux_err_combine[ii] = np.sqrt(np.sum(flux_err[:,ii]**2)) / len(flux_err[:,ii])\n",
" flux_combine[ii] = np.sum(flux[:, ii] * 1 / flux_err[:, ii]**2) / np.sum(1 / flux_err[:, ii]**2) \n",
" flux_err_combine[ii] = np.sqrt(np.sum(flux_err[:, ii]**2)) / len(flux_err[:, ii])\n",
"\n",
" # Normalize;\n",
" filt_data = ascii.read('%s/NIRISS_%s.txt'%(DIR_FIL, filt.upper()))\n",
" filt_data = ascii.read(f'{DIR_FIL}/NIRISS_{filt.upper()}.txt'\n",
" wave_filt = filt_data['Wavelength']\n",
" flux_filt = filt_data['FilterTrans']\n",
"\n",
" con = (wave > mask_lw[ff]) & (wave < mask_uw[ff])\n",
" lcen, fnu = filconv(wave_filt, flux_filt, wave[con], flux_combine[con])\n",
" iix = np.where(id_cat[:] == int(id))\n",
" Cnorm = fd_cat['F%d'%(eazy_filts[ff])][iix] / fnu\n",
" Cnorm = fd_cat[f'F{eazy_filts[ff]}'][iix] / fnu\n",
"\n",
" # Wirte:\n",
" file_1d = '%sl3_nis_%s_%s_s%s_combine_1d_opt.fits'%(DIR_OUT, filt, grism, id)\n",
" # Write:\n",
" file_1d = f'{DIR_OUT}l3_nis_{filt}_{grism}_s{id}_combine_1d_opt.fits' \n",
" \n",
"\n",
" obs = Spectrum1D(spectral_axis=wave*u.um,\n",
" flux=flux_combine * Cnorm * u.dimensionless_unscaled,\n",
" uncertainty=StdDevUncertainty(flux_err_combine * Cnorm), unit='')\n",
" obs.write(file_1d, format='tabular-fits', overwrite=True)\n",
" "
" obs.write(file_1d, format='tabular-fits', overwrite=True)"
]
},
{
Expand All @@ -445,27 +438,27 @@
"metadata": {},
"outputs": [],
"source": [
"filts = ['f115w', 'f150w', 'f200w']\n",
"cols = ['lightblue', 'orange', 'purple']\n",
"filts = ['f115w', 'f150w', 'f200w']\n",
"cols = ['lightblue', 'orange', 'purple']\n",
"\n",
"for ff,filt in enumerate(filts):\n",
" file_1d = '%sl3_nis_%s_%s_s%s_combine_1d_opt.fits'%(DIR_OUT, filt, grism, id)\n",
" file_1d = f'{DIR_OUT}l3_nis_{filt}_{grism}_s{id}_combine_1d_opt.fits' \n",
" fd = fits.open(file_1d)[1].data\n",
" wave = fd['wavelength']\n",
" flux = fd['flux']\n",
" flux_err = fd['uncertainty']\n",
"\n",
" con = (wave > mask_lw[ff]) & (wave < mask_uw[ff])\n",
" plt.plot(wave[con], flux[con], ls='-', label='%s %s'%(grism, filts[ff]), color=cols[ff])\n",
" plt.plot(wave[con], flux[con], ls='-', label=f'{grism} {filts[ff]}', color=cols[ff])\n",
"\n",
" filt_data = ascii.read('%s/NIRISS_%s.txt'%(DIR_FIL, filt.upper()))\n",
" filt_data = ascii.read(f'{DIR_FIL}/NIRISS_{filt.upper()}.txt')\n",
" wave_filt = filt_data['Wavelength']\n",
" flux_filt = filt_data['FilterTrans']\n",
"\n",
" con = (wave > mask_lw[ff]) & (wave < mask_uw[ff])\n",
" lcen, fnu = filconv(wave_filt, flux_filt, wave[con], flux[con])\n",
" iix = np.where(id_cat[:] == int(id))\n",
" plt.scatter(lcen, fd_cat['F%d'%(eazy_filts[ff])][iix], marker='s', s=50, edgecolor='k', color=cols[ff], label='BB %s'%filts[ff])\n",
" plt.scatter(lcen, fd_cat[f'F{eazy_filts[ff]}'][iix], marker='s', s=50, edgecolor='k', color=cols[ff], label=f'BB {filts[ff]}')\n",
"\n",
"\n",
"plt.xlabel('wavelength')\n",
Expand Down Expand Up @@ -500,49 +493,47 @@
"for ff, filt in enumerate(filts):\n",
" \n",
" for dither in dithers:\n",
" file_1d = '%sl3_nis_%s_%s_s%s_ndither%d_1d_opt.fits'%(DIR_OUT, filt, grism, id, dither)\n",
" file_1d = f'{DIR_OUT}l3_nis_{filt}_{grism}_s{id}_ndither{dither}_1d_opt.fits'\n",
" fd = fits.open(file_1d)[1].data\n",
" if dither == 0:\n",
" wave = fd['wavelength']\n",
" flux = np.zeros((ndither, len(wave)),'float')\n",
" flux_err = np.zeros((ndither, len(wave)),'float')\n",
"\n",
" flux[dither,:] = fd['flux']\n",
" flux_err[dither,:] = fd['uncertainty']\n",
" flux[dither, :] = fd['flux']\n",
" flux_err[dither, :] = fd['uncertainty']\n",
" else:\n",
" wave_tmp = fd['wavelength']\n",
" flux_tmp = fd['flux']\n",
" flux_err_tmp = fd['uncertainty']\n",
"\n",
" flux[dither,:] = np.interp(wave, wave_tmp, flux_tmp)\n",
" flux_err[dither,:] = np.sqrt(np.interp(wave, wave_tmp, flux_err_tmp[:]**2))\n",
" flux[dither, :] = np.interp(wave, wave_tmp, flux_tmp)\n",
" flux_err[dither, :] = np.sqrt(np.interp(wave, wave_tmp, flux_err_tmp[:]**2))\n",
"\n",
" # Combine;\n",
" flux_combine = np.zeros(len(wave), 'float')\n",
" flux_err_combine = np.zeros(len(wave), 'float')\n",
"\n",
" for ii in range(len(wave)):\n",
" flux_combine[ii] = np.sum(flux[:,ii] * 1 / flux_err[:,ii]**2) / np.sum(1 / flux_err[:,ii]**2) \n",
" flux_err_combine[ii] = np.sqrt(np.sum(flux_err[:,ii]**2)) / len(flux_err[:,ii])\n",
" flux_combine[ii] = np.sum(flux[:, ii] * 1 / flux_err[:, ii]**2) / np.sum(1 / flux_err[:, ii]**2) \n",
" flux_err_combine[ii] = np.sqrt(np.sum(flux_err[:, ii]**2)) / len(flux_err[:, ii])\n",
"\n",
" # Normalize;\n",
" filt_data = ascii.read('%s/NIRISS_%s.txt'%(DIR_FIL, filt.upper()))\n",
" filt_data = ascii.read(f'{DIR_FIL}/NIRISS_{filt.upper()}.txt')\n",
" wave_filt = filt_data['Wavelength']\n",
" flux_filt = filt_data['FilterTrans']\n",
"\n",
" con = (wave > mask_lw[ff]) & (wave < mask_uw[ff])\n",
" lcen, fnu = filconv(wave_filt, flux_filt, wave[con], flux_combine[con])\n",
" iix = np.where(id_cat[:] == int(id))\n",
" Cnorm = fd_cat['F%d'%(eazy_filts[ff])][iix] / fnu\n",
" Cnorm = fd_cat[f'F{eazy_filts[ff]}'][iix] / fnu\n",
" \n",
" # Wirte:\n",
" file_1d = '%sl3_nis_%s_%s_s%s_combine_1d_opt.fits'%(DIR_OUT, filt, grism, id)\n",
"\n",
" # Write:\n",
" file_1d = f'{DIR_OUT}l3_nis_{filt}_{grism}_s{id}_combine_1d_opt.fits'\n",
" obs = Spectrum1D(spectral_axis=wave*u.um,\n",
" flux=flux_combine * Cnorm * u.dimensionless_unscaled,\n",
" uncertainty=StdDevUncertainty(flux_err_combine * Cnorm), unit='')\n",
" obs.write(file_1d, format='tabular-fits', overwrite=True)\n",
" "
" obs.write(file_1d, format='tabular-fits', overwrite=True)"
]
},
{
Expand All @@ -551,28 +542,27 @@
"metadata": {},
"outputs": [],
"source": [
"filts = ['f115w', 'f150w', 'f200w']\n",
"cols = ['lightblue', 'orange', 'purple']\n",
"filts = ['f115w', 'f150w', 'f200w']\n",
"cols = ['lightblue', 'orange', 'purple']\n",
"\n",
"for ff,filt in enumerate(filts):\n",
" file_1d = '%sl3_nis_%s_%s_s%s_combine_1d_opt.fits'%(DIR_OUT, filt, grism, id)\n",
"for ff, filt in enumerate(filts):\n",
" file_1d = f'{DIR_OUT}l3_nis_{filt}_{grism}_s{id}_combine_1d_opt.fits' \n",
" fd = fits.open(file_1d)[1].data\n",
" wave = fd['wavelength']\n",
" flux = fd['flux']\n",
" flux_err = fd['uncertainty']\n",
"\n",
" con = (wave > mask_lw[ff]) & (wave < mask_uw[ff])\n",
" plt.plot(wave[con], flux[con], ls='-', label='%s %s'%(grism, filts[ff]), color=cols[ff])\n",
" plt.plot(wave[con], flux[con], ls='-', label=f'{grism} {filts[ff]}', color=cols[ff])\n",
"\n",
" filt_data = ascii.read('%s/NIRISS_%s.txt'%(DIR_FIL, filt.upper()))\n",
" filt_data = ascii.read(f'{DIR_FIL}/NIRISS_{filt.upper()}.txt'\n",
" wave_filt = filt_data['Wavelength']\n",
" flux_filt = filt_data['FilterTrans']\n",
"\n",
" con = (wave > mask_lw[ff]) & (wave < mask_uw[ff])\n",
" lcen, fnu = filconv(wave_filt, flux_filt, wave[con], flux[con])\n",
" iix = np.where(id_cat[:] == int(id))\n",
" plt.scatter(lcen, fd_cat['F%d'%(eazy_filts[ff])][iix], marker='s', s=50, edgecolor='k', color=cols[ff], label='BB %s'%filts[ff])\n",
"\n",
" plt.scatter(lcen, fd_cat[f'F{eazy_filts[ff]}'][iix], marker='s', s=50, edgecolor='k', color=cols[ff], label=f'BB {filts[ff]}')\n",
"\n",
"plt.xlabel('wavelength')\n",
"plt.ylabel('flux')\n",
Expand All @@ -597,7 +587,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.6"
"version": "3.9.13"
}
},
"nbformat": 4,
Expand Down

0 comments on commit 434401e

Please sign in to comment.