def high_res_plot_img(array, filename=None, down=None, verbose=True, overwrite=True, crange=None, cmap="planck"): if not (overwrite): if os.path.isfile(filename): return try: from pixell import enmap, enplot except: traceback.print_exc() cprint( "Could not produce plot " + filename + ". High resolution plotting requires enlib, which couldn't be imported. Continuing without plotting.", color='fail') return if (down is not None) and (down != 1): downmap = enmap.downgrade(enmap.enmap(array)[None], down) else: downmap = enmap.enmap(array)[None] img = enplot.draw_map_field(downmap, enplot.parse_args("-c " + cmap + " -vvvg moo"), crange=crange) #img = enplot.draw_map_field(downmap,enplot.parse_args("--grid 1"),crange=crange) if filename is None: img.show() else: img.save(filename) if verbose: print(bcolors.OKGREEN + "Saved high-res plot to", filename + bcolors.ENDC)
import tile_utils_sigurd from pspy import so_map import os map_name = "cmb.fits" odir = "leaflet" if os.path.exists(odir): os.system("rm -rf %s" % odir) comm = mpi.COMM_WORLD tile_utils_sigurd.leaftile(map_name, odir, verbose=True, comm=comm, monolithic=True) args = enplot.parse_args() for plot in enplot.plot_iterator(*args.ifiles, comm=mpi.COMM_WORLD, **args): enplot.write(plot.name, plot) filename = "test_plot.html" g = open(filename, mode='w') g.write('<html>\n') g.write('<head>\n') g.write('<link rel=stylesheet href=html_utils/leaflet.css>\n') g.write('<link rel=stylesheet href=html_utils/L.Control.MousePosition.css>\n') g.write('<script src=html_utils/leaflet-src.js></script>\n') g.write('<script src=html_utils/L.Control.MousePosition.js></script>\n') g.write('<script src=html_utils/Leaflet.Graticule.js></script>\n') g.write('<script src=html_utils/leaflet-ellipse.js></script>\n') g.write('<script src=html_utils/L.ColorizableLayer.js></script>\n')
def car2tiles( input_file, mask_file=None, enplot_args=None, output_dir=None, delete_fits=True, use_webplot=True, pre_operation=None, ): """Convert CAR map to PNG tiles Parameters ---------- input_file: fits file name of the input CAR fits file mask_file: fits file name of the CAR mask file enplot_args: list of enplot/webplot options (see corresponding programs) output_dir: string name of the output directory holding PNG files delete_fits: boolean delete the FITS files corresponding to the tiles use_webplot: boolean use webplot in place of enplot program """ enplot_args = enplot_args or [] comm = mpi.COMM_WORLD if comm.rank == 0: if output_dir is None: output_dir = os.path.join("tiles", os.path.basename(input_file)) # Check if path to fits file are already stored fits_files = os.path.join(output_dir, "*/*.fits") if fits_files not in enplot_args: enplot_args.append(fits_files) if os.path.exists(output_dir): os.system("rm -rf %s" % output_dir) if mask_file is not None: car = so_map.read_map(input_file) mask = so_map.read_map(mask_file) # from pixell import wcsutils # if not wcsutils.is_compatible(mask.geometry[0], car.geometry[0]): # raise ValueError("Map and mask must have compatible geometries") if mask.ncomp == car.ncomp == 1: car.data *= np.where(mask.data < 0.5, 0, 1) elif mask.ncomp == 1: for i in range(car.ncomp): car.data[i] *= np.where(mask.data < 0.5, 0, 1) else: if mask.ncomp != car.ncomp: raise ValueError("Map and mask have different number of components") for i in range(mask.ncomp): car.data[i] *= np.where(mask.data[i] < 0.5, 0, 1) input_file += ".tmp" car.write_map(input_file) if pre_operation is not None: car = so_map.read_map(input_file) car.data = eval(pre_operation, {"m": car.data}, np.__dict__) input_file += ".tmp" car.write_map(input_file) if not mpi.disabled: comm.barrier() tile_utils_sigurd.leaftile( input_file, output_dir, verbose="-v" in enplot_args, comm=comm if not mpi.disabled else None, monolithic=True, ) if use_webplot: args = webplot.parse_args(enplot_args) webplot.plot(args) else: args = enplot.parse_args(enplot_args) for plot in enplot.plot_iterator(*args.ifiles, comm=comm, **args): enplot.write(plot.name, plot) if comm.rank == 0: if mask_file is not None or pre_operation is not None: os.remove(input_file) if delete_fits: for fits in args.ifiles: os.remove(fits)