Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
startdate_str = "201701311030"
data_source = "mch"
## parameters
r_threshold = 0.1 # [mm/h]
num_cascade_levels = 6
unit = "mm/h" # mm/h or dBZ
transformation = "dB" # None or dB
adjust_domain = None # None or "square"
# Read-in the data
print('Read the data...')
startdate = datetime.datetime.strptime(startdate_str, "%Y%m%d%H%M")
## import data specifications
ds = stp.rcparams.data_sources[data_source]
## find radar field filenames
input_files = stp.io.find_by_date(startdate, ds.root_path, ds.path_fmt, ds.fn_pattern,
ds.fn_ext, ds.timestep, 0, 0)
## read radar field files
importer = stp.io.get_method(ds.importer, "importer")
R, _, metadata = stp.io.read_timeseries(input_files, importer, **ds.importer_kwargs)
R = R.squeeze() # since this contains just one frame
Rmask = np.isnan(R)
# Prepare input files
print("Prepare the data...")
## if necessary, convert to rain rates [mm/h]
converter = stp.utils.get_method(unit)
n_prvs_times = 3 # use at least 9 with DARTS
n_lead_times = 24
unit = "mm/h" # mm/h or dBZ
transformation = "dB" # None or dB
r_threshold = 0.1 # rain/no-rain threshold [mm/h]
## verification parameters
skill_score = "CSI"
v_threshold = 1 # [mm/h]
# Read-in the data
print('Read the data...')
startdate = datetime.datetime.strptime(startdate_str, "%Y%m%d%H%M")
## import data specifications
ds = stp.rcparams.data_sources[data_source]
## find radar field filenames
input_files = stp.io.find_by_date(startdate, ds.root_path, ds.path_fmt, ds.fn_pattern,
ds.fn_ext, ds.timestep, n_prvs_times, 0)
## read radar field files
importer = stp.io.get_method(ds.importer, "importer")
R, _, metadata = stp.io.read_timeseries(input_files, importer, **ds.importer_kwargs)
Rmask = np.isnan(R)
print("The data array has size [nleadtimes,nrows,ncols] =", R.shape)
# Prepare input files
print("Prepare the data...")
## if necessary, convert to rain rates [mm/h]
converter = stp.utils.get_method("mm/h")
import numpy as np
from pysteps import io, rcparams
from pysteps.utils import conversion, transformation
from scipy.stats import skew
###############################################################################
# Read the radar input images
# ---------------------------
#
# First, we will import the sequence of radar composites.
# You need the pysteps-data archive downloaded and the pystepsrc file
# configured with the data_source paths pointing to data folders.
# Selected case
date = datetime.strptime("201609281600", "%Y%m%d%H%M")
data_source = rcparams.data_sources["fmi"]
###############################################################################
# Load the data from the archive
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
root_path = data_source["root_path"]
path_fmt = data_source["path_fmt"]
fn_pattern = data_source["fn_pattern"]
fn_ext = data_source["fn_ext"]
importer_name = data_source["importer"]
importer_kwargs = data_source["importer_kwargs"]
timestep = data_source["timestep"]
# Get 1 hour of observations in the data archive
fns = io.archive.find_by_date(
#
# First thing, the sequence of Swiss radar composites is imported, converted and
# transformed into units of dBR.
date = datetime.strptime("201701311200", "%Y%m%d%H%M")
data_source = "mch"
# Load data source config
root_path = rcparams.data_sources[data_source]["root_path"]
path_fmt = rcparams.data_sources[data_source]["path_fmt"]
fn_pattern = rcparams.data_sources[data_source]["fn_pattern"]
fn_ext = rcparams.data_sources[data_source]["fn_ext"]
importer_name = rcparams.data_sources[data_source]["importer"]
importer_kwargs = rcparams.data_sources[data_source]["importer_kwargs"]
timestep = rcparams.data_sources[data_source]["timestep"]
# Find the radar files in the archive
fns = io.find_by_date(
date, root_path, path_fmt, fn_pattern, fn_ext, timestep, num_prev_files=2
)
# Read the data from the archive
importer = io.get_method(importer_name, "importer")
R, _, metadata = io.read_timeseries(fns, importer, **importer_kwargs)
# Convert to rain rate
R, metadata = conversion.to_rainrate(R, metadata)
# Upscale data to 2 km to limit memory usage
R, metadata = dimension.aggregate_fields_space(R, metadata, 2000)
ar_order = 2
r_threshold = 0.1 # rain/no-rain threshold [mm/h]
adjust_noise = "auto"
mask_method = "incremental" # sprog, obs or incremental
conditional = False
unit = "mm/h" # mm/h or dBZ
transformation = "dB" # None or dB
adjust_domain = None # None or square
seed = 42 # for reproducibility
# Read-in the data
print('Read the data...')
startdate = datetime.datetime.strptime(startdate_str, "%Y%m%d%H%M")
## import data specifications
ds = stp.rcparams.data_sources[data_source]
## find radar field filenames
input_files = stp.io.find_by_date(startdate, ds.root_path, ds.path_fmt, ds.fn_pattern,
ds.fn_ext, ds.timestep, n_prvs_times, 0)
## read radar field files
importer = stp.io.get_method(ds.importer, "importer")
R, _, metadata = stp.io.read_timeseries(input_files, importer, **ds.importer_kwargs)
Rmask = np.isnan(R)
# Prepare input files
print("Prepare the data...")
## if requested, make sure we work with a square domain
reshaper = stp.utils.get_method(adjust_domain)
R, metadata = reshaper(R, metadata, method="pad")
from pysteps import io, motion, rcparams
from pysteps.utils import conversion, transformation
from pysteps.visualization import plot_precip_field, quiver
###############################################################################
# Read the radar input images
# ---------------------------
#
# First thing, the sequence of radar composites is imported, converted and
# transformed into units of dBR.
date = datetime.strptime("201505151630", "%Y%m%d%H%M")
data_source = "mch"
# Load data source config
root_path = rcparams.data_sources[data_source]["root_path"]
path_fmt = rcparams.data_sources[data_source]["path_fmt"]
fn_pattern = rcparams.data_sources[data_source]["fn_pattern"]
fn_ext = rcparams.data_sources[data_source]["fn_ext"]
importer_name = rcparams.data_sources[data_source]["importer"]
importer_kwargs = rcparams.data_sources[data_source]["importer_kwargs"]
timestep = rcparams.data_sources[data_source]["timestep"]
# Find the input files from the archive
fns = io.archive.find_by_date(
date, root_path, path_fmt, fn_pattern, fn_ext, timestep, num_prev_files=9
)
# Read the radar composites
importer = io.get_method(importer_name, "importer")
R, _, metadata = io.read_timeseries(fns, importer, **importer_kwargs)
###############################################################################
# Read precipitation field
# ------------------------
#
# First thing, the sequence of Swiss radar composites is imported, converted and
# transformed into units of dBR.
date = datetime.strptime("201607112100", "%Y%m%d%H%M")
data_source = "mch"
# Load data source config
root_path = rcparams.data_sources[data_source]["root_path"]
path_fmt = rcparams.data_sources[data_source]["path_fmt"]
fn_pattern = rcparams.data_sources[data_source]["fn_pattern"]
fn_ext = rcparams.data_sources[data_source]["fn_ext"]
importer_name = rcparams.data_sources[data_source]["importer"]
importer_kwargs = rcparams.data_sources[data_source]["importer_kwargs"]
timestep = rcparams.data_sources[data_source]["timestep"]
# Find the radar files in the archive
fns = io.find_by_date(
date, root_path, path_fmt, fn_pattern, fn_ext, timestep, num_prev_files=2
)
# Read the data from the archive
importer = io.get_method(importer_name, "importer")
R, _, metadata = io.read_timeseries(fns, importer, **importer_kwargs)
# Convert to rain rate
R, metadata = conversion.to_rainrate(R, metadata)
# Upscale data to 2 km to limit memory usage
# include only variables that change
if len(experiment.get(key,[None])) > 1 and key.lower() is not "data":
path_to_nwc = os.path.join(path_to_nwc, '-'.join([key, str(item)]))
try:
os.makedirs(path_to_nwc)
except FileExistsError:
pass
# **************************************************************************
# NOWCASTING
# **************************************************************************
# Loop forecasts within given event using the prescribed update cycle interval
## import data specifications
ds = stp.rcparams.data_sources[p["data"][3]]
if p["v_accu"] is None:
p["v_accu"] = ds.timestep
# Loop forecasts for given event
startdate = datetime.datetime.strptime(p["data"][0], "%Y%m%d%H%M")
enddate = datetime.datetime.strptime(p["data"][1], "%Y%m%d%H%M")
countnwc = 0
while startdate + datetime.timedelta(minutes = p["n_lead_times"]*ds.timestep) <= enddate:
# filename of the nowcast netcdf
outfn = os.path.join(path_to_nwc, "%s_nowcast.netcdf" % startdate.strftime("%Y%m%d%H%M"))
## check if results already exists
run_exist = False
if os.path.isfile(outfn):
from pysteps.noise.fftgenerators import initialize_nonparam_2d_fft_filter
from pysteps.noise.fftgenerators import generate_noise_2d_fft_filter
from pysteps.utils import conversion, rapsd, transformation
from pysteps.visualization import plot_precip_field, plot_spectrum1d
###############################################################################
# Read precipitation field
# ------------------------
#
# First thing, the radar composite is imported and transformed in units
# of dB.
# This image will be used to train the Fourier filters that are necessary to
# produce the fields of spatially correlated noise.
# Import the example radar composite
root_path = rcparams.data_sources["mch"]["root_path"]
filename = os.path.join(root_path, "20160711", "AQC161932100V_00005.801.gif")
R, _, metadata = io.import_mch_gif(filename, product="AQC", unit="mm", accutime=5.0)
# Convert to mm/h
R, metadata = conversion.to_rainrate(R, metadata)
# Nicely print the metadata
pprint(metadata)
# Plot the rainfall field
plot_precip_field(R, geodata=metadata)
# Log-transform the data
R, metadata = transformation.dB_transform(R, metadata, threshold=0.1, zerovalue=-15.0)
# Assign the fill value to all the Nans
from pysteps.utils import conversion, transformation
from pysteps.visualization import plot_precip_field, quiver
###############################################################################
# Read the radar input images
# ---------------------------
#
# First thing, the sequence of radar composites is imported, converted and
# transformed into units of dBR.
date = datetime.strptime("201609281600", "%Y%m%d%H%M")
data_source = "fmi"
n_leadtimes = 12
# Load data source config
root_path = rcparams.data_sources[data_source]["root_path"]
path_fmt = rcparams.data_sources[data_source]["path_fmt"]
fn_pattern = rcparams.data_sources[data_source]["fn_pattern"]
fn_ext = rcparams.data_sources[data_source]["fn_ext"]
importer_name = rcparams.data_sources[data_source]["importer"]
importer_kwargs = rcparams.data_sources[data_source]["importer_kwargs"]
timestep = rcparams.data_sources[data_source]["timestep"]
# Find the input files from the archive
fns = io.archive.find_by_date(
date, root_path, path_fmt, fn_pattern, fn_ext, timestep, num_prev_files=2
)
# Read the radar composites
importer = io.get_method(importer_name, "importer")
Z, _, metadata = io.read_timeseries(fns, importer, **importer_kwargs)