From bbe9298524d543006e4d94dfee90292990367f15 Mon Sep 17 00:00:00 2001 From: Tito Vintimilla Date: Wed, 5 Feb 2025 11:20:52 +0100 Subject: [PATCH 1/2] Modified to run in JupyterLab and display plots inline --- mapies/grids/monarch.py | 2 +- mapies/mapies.py | 21 ++++++++++++++------- run/run_viirs.py | 12 ++++++------ 3 files changed, 21 insertions(+), 14 deletions(-) diff --git a/mapies/grids/monarch.py b/mapies/grids/monarch.py index 1b11ec9c..991b8c7a 100644 --- a/mapies/grids/monarch.py +++ b/mapies/grids/monarch.py @@ -14,7 +14,7 @@ from mapies.util.func_tools import * import logging logger = logging.getLogger(__name__) -logging.basicConfig(filename='../logs/mapies.log', level=logging.INFO) +logging.basicConfig(filename='./logs/mapies.log', level=logging.INFO) def regridding_function(r, lon, lat, obs, obserr=None): diff --git a/mapies/mapies.py b/mapies/mapies.py index b2466f42..9dfaef46 100644 --- a/mapies/mapies.py +++ b/mapies/mapies.py @@ -172,7 +172,9 @@ class MAPIES: ax.set_title(f'Observation 2D plot of {self.datatype.upper()} data from {self.start_date} to {self.end_date}') print("Saving Figure") plt.savefig(f"{self.dest}{self.datatype}_2D_obs.png", format="png") - plt.close(fig) + #plt.close(fig) + plt.show() + #return fig @timeit @@ -221,11 +223,12 @@ class MAPIES: ax.set_title(title) # Ensure the output directory exists - os.makedirs(outdir, exist_ok=True) + #os.makedirs(outdir, exist_ok=True) filepath = os.path.join(outdir, filename) plt.savefig(filepath, format="png") - plt.close(fig) + #plt.close(fig) + plt.show() print(f"Saved plot: {filepath}") @timeit @@ -275,7 +278,8 @@ class MAPIES: filepath = os.path.join(outdir, filename) plt.savefig(filepath, format="png") - plt.close(fig) + #plt.close(fig) + plt.show() print(f"Saved plot: {filepath}") @@ -387,11 +391,12 @@ class MAPIES: ax.set_title(title or "Observation 2D Plot") # Ensure the output directory exists - os.makedirs(outdir, exist_ok=True) + #os.makedirs(outdir, exist_ok=True) filepath = os.path.join(self.dest, filename or "observation_2D_plot.png") plt.savefig(filepath, format="png") - plt.close(fig) + #plt.close(fig) + plt.show() print(f"Saved plot: {filepath}") @timeit @@ -446,8 +451,10 @@ class MAPIES: filepath = os.path.join(self.dest, filename or "num_observation_2D_plot.png") plt.savefig(filepath, format="png") - plt.close(fig) + #plt.close(fig) + plt.show() print(f"Saved plot: {filepath}") + #return fig diff --git a/run/run_viirs.py b/run/run_viirs.py index bd8b1504..d19c429b 100644 --- a/run/run_viirs.py +++ b/run/run_viirs.py @@ -24,18 +24,18 @@ if __name__ == "__main__": # outdir="/home/cmeikle/Projects/data/" # indir="/home/cmeikle/Projects/data/VIIRS/original_files/AERDB_L2_VIIRS_NOAA20" - outdir = "/home/cgile/Documents/mapies/figures" - # indir = "/home/cgile/bscearth000/esarchive/obs/nasa/viirs_noaa20_aerdb_l2/original_files/VIIRS" - indir = "/home/cgile/Documents/mapies/VIIRS" + outdir = "/home/tvintimi/Documents/mapies/figures" + indir = "/home/tvintimi/esarchive/obs/nasa/viirs_noaa20_aerdb_l2/original_files/VIIRS" + #indir = "/home/tvintimi/Documents/mapies/VIIRS" - filename_obs = 'carlotta' - filename_num_obs = 'carlotta' + filename_obs = 'atarri_obs' + filename_num_obs = 'atarri_num_obs' start_time = time.time() c = VIIRS(start_date, end_date, dest=outdir, indir=indir, apply_qa=False, grid_repr="rotated") c.read_nc() - c.process_data(monthly_avg=False, batch_size = 100) + c.process_data(monthly_avg=False, batch_size = 50) # c.yearly_average() c.plot_2D_observations(months=[0], filename=filename_obs, outdir=outdir) c.plot_2D_num_obs(months=[0], filename=filename_num_obs, outdir=outdir) -- GitLab From adf6f047b6ca358e850919d78f36c8b68fffe257 Mon Sep 17 00:00:00 2001 From: Calum Meikle Date: Wed, 5 Feb 2025 17:38:27 +0100 Subject: [PATCH 2/2] Updated with parameter option of display_fig, default is False --- mapies/mapies.py | 79 ++++++++---------------------------------------- mapies/viirs.py | 12 ++++++-- 2 files changed, 21 insertions(+), 70 deletions(-) diff --git a/mapies/mapies.py b/mapies/mapies.py index 2989645d..206969bc 100644 --- a/mapies/mapies.py +++ b/mapies/mapies.py @@ -178,7 +178,7 @@ class MAPIES: @timeit - def plot_2D_obs_custom(self, lon_values=None, lat_values=None, obs_values=None, outdir=None, title=None, filename=None): + def plot_2D_obs_custom(self, lon_values=None, lat_values=None, obs_values=None, outdir=None, title=None, filename=None, display_fig=False): """ General method for plotting observations with CB-RdYlBu colormap and arrow-like ends for out-of-bound values. """ @@ -227,12 +227,13 @@ class MAPIES: filepath = os.path.join(outdir, filename) plt.savefig(filepath, format="png") - #plt.close(fig) - plt.show() + if display_fig: + plt.show() print(f"Saved plot: {filepath}") + @timeit - def plot_2D_cumulative_count(self, lon_values, lat_values, cumulative_count, outdir="/home/cgile/Documents/mapies/mapies/figures", title=None, filename=None): + def plot_2D_cumulative_count(self, lon_values, lat_values, cumulative_count, outdir="/home/cgile/Documents/mapies/mapies/figures", title=None, filename=None, display_fig=False): """ Plot the cumulative count of observations contributing to the final averages. @@ -278,8 +279,8 @@ class MAPIES: filepath = os.path.join(outdir, filename) plt.savefig(filepath, format="png") - #plt.close(fig) - plt.show() + if display_fig: + plt.show() print(f"Saved plot: {filepath}") @@ -341,72 +342,15 @@ class MAPIES: return ds - @timeit - def plot_2D_obs_custom(self, lon_values=None, lat_values=None, obs_values=None, outdir="./", title=None, filename=None): - """ - General method for plotting observations with CB-RdYlBu colormap and arrow-like ends for out-of-bound values. - """ - if lon_values is None: - lon_values = self.lon_values - if lat_values is None: - lat_values = self.lat_values - if obs_values is None: - obs_values = self.obs - - # Set Basemap projection - figsize = (15, 10) - markersize = 2.5 - proj = ccrs.PlateCarree() - - # Define discrete levels for the color bar - levels = [0.02, 0.04, 0.06, 0.08, 0.10, 0.15, 0.20, 0.30, 0.40, 0.50, 0.60, 0.70, 0.80, 1.00] - - # Define the custom color list - colors = [ - '#290AD8', '#2645FA', '#3A91FF', '#66CBFF', '#99EEFF', - '#CDFCFF', '#F2FFD9', '#FFEEAA', '#FFCA88', '#FC966A', - '#ED5750', '#CB1C2D', '#9A001E', '#68001D', '#690067' - ] - - # Create the custom colormap - cmap = ListedColormap(colors) - norm = mcolors.BoundaryNorm(boundaries=levels, ncolors=len(colors), extend="both") - - # Create the plot and add features - fig, ax = plt.subplots(subplot_kw={"projection": proj}, figsize=figsize) - ax.gridlines() - ax.coastlines(resolution="10m") - - # Scatter plot with discrete color mapping - im = ax.scatter( - lon_values, lat_values, markersize, c=obs_values, cmap=cmap, norm=norm, transform=proj - ) - - # Add the discrete color bar with arrow-like ends - cbar = fig.colorbar(im, ax=ax, orientation="vertical", pad=0.05, ticks=levels, extend="both") - cbar.set_label("AOD") - cbar.ax.set_yticklabels([f"{level:.2f}" for level in levels]) # Format tick labels - - # Add title and save the plot - ax.set_title(title or "Observation 2D Plot") - - # Ensure the output directory exists - #os.makedirs(outdir, exist_ok=True) - - filepath = os.path.join(self.dest, filename or "observation_2D_plot.png") - plt.savefig(filepath, format="png") - #plt.close(fig) - plt.show() - print(f"Saved plot: {filepath}") - @timeit def plot_2D_num_obs_custom( self, lon_values=None, lat_values=None, num_obs=None, - outdir="./", + outdir="./", title=None, filename=None, + display_fig=False ): """ General method for plotting the number of observations contributing to the final averages. @@ -451,9 +395,10 @@ class MAPIES: filepath = os.path.join(self.dest, filename or "num_observation_2D_plot.png") plt.savefig(filepath, format="png") - #plt.close(fig) - plt.show() + if display_fig: + plt.show() print(f"Saved plot: {filepath}") + #return fig diff --git a/mapies/viirs.py b/mapies/viirs.py index ff8a3576..c00bd0a3 100644 --- a/mapies/viirs.py +++ b/mapies/viirs.py @@ -18,7 +18,7 @@ import xarray as xr import matplotlib.pyplot as plt import cartopy import cartopy.crs as ccrs -from netCDF4 import Dataset +#from netCDF4 import Dataset from .mapies import MAPIES from mapies.util.func_tools import * @@ -678,7 +678,7 @@ class VIIRS(MAPIES): # ============================================================================= # Plotting # ============================================================================= - def plot_2D_observations(self, months=[0], filename=None, outdir=None): + def plot_2D_observations(self, months=[0], filename=None, outdir=None, display_fig=False): if months == [0]: try: @@ -697,6 +697,7 @@ class VIIRS(MAPIES): outdir=outdir, title=title, filename=filename, + display_fig=display_fig ) return elif months is not None: @@ -718,6 +719,7 @@ class VIIRS(MAPIES): outdir=outdir, title=title, filename=filename, + display_fig=display_fig ) else: try: @@ -736,9 +738,10 @@ class VIIRS(MAPIES): outdir=outdir, title=title, filename=filename, + display_fig=display_fig, ) - def plot_2D_num_obs(self, months=[0], filename=None, outdir=None): + def plot_2D_num_obs(self, months=[0], filename=None, outdir=None, display_fig=False): if months == [0]: try: @@ -757,6 +760,7 @@ class VIIRS(MAPIES): outdir=outdir, title=title, filename=filename, + display_fig=display_fig ) return elif months is not None: @@ -778,6 +782,7 @@ class VIIRS(MAPIES): outdir=outdir, title=title, filename=filename, + display_fig=display_fig ) else: try: @@ -796,4 +801,5 @@ class VIIRS(MAPIES): outdir=outdir, title=title, filename=filename, + display_fig=display_fig ) -- GitLab