tidy up main script making use of new query library

This commit is contained in:
Thibault Barnouin
2023-05-10 13:03:54 +02:00
parent 4b95bdc673
commit b566052b07
12 changed files with 11363 additions and 553 deletions

View File

@@ -10,6 +10,7 @@ prototypes :
Compute the error (noise) of the input array by looking at the sub-region of minimal flux in every image and of shape sub_shape.
"""
import sys
from os.path import join as path_join
from copy import deepcopy
import numpy as np
import matplotlib.pyplot as plt
@@ -59,7 +60,7 @@ def display_bkg(data, background, std_bkg, headers, histograms=None, binning=Non
ax.set_ylabel(r"Flux [$ergs \cdot cm^{-2} \cdot s^{-1} \cdot \AA^{-1}$]")
plt.legend()
if not(savename is None):
fig.savefig(plots_folder+savename+"_background_flux.png", bbox_inches='tight')
fig.savefig(path_join(plots_folder,savename+"_background_flux.png"), bbox_inches='tight')
if not(histograms is None):
filt_obs = {"POL0":0, "POL60":0, "POL120":0}
@@ -78,7 +79,7 @@ def display_bkg(data, background, std_bkg, headers, histograms=None, binning=Non
ax_h.set_title("Histogram for each observation")
plt.legend()
if not(savename is None):
fig_h.savefig(plots_folder+savename+'_histograms.png', bbox_inches='tight')
fig_h.savefig(path_join(plots_folder,savename+'_histograms.png'), bbox_inches='tight')
fig2, ax2 = plt.subplots(figsize=(10,10))
data0 = data[0]*convert_flux[0]
@@ -103,7 +104,7 @@ def display_bkg(data, background, std_bkg, headers, histograms=None, binning=Non
fig2.colorbar(im, cax=cbar_ax, label=r"Flux [$ergs \cdot cm^{-2} \cdot s^{-1} \cdot \AA^{-1}$]")
if not(savename is None):
fig2.savefig(plots_folder+savename+'_'+filt+'_background_location.png', bbox_inches='tight')
fig2.savefig(path_join(plots_folder,savename+'_'+filt+'_background_location.png'), bbox_inches='tight')
if not(rectangle is None):
plot_obs(data, headers, vmin=data[data > 0.].min()*convert_flux.mean(), vmax=data[data > 0.].max()*convert_flux.mean(), rectangle=rectangle,
savename=savename+"_background_location",plots_folder=plots_folder)

View File

@@ -10,6 +10,7 @@ prototypes :
"""
import numpy as np
from os.path import join as path_join
from astropy.io import fits
from astropy import wcs
from lib.convex_hull import image_hull, clean_ROI
@@ -39,7 +40,7 @@ def get_obs_data(infiles, data_folder="", compute_flux=False):
"""
data_array, headers = [], []
for i in range(len(infiles)):
with fits.open(data_folder+infiles[i]) as f:
with fits.open(path_join(data_folder,infiles[i])) as f:
headers.append(f[0].header)
data_array.append(f[0].data)
data_array = np.array(data_array,dtype=np.double)
@@ -188,7 +189,7 @@ def save_Stokes(I_stokes, Q_stokes, U_stokes, Stokes_cov, P, debiased_P, s_P,
hdul.append(hdu)
#Save fits file to designated filepath
hdul.writeto(data_folder+filename+".fits", overwrite=True)
hdul.writeto(path_join(data_folder,filename+".fits"), overwrite=True)
if return_hdul:
return hdul

View File

@@ -38,6 +38,7 @@ prototypes :
from copy import deepcopy
import numpy as np
from os.path import join as path_join
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle, Circle
from matplotlib.path import Path
@@ -161,7 +162,7 @@ def plot_obs(data_array, headers, shape=None, vmin=None, vmax=None, rectangle=No
if not (savename is None):
#fig.suptitle(savename)
fig.savefig(plots_folder+savename+".png",bbox_inches='tight')
fig.savefig(path_join(plots_folder,savename+".png"),bbox_inches='tight')
plt.show()
return 0
@@ -211,7 +212,7 @@ def plot_Stokes(Stokes, savename=None, plots_folder=""):
if not (savename is None):
#fig.suptitle(savename+"_IQU")
fig.savefig(plots_folder+savename+"_IQU.png",bbox_inches='tight')
fig.savefig(path_join(plots_folder,savename+"_IQU.png"),bbox_inches='tight')
plt.show()
return 0
@@ -320,7 +321,7 @@ def polarization_map(Stokes, data_mask=None, rectangle=None, SNRp_cut=3., SNRi_c
if display.lower() in ['intensity']:
# If no display selected, show intensity map
display='i'
vmin, vmax = 1.*np.mean(np.sqrt(stk_cov.data[0,0][mask])*convert_flux), np.max(stkI.data[stkI.data > 0.]*convert_flux)
vmin, vmax = 1/5.0*np.mean(np.sqrt(stk_cov.data[0,0][mask])*convert_flux), np.max(stkI.data[stkI.data > 0.]*convert_flux)
im = ax.imshow(stkI.data*convert_flux, norm=LogNorm(vmin,vmax), aspect='equal', cmap='inferno', alpha=1.)
cbar = plt.colorbar(im, cax=cbar_ax, label=r"$F_{\lambda}$ [$ergs \cdot cm^{-2} \cdot s^{-1} \cdot \AA^{-1}$]")
levelsI = np.linspace(vmax*0.01, vmax*0.99, 10)
@@ -446,7 +447,7 @@ def polarization_map(Stokes, data_mask=None, rectangle=None, SNRp_cut=3., SNRi_c
if not savename is None:
#fig.suptitle(savename)
fig.savefig(plots_folder+savename+".png",bbox_inches='tight',dpi=300)
fig.savefig(path_join(plots_folder,savename+".png"),bbox_inches='tight',dpi=300)
plt.show()
return fig, ax
@@ -1766,7 +1767,7 @@ class pol_map(object):
self.display_selection = "total_flux"
if self.display_selection.lower() in ['total_flux']:
self.data = self.I*self.convert_flux
vmin, vmax = 1/2.0*np.median(self.data[self.data > 0.]), np.max(self.data[self.data > 0.])
vmin, vmax = 1/5.0*np.median(self.data[self.data > 0.]), np.max(self.data[self.data > 0.])
norm = LogNorm(vmin, vmax)
label = r"$F_{\lambda}$ [$ergs \cdot cm^{-2} \cdot s^{-1} \cdot \AA^{-1}$]"
elif self.display_selection.lower() in ['pol_flux']:

View File

@@ -1,5 +1,3 @@
from sys import exit as sys_exit, argv as sys_argv
from getopt import getopt, error as geterr
from os import system
from os.path import join as path_join, exists as path_exists
from astroquery.mast import MastMissions, Observations
@@ -102,10 +100,11 @@ def get_product_list(target, proposal_id=None):
return products
def retrieve_products(target, products):
def retrieve_products(target, proposal_id=None):
"""
Given a target name and a products list, create the local directories and retrieve the fits files from the MAST Archive
Given a target name and a proposal_id, create the local directories and retrieve the fits files from the MAST Archive
"""
products = get_product_list(target=target,proposal_id=proposal_id)
prodpaths = []
data_dir = path_join("../data", target)
out = ""
@@ -113,7 +112,7 @@ def retrieve_products(target, products):
filepaths = []
obs_dir = path_join(data_dir, obs_id)
if not path_exists(obs_dir):
system("mkdir -p "+obs_dir)
system("mkdir -p {0:s} {1:s}".format(obs_dir,path_join("../plots",target,obs_id)))
for file in products['productFilename'][products['proposal_id'] == obs_id]:
fpath = path_join(obs_dir, file)
if not path_exists(fpath):
@@ -121,37 +120,20 @@ def retrieve_products(target, products):
products['dataURI'][products['productFilename'] == file][0], local_path=fpath)[0])
else:
out += "{0:s} : Exists\n".format(file)
filepaths.append(fpath)
prodpaths.append(filepaths)
filepaths.append([obs_dir,file])
prodpaths.append(np.array(filepaths,dtype=str))
return prodpaths
def main():
target, proposal_id = None, None
try:
arg, _ = getopt(sys_argv[1:], "ht:p:", ["help", "target=", "proposal_id="])
for curr_arg, curr_val in arg:
if curr_arg in ("-h", "--help"):
print("python3 query.py -t <target_name> -p <proposal_id>")
elif curr_arg in ("-t", "--target"):
target = str(curr_val)
elif curr_arg in ("-p", "--proposal_id"):
proposal_id = int(curr_val)
except geterr:
print(str(geterr))
if target is None:
target = input("Target name\n>")
products = get_product_list(target=target,proposal_id=proposal_id)
products_paths = retrieve_products(target=target, products=products)
print(products_paths)
return 0
if __name__ == "__main__":
sys_exit(main())
import argparse
parser = argparse.ArgumentParser(description='Query MAST for target products')
parser.add_argument('-t','--target', metavar='targetname', required=False,
help='the name of the target', type=str, default=None)
parser.add_argument('-p','--proposal_id', metavar='proposal_id', required=False,
help='the proposal id of the data products', type=int, default=None)
args = parser.parse_args()
prodpaths = retrieve_products(target=args.target, proposal_id=args.proposal_id)
print(prodpaths)