Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions RAPIDpy/gis/weight.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ def find_nearest(array, value):
def rtree_create_weight_table(lsm_grid_lat, lsm_grid_lon,
in_catchment_shapefile, river_id,
in_rapid_connect, out_weight_table,
file_geodatabase=None, area_id=None):
file_geodatabase=None, area_id=None,
lsm_grid_mask=None):
"""
Create Weight Table for Land Surface Model Grids
"""
Expand Down Expand Up @@ -242,6 +243,11 @@ def rtree_create_weight_table(lsm_grid_lat, lsm_grid_lon,
lsm_grid_lon,
lsm_grid_feature_list[sub_lsm_grid_pos]['lat'],
lsm_grid_feature_list[sub_lsm_grid_pos]['lon'])

if lsm_grid_mask is not None:
if lsm_grid_mask[int(index_lsm_grid_lat), int(index_lsm_grid_lon)] > 0:
poly_area /= lsm_grid_mask[int(index_lsm_grid_lat), int(index_lsm_grid_lon)]

intersect_grid_info_list.append({
'rivid': rapid_connect_rivid,
'area': poly_area,
Expand All @@ -257,7 +263,6 @@ def rtree_create_weight_table(lsm_grid_lat, lsm_grid_lon,
# If no intersection found, add dummy row
if npoints <= 0:
connectwriter.writerow([rapid_connect_rivid] + dummy_row_end)

for intersect_grid_info in intersect_grid_info_list:
connectwriter.writerow([
intersect_grid_info['rivid'],
Expand All @@ -280,7 +285,8 @@ def CreateWeightTableECMWF(in_ecmwf_nc,
in_connectivity_file,
out_weight_table,
area_id=None,
file_geodatabase=None):
file_geodatabase=None,
in_ecmwf_mask_var=None):
"""
Create Weight Table for ECMWF Grids

Expand Down Expand Up @@ -338,12 +344,25 @@ def CreateWeightTableECMWF(in_ecmwf_nc,
(data_ecmwf_nc.variables[in_ecmwf_lon_var][:] + 180) % 360 - 180
# assume [-90, 90]
ecmwf_lat = data_ecmwf_nc.variables[in_ecmwf_lat_var][:]

if in_ecmwf_mask_var is not None:
if in_ecmwf_mask_var in variables_list:
ecmwf_mask = data_ecmwf_nc.variables[in_ecmwf_mask_var][0,:,:]
else:
print('Variable "{}" not found in {}.'.format(
in_ecmwf_mask_var, in_ecmwf_nc))
print('Continuing with no land mask.')
ecmwf_mask = None
else:
ecmwf_mask = None

data_ecmwf_nc.close()

rtree_create_weight_table(ecmwf_lat, ecmwf_lon,
in_catchment_shapefile, river_id,
in_connectivity_file, out_weight_table,
file_geodatabase, area_id)
file_geodatabase, area_id,
lsm_grid_mask=ecmwf_mask)


def CreateWeightTableLDAS(in_ldas_nc,
Expand Down
88 changes: 88 additions & 0 deletions RAPIDpy/inflow/CreateInflowFileFromERARunoff.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# -*- coding: utf-8 -*-
"""
CreateInflowFileFromERARunoff.py
RAPIDpy

Created by Alan D. Snow, 2015
Adapted from CreateInflowFileFromECMWFRunoff.py.
License: BSD-3-Clause
"""
from netCDF4 import Dataset

from .CreateInflowFileFromGriddedRunoff import \
CreateInflowFileFromGriddedRunoff


class CreateInflowFileFromERARunoff(CreateInflowFileFromGriddedRunoff):
"""Create Inflow File From ERA Runoff

Creates RAPID NetCDF input of water inflow based on
ERA runoff and previously created weight table.
"""
land_surface_model_name = "ERA"
header_wt = ['rivid', 'area_sqm', 'lon_index', 'lat_index', 'npoints']
dims_oi = [['lon', 'lat', 'time'],
['longitude', 'latitude', 'time'],
['time','lon','lat'],
['time','longitude','latitude'],
['latitude','longitude','time'],
['time','latitude','longitude']]
vars_oi = [['lon', 'lat', 'time', 'RO'],['lon','lat','time','ro'],
['time','lon','lat','RO'],['time','lon','lat','ro'],
['time','longitude','latitude','RO'],['time','longitude','latitude','ro'],
['longitude', 'latitude', 'time', 'RO'],['longitude', 'latitude', 'time', 'ro'],
['latitude','longitude','time','RO'],['latitude','longitude','time','ro'],
['latitude','longitude','RO','time'],['latitude','longitude','ro','time'],
['time','latitude','longitude','RO'],['time','latitude','longitude','ro']]
length_time = {"Daily": 1, "3-Hourly": 8, "1-Hourly":24}

def __init__(self):
"""Define the attributes to look for"""
self.runoff_vars = ['ro']
super(CreateInflowFileFromERARunoff, self).__init__()

def data_validation(self, in_nc):
"""Check the necessary dimensions and variables in the input
netcdf data"""
data_nc = Dataset(in_nc)

dims = list(data_nc.dimensions)

if dims not in self.dims_oi:
data_nc.close()
raise Exception("{0} {1}".format(self.error_messages[1], dims))

nc_vars = list(data_nc.variables)

if nc_vars == self.vars_oi[0]:
self.runoff_vars = [self.vars_oi[0][-1]]
elif nc_vars == self.vars_oi[1]:
self.runoff_vars = [self.vars_oi[1][-1]]
elif nc_vars == self.vars_oi[2]:
self.runoff_vars = [self.vars_oi[2][-1]]
elif nc_vars == self.vars_oi[3]:
self.runoff_vars = [self.vars_oi[3][-1]]
elif nc_vars == self.vars_oi[4]:
self.runoff_vars = [self.vars_oi[4][-1]]
elif nc_vars == self.vars_oi[5]:
self.runoff_vars = [self.vars_oi[5][-1]]
elif nc_vars == self.vars_oi[6]:
self.runoff_vars = [self.vars_oi[6][-1]]
elif nc_vars == self.vars_oi[7]:
self.runoff_vars = [self.vars_oi[7][-1]]
elif nc_vars == self.vars_oi[8]:
self.runoff_vars = [self.vars_oi[8][-1]]
elif nc_vars == self.vars_oi[9]:
self.runoff_vars = [self.vars_oi[9][-1]]
elif nc_vars == self.vars_oi[10]:
self.runoff_vars = [self.vars_oi[10][-2]]
elif nc_vars == self.vars_oi[11]:
self.runoff_vars = [self.vars_oi[11][-2]]
elif nc_vars == self.vars_oi[12]:
self.runoff_vars = [self.vars_oi[12][-1]]
elif nc_vars == self.vars_oi[13]:
self.runoff_vars = [self.vars_oi[13][-1]]
else:
data_nc.close()
raise Exception("{0} {1}".format(self.error_messages[2], nc_vars))
data_nc.close()
20 changes: 20 additions & 0 deletions RAPIDpy/inflow/CreateInflowFileFromGriddedRunoff.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,26 @@ def execute(self, nc_file_list, index_list, in_weight_table,
np.concatenate([ro_first_half, ro_second_half]),
area_sqm_npoints)

elif grid_type == 't1279':
# A) ERA Interim Low Res (T1279) - data is cumulative
# from time 6/12
# 0 1 2 3 4
# (time zero not included, so assumed to be zero)
ro_first_half = \
np.concatenate([data_goal[0:1, ],
np.subtract(data_goal[1:2, ],
data_goal[0:1, ])])
# from time 15/18/21/24
# (time restarts at time 12, assumed to be zero)
ro_second_half = \
np.concatenate([data_goal[2:3, ],
np.subtract(data_goal[3:, ],
data_goal[2:3, ])])
ro_stream = \
np.multiply(
np.concatenate([ro_first_half, ro_second_half]),
area_sqm_npoints)

else:
ro_stream = data_goal * area_sqm_npoints * \
conversion_factor
Expand Down
Loading