-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpre_process.R
36 lines (29 loc) · 1.08 KB
/
pre_process.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/bin/Rscript
# this script identifies "terminal" stream segments (those entering lake/ocean)
# and also creates the variable "from" which indicates the upstream IDs for each feature
# author: James Kessler ([email protected])
# July 2020
rtlink_file <- 'routelink_mich.nc'
# ======== generate "from" variable =============
# read stuff in
ncid <- nc_open(rtlink_file)
to <- ncvar_get(ncid,'to')
comid <- ncvar_get(ncid,'link')
names(to) <- comid
# allocate from
from <- vector('list',length=length(to))
names(from) <- names(to)
print('tracing network upstream...')
for (id in names(to)){ from[[id]]<-names(to[to %in% id]) }
save(from, file='mi_from.Rdata')
# ===== find/generate terminal segments =============
lk_file <- '../shp/mihu_shoreline.shp'
fl_file <- '../shp/mi_fl.shp'
min_order <- 3
# find flowlines that intersect the shoreline (terminal segments)
fl <- readOGR(fl_file)
lk <- as(readOGR(lk_file), 'SpatialLinesDataFrame')
fl_sel <- fl[fl$order_ > min_order,]
isterm <- gIntersects(fl_sel, lk, byid=T);
term_seg <- fl_sel[c(isterm),];
save(term_seg, file='term_seg.Rdata')