Examples¶
Current example code is located on GitLab: WaveVal examples.
The example scripts demonstrate how the WaveVal tools can be used to process a set of ResourceCode hindcast model
data extracts against CMEMS InSituTAC wavebuoy data. The data for these example cases are included in the ./examples/data subdirectory. The model extract data are in ./data/RSCD_HINDCAST/2017 and the corresponding wavebuoy
data are in the ./data/INSITU_GLO_WAVE/MO subdirectory. For Example 1 and Example 2 the 1-D frequency spectra
data store in the /FREQ_NC subdirectory within each month directories (e.g. ./data/RSCD_HINDCAST/2017/01/FREQ_NC
contains the 1-D spectral data for January 2017).
Example 1 shows how to create a matchup database for a set of model and wavebuoy records and how to convert the
database records to a CSV file that can be used by the Validation module.
Example 2 shows how to generate validation statistics for the records in a CSV input file. This example uses the output
from Example 1, but the corresponding file is included in the ./data/MATCHUPDB subdirectory.
Note
The example scripts use absolute paths, so the examples must be run from within the ./examples subdirectory.
Example 1: Create a matchup database¶
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3
4# ----------------------------------------------------------------------------
5# IMPORTS
6# ----------------------------------------------------------------------------
7from waveval import MatchUpDatabase as mdb
8
9# ----------------------------------------------------------------------------
10# Exampl 1: Build Match Up Database
11# ----------------------------------------------------------------------------
12
13# ========================= Define Database ===============================
14db_path = './data/MATCHUPDB'
15db_name = 'rscd_matchup'
16
17# ==================== Construct File Filter ==============================
18scover = mdb.spatialCoverage(36.00104, 62.99896, -11.998958, 13.489299)
19bfilt = mdb.recordFilter([scover])
20
21# ===================== Construct Match-up Database =======================
22modDataPath = './data/RSCD_HINDCAST/2017'
23modDataset = 'FREQ_NC'
24months = []
25
26obsDataPath = './data/INSITU_GLO_WAVE/MO'
27obsDataset = 'TS'
28obsDataFmt = 'InSituTAC'
29
30rscd_cnt, buoy_cnt = mdb.construct_rscd_mdb(
31 db_path, db_name,
32 modDataPath, modDataset, months,
33 obsDataPath, obsDataset, obsDataFmt,
34 bfilt)
35
36print('MatchUp DB Created:', rscd_cnt, ' model records,',
37 buoy_cnt, 'buoy records added.')
38
39# =============== Create Matched Records CSV File =========================
40csv_path = db_path
41csv_fname = db_name+'.csv'
42mdb.db_match_to_csv(db_path, db_name, csv_path, csv_fname)
43
44records = mdb.csv2tuples(csv_path+'/'+csv_fname)
45
46# Display records to be processed
47print(records)
Example 2: Calculate validation parameters¶
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3
4# ----------------------------------------------------------------------------
5# IMPORTS
6# ----------------------------------------------------------------------------
7from waveval.MatchUpDatabase import csv2tuples, getPlatformRecords, getPlatformList
8from waveval.Validation import validate_records
9from waveval.Validation import save_tabulated_results
10
11# ----------------------------------------------------------------------------
12# Example 2: Calculate validation statistics for match records
13# ----------------------------------------------------------------------------
14
15# ================= Read Matched Records CSV File ===========================
16csv_path = './data/MATCHUPDB'
17csv_fname = 'rscd_matchup.csv'
18# Load matched data records from CSV file
19matched_records = csv2tuples(csv_path+'/'+csv_fname)
20# Get list of uniques platform names
21platforms = getPlatformList(matched_records)
22# Define wavebuoy netCDF data standard used
23buoyFmt = 'InSituTAC'
24
25# ===================== Process Each Platform ===============================
26for platform in platforms:
27 # Extract matched data records for current platform
28 records = getPlatformRecords(matched_records,platform)
29
30 # ========================= Process Record ==============================
31 fields = [0,1]
32 for field in fields:
33 # Process match up records
34 # Choose variable to process
35 if field == 0:
36 oVarName = 'Hm0' # Significant wave height
37 if field == 1:
38 oVarName = 'Tp' # Peak wave period
39 if field == 2:
40 oVarName = 'Tm02' # Mean zero crossing period
41 if field == 3:
42 oVarName = 'Dir' # Wave direction
43 if field == 4:
44 oVarName = 'Spr' # Wave directional spreading
45
46 # Set MDB variable names
47 if oVarName == 'Hm0':
48 mVarName = 'hs'
49 varOptions = ['VHM0']
50 elif oVarName == 'Tp':
51 mVarName = 'fp'
52 varOptions = ['VTPK']
53 elif oVarName == 'Tm02':
54 mVarName = 'f02'
55 varOptions = ['VTM02']
56 elif oVarName == 'Dir':
57 mVarName = 'dir'
58 varOptions = ['VPED']
59 elif oVarName == 'Spr':
60 mVarName = 'spr'
61 varOptions = ['VPSP']
62
63 # ================== Generate Validation Stats =======================
64 results_dir = './data/VALIDATION'
65 plot_results = True
66 n_valid, valid_stats = validate_records(records,
67 buoyFmt, platform,
68 mVarName, varOptions,
69 oVarName,
70 results_dir,
71 plot_results)
72
73 # ================== Display Tabulated Results =======================
74 if n_valid > 0:
75 save_tabulated_results(valid_stats, platform, oVarName, results_dir)
76 else:
77 print('No validations statistics generated from match up records.')
78