Repository: climate Updated Branches: refs/heads/master fe190b636 -> 6b24b8d18
To use time information of an OCW dataset object, calc_climatology_season needs to be in dataset_processor. Project: http://git-wip-us.apache.org/repos/asf/climate/repo Commit: http://git-wip-us.apache.org/repos/asf/climate/commit/30d9ff48 Tree: http://git-wip-us.apache.org/repos/asf/climate/tree/30d9ff48 Diff: http://git-wip-us.apache.org/repos/asf/climate/diff/30d9ff48 Branch: refs/heads/master Commit: 30d9ff48ce1fc663ed5a4ebac8e23a1a1123cf6d Parents: 6ab02d8 Author: huikyole <[email protected]> Authored: Tue May 12 01:15:17 2015 -0700 Committer: huikyole <[email protected]> Committed: Tue May 12 01:15:17 2015 -0700 ---------------------------------------------------------------------- .../simple_model_to_model_bias_DJF_and_JJA.py | 64 ++++++++++++++++++++ ocw/dataset_processor.py | 35 +++++++++++ ocw/utils.py | 20 +++--- 3 files changed, 108 insertions(+), 11 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/climate/blob/30d9ff48/examples/simple_model_to_model_bias_DJF_and_JJA.py ---------------------------------------------------------------------- diff --git a/examples/simple_model_to_model_bias_DJF_and_JJA.py b/examples/simple_model_to_model_bias_DJF_and_JJA.py new file mode 100644 index 0000000..36d10c3 --- /dev/null +++ b/examples/simple_model_to_model_bias_DJF_and_JJA.py @@ -0,0 +1,64 @@ +# 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 datetime +from os import path +import urllib + +import numpy as np + +import ocw.data_source.local as local +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 + +# File URL leader +FILE_LEADER = "http://zipper.jpl.nasa.gov/dist/" +# Two Local Model Files +FILE_1 = "AFRICA_KNMI-RACMO2.2b_CTL_ERAINT_MM_50km_1989-2008_tasmax.nc" +FILE_2 = "AFRICA_UC-WRF311_CTL_ERAINT_MM_50km-rg_1989-2008_tasmax.nc" +# Filename for the output image/plot (without file extension) +OUTPUT_PLOT = "wrf_bias_compared_to_knmi" + +FILE_1_PATH = path.join('/tmp', FILE_1) +FILE_2_PATH = path.join('/tmp', FILE_2) + +if not path.exists(FILE_1_PATH): + urllib.urlretrieve(FILE_LEADER + FILE_1, FILE_1_PATH) +if not path.exists(FILE_2_PATH): + urllib.urlretrieve(FILE_LEADER + FILE_2, FILE_2_PATH) + +""" Step 1: Load Local NetCDF Files into OCW Dataset Objects """ +print("Loading %s into an OCW Dataset Object" % (FILE_1_PATH,)) +knmi_dataset = local.load_file(FILE_1_PATH, "tasmax") +print("KNMI_Dataset.values shape: (times, lats, lons) - %s \n" % (knmi_dataset.values.shape,)) + +print("Loading %s into an OCW Dataset Object" % (FILE_2_PATH,)) +wrf_dataset = local.load_file(FILE_2_PATH, "tasmax") +print("WRF_Dataset.values shape: (times, lats, lons) - %s \n" % (wrf_dataset.values.shape,)) + +""" Step 2: Calculate seasonal average """ +print("Calculate seasonal average") +knmi_DJF, knmi_DJF_mean = dsp.calc_climatology_season(month_start=12, month_end=2, dataset=knmi_dataset) +wrf_DJF, wrf_DJF_mean = dsp.calc_climatology_season(month_start=12, month_end=2, dataset=wrf_dataset) +print("Seasonally averaged KNMI_Dataset.values shape: (times, lats, lons) - %s \n" % (knmi_DJF.values.shape,)) +print("Seasonally averaged wrf_Dataset.values shape: (times, lats, lons) - %s \n" % (wrf_DJF.values.shape,)) +knmi_JJA, knmi_JJA_mean = dsp.calc_climatology_season(month_start=6, month_end=8, dataset=knmi_dataset) +wrf_JJA, wrf_JJA_mean = dsp.calc_climatology_season(month_start=6, month_end=8, dataset=wrf_dataset) + http://git-wip-us.apache.org/repos/asf/climate/blob/30d9ff48/ocw/dataset_processor.py ---------------------------------------------------------------------- diff --git a/ocw/dataset_processor.py b/ocw/dataset_processor.py index f600429..469eb76 100644 --- a/ocw/dataset_processor.py +++ b/ocw/dataset_processor.py @@ -29,6 +29,41 @@ import logging logger = logging.getLogger(__name__) +def temporal_subset(target_dataset, month_index): + """ Temporally subset data given month_index. + + :param target_dataset: Dataset object that needs temporal subsetting + :type target_dataset: Open Climate Workbench Dataset Object + :param month_index: an integer array of subset months (June ~ August: [6, 7, 8]) + + :returns: A temporal subset OCW Dataset + :rtype: Open Climate Workbench Dataset Object + """ + + dates = target_dataset.times + months = np.array([d.month for d in dates]) + time_index = [] + for m_value in month_index: + time_index = np.append(time_index, np.where(months == m_value)[0]) + if m_value == month_index[0]: + time_index_first = np.min(np.where(months == m_value)[0]) + if m_value == month_index[-1]: + time_index_last = np.max(np.where(months == m_value)[0]) + + time_index = np.sort(time_index) + + time_index = time_index[np.where((time_index >= time_index_first) & (time_index <= time_index_last))] + + time_index = list(time_index) + + new_dataset = ds.Dataset(target_dataset.lats, + target_dataset.lons, + target_dataset.times[time_index], + target_dataset.values[time_index,:], + target_dataset.variable, + target_dataset.name) + return new_dataset + def temporal_rebin(target_dataset, temporal_resolution): """ Rebin a Dataset to a new temporal resolution http://git-wip-us.apache.org/repos/asf/climate/blob/30d9ff48/ocw/utils.py ---------------------------------------------------------------------- diff --git a/ocw/utils.py b/ocw/utils.py index e8a004c..607b218 100644 --- a/ocw/utils.py +++ b/ocw/utils.py @@ -24,6 +24,7 @@ import datetime from mpl_toolkits.basemap import shiftgrid from dateutil.relativedelta import relativedelta +import ocw.dataset_processor as dsp def decode_time_values(dataset, time_var_name): ''' Decode NetCDF time values into Python datetime objects. @@ -292,7 +293,7 @@ def calc_climatology_season(month_start, month_end, dataset): :param month_end: An integer for ending month (Jan=1) :type month_end: :class:`int` - :param dataset: Dataset object with full-year format + :param dataset: OCW dataset object with full-year format :type dataset: :class:`dataset.Dataset` :returns: t_series - monthly average over the given season @@ -301,17 +302,14 @@ def calc_climatology_season(month_start, month_end, dataset): ''' if month_start > month_end: - # Offset the original array so that the the first month - # becomes month_start, note that this cuts off the first year of data - offset = slice(month_start - 1, month_start - 13) - reshape_data = reshape_monthly_to_annually(dataset[offset]) - month_index = slice(0, 13 - month_start + month_end) + month_index = range(month_start,13) + month_index.extend(range(1, month_end+1)) else: - # Since month_start <= month_end, just take a slice containing those months - reshape_data = reshape_monthly_to_annually(dataset) - month_index = slice(month_start - 1, month_end) - - t_series = reshape_data[:, month_index].mean(axis=1) + month_index = range(month_start, month_end+1) + + # t_series only includes data of months in month_index + t_series = dsp.temporal_subset(dataset, month_index).values + print dsp.temporal_subset(dataset, month_index).times means = t_series.mean(axis=0) return t_series, means
