Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

Commit

Permalink
CLIMATE-920 Make examples Python 3 compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
lewismc committed Jul 29, 2017
1 parent ec3fdb4 commit 9bb2170
Show file tree
Hide file tree
Showing 8 changed files with 3,227 additions and 3,176 deletions.
17 changes: 11 additions & 6 deletions examples/esgf_integration_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -39,7 +44,7 @@
# 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])
7 changes: 4 additions & 3 deletions examples/podaac_integration_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@
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" %
Expand Down Expand Up @@ -67,7 +68,7 @@

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,
Expand Down
14 changes: 11 additions & 3 deletions examples/simple_model_to_model_bias.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -39,9 +47,9 @@
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,))
Expand Down
13 changes: 10 additions & 3 deletions examples/taylor_diagram_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -36,10 +43,10 @@
# 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
Expand Down
21 changes: 16 additions & 5 deletions examples/time_series_with_regions.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,15 @@
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
Expand All @@ -29,7 +37,7 @@
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,
Expand All @@ -48,13 +56,16 @@

# 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"))
Expand Down
Loading

0 comments on commit 9bb2170

Please sign in to comment.