def setUp(self) -> None: self.testfile = '3-GLDAS.SoilMoi0_10cm_inst_with_1-C3S.sm_with_2-SMOS.Soil_Moisture.nc' self.testfile_path = os.path.join(os.path.dirname(__file__), '..','tests', 'test_data', 'tc', self.testfile) self.plotdir = tempfile.mkdtemp() self.img = QA4SMImg(self.testfile_path) self.plotter = QA4SMPlotter(self.img, self.plotdir)
def setUp(self) -> None: self.testfile = '0-ISMN.soil moisture_with_1-C3S.sm.nc' self.testfile_path = os.path.join(os.path.dirname(__file__), '..','tests', 'test_data', 'basic', self.testfile) self.plotdir = tempfile.mkdtemp() self.img = QA4SMImg(self.testfile_path) self.plotter = QA4SMPlotter(self.img, self.plotdir)
def plot_all(filepath, metrics=None, extent=None, out_dir=None, out_type='png', boxplot_kwargs=dict(), mapplot_kwargs=dict()): """ Creates boxplots for all metrics and map plots for all variables. Saves the output in a folder-structure. Parameters ---------- filepath : str Path to the *.nc file to be processed. metrics : set or list, optional (default: None) metrics to be plotted, if None are passed, all are plotted (that have data) extent : list [x_min,x_max,y_min,y_max] to create a subset of the values out_dir : [ None | str ], optional Parrent directory where to generate the folder structure for all plots. If None, defaults to the current working directory. The default is None. out_types : list, optional (default: [png, svg]) File types, e.g. 'png', 'pdf', 'svg', 'tiff'... A list, a plot is saved for each type. **boxplot_kwargs : dict, optional Additional keyword arguments that are passed to the boxplot function. **mapplot_kwargs : dict, optional Additional keyword arguments that are passed to the mapplot function. """ if not out_dir: out_dir = os.path.join(os.getcwd(), os.path.basename(filepath)) img = QA4SMImg(filepath, extent=extent, ignore_empty=True) plotter = QA4SMPlotter(image=img, out_dir=out_dir) # === Metadata === if not metrics: metrics = img.ls_metrics(False) fnames_maps, fnames_boxes = [], [] for metric in metrics: # === load values and metadata === if metric not in globals.metric_groups[3]: fns_box = plotter.boxplot_basic(metric, out_type=out_type, **boxplot_kwargs) else: fns_box = plotter.boxplot_tc(metric, out_type=out_type, **boxplot_kwargs) fns_maps = plotter.mapplot(metric, out_type=out_type, **mapplot_kwargs) plt.close('all') for fn in fns_box: fnames_boxes.append(fn) for fn in fns_maps: fnames_maps.append(fn) return fnames_boxes, fnames_maps
def setUp(self) -> None: self.testfile = "0-ERA5.swvl1_with_1-ESA_CCI_SM_combined.sm_with_2-ESA_CCI_SM_combined.sm_with_3-ESA_CCI_SM_combined.sm_with_4-ESA_CCI_SM_combined.sm.CI.nc" self.testfile_path = os.path.join(os.path.dirname(__file__), '..', 'tests', 'test_data', 'tc', self.testfile) self.plotdir = tempfile.mkdtemp() self.img = QA4SMImg(self.testfile_path) self.plotter = QA4SMPlotter(self.img, self.plotdir)
def get_img_stats(filepath, extent=None): """ Creates a dataframe containing summary statistics of the result metrics values which is rendered in the file qa4sm/validator/templates/validator/results.html Parameters ---------- filepath : str Path to the *.nc file to be processed. extent : list, optional [x_min,x_max,y_min,y_max] to create a subset of the values. The default is None. Returns ------- table : pd.DataFrame Quick inspection table of the results. """ img = QA4SMImg(filepath, extent=extent, ignore_empty=True) table = img.stats_df() return table
def get_img_stats( filepath:str, extent:tuple=None ) -> pd.DataFrame: """ Creates a dataframe containing summary statistics for each metric Parameters ---------- filepath : str path to the *.nc file to be processed. extent : list list(x_min, x_max, y_min, y_max) to create a subset of the values Returns ------- table : pd.DataFrame Quick inspection table of the results. """ img = QA4SMImg(filepath, extent=extent, ignore_empty=True) table = img.stats_df() return table
def setUp(self) -> None: self.testfile = '3-ERA5_LAND.swvl1_with_1-C3S.sm_with_2-SMOS.Soil_Moisture.nc' self.testfile_path = os.path.join(os.path.dirname(__file__), '..', 'tests', 'test_data', 'basic', self.testfile) self.img = QA4SMImg(self.testfile_path, ignore_empty=False)
def plot_all( filepath:str, metrics:list=None, extent:tuple=None, out_dir:str=None, out_type:str='png', save_all:bool=True, **plotting_kwargs ) -> (list, list): """ Creates boxplots for all metrics and map plots for all variables. Saves the output in a folder-structure. Parameters ---------- filepath : str path to the *.nc file to be processed. metrics : set or list, optional (default: None) metrics to be plotted. If None, all metrics with data are plotted extent : tuple, optional (default: None) Area to subset the values for -> (min_lon, max_lon, min_lat, max_lat) out_dir : str, optional (default: None) Path to output generated plot. If None, defaults to the current working directory. out_type: str or list extensions which the files should be saved in save_all: bool, optional. Default is True. all plotted images are saved to the output directory plotting_kwargs: arguments for plotting functions. Returns ------- fnames_boxplots: list fnames_mapplots: list lists of filenames for created mapplots and boxplots """ # initialise image and plotter fnames_bplot, fnames_mapplot = [], [] periods = extract_periods(filepath) for period in periods: img = QA4SMImg( filepath, period=period, extent=extent, ignore_empty=True ) plotter = QA4SMPlotter( image=img, out_dir=os.path.join(out_dir, str(period)) if period else out_dir ) if metrics is None: metrics = img.metrics # iterate metrics and create files in output directory for metric in metrics: metric_bplots, metric_mapplots = plotter.plot_metric( metric=metric, out_types=out_type, save_all=save_all, **plotting_kwargs ) # there can be boxplots with no mapplots if metric_bplots: fnames_bplot.extend(metric_bplots) if metric_mapplots: fnames_mapplot.extend(metric_mapplots) return fnames_bplot, fnames_mapplot
def test_metrics(self): metrics = QA4SMImg(self.testfile_path, metrics=['R']) assert metrics.common != self.img.common assert metrics.double != self.img.double assert 'R' in metrics.double.keys()
def test_extent(self): extent = QA4SMImg(self.testfile_path, extent=(113.7, 123.7, -19.8, -9.8)) assert self.img.extent != extent.extent assert self.img.extent == (113.7, 153.5, -43.1, -9.8)
def setUp(self) -> None: self.testfile = "0-ERA5.swvl1_with_1-ESA_CCI_SM_combined.sm_with_2-ESA_CCI_SM_combined.sm_with_3-ESA_CCI_SM_combined.sm_with_4-ESA_CCI_SM_combined.sm.CI.nc" self.testfile_path = os.path.join(os.path.dirname(__file__), '..', 'tests', 'test_data', 'tc', self.testfile) self.img = QA4SMImg(self.testfile_path, ignore_empty=False)
def test_load_data(self): unloaded = QA4SMImg(self.testfile_path, load_data=False) assert 'varnames' not in unloaded.__dict__.keys()