Repository: climate Updated Branches: refs/heads/master 0edac0b1b -> 4929e2cfb
CLIMATE-425 Example for Temporal STD Metric Project: http://git-wip-us.apache.org/repos/asf/climate/repo Commit: http://git-wip-us.apache.org/repos/asf/climate/commit/89f5d0c4 Tree: http://git-wip-us.apache.org/repos/asf/climate/tree/89f5d0c4 Diff: http://git-wip-us.apache.org/repos/asf/climate/diff/89f5d0c4 Branch: refs/heads/master Commit: 89f5d0c4f485f9fbc3b6a296b8608fc6dda9b90f Parents: b6db8f8 Author: Maziyar Boustani <[email protected]> Authored: Thu May 15 15:50:50 2014 -0700 Committer: Maziyar Boustani <[email protected]> Committed: Thu May 15 15:50:50 2014 -0700 ---------------------------------------------------------------------- examples/simple_model_tstd.py | 86 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/climate/blob/89f5d0c4/examples/simple_model_tstd.py ---------------------------------------------------------------------- diff --git a/examples/simple_model_tstd.py b/examples/simple_model_tstd.py new file mode 100644 index 0000000..46ff7ae --- /dev/null +++ b/examples/simple_model_tstd.py @@ -0,0 +1,86 @@ +# 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 +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + + +import urllib + +import ocw.data_source.local as local +import ocw.evaluation as evaluation +import ocw.metrics as metrics +import ocw.plotter as plotter + +# File URL leader +FILE_LEADER = "http://zipper.jpl.nasa.gov/dist/" +# One Local Model Files +FILE_1 = "AFRICA_KNMI-RACMO2.2b_CTL_ERAINT_MM_50km_1989-2008_tasmax.nc" + +# Filename for the output image/plot (without file extension) +OUTPUT_PLOT = "knmi_temporal_std" + +# Download necessary NetCDF file +urllib.urlretrieve(FILE_LEADER + FILE_1, FILE_1) + +""" Step 1: Load Local NetCDF File into OCW Dataset Objects """ +print "Loading %s into an OCW Dataset Object" % (FILE_1,) +# 'tasmax' is variable name of values +knmi_dataset = local.load_file(FILE_1, "tasmax") + +print "KNMI_Dataset.values shape: (times, lats, lons) - %s \n" % (knmi_dataset.values.shape,) + +# Acessing latittudes and longitudes of netCDF file +lats = knmi_dataset.lats +lons = knmi_dataset.lons + +""" Step 2: Build a Metric to use for Evaluation - Temporal STD for this example """ +# You can build your own metrics, but OCW also ships with some common metrics +print "Setting up a Temporal STD metric to use for evaluation" +std = metrics.TemporalStdDev() + +""" Step 3: Create an Evaluation Object using Datasets and our Metric """ +# The Evaluation Class Signature is: +# Evaluation(reference, targets, metrics, subregions=None) +# Evaluation can take in multiple targets and metrics, so we need to convert +# our examples into Python lists. Evaluation will iterate over the lists +print "Making the Evaluation definition" +# Temporal STD Metric gets one target dataset then reference dataset should be None +std_evaluation = evaluation.Evaluation(None, [knmi_dataset], [std]) +print "Executing the Evaluation using the object's run() method" +std_evaluation.run() + +""" Step 4: Make a Plot from the Evaluation.results """ +# The Evaluation.results are a set of nested lists to support many different +# possible Evaluation scenarios. +# +# The Evaluation results docs say: +# The shape of results is (num_metrics, num_target_datasets) if no subregion +# Accessing the actual results when we have used 1 metric and 1 dataset is +# done this way: +print "Accessing the Results of the Evaluation run" +results = std_evaluation.unary_results[0][0] +print "The results are of type: %s" % type(results) + +# From the temporal std output I want to make a Contour Map of the region +print "Generating a contour map using ocw.plotter.draw_contour_map()" + +fname = OUTPUT_PLOT +gridshape = (4, 5) # 20 Years worth of plots. 20 rows in 1 column +plot_title = "TASMAX Temporal Standard Deviation (1989 - 2008)" +sub_titles = range(1989, 2009, 1) + +plotter.draw_contour_map(results, lats, lons, fname, + gridshape=gridshape, ptitle=plot_title, + subtitles=sub_titles)
