diff --git a/act/plotting/skewtdisplay.py b/act/plotting/skewtdisplay.py index dd24bfc4cd..51f289cb06 100644 --- a/act/plotting/skewtdisplay.py +++ b/act/plotting/skewtdisplay.py @@ -211,8 +211,10 @@ def plot_from_spd_and_dir(self, spd_field, dir_field, # Make temporary field called tempu, tempv spd = self._obj[dsname][spd_field].values dir = self._obj[dsname][dir_field].values - tempu = -np.sin(np.deg2rad(dir)) * spd - tempv = -np.cos(np.deg2rad(dir)) * spd + with warnings.catch_warnings(): + warnings.filterwarnings("ignore", category=RuntimeWarning) + tempu = -np.sin(np.deg2rad(dir)) * spd + tempv = -np.cos(np.deg2rad(dir)) * spd self._obj[dsname]["temp_u"] = deepcopy(self._obj[dsname][spd_field]) self._obj[dsname]["temp_v"] = deepcopy(self._obj[dsname][spd_field]) self._obj[dsname]["temp_u"].values = tempu diff --git a/act/plotting/timeseriesdisplay.py b/act/plotting/timeseriesdisplay.py index 5f3f8bc55c..cba4e4dc52 100644 --- a/act/plotting/timeseriesdisplay.py +++ b/act/plotting/timeseriesdisplay.py @@ -444,6 +444,10 @@ def plot(self, field, dsname=None, subplot_index=(0, ), flag_data = self._obj[dsname].qcfilter.get_masked_data( field, rm_assessments=categories, return_inverse=True) if np.invert(flag_data.mask).any() and np.isfinite(flag_data).any(): + try: + flag_data.mask = np.logical_or(data.mask, flag_data.mask) + except AttributeError: + pass qc_ax = ax.plot( xdata, flag_data, marker=overplot_marker, linestyle='', markersize=overplot_markersize, diff --git a/act/qc/arm.py b/act/qc/arm.py index 0bafeec687..1de5ea829f 100644 --- a/act/qc/arm.py +++ b/act/qc/arm.py @@ -68,15 +68,16 @@ def add_dqr_to_qc(obj, variable=None, assessment='incorrect,suspect', # If add_qc_variable is none, set to variables list if add_qc_variable is None: add_qc_variable = variable + if not isinstance(add_qc_variable, (list, tuple)): add_qc_variable = [add_qc_variable] # Loop through each variable and call web service for that variable - for i, var in enumerate(variable): + for var_name in variable: # Create URL url = 'http://www.archive.arm.gov/dqrws/ARMDQR?datastream=' url += datastream - url += '&varname=' + var + url += '&varname=' + var_name url += ''.join(['&searchmetric=', assessment, '&dqrfields=dqrid,starttime,endtime,metric,subject']) @@ -93,29 +94,37 @@ def add_dqr_to_qc(obj, variable=None, assessment='incorrect,suspect', # Get data and run through each dqr dqrs = req.text.splitlines() time = obj['time'].values + dqr_results = {} for line in dqrs: line = line.split('|') + dqr_no = line[0] + # Exclude DQRs if in list - if exclude is not None and line[0] in exclude: + if exclude is not None and dqr_no in exclude: continue # Only include if in include list - if include is not None and line[0] not in include: + if include is not None and dqr_no not in include: continue - line[1] = dt.datetime.utcfromtimestamp(int(line[1])) - line[2] = dt.datetime.utcfromtimestamp(int(line[2])) - ind = np.where((time >= np.datetime64(line[1])) & (time <= np.datetime64(line[2]))) - if len(ind[0]) == 0: + starttime = np.datetime64(dt.datetime.utcfromtimestamp(int(line[1]))) + endtime = np.datetime64(dt.datetime.utcfromtimestamp(int(line[2]))) + ind = np.where((time >= starttime) & (time <= endtime)) + if ind[0].size == 0: continue - # Add flag to object - index = sorted(list(ind)) - name = ': '.join([line[0], line[-1]]) - assess = line[3] - obj.qcfilter.add_test(add_qc_variable[i], index=index, test_meaning=name, test_assessment=assess) + if dqr_no in dqr_results.keys(): + dqr_results[dqr_no]['index'] = np.append(dqr_results[dqr_no]['index'], ind) + else: + dqr_results[dqr_no] = {'index': ind, 'test_assessment': line[3], + 'test_meaning': ': '.join([dqr_no, line[-1]])} + + for key, value in dqr_results.items(): + obj.qcfilter.add_test(var_name, index=value['index'], + test_meaning=value['test_meaning'], + test_assessment=value['test_assessment']) if normalize_assessment: - obj.clean.normalize_assessment(variables=add_qc_variable[i]) + obj.clean.normalize_assessment(variables=var_name) return obj diff --git a/act/tests/test_plotting.py b/act/tests/test_plotting.py index 48bb9b4b3f..320a869250 100644 --- a/act/tests/test_plotting.py +++ b/act/tests/test_plotting.py @@ -46,7 +46,11 @@ def test_plot(): spd_bins=np.linspace(0, 10, 4)) windrose.axes[0].legend(loc='best') met.close() - return display.fig + + try: + return display.fig + finally: + matplotlib.pyplot.close(display.fig) def test_errors(): @@ -127,6 +131,8 @@ def test_errors(): assert display.fig is not None assert display.axes is not None + matplotlib.pyplot.close(fig=display.fig) + def test_histogram_errors(): files = sample_files.EXAMPLE_MET1 @@ -177,6 +183,8 @@ def test_histogram_errors(): assert histdisplay.fig is not None assert histdisplay.axes is not None + matplotlib.pyplot.close(fig=histdisplay.fig) + def test_xsection_errors(): obj = arm.read_netcdf(sample_files.EXAMPLE_CEIL1) @@ -193,6 +201,7 @@ def test_xsection_errors(): display.plot_xsection(None, 'backscatter', x='time') obj.close() + matplotlib.pyplot.close(fig=display.fig) @pytest.mark.mpl_image_compare(tolerance=30) @@ -217,7 +226,11 @@ def test_multidataset_plot_tuple(): ax, fig = act.plotting.common.parse_ax_fig(ax=None, fig=None) obj.close() obj2.close() - return display.fig + + try: + return display.fig + finally: + matplotlib.pyplot.close(display.fig) @pytest.mark.mpl_image_compare(tolerance=30) @@ -238,7 +251,10 @@ def test_multidataset_plot_dict(): obj.close() obj2.close() - return display.fig + try: + return display.fig + finally: + matplotlib.pyplot.close(display.fig) @pytest.mark.mpl_image_compare(tolerance=30) @@ -254,7 +270,11 @@ def test_wind_rose(): WindDisplay.set_rrng((0., 14)) sonde_ds.close() - return WindDisplay.fig + + try: + return WindDisplay.fig + finally: + matplotlib.pyplot.close(WindDisplay.fig) @pytest.mark.mpl_image_compare(tolerance=30) @@ -269,7 +289,11 @@ def test_barb_sounding_plot(): BarbDisplay.plot_barbs_from_spd_dir('deg', 'wspd', 'pres', num_barbs_x=20) sonde_ds.close() - return BarbDisplay.fig + + try: + return BarbDisplay.fig + finally: + matplotlib.pyplot.close(BarbDisplay.fig) @pytest.mark.mpl_image_compare(tolerance=30) @@ -281,7 +305,10 @@ def test_skewt_plot(): skewt = SkewTDisplay(sonde_ds) skewt.plot_from_u_and_v('u_wind', 'v_wind', 'pres', 'tdry', 'dp') sonde_ds.close() - return skewt.fig + try: + return skewt.fig + finally: + matplotlib.pyplot.close(skewt.fig) @pytest.mark.mpl_image_compare(tolerance=30) @@ -292,7 +319,10 @@ def test_skewt_plot_spd_dir(): skewt = SkewTDisplay(sonde_ds, ds_name='act_datastream') skewt.plot_from_spd_and_dir('wspd', 'deg', 'pres', 'tdry', 'dp') sonde_ds.close() - return skewt.fig + try: + return skewt.fig + finally: + matplotlib.pyplot.close(skewt.fig) @pytest.mark.mpl_image_compare(tolerance=67) @@ -319,7 +349,10 @@ def test_multi_skewt_plot(): j = 0 elif j == 0: j += 1 - return skewt.fig + try: + return skewt.fig + finally: + matplotlib.pyplot.close(skewt.fig) @pytest.mark.mpl_image_compare(tolerance=31) @@ -331,7 +364,11 @@ def test_xsection_plot(): xsection.plot_xsection(None, 'backscatter', x='time', y='range', cmap='coolwarm', vmin=0, vmax=320) visst_ds.close() - return xsection.fig + + try: + return xsection.fig + finally: + matplotlib.pyplot.close(xsection.fig) @pytest.mark.mpl_image_compare(tolerance=30) @@ -344,7 +381,10 @@ def test_xsection_plot_map(): xsection.plot_xsection_map(None, 'ir_temperature', vmin=220, vmax=300, cmap='Greys', x='longitude', y='latitude', isel_kwargs={'time': 0}) radar_ds.close() - return xsection.fig + try: + return xsection.fig + finally: + matplotlib.pyplot.close(xsection.fig) except Exception: pass @@ -355,10 +395,14 @@ def test_geoplot(): sample_files.EXAMPLE_SONDE1) try: geodisplay = GeographicPlotDisplay({'sgpsondewnpnC1.b1': sonde_ds}) - geodisplay.geoplot('tdry', marker='.', cartopy_feature=['STATES', 'LAND', 'OCEAN', 'COASTLINE', - 'BORDERS', 'LAKES', 'RIVERS'], + geodisplay.geoplot('tdry', marker='.', cartopy_feature=['STATES', 'LAND', 'OCEAN', + 'COASTLINE', 'BORDERS', + 'LAKES', 'RIVERS'], text={'Ponca City': [-97.0725, 36.7125]}) - return geodisplay.fig + try: + return geodisplay.fig + finally: + matplotlib.pyplot.close(geodisplay.fig) except Exception: pass sonde_ds.close() @@ -373,7 +417,10 @@ def test_stair_graph(): histdisplay.plot_stairstep_graph('tdry', bins=np.arange(-60, 10, 1)) sonde_ds.close() - return histdisplay.fig + try: + return histdisplay.fig + finally: + matplotlib.pyplot.close(histdisplay.fig) @pytest.mark.mpl_image_compare(tolerance=30) @@ -387,7 +434,10 @@ def test_stair_graph_sorted(): sortby_bins=np.linspace(0, 10000., 6)) sonde_ds.close() - return histdisplay.fig + try: + return histdisplay.fig + finally: + matplotlib.pyplot.close(histdisplay.fig) @pytest.mark.mpl_image_compare(tolerance=30) @@ -399,7 +449,10 @@ def test_stacked_bar_graph(): histdisplay.plot_stacked_bar_graph('tdry', bins=np.arange(-60, 10, 1)) sonde_ds.close() - return histdisplay.fig + try: + return histdisplay.fig + finally: + matplotlib.pyplot.close(histdisplay.fig) @pytest.mark.mpl_image_compare(tolerance=30) @@ -413,7 +466,10 @@ def test_stacked_bar_graph2(): histdisplay.set_xrng([-70, 0]) sonde_ds.close() - return histdisplay.fig + try: + return histdisplay.fig + finally: + matplotlib.pyplot.close(histdisplay.fig) @pytest.mark.mpl_image_compare(tolerance=30) @@ -427,7 +483,10 @@ def test_stacked_bar_graph_sorted(): sortby_bins=np.linspace(0, 10000., 6)) sonde_ds.close() - return histdisplay.fig + try: + return histdisplay.fig + finally: + matplotlib.pyplot.close(histdisplay.fig) @pytest.mark.mpl_image_compare(tolerance=30) @@ -441,7 +500,10 @@ def test_heatmap(): y_bins=np.linspace(0, 10000., 50), cmap='coolwarm') sonde_ds.close() - return histdisplay.fig + try: + return histdisplay.fig + finally: + matplotlib.pyplot.close(histdisplay.fig) @pytest.mark.mpl_image_compare(tolerance=30) @@ -455,7 +517,10 @@ def test_size_distribution(): my_fake_ds = xr.Dataset({'bins': bins, 'ydata': y_array}) histdisplay = HistogramDisplay(my_fake_ds) histdisplay.plot_size_distribution('ydata', 'bins', set_title='Fake distribution.') - return histdisplay.fig + try: + return histdisplay.fig + finally: + matplotlib.pyplot.close(histdisplay.fig) @pytest.mark.mpl_image_compare(tolerance=30) @@ -476,10 +541,14 @@ def test_contour(): display = ContourDisplay(data, figsize=(8, 8)) display.create_contour(fields=fields, time=time, levels=50, contour='contour', cmap='viridis') - display.plot_vectors_from_spd_dir(fields=wind_fields, time=time, mesh=True, grid_delta=(0.1, 0.1)) + display.plot_vectors_from_spd_dir(fields=wind_fields, time=time, mesh=True, + grid_delta=(0.1, 0.1)) display.plot_station(fields=station_fields, time=time, markersize=7, color='red') - return display.fig + try: + return display.fig + finally: + matplotlib.pyplot.close(display.fig) @pytest.mark.mpl_image_compare(tolerance=30) @@ -499,7 +568,10 @@ def test_contour_stamp(): display.create_contour(fields=stamp_fields, time=time, levels=50, alpha=0.5, twod_dim_value=5) - return display.fig + try: + return display.fig + finally: + matplotlib.pyplot.close(display.fig) @pytest.mark.mpl_image_compare(tolerance=30) @@ -520,10 +592,14 @@ def test_contour2(): display = ContourDisplay(data, figsize=(8, 8)) display.create_contour(fields=fields, time=time, levels=50, contour='contour', cmap='viridis') - display.plot_vectors_from_spd_dir(fields=wind_fields, time=time, mesh=False, grid_delta=(0.1, 0.1)) + display.plot_vectors_from_spd_dir(fields=wind_fields, time=time, mesh=False, + grid_delta=(0.1, 0.1)) display.plot_station(fields=station_fields, time=time, markersize=7, color='pink') - return display.fig + try: + return display.fig + finally: + matplotlib.pyplot.close(display.fig) @pytest.mark.mpl_image_compare(tolerance=30) @@ -545,10 +621,14 @@ def test_contourf(): display = ContourDisplay(data, figsize=(8, 8)) display.create_contour(fields=fields, time=time, levels=50, contour='contourf', cmap='viridis') - display.plot_vectors_from_spd_dir(fields=wind_fields, time=time, mesh=True, grid_delta=(0.1, 0.1)) + display.plot_vectors_from_spd_dir(fields=wind_fields, time=time, mesh=True, + grid_delta=(0.1, 0.1)) display.plot_station(fields=station_fields, time=time, markersize=7, color='red') - return display.fig + try: + return display.fig + finally: + matplotlib.pyplot.close(display.fig) @pytest.mark.mpl_image_compare(tolerance=30) @@ -570,10 +650,14 @@ def test_contourf2(): display = ContourDisplay(data, figsize=(8, 8)) display.create_contour(fields=fields, time=time, levels=50, contour='contourf', cmap='viridis') - display.plot_vectors_from_spd_dir(fields=wind_fields, time=time, mesh=False, grid_delta=(0.1, 0.1)) + display.plot_vectors_from_spd_dir(fields=wind_fields, time=time, mesh=False, + grid_delta=(0.1, 0.1)) display.plot_station(fields=station_fields, time=time, markersize=7, color='pink') - return display.fig + try: + return display.fig + finally: + matplotlib.pyplot.close(display.fig) # Due to issues with pytest-mpl, for now we just test to see if it runs @@ -587,7 +671,10 @@ def test_time_height_scatter(): sonde_ds.close() - return display.fig + try: + return display.fig + finally: + matplotlib.pyplot.close(display.fig) @pytest.mark.mpl_image_compare(tolerance=30) @@ -614,7 +701,10 @@ def test_qc_bar_plot(): ds_object.close() - return display.fig + try: + return display.fig + finally: + matplotlib.pyplot.close(display.fig) @pytest.mark.mpl_image_compare(tolerance=30) @@ -627,7 +717,10 @@ def test_2d_as_1d(): obj.close() del obj - return display.fig + try: + return display.fig + finally: + matplotlib.pyplot.close(display.fig) @pytest.mark.mpl_image_compare(tolerance=30) @@ -642,7 +735,10 @@ def test_fill_between(): obj.close() del obj - return display.fig + try: + return display.fig + finally: + matplotlib.pyplot.close(display.fig) @pytest.mark.mpl_image_compare(tolerance=30) @@ -658,7 +754,10 @@ def test_qc_flag_block_plot(): obj.close() del obj - return display.fig + try: + return display.fig + finally: + matplotlib.pyplot.close(display.fig) @pytest.mark.mpl_image_compare(tolerance=30) @@ -679,7 +778,10 @@ def test_assessment_overplot(): display.plot(var_name, day_night_background=True, assessment_overplot=True) ds.close() - return display.fig + try: + return display.fig + finally: + matplotlib.pyplot.close(display.fig) @pytest.mark.mpl_image_compare(tolerance=30) @@ -702,7 +804,10 @@ def test_assessment_overplot_multi(): label=var_name2, assessment_overplot=True) ds.close() - return display.fig + try: + return display.fig + finally: + matplotlib.pyplot.close(display.fig) @pytest.mark.mpl_image_compare(tolerance=30) @@ -713,7 +818,10 @@ def test_plot_barbs_from_u_v(): BarbDisplay.plot_barbs_from_u_v('u_wind', 'v_wind', 'pres', num_barbs_x=20) sonde_ds.close() - return BarbDisplay.fig + try: + return BarbDisplay.fig + finally: + matplotlib.pyplot.close(BarbDisplay.fig) @pytest.mark.mpl_image_compare(tolerance=30) @@ -735,7 +843,10 @@ def test_plot_barbs_from_u_v2(): BarbDisplay.plot_barbs_from_u_v('xdata', 'ydata', None, num_barbs_x=20, num_barbs_y=20, set_title='test plot', cmap='jet') fake_obj.close() - return BarbDisplay.fig + try: + return BarbDisplay.fig + finally: + matplotlib.pyplot.close(BarbDisplay.fig) @pytest.mark.mpl_image_compare(tolerance=30) @@ -743,4 +854,7 @@ def test_2D_timeseries_plot(): obj = arm.read_netcdf(sample_files.EXAMPLE_CEIL1) display = TimeSeriesDisplay(obj) display.plot('backscatter', y_rng=[0, 5000], use_var_for_y='range') - return display.fig + try: + return display.fig + finally: + matplotlib.pyplot.close(display.fig)