diff --git a/pcigale_plots/__init__.py b/pcigale_plots/__init__.py index 7f54d825714482d335cfe11c308bbbc700e44db7..0070632efc791f26dcebf504de5e0cebce4b68f1 100644 --- a/pcigale_plots/__init__.py +++ b/pcigale_plots/__init__.py @@ -20,6 +20,23 @@ from .plot_types.mock import mock as mock_action __version__ = "0.2-alpha" +def parser_range(range_str): + """ + Auxiliary parser for plot X and Y ranges + :param range_str: a string like []:[] + :return: + """ + rmin, rmax = range_str.split(':') + try: + rmin = float(rmin) if rmin else False + rmax = float(rmax) if rmax else False + except ValueError: + msg = '{} has not the format []:[], where ' \ + 'min and max are either float or empty'.format(range_str) + raise argparse.ArgumentTypeError(msg) + return rmin, rmax + + def main(): if sys.version_info[:2] >= (3, 4): @@ -47,6 +64,12 @@ def main(): sed_parser.add_argument('--type', default='mJy') sed_parser.add_argument('--nologo', action='store_true') sed_parser.add_argument('--outdir', dest='outdir', default='out') + sed_parser.add_argument('--xrange', dest='xrange', default=':', + type=parser_range, + help='Format []:[], for plot observed wavelength axis') + sed_parser.add_argument('--yrange', dest='yrange', default=':', + type=parser_range, + help='Format []:[], for plot flux axis') sed_parser.set_defaults(parser='sed') mock_parser = subparsers.add_parser('mock', help=mock_action.__doc__) @@ -70,6 +93,6 @@ def main(): elif args.parser == 'pdf': pdf_action(config, outdir) elif args.parser == 'sed': - sed_action(config, args.type, args.nologo, outdir) + sed_action(config, args.type, args.nologo, args.xrange, args.yrange, outdir) elif args.parser == 'mock': mock_action(config, args.nologo, outdir) diff --git a/pcigale_plots/plot_types/sed.py b/pcigale_plots/plot_types/sed.py index cda5a169261a7c8aad113559c7acf685d61f1fcb..ed553377256eca4039fe5671c9ef9d6af5c8a6a1 100644 --- a/pcigale_plots/plot_types/sed.py +++ b/pcigale_plots/plot_types/sed.py @@ -46,7 +46,7 @@ def pool_initializer(counter): gbl_counter = counter -def sed(config, sed_type, nologo, outdir): +def sed(config, sed_type, nologo, xrange, yrange, outdir): """Plot the best SED with associated observed and modelled fluxes. """ obs = read_table(path.join(path.dirname(outdir), config.configuration['data_file'])) @@ -63,13 +63,14 @@ def sed(config, sed_type, nologo, outdir): counter = Counter(len(obs)) with mp.Pool(processes=config.configuration['cores'], initializer=pool_initializer, initargs=(counter,)) as pool: - pool.starmap(_sed_worker, zip(obs, mod, repeat(filters), - repeat(sed_type), repeat(logo), repeat(outdir))) + pool.starmap(_sed_worker, zip( + obs, mod, repeat(filters), repeat(sed_type), repeat(logo), + repeat(xrange), repeat(yrange), repeat(outdir))) pool.close() pool.join() -def _sed_worker(obs, mod, filters, sed_type, logo, outdir): +def _sed_worker(obs, mod, filters, sed_type, logo, xrange, yrange, outdir): """Plot the best SED with the associated fluxes in bands Parameters @@ -89,6 +90,8 @@ def _sed_worker(obs, mod, filters, sed_type, logo, outdir): (M, N, 3) for RGB images. (M, N, 4) for RGBA images. Do not add the logo when set to False. + xrange: tuple(float|boolean, float|boolean) + yrange: tuple(float|boolean, float|boolean) outdir: string The absolute path to outdir @@ -113,10 +116,10 @@ def _sed_worker(obs, mod, filters, sed_type, logo, outdir): z = mod['best.universe.redshift'] DL = mod['best.universe.luminosity_distance'] - if sed_type == 'lum': - xmin = PLOT_L_MIN - xmax = PLOT_L_MAX + xmin = PLOT_L_MIN if xrange[0] is False else xrange[0] + xmax = PLOT_L_MAX if xrange[1] is False else xrange[1] + if sed_type == 'lum': k_corr_SED = 1e-29 * (4.*np.pi*DL*DL) * c / (filters_wl*1e-9) obs_fluxes *= k_corr_SED obs_fluxes_err *= k_corr_SED @@ -128,8 +131,8 @@ def _sed_worker(obs, mod, filters, sed_type, logo, outdir): filters_wl /= 1. + z wavelength_spec /= 1. + z elif sed_type == 'mJy': - xmin = PLOT_L_MIN * (1. + z) - xmax = PLOT_L_MAX * (1. + z) + xmin = xmin * (1. + z) + xmax = xmax * (1. + z) k_corr_SED = 1. for cname in sed.colnames[1:]: @@ -262,17 +265,31 @@ def _sed_worker(obs, mod, filters, sed_type, logo, outdir): figure.subplots_adjust(hspace=0., wspace=0.) ax1.set_xlim(xmin, xmax) - ymin = min(np.min(obs_fluxes[mask_ok]), - np.min(mod_fluxes[mask_ok])) - if not mask_uplim.any() == False: - ymax = max(max(np.max(obs_fluxes[mask_ok]), + + if yrange[0] is not False: + ymin = yrange[0] + else: + ymin = min(np.min(obs_fluxes[mask_ok]), + np.min(mod_fluxes[mask_ok])) + ymin *= 1e-1 + + if yrange[1] is not False: + ymax = yrange[1] + else: + if not mask_uplim.any() == False: + ymax = max(max(np.max(obs_fluxes[mask_ok]), np.max(obs_fluxes[mask_uplim])), max(np.max(mod_fluxes[mask_ok]), np.max(mod_fluxes[mask_uplim]))) - else: - ymax = max(np.max(obs_fluxes[mask_ok]), + else: + ymax = max(np.max(obs_fluxes[mask_ok]), np.max(mod_fluxes[mask_ok])) - ax1.set_ylim(1e-1*ymin, 1e1*ymax) + ymax *= 1e1 + + xmin = xmin if xmin < xmax else xmax - 1e1 + ymin = ymin if ymin < ymax else ymax - 1e1 + + ax1.set_ylim(ymin, ymax) ax2.set_xlim(xmin, xmax) ax2.set_ylim(-1.0, 1.0) if sed_type == 'lum':