Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Island effect #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
69 changes: 50 additions & 19 deletions RAPIDpy/gis/weight.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from shapely.geos import TopologicalError
import rtree # http://toblerity.org/rtree/install.html
from osgeo import gdal, ogr, osr
import array

# local
from .voronoi import pointsToVoronoiGridArray
Expand Down Expand Up @@ -80,7 +81,7 @@ def find_nearest(array, value):
return (np.abs(array-value)).argmin()


def rtree_create_weight_table(lsm_grid_lat, lsm_grid_lon,
def rtree_create_weight_table(lsm_grid_lat, lsm_grid_lon, lsm_grid_mask,
in_catchment_shapefile, river_id,
in_rapid_connect, out_weight_table,
file_geodatabase=None, area_id=None):
Expand All @@ -103,7 +104,6 @@ def rtree_create_weight_table(lsm_grid_lat, lsm_grid_lon,
else:
ogr_catchment_shapefile = ogr.Open(in_catchment_shapefile)
ogr_catchment_shapefile_lyr = ogr_catchment_shapefile.GetLayer()

ogr_catchment_shapefile_lyr_proj = \
ogr_catchment_shapefile_lyr.GetSpatialRef()
original_catchment_proj = \
Expand Down Expand Up @@ -149,6 +149,7 @@ def rtree_create_weight_table(lsm_grid_lat, lsm_grid_lon,
enumerate(ogr_catchment_shapefile_lyr):
catchment_rivid_list[feature_idx] = \
catchment_feature.GetField(river_id)

log("Reading in RAPID connect file ...")
rapid_connect_rivid_list = np.loadtxt(in_rapid_connect,
delimiter=",",
Expand Down Expand Up @@ -184,7 +185,7 @@ def rtree_create_weight_table(lsm_grid_lat, lsm_grid_lon,
proj_transform = \
osr.CoordinateTransformation(ogr_catchment_shapefile_lyr_proj,
osr_geographic_proj)
print(catchment_rivid_list)

for rapid_connect_rivid in rapid_connect_rivid_list:
intersect_grid_info_list = []
try:
Expand All @@ -194,9 +195,12 @@ def rtree_create_weight_table(lsm_grid_lat, lsm_grid_lon,
# if it is not in the catchment, add dummy row in its place
connectwriter.writerow([rapid_connect_rivid] + dummy_row_end)
continue


get_catchment_feature = \
ogr_catchment_shapefile_lyr.GetFeature(catchment_pos)
feat_geom = get_catchment_feature.GetGeometryRef()

# make sure coordinates are geographic
if proj_transform:
feat_geom.Transform(proj_transform)
Expand Down Expand Up @@ -240,22 +244,40 @@ 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'])
intersect_grid_info_list.append({
'rivid': rapid_connect_rivid,
'area': poly_area,
'lsm_grid_lat':
lsm_grid_feature_list[sub_lsm_grid_pos]['lat'],
'lsm_grid_lon':
lsm_grid_feature_list[sub_lsm_grid_pos]['lon'],
'index_lsm_grid_lon': index_lsm_grid_lon,
'index_lsm_grid_lat': index_lsm_grid_lat
})
# If ocean/water point, ECMWF sets ro to very negative number.
# This resulted at times in "island effect", i.e., large
# grid cell overlaying rivid and thus weird results. So,
# use land mask field to address these ECMWF points.
# There are arguments for and against division by land fraction. E.g.,
# it's arguable that division by mask might overemphasis some
# pixels that contain fine grained islands with runoff.
if lsm_grid_mask[int(index_lsm_grid_lat), int(index_lsm_grid_lon)] > 0:
intersect_grid_info_list.append({
'rivid': rapid_connect_rivid,
'area': poly_area/lsm_grid_mask[int(index_lsm_grid_lat), int(index_lsm_grid_lon)],
'lsm_grid_lat':
lsm_grid_feature_list[sub_lsm_grid_pos]['lat'],
'lsm_grid_lon':
lsm_grid_feature_list[sub_lsm_grid_pos]['lon'],
'index_lsm_grid_lon': index_lsm_grid_lon,
'index_lsm_grid_lat': index_lsm_grid_lat
})
# Prior weight table write without addressing land mask.
# intersect_grid_info_list.append({
# 'rivid': rapid_connect_rivid,
# 'area': poly_area,
# 'lsm_grid_lat':
# lsm_grid_feature_list[sub_lsm_grid_pos]['lat'],
# 'lsm_grid_lon':
# lsm_grid_feature_list[sub_lsm_grid_pos]['lon'],
# 'index_lsm_grid_lon': index_lsm_grid_lon,
# 'index_lsm_grid_lat': index_lsm_grid_lat
# })

npoints = len(intersect_grid_info_list)
# 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 @@ -266,7 +288,6 @@ def rtree_create_weight_table(lsm_grid_lat, lsm_grid_lon,
intersect_grid_info['lsm_grid_lon'],
intersect_grid_info['lsm_grid_lat']
])

time_end_all = datetime.utcnow()
log(time_end_all - time_end_lsm_grid_rtree)
log("TOTAL TIME: {0}".format(time_end_all - time_start_all))
Expand Down Expand Up @@ -299,7 +320,7 @@ def CreateWeightTableECMWF(in_ecmwf_nc,
The path to the output weight table file.
area_id: str, optional
The name of the field with the area of each catchment stored in meters
squared. Default is to calculate the area.
squared. Default is it calculate the area.
file_geodatabase: str, optional
Path to the file geodatabase. If you use this option, in_drainage_line
is the name of the stream network feature class.
Expand Down Expand Up @@ -330,15 +351,24 @@ def CreateWeightTableECMWF(in_ecmwf_nc,
in_ecmwf_lon_var = 'lon'
if 'longitude' in variables_list:
in_ecmwf_lon_var = 'longitude'
in_ecmwf_mask_var = 'mask'
# in_ecmwf_mask_var = 'lsm'
# if 'lsm' in variables_list:
# in_ecmwf_mask_var = 'lsm'
# in_ecmwf_mask_var = 'LAND_P0_L1_GLL0'
if 'mask' in variables_list:
in_ecmwf_mask_var = 'mask'

# convert [0, 360] to [-180, 180]
ecmwf_lon = \
(data_ecmwf_nc.variables[in_ecmwf_lon_var][:] + 180) % 360 - 180
# assume [-90, 90]
ecmwf_lat = data_ecmwf_nc.variables[in_ecmwf_lat_var][:]
ecmwf_mask = data_ecmwf_nc.variables[in_ecmwf_mask_var][0,:,:]

data_ecmwf_nc.close()

rtree_create_weight_table(ecmwf_lat, ecmwf_lon,
rtree_create_weight_table(ecmwf_lat, ecmwf_lon, ecmwf_mask,
in_catchment_shapefile, river_id,
in_connectivity_file, out_weight_table,
file_geodatabase, area_id)
Expand Down Expand Up @@ -371,6 +401,8 @@ def CreateWeightTableLDAS(in_ldas_nc,
The name of the field with the river ID (Ex. 'DrainLnID' or 'LINKNO').
in_connectivity_file: str
The path to the RAPID connectivity file.
is the name of the stream network feature class.
(WARNING: Not always stable with GDAL.)
out_weight_table: str
The path to the output weight table file.
area_id: str, optional
Expand All @@ -381,7 +413,6 @@ def CreateWeightTableLDAS(in_ldas_nc,
is the name of the stream network feature class.
(WARNING: Not always stable with GDAL.)


Example:

.. code:: python
Expand All @@ -396,7 +427,7 @@ def CreateWeightTableLDAS(in_ldas_nc,
river_id='LINKNO',
in_connectivity_file='/path/to/rapid_connect.csv',
out_weight_table='/path/to/ldas_weight.csv',
)
)
"""
# extract LDAS GRID
data_ldas_nc = Dataset(in_ldas_nc)
Expand Down