|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +import datetime |
| 4 | +import pytest |
| 5 | +import numpy as np |
| 6 | +from pysteps import io, motion, nowcasts, rcparams, utils, verification |
| 7 | + |
| 8 | + |
| 9 | +def _import_mch_gif(prv, nxt): |
| 10 | + |
| 11 | + date = datetime.datetime.strptime("201505151630", "%Y%m%d%H%M") |
| 12 | + data_source = rcparams.data_sources["mch"] |
| 13 | + |
| 14 | + # Load data source config |
| 15 | + root_path = data_source["root_path"] |
| 16 | + path_fmt = data_source["path_fmt"] |
| 17 | + fn_pattern = data_source["fn_pattern"] |
| 18 | + fn_ext = data_source["fn_ext"] |
| 19 | + importer_name = data_source["importer"] |
| 20 | + importer_kwargs = data_source["importer_kwargs"] |
| 21 | + timestep = data_source["timestep"] |
| 22 | + |
| 23 | + # Find the input files from the archive |
| 24 | + fns = io.archive.find_by_date( |
| 25 | + date, |
| 26 | + root_path, |
| 27 | + path_fmt, |
| 28 | + fn_pattern, |
| 29 | + fn_ext, |
| 30 | + timestep=timestep, |
| 31 | + num_prev_files=prv, |
| 32 | + num_next_files=nxt, |
| 33 | + ) |
| 34 | + |
| 35 | + # Read the radar composites |
| 36 | + importer = io.get_method(importer_name, "importer") |
| 37 | + R, _, metadata = io.read_timeseries(fns, importer, **importer_kwargs) |
| 38 | + |
| 39 | + # Convert to rain rate |
| 40 | + R, metadata = utils.conversion.to_rainrate(R, metadata) |
| 41 | + |
| 42 | + # Upscale data to 2 km |
| 43 | + R, metadata = utils.dimension.aggregate_fields_space(R, metadata, 2000) |
| 44 | + |
| 45 | + # Log-transform the data to unit of dBR, set the threshold to 0.1 mm/h, |
| 46 | + # set the fill value to -15 dBR |
| 47 | + R, metadata = utils.transformation.dB_transform( |
| 48 | + R, metadata, threshold=0.1, zerovalue=-15.0 |
| 49 | + ) |
| 50 | + |
| 51 | + # Set missing values with the fill value |
| 52 | + R[~np.isfinite(R)] = -15.0 |
| 53 | + |
| 54 | + return R, metadata |
| 55 | + |
| 56 | + |
| 57 | +steps_arg_names = ( |
| 58 | + "n_ens_members", |
| 59 | + "n_cascade_levels", |
| 60 | + "ar_order", |
| 61 | + "mask_method", |
| 62 | + "probmatching_method", |
| 63 | + "max_crps", |
| 64 | +) |
| 65 | + |
| 66 | +steps_arg_values = [ |
| 67 | + (5, 6, 2, None, None, 1.51), |
| 68 | + (5, 6, 2, "incremental", None, 6.38), |
| 69 | + (5, 6, 2, "sprog", None, 7.35), |
| 70 | + (5, 6, 2, "obs", None, 7.36), |
| 71 | + (5, 6, 2, None, "cdf", 0.66), |
| 72 | + (5, 6, 2, None, "mean", 1.55), |
| 73 | +] |
| 74 | + |
| 75 | + |
| 76 | +@pytest.mark.parametrize(steps_arg_names, steps_arg_values) |
| 77 | +def test_steps( |
| 78 | + n_ens_members, |
| 79 | + n_cascade_levels, |
| 80 | + ar_order, |
| 81 | + mask_method, |
| 82 | + probmatching_method, |
| 83 | + max_crps, |
| 84 | +): |
| 85 | + """Tests STEPS nowcast.""" |
| 86 | + # inputs |
| 87 | + R, metadata = _import_mch_gif(2, 0) |
| 88 | + R_o = _import_mch_gif(0, 3)[0][1:, :, :] |
| 89 | + # optical flow |
| 90 | + of_method = motion.get_method("LK") |
| 91 | + V = of_method(R) |
| 92 | + # nowcast |
| 93 | + nowcast_method = nowcasts.get_method("steps") |
| 94 | + num_timesteps = 1 |
| 95 | + R_f = nowcast_method( |
| 96 | + R, |
| 97 | + V, |
| 98 | + n_timesteps=3, |
| 99 | + R_thr=metadata["threshold"], |
| 100 | + kmperpixel=2.0, |
| 101 | + timestep=metadata["accutime"], |
| 102 | + seed=42, |
| 103 | + n_ens_members=n_ens_members, |
| 104 | + n_cascade_levels=n_cascade_levels, |
| 105 | + ar_order=ar_order, |
| 106 | + mask_method=mask_method, |
| 107 | + probmatching_method=probmatching_method, |
| 108 | + ) |
| 109 | + # result |
| 110 | + result = verification.probscores.CRPS(R_f[-1], R_o[-1]) |
| 111 | + assert result < max_crps |
0 commit comments