CLIMATE-920 Make examples Python 3 compatible
Project: http://git-wip-us.apache.org/repos/asf/climate/repo Commit: http://git-wip-us.apache.org/repos/asf/climate/commit/9bb21700 Tree: http://git-wip-us.apache.org/repos/asf/climate/tree/9bb21700 Diff: http://git-wip-us.apache.org/repos/asf/climate/diff/9bb21700 Branch: refs/heads/master Commit: 9bb2170084ec61cee178c3742cb5a1be709966fe Parents: ec3fdb4 Author: Lewis John McGibbney <lewis.mcgibb...@gmail.com> Authored: Sat Jul 29 13:36:05 2017 -0700 Committer: Lewis John McGibbney <lewis.mcgibb...@gmail.com> Committed: Sat Jul 29 13:36:05 2017 -0700 ---------------------------------------------------------------------- examples/esgf_integration_example.py | 17 +- examples/podaac_integration_example.py | 7 +- examples/simple_model_to_model_bias.py | 14 +- examples/taylor_diagram_example.py | 13 +- examples/time_series_with_regions.py | 21 +- mccsearch/code/mccSearch.py | 6292 +++++++++++++-------------- ocw/data_source/esgf.py | 11 +- ocw/esgf/download.py | 28 +- 8 files changed, 3227 insertions(+), 3176 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/climate/blob/9bb21700/examples/esgf_integration_example.py ---------------------------------------------------------------------- diff --git a/examples/esgf_integration_example.py b/examples/esgf_integration_example.py index 7a02632..8247435 100644 --- a/examples/esgf_integration_example.py +++ b/examples/esgf_integration_example.py @@ -18,14 +18,19 @@ import ocw.data_source.esgf as esgf from getpass import getpass import ssl +import sys if hasattr(ssl, '_create_unverified_context'): ssl._create_default_https_context = ssl._create_unverified_context -dataset_id = 'obs4MIPs.CNES.AVISO.zos.mon.v20110829|esgf-data.jpl.nasa.gov' +dataset_id = 'obs4mips.CNES.AVISO.zos.mon.v20110829|esgf-data.jpl.nasa.gov' variable = 'zosStderr' -username = raw_input('Enter your ESGF OpenID:\n') +if sys.version_info[0] >= 3: + username = input('Enter your ESGF OpenID:\n') +else: + username = raw_input('Enter your ESGF OpenID:\n') + password = getpass(prompt='Enter your ESGF Password:\n') # Multiple datasets are returned in a list if the ESGF dataset is @@ -39,7 +44,7 @@ datasets = esgf.load_dataset(dataset_id, # we only need to look at the 0-th value in the returned list. ds = datasets[0] -print '\n--------\n' -print 'Variable: ', ds.variable -print 'Shape: ', ds.values.shape -print 'A Value: ', ds.values[100][100][100] +print('\n--------\n') +print('Variable: ', ds.variable) +print('Shape: ', ds.values.shape) +print('A Value: ', ds.values[100][100][100]) http://git-wip-us.apache.org/repos/asf/climate/blob/9bb21700/examples/podaac_integration_example.py ---------------------------------------------------------------------- diff --git a/examples/podaac_integration_example.py b/examples/podaac_integration_example.py index 7b8bb10..61663d7 100644 --- a/examples/podaac_integration_example.py +++ b/examples/podaac_integration_example.py @@ -24,8 +24,9 @@ datasetId = 'PODAAC-CCF30-01XXX' variable = 'uwnd' name = 'PO.DAAC_test_dataset' OUTPUT_PLOT = "ccmp_temporal_std" -""" Step 1: Load Local NetCDF Files into OCW Dataset Objects """ -print("Extracting Level4 granule %s and converting it into a OCW dataset object." % datasetId) +""" Step 1: Download remote PO.DAAC Dataset and read it into an OCW Dataset Object""" +print("Available Level4 PO.DAAC Granules: %s" % podaac.list_available_extract_granule_dataset_ids()) +print("Extracting variable '%s' from Level4 granule '%s' and converting it into a OCW dataset object." % (variable, datasetId)) ccmp_dataset = podaac.extract_l4_granule( variable=variable, dataset_id=datasetId, name=name) print("CCMP_Dataset.values shape: (times, lats, lons) - %s \n" % @@ -67,7 +68,7 @@ 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 = "CCMP Temporal Standard Deviation" +plot_title = "Cross-Calibrated Multi-Platform Temporal Standard Deviation" sub_titles = range(2002, 2010, 1) plotter.draw_contour_map(results, lats, lons, fname, http://git-wip-us.apache.org/repos/asf/climate/blob/9bb21700/examples/simple_model_to_model_bias.py ---------------------------------------------------------------------- diff --git a/examples/simple_model_to_model_bias.py b/examples/simple_model_to_model_bias.py index ffa5cda..8e834b6 100644 --- a/examples/simple_model_to_model_bias.py +++ b/examples/simple_model_to_model_bias.py @@ -17,7 +17,15 @@ import datetime from os import path -import urllib +import sys + +if sys.version_info[0] >= 3: + from urllib.request import urlretrieve +else: + # 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 import numpy as np @@ -39,9 +47,9 @@ 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) + urlretrieve(FILE_LEADER + FILE_1, FILE_1_PATH) if not path.exists(FILE_2_PATH): - urllib.urlretrieve(FILE_LEADER + FILE_2, FILE_2_PATH) + 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,)) http://git-wip-us.apache.org/repos/asf/climate/blob/9bb21700/examples/taylor_diagram_example.py ---------------------------------------------------------------------- diff --git a/examples/taylor_diagram_example.py b/examples/taylor_diagram_example.py index 86236c8..8d5bbf0 100644 --- a/examples/taylor_diagram_example.py +++ b/examples/taylor_diagram_example.py @@ -18,7 +18,14 @@ import datetime import sys from os import path -import urllib + +if sys.version_info[0] >= 3: + from urllib.request import urlretrieve +else: + # 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 import numpy @@ -36,10 +43,10 @@ FILE_2 = "AFRICA_UC-WRF311_CTL_ERAINT_MM_50km-rg_1989-2008_tasmax.nc" # Download some example NetCDF files for the evaluation ########################################################################## if not path.exists(FILE_1): - urllib.urlretrieve(FILE_LEADER + FILE_1, FILE_1) + urlretrieve(FILE_LEADER + FILE_1, FILE_1) if not path.exists(FILE_2): - urllib.urlretrieve(FILE_LEADER + FILE_2, FILE_2) + urlretrieve(FILE_LEADER + FILE_2, FILE_2) # Load the example datasets into OCW Dataset objects. We want to load # the 'tasmax' variable values. We'll also name the datasets for use http://git-wip-us.apache.org/repos/asf/climate/blob/9bb21700/examples/time_series_with_regions.py ---------------------------------------------------------------------- diff --git a/examples/time_series_with_regions.py b/examples/time_series_with_regions.py index 05c4721..3bb133c 100644 --- a/examples/time_series_with_regions.py +++ b/examples/time_series_with_regions.py @@ -12,7 +12,15 @@ import datetime import numpy as np import numpy.ma as ma from os import path -import urllib +import sys + +if sys.version_info[0] >= 3: + from urllib.request import urlretrieve +else: + # 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 import ssl if hasattr(ssl, '_create_unverified_context'): ssl._create_default_https_context = ssl._create_unverified_context @@ -29,7 +37,7 @@ LAT_MIN = -45.0 LAT_MAX = 42.24 LON_MIN = -24.0 LON_MAX = 60.0 -START = datetime.datetime(2000, 01, 1) +START = datetime.datetime(2000, 1, 1) END = datetime.datetime(2007, 12, 31) EVAL_BOUNDS = Bounds(lat_min=LAT_MIN, lat_max=LAT_MAX, @@ -48,13 +56,16 @@ region_counter = 0 # Download necessary NetCDF file if not present if not path.exists(FILE_1): - urllib.urlretrieve(FILE_LEADER + FILE_1, FILE_1) + print("Downloading %s" % (FILE_LEADER + FILE_1)) + urlretrieve(FILE_LEADER + FILE_1, FILE_1) if not path.exists(FILE_2): - urllib.urlretrieve(FILE_LEADER + FILE_2, FILE_2) + print("Downloading %s" % (FILE_LEADER + FILE_2)) + urlretrieve(FILE_LEADER + FILE_2, FILE_2) if not path.exists(FILE_3): - urllib.urlretrieve(FILE_LEADER + FILE_3, FILE_3) + print("Downloading %s" % (FILE_LEADER + 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"))