def test_calculate(testdata): ds = twinotter.load_flight(testdata["flight_data_path"]) theta = twinotter.derive.calculate("air_potential_temperature", ds) # This number is correct for the testdata using r004 assert np.isclose(theta[0], 298.76865)
def test_calculate_equivalent(testdata, variable, function, arguments): # Check that "calculate" returns an equivalent variable to calling the specific # function Currently only setup for "first-order" functions. i.e. functions that # calculate a new variable using only variables that are already in the basic # MASIN dataset ds = twinotter.load_flight(testdata["flight_data_path"]) result1 = twinotter.derive.calculate(variable, ds) result2 = function(*[ds[arg] for arg in arguments]) result1 = _filter_nans(result1) result2 = _filter_nans(result2) assert (result1 == result2).all()
def test_load_flight(testdata, load_from, revision): ds = twinotter.load_flight(flight_data_path=testdata[load_from], revision=revision) # Check that the dataset still contains the right number of variables assert len(ds) == 58 # Check that the time period of the dataset is the same assert len(ds.Time == 9928) # Check that there are no NaNs left in the dataset # Awkward syntax because xarray.DataArray.any returns an array # Numpy now has it's own bool type so we have to use "==" rather than "is" # Using pd.isnull because np.isnan doesn't work on object arrays # - https://stackoverflow.com/a/36001191/8270394 assert pd.isnull(ds.Time.values).any() == False return
def main(flight_data_path, alt_max=100.0): ds = twinotter.load_flight(flight_data_path) da_alt = ds.ALT_OXTS da_lat = ds.LAT_OXTS da_lon = ds.LON_OXTS da_lw_up = ds.LW_UP_C fig, axes = plt.subplots( subplot_kw=dict(projection=ccrs.PlateCarree()), nrows=2, figsize=(10, 12), sharey=True, ) def _bootstrap(ax): ax.coastlines(resolution='10m') twinotter.external.eurec4a.add_halo_circle(ax=ax) gl = ax.gridlines(draw_labels=True) gl.top_labels = False gl.right_labels = False ax = axes[0] _bootstrap(ax) sc = ax.scatter(da_lon, da_lat, c=da_lw_up.where(da_alt < alt_max, np.nan)) cb = fig.colorbar(sc, ax=ax) cb.set_label(xr.plot.utils.label_from_attrs(da_lw_up)) ax = axes[1] _bootstrap(ax) sc = ax.scatter(da_lon, da_lat, c=da_alt.where(da_alt < alt_max, np.nan)) cb = fig.colorbar(sc, ax=ax) cb.set_label(xr.plot.utils.label_from_attrs(da_alt)) plt.tight_layout() date = ds.Time.isel(Time=0).dt.strftime("%d/%m/%Y").item() plt.suptitle(f"Flight {ds.flight_number} - {date} - below {alt_max}m") return fig, ds
def test_calculate_nonsense(testdata): ds = twinotter.load_flight(testdata["flight_data_path"]) with pytest.raises(ValueError): twinotter.derive.calculate("nonsense", ds)
def test_extract_segments(testdata): ds = twinotter.load_flight(flight_data_path=testdata["flight_data_path"]) flight_segments = twinotter.load_segments(testdata["flight_segments_file"]) ds_segs = twinotter.extract_segments(ds, flight_segments, "level") assert len(ds_segs.Time) == 5684
def test_load_flight_empty_fails(testdata_empty): with pytest.raises(FileNotFoundError): twinotter.load_flight( flight_data_path=testdata_empty["flight_data_path"]) return