blob: 197b9222ebcc66a40605e78561fa50b76d4d5912 [file] [log] [blame]
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
Use OCW to download, normalize and evaluate three datasets
against a reference dataset and OCW standard metrics
drawing a Taylor diagram of the results of the evaluation.
In this example:
1. Download three netCDF files from a local site.
2. Load the local files into OCW dataset objects.
3. Process each dataset to the same same shape.
a.) Restrict the datasets re: geographic and time boundaries.
b.) Temporally rebin the data (monthly).
c.) Spatially regrid each dataset.
4. Extract the metrics used for the evaluation and evaluate
against a reference dataset and standard OCW metrics.
5. Draw evaluation results Taylor diagram.
OCW modules demonstrated:
1. datasource/local
2. dataset
3. dataset_processor
4. evaluation
5. metrics
6. plotter
7. utils
from __future__ import print_function
import datetime
import ssl
import sys
from os import path
import numpy as np
import ocw.data_source.local as local
import ocw.data_source.rcmed as rcmed
import ocw.dataset_processor as dsp
import ocw.evaluation as evaluation
import ocw.metrics as metrics
import ocw.plotter as plotter
import ocw.utils as utils
from ocw.dataset import Bounds
if sys.version_info[0] >= 3:
from urllib.request import urlretrieve
# Not Python 3 - today, it is most likely to be Python 2
# But note that this might need an update when Python 4
# might be around one day
from urllib import urlretrieve
if hasattr(ssl, '_create_unverified_context'):
ssl._create_default_https_context = ssl._create_unverified_context
# File URL leader
# Three Local Model Files
FILE_1 = ""
FILE_2 = ""
FILE_3 = ""
# Filename for the output image/plot (without file extension)
OUTPUT_PLOT = "pr_africa_taylor"
# Spatial and temporal configurations
LAT_MIN = -45.0
LAT_MAX = 42.24
LON_MIN = -24.0
LON_MAX = 60.0
START = datetime.datetime(2000, 1, 1)
END = datetime.datetime(2007, 12, 31)
EVAL_BOUNDS = Bounds(lat_min=LAT_MIN, lat_max=LAT_MAX,
lon_min=LON_MIN, lon_max=LON_MAX, start=START, end=END)
# variable that we are analyzing
varName = 'pr'
# regridding parameters
gridLonStep = 0.5
gridLatStep = 0.5
# some vars for this evaluation
target_datasets_ensemble = []
target_datasets = []
ref_datasets = []
# Download necessary NetCDF file if not present
if not path.exists(FILE_1):
urlretrieve(FILE_LEADER + FILE_1, FILE_1)
if not path.exists(FILE_2):
urlretrieve(FILE_LEADER + FILE_2, FILE_2)
if not path.exists(FILE_3):
urlretrieve(FILE_LEADER + FILE_3, FILE_3)
# Step 1: Load Local NetCDF File into OCW Dataset Objects and store in list.
target_datasets.append(local.load_file(FILE_1, varName, name="KNMI"))
target_datasets.append(local.load_file(FILE_2, varName, name="REGM3"))
target_datasets.append(local.load_file(FILE_3, varName, name="UCT"))
# Step 2: Fetch an OCW Dataset Object from the data_source.rcmed module.
print("Working with the rcmed interface to get CRU3.1 Monthly Mean Precipitation")
# the dataset_id and the parameter id were determined from
CRU31 = rcmed.parameter_dataset(
# Step 3: Resample Datasets so they are the same shape.
print("Resampling datasets ...")
print("... on units")
CRU31 = dsp.water_flux_unit_conversion(CRU31)
print("... temporal")
CRU31 = dsp.temporal_rebin(CRU31, temporal_resolution='monthly')
for member, each_target_dataset in enumerate(target_datasets):
target_datasets[member] =\
target_datasets[member] =\
dsp.temporal_rebin(target_datasets[member], temporal_resolution='monthly')
target_datasets[member] = dsp.subset(target_datasets[member], EVAL_BOUNDS)
# Regrid
print("... regrid")
new_lats = np.arange(LAT_MIN, LAT_MAX, gridLatStep)
new_lons = np.arange(LON_MIN, LON_MAX, gridLonStep)
CRU31 = dsp.spatial_regrid(CRU31, new_lats, new_lons)
for member, each_target_dataset in enumerate(target_datasets):
target_datasets[member] = dsp.spatial_regrid(
target_datasets[member], new_lats, new_lons)
# find the mean values
# way to get the mean. Note the function exists in as def
# calc_climatology_year(dataset):
CRU31.values = utils.calc_temporal_mean(CRU31)
# make the model ensemble
target_datasets_ensemble = dsp.ensemble(target_datasets) = "ENS"
# append to the target_datasets for final analysis
for member, each_target_dataset in enumerate(target_datasets):
target_datasets[member].values =\
allNames = []
for target in target_datasets:
# calculate the metrics
taylor_diagram = metrics.SpatialPatternTaylorDiagram()
# create the Evaluation object
RCMs_to_CRU_evaluation = evaluation.Evaluation(CRU31, # Reference dataset for the evaluation
# 1 or more target datasets for
# the evaluation
# 1 or more metrics to use in
# the evaluation
taylor_data = RCMs_to_CRU_evaluation.results[0]