Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files
Resolve merge conflict with CLIMATE-827
  • Loading branch information
agoodm committed Aug 4, 2016
2 parents 711f86b + 91ec7f3 commit 9797d97152a2cc8bcd46a0d05ff5c56645026bda
Show file tree
Hide file tree
Showing 28 changed files with 372 additions and 434 deletions.
@@ -1,3 +1,4 @@
include distribute_setup.py
recursive-include docs *.txt
recursive-include ocw/shape *
include README.md
@@ -634,7 +634,7 @@ def run_screen(model_datasets, models_info, observations_info,
screen.addstr(4, 4, "--> Data retrieved.")
screen.refresh()

EVAL_BOUNDS = Bounds(overlap_min_lat, overlap_max_lat, overlap_min_lon, overlap_max_lon, overlap_start_time, overlap_end_time)
EVAL_BOUNDS = Bounds(lat_min=overlap_min_lat, lat_max=overlap_max_lat, lon_min=overlap_min_lon, lon_max=overlap_max_lon, start=overlap_start_time, end=overlap_end_time)

screen.addstr(5, 4, "Temporally regridding...")
screen.refresh()
@@ -141,7 +141,7 @@
max_lat = np.min([max_lat, obs_dataset.lats.max()])
min_lon = np.max([min_lon, obs_dataset.lons.min()])
max_lon = np.min([max_lon, obs_dataset.lons.max()])
bounds = Bounds(min_lat, max_lat, min_lon, max_lon, start_time, end_time)
bounds = Bounds(lat_min=min_lat, lat_max=max_lat, lon_min=min_lon, lon_max=max_lon, start=start_time, end=end_time)

obs_dataset = dsp.subset(obs_dataset, bounds)
if obs_dataset.temporal_resolution() != temporal_resolution:
@@ -111,7 +111,7 @@
print("Our two datasets have a mis-match in time. We will subset on time to %s years\n" % YEARS)

# Create a Bounds object to use for subsetting
new_bounds = Bounds(min_lat, max_lat, min_lon, max_lon, start_time, end_time)
new_bounds = Bounds(lat_min=min_lat, lat_max=max_lat, lon_min=min_lon, lon_max=max_lon, start=start_time, end=end_time)
knmi_dataset = dsp.subset(knmi_dataset, new_bounds)

print("CRU31_Dataset.values shape: (times, lats, lons) - %s" % (cru31_dataset.values.shape,))
@@ -111,7 +111,7 @@
print("Our two datasets have a mis-match in time. We will subset on time to %s years\n" % YEARS)

# Create a Bounds object to use for subsetting
new_bounds = Bounds(min_lat, max_lat, min_lon, max_lon, start_time, end_time)
new_bounds = Bounds(lat_min=min_lat, lat_max=max_lat, lon_min=min_lon, lon_max=max_lon, start=start_time, end=end_time)
knmi_dataset = dsp.subset(knmi_dataset, new_bounds)

print("CRU31_Dataset.values shape: (times, lats, lons) - %s" % (cru31_dataset.values.shape,))
@@ -128,7 +128,7 @@
# Running Temporal Rebin early helps negate the issue of datasets being on different
# days of the month (1st vs. 15th)
# Create a Bounds object to use for subsetting
new_bounds = Bounds(min_lat, max_lat, min_lon, max_lon, start_time, end_time)
new_bounds = Bounds(lat_min=min_lat, lat_max=max_lat, lon_min=min_lon, lon_max=max_lon, start=start_time, end=end_time)

# Subset our model datasets so they are the same size
knmi_dataset = dsp.subset(knmi_dataset, new_bounds)
@@ -50,7 +50,7 @@
LON_MAX = 60.0
START = datetime.datetime(2000, 1, 1)
END = datetime.datetime(2007, 12, 31)
EVAL_BOUNDS = Bounds(LAT_MIN, LAT_MAX, LON_MIN, LON_MAX, START, END)
EVAL_BOUNDS = Bounds(lat_min=LAT_MIN, lat_max=LAT_MAX, lon_min=LON_MIN, lon_max=LON_MAX, start=START, end=END)

#regridding parameters
gridLonStep=0.5
@@ -34,7 +34,7 @@
LON_MAX = 60.0
START = datetime.datetime(2000, 01, 1)
END = datetime.datetime(2007, 12, 31)
EVAL_BOUNDS = Bounds(LAT_MIN, LAT_MAX, LON_MIN, LON_MAX, START, END)
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'
@@ -61,7 +61,7 @@
# We're only going to run this evaluation over a years worth of data. We'll
# make a Bounds object and use it to subset our datasets.
################################################################################
subset = Bounds(-45, 42, -24, 60, datetime.datetime(1989, 1, 1), datetime.datetime(1989, 12, 1))
subset = Bounds(lat_min=-45, lat_max=42, lon_min=-24, lon_max=60, start=datetime.datetime(1989, 1, 1), end=datetime.datetime(1989, 12, 1))
knmi_dataset = dsp.subset(knmi_dataset, subset)
wrf_dataset = dsp.subset(wrf_dataset, subset)

@@ -32,7 +32,7 @@
START = datetime.datetime(2000, 01, 1)
END = datetime.datetime(2007, 12, 31)

EVAL_BOUNDS = Bounds(LAT_MIN, LAT_MAX, LON_MIN, LON_MAX, START, END)
EVAL_BOUNDS = Bounds(lat_min=LAT_MIN, lat_max=LAT_MAX, lon_min=LON_MIN, lon_max=LON_MAX, start=START, end=END)

varName = 'pr'
gridLonStep=0.44
@@ -350,7 +350,7 @@ def load_multiple_files(file_path,
datasets.append(load_file(filename, variable_name, variable_unit, name=data_name[ifile],
lat_name=lat_name, lon_name=lon_name, time_name=time_name))

return datasets, data_name
return datasets

def load_WRF_2d_files_RAIN(file_path=None,
filename_pattern=None,
@@ -22,10 +22,14 @@
for operations on a Dataset.
'''

import os
import numpy
import logging
import datetime as dt
from mpl_toolkits.basemap import Basemap
import netCDF4

import ocw
import ocw.utils as utils

logger = logging.getLogger(__name__)
@@ -231,18 +235,35 @@ class Bounds(object):
correct functioning. Bounds guarantees that a function receives well
formed information without the need to do the validation manually.
Spatial and temporal bounds must follow the following guidelines.
boundary_type may be one of the following:
* 'rectangular'
* 'CORDEX (CORDEX region name)': pre-defined CORDEX boundary
* 'us_states': an array of US states abbreviation is required (ex) us_states = ['CA','NV'])
* 'countries': an array of county names is required (ex) countries = ['United States','Canada','Mexico']
* 'user': user_mask_file in a netCDF format with two dimensional mask variable is required.
If boundary_type == 'rectangular', spatial and temporal bounds must follow the following guidelines.
* Latitude values must be in the range [-90, 90]
* Longitude values must be in the range [-180, 180]
* Lat/Lon Min values must be less than the corresponding Lat/Lon Max
values.
* Temporal bounds must a valid datetime object
Temporal bounds must a valid datetime object
'''

def __init__(self, lat_min, lat_max, lon_min,
lon_max, start=None, end=None):
def __init__(self, boundary_type='rectangular',
us_states=None, countries=None,
user_mask_file=None, mask_variable_name=None, longitude_name=None, latitude_name=None,
lat_min=-90, lat_max=90, lon_min=-180, lon_max=180,
start=None, end=None):
'''Default Bounds constructor
:param boundary_type: The type of spatial subset boundary.
:type boundary_type: :mod:`string'
:param lat_min: The minimum latitude bound.
:type lat_min: :class:`float`
:param lat_min: The minimum latitude bound.
:type lat_min: :class:`float`
@@ -265,11 +286,7 @@ def __init__(self, lat_min, lat_max, lon_min,
:raises: ValueError
'''
self._lat_min = float(lat_min)
self._lat_max = float(lat_max)
self._lon_min = float(lon_min)
self._lon_max = float(lon_max)

self.boundary_type = boundary_type
if start:
self._start = start
else:
@@ -280,57 +297,44 @@ def __init__(self, lat_min, lat_max, lon_min,
else:
self._end = None

@property
def lat_min(self):
return self._lat_min

@lat_min.setter
def lat_min(self, value):
if not (-90 <= value <= 90 and value < self._lat_max):
error = "Attempted to set lat_min to invalid value: %s" % (value)
logger.error(error)
raise ValueError(error)

self._lat_min = value

@property
def lat_max(self):
return self._lat_max

@lat_max.setter
def lat_max(self, value):
if not (-90 <= value <= 90 and value > self._lat_min):
error = "Attempted to set lat_max to invalid value: %s" % (value)
logger.error(error)
raise ValueError(error)

self._lat_max = value

@property
def lon_min(self):
return self._lon_min

@lon_min.setter
def lon_min(self, value):
if not (-180 <= value <= 180 and value < self._lon_max):
error = "Attempted to set lon_min to invalid value: %s" % (value)
logger.error(error)
raise ValueError(error)

self._lon_min = value

@property
def lon_max(self):
return self._lon_max

@lon_max.setter
def lon_max(self, value):
if not (-180 <= value <= 180 and value > self._lon_min):
error = "Attempter to set lon_max to invalid value: %s" % (value)
logger.error(error)
raise ValueError(error)
if boundary_type == 'us_states':
self.masked_regions = shapefile_boundary(boundary_type, us_states)
if boundary_type == 'countries':
self.masked_regions = shapefile_boundary(boundary_type, countries)
if boundary_type == 'user':
file_object = netCDF4.Dataset(user_mask_file)
self.mask_variable = file_object.variables[mask_variable_name][:]
mask_longitude = file_object.variables[longitude_name][:]
mask_latitude = file_object.variables[latitude_name][:]
if mask_longitude.ndim == 1 and mask_latitude.ndim == 1:
self.mask_longitude, self.mask_latitude = numpy.meshgrid(mask_longitude, mask_latitude)
elif mask_longitude.ndim == 2 and mask_latitude.ndim == 2:
self.mask_longitude = mask_longitude
self.mask_latitude = mask_latitude
if boundary_type == 'rectangular':
if not (-90 <= float(lat_min) <=90) or float(lat_min) > float(lat_max):
error = "Attempted to set lat_min to invalid value: %s" % (lat_min)
logger.error(error)
raise ValueError(error)
if not (-90 <= float(lat_max) <=90):
error = "Attempted to set lat_max to invalid value: %s" % (lat_max)
logger.error(error)
raise ValueError(error)
if not (-180 <= float(lon_min) <=180) or float(lon_min) > float(lon_max):
error = "Attempted to set lon_min to invalid value: %s" % (lon_min)
logger.error(error)
raise ValueError(error)
if not (-180 <= float(lon_max) <=180):
error = "Attempted to set lat_max to invalid value: %s" % (lon_max)
logger.error(error)
raise ValueError(error)

self._lon_max = value
self.lat_min = float(lat_min)
self.lat_max = float(lat_max)
self.lon_min = float(lon_min)
self.lon_max = float(lon_max)
if boundary_type[:6].upper() == 'CORDEX':
self.lat_min, self.lat_max, self.lon_min, self.lon_max = CORDEX_boundary(boundary_type[6:].replace(" ","").lower())

@property
def start(self):
@@ -360,20 +364,62 @@ def end(self, value):

self._end = value

def __str__(self):
lat_range = "({}, {})".format(self._lat_min, self._lat_max)
lon_range = "({}, {})".format(self._lon_min, self._lon_max)
temporal_boundaries = "({}, {})".format(self._start, self._end)

formatted_repr = (
"<Bounds - "
"lat-range: {}, "
"lon-range: {}, "
"temporal_boundaries: {}> "
)
def shapefile_boundary(boundary_type, region_names):
'''
:param boundary_type: The type of spatial subset boundary
:type boundary_type: :mod:'string'
return formatted_repr.format(
lat_range,
lon_range,
temporal_boundaries,
)
:param region_names: An array of regions for spatial subset
:type region_names: :mod:'list'
'''
# Read the shapefile
map_read = Basemap()
regions = []
shapefile_dir = os.sep.join([os.path.dirname(__file__), 'shape'])
map_read.readshapefile(os.path.join(shapefile_dir, boundary_type),
boundary_type)
if boundary_type == 'us_states':
for region_name in region_names:
for iregion, region_info in enumerate(map_read.us_states_info):
if region_info['st'] == region_name:
regions.append(numpy.array(map_read.us_states[iregion]))
elif boundary_type == 'countries':
for region_name in region_names:
for iregion, region_info in enumerate(map_read.countries_info):
if region_info['COUNTRY'] == region_name:
regions.append(numpy.array(map_read.countries[iregion]))
return regions

def CORDEX_boundary(domain_name):
'''
:param domain_name: CORDEX domain name (http://www.cordex.org/)
:type domain_name: :mod:'string'
'''
if domain_name =='southamerica':
return -57.61, 18.50, 254.28-360., 343.02-360.
if domain_name =='centralamerica':
return -19.46, 34.83, 235.74-360., 337.78-360.
if domain_name =='northamerica':
return 12.55, 75.88, 189.26-360., 336.74-360.
if domain_name =='europe':
return 22.20, 71.84, 338.23-360., 64.4
if domain_name =='africa':
return -45.76, 42.24, 335.36-360., 60.28
if domain_name =='southasia':
return -15.23, 45.07, 19.88, 115.55
if domain_name =='eastasia':
return -0.10, 61.90, 51.59, 179.99
if domain_name =='centralasia':
return 18.34, 69.37, 11.05, 139.13
if domain_name =='australasia':
return -52.36, 12.21, 89.25, 179.99
if domain_name =='antartica':
return -89.48,-56.00, -179.00, 179.00
if domain_name =='artic':
return 46.06, 89.50, -179.00, 179.00
if domain_name =='mediterranean':
return 25.63, 56.66, 339.79-360.00, 50.85
if domain_name =='middleeastnorthafrica':
return -7.00, 45.00, 333.00-360.00, 76.00
if domain_name =='southeastasia':
return -15.14, 27.26, 89.26, 146.96

0 comments on commit 9797d97

Please sign in to comment.