33Optical flow
44============
55
6- This tutorial offers a short overview to the optical flow routines available in
6+ This tutorial offers a short overview of the optical flow routines available in
77pysteps and it will cover how to compute and plot the motion field from a
88sequence of radar images.
99
1010"""
1111
1212from datetime import datetime
13- from matplotlib import pyplot
1413import numpy as np
1514from pprint import pprint
1615from pysteps import io , motion , rcparams
2120# Read the radar input images
2221# ---------------------------
2322#
24- # First thing, the sequence of radar composites is imported, converted and
23+ # First thing, the sequence of radar composites is imported, converted and
2524# transformed into units of dBR.
2625
2726date = datetime .strptime ("201505151630" , "%Y%m%d%H%M" )
2827data_source = "mch"
28+
29+ # Load data source config
2930root_path = rcparams .data_sources [data_source ]["root_path" ]
3031path_fmt = rcparams .data_sources [data_source ]["path_fmt" ]
3132fn_pattern = rcparams .data_sources [data_source ]["fn_pattern" ]
3233fn_ext = rcparams .data_sources [data_source ]["fn_ext" ]
3334importer_name = rcparams .data_sources [data_source ]["importer" ]
3435importer_kwargs = rcparams .data_sources [data_source ]["importer_kwargs" ]
36+ timestep = rcparams .data_sources [data_source ]["timestep" ]
3537
3638# Find the input files from the archive
37- fns = io .archive .find_by_date (date , root_path , path_fmt , fn_pattern , fn_ext , timestep = 5 , num_prev_files = 9 )
39+ fns = io .archive .find_by_date (
40+ date , root_path , path_fmt , fn_pattern , fn_ext , timestep , num_prev_files = 9
41+ )
3842
3943# Read the radar composites
4044importer = io .get_method (importer_name , "importer" )
4448R , metadata = conversion .to_rainrate (R , metadata )
4549
4650# Store the last frame for polotting it later later
47- R_ = R [- 1 , : , :].copy ()
51+ R_ = R [- 1 , :, :].copy ()
4852
4953# Log-transform the data
5054R , metadata = transformation .dB_transform (R , metadata , threshold = 0.1 , zerovalue = - 15.0 )
5761# -----------------
5862#
5963# The Lucas-Kanade optical flow method implemented in pysteps is a local
60- # tracking apporach that relies on the OpenCV package.
64+ # tracking approach that relies on the OpenCV package.
6165# Local features are tracked in a sequence of two or more radar images. The
62- # scheme includes a final interpolation step in order to produce a smooth
66+ # scheme includes a final interpolation step in order to produce a smooth
6367# field of motion vectors.
6468
6569oflow_method = motion .get_method ("LK" )
6670V1 = oflow_method (R [- 3 :, :, :])
6771
6872# Plot the motion field
69- plot_precip_field (R_ , geodata = metadata , title = "Lucas-Kanade " )
73+ plot_precip_field (R_ , geodata = metadata , title = "LK " )
7074quiver (V1 , geodata = metadata , step = 25 )
7175
7276###############################################################################
7377# Variational echo tracking (VET)
7478# -------------------------------
7579#
76- # This module implements the VET algorithm presented
77- # by Laroche and Zawadzki (1995) and used in the McGill Algorithm for
78- # Prediction by Lagrangian Extrapolation (MAPLE) described in
80+ # This module implements the VET algorithm presented
81+ # by Laroche and Zawadzki (1995) and used in the McGill Algorithm for
82+ # Prediction by Lagrangian Extrapolation (MAPLE) described in
7983# Germann and Zawadzki (2002).
8084# The approach essentially consists of a global optimization routine that seeks
81- # at minimizing a cost function between the displaced and the reference image.
85+ # at minimizing a cost function between the displaced and the reference image.
8286
8387oflow_method = motion .get_method ("VET" )
8488V2 = oflow_method (R [- 3 :, :, :])
8589
8690# Plot the motion field
87- plot_precip_field (R_ , geodata = metadata , title = "Variational echo tracking " )
91+ plot_precip_field (R_ , geodata = metadata , title = "VET " )
8892quiver (V2 , geodata = metadata , step = 25 )
8993
9094###############################################################################
9195# Dynamic and adaptive radar tracking of storms (DARTS)
9296# -----------------------------------------------------
9397#
9498# DARTS uses a spectral approach to optical flow that is based on the discrete
95- # Fourier transform (DFT) of a temporal sequence of radar fields.
96- # The level of truncation of the DFT coefficients controls the degree of
97- # smoothness of the estimated motion field, allowing for an efficient
98- # motion estimation. DARTS requires a longer sequence of radar fields for
99+ # Fourier transform (DFT) of a temporal sequence of radar fields.
100+ # The level of truncation of the DFT coefficients controls the degree of
101+ # smoothness of the estimated motion field, allowing for an efficient
102+ # motion estimation. DARTS requires a longer sequence of radar fields for
99103# estimating the motion, here we are going to use all the available 10 fields.
100104
101105oflow_method = motion .get_method ("DARTS" )
102106R [~ np .isfinite (R )] = metadata ["zerovalue" ]
103- V3 = oflow_method (R ) # needs longer training sequence
107+ V3 = oflow_method (R ) # needs longer training sequence
104108
105109# Plot the motion field
106110plot_precip_field (R_ , geodata = metadata , title = "DARTS" )
107111quiver (V3 , geodata = metadata , step = 25 )
108112
109- # sphinx_gallery_thumbnail_number = 1
113+ # sphinx_gallery_thumbnail_number = 1
0 commit comments