---
title: "Curve Number review"
author: "VM"
always_allow_html: yes
format:
html:
toc: true
code-fold: true
code-tools: true
code-summary: "Show the code"
theme: sandstone
highlight-style: github
editor_options:
chunk_output_type: console
---
```{r setup}
knitr:: opts_chunk$ set (
echo = TRUE ,
message = FALSE , warning = FALSE ,
cache= FALSE ,
dpi= 150 ,
results= "hide" ,fig.height= 7 ,fig.width= 9 ,
figcap.prefix = "Figure" ,
figcap.sep = ":" , figcap.prefix.highlight = "**" )
```
```{r}
library (terra)
library (tidyverse)
library (tidyterra)
library (sf)
library (prismatic)
aoi_ras<- rast (xmin= - 162.94 ,xmax= - 162.75 ,ymin= 68.04 ,ymax= 68.085 )
```
# SOURCES
## AMC II
```{r}
cn_II_ras<- crop (rast ("V:/Data/CN/Global_CN/GCN250_ARCII.tif" ),
aoi_ras)
terra:: plot (cn_II_ras,type= "classes" )
```
As a conservative approach the ponds or NA were defined as CN=99
```{r}
cn_II_ras[is.na (cn_II_ras)]<- 99
terra:: plot (cn_II_ras,type= "classes" )
```
## AMC III
```{r}
cn_III_ras<- crop (rast ("V:/Data/CN/Global_CN/GCN250_ARCIII.tif" ),
aoi_ras)
plot (cn_III_ras,type= "classes" )
```
As a conservative approach the ponds or NA were defined as CN=99
```{r}
cn_III_ras[is.na (cn_III_ras)]<- 99
plot (cn_III_ras,type= "classes" )
```
```{r}
cn_III_tbl<- cn_III_ras%>%
fortify () %>%
dplyr:: rename (CN= GCN250_ARCIII)
cn_II_tbl<- cn_II_ras%>%
fortify () %>%
dplyr:: rename (CN= GCN250_ARCII)
```
## Orthophoto
```{r, fig.height=8,fig.width=10}
# Orthophoto
ortho_ras<- rast (here:: here ("data" ,"photo" ,"20220929-PL1A-CGG-RedDog-orthophoto-LDP.tif" ))
#upscale 16 times
ortho_ras2<- terra:: aggregate (ortho_ras,8 )
#coordinate system
ortho_ras_ll<- project (ortho_ras2,crs (aoi_ras))
#to data.frame
ortho_ras_ll_df<- fortify (ortho_ras_ll)
#rename colors
names (ortho_ras_ll_df)[3 : 5 ]<- c ("r" ,"g" ,"b" )
#add colors form layers
ortho_ras_ll_df %<>%
dplyr:: filter (complete.cases (.)) %>%
mutate (color= rgb (green = g/ 255 ,
blue = b/ 255 ,
red= r/ 255 )
)
ggplot ()+
geom_raster (data= ortho_ras_ll_df, aes (x= x, y= y, fill= color)) +
scale_fill_identity ()+
coord_quickmap ()+
theme_light ()+
labs (x= "lon [deg]" ,y= "lat [deg]" )
```
# FUNCTIONS
```{r}
gp_tbl_fn<- function (cn_tbl= cn_II_tbl,cn_ras= cn_II_ras,file_sh= file_sh_ll,id= "Basin_ID" ){
g1<- ggplot ()+
geom_tile (data= cn_tbl,aes (x= x,y= y,fill= CN),col= "grey" )+
geom_text (data= cn_tbl,aes (x= x,y= y,label= CN),size= 2.0 )+
scale_fill_binned (type = "viridis" ,breaks = seq (0 ,100 ,5 ))+
geom_sf (data= file_sh,aes (),fill= "grey" ,col= "black" ,alpha= 0.2 )+
# geom_sf_label(data=file_sh,aes(label=NAME))+
coord_sf ()+
labs (x= "longitude" ,y= "latitude" )+
theme (legend.position= "bottom" )
file_sh$ CN<- terra:: extract (cn_ras,vect (file_sh),
fun= mean,na.rm= T,method= "bilinear" ,weights= T) %>%
.[,2 ]
g2<- ggplot ()+
scale_fill_binned (type = "viridis" )+
geom_sf (data= file_sh,aes (fill= CN),alpha= 0.8 ,size= 2 ,col= "black" )+
geom_sf_label (data= file_sh,aes (label= round (CN,1 )),alpha= 0.2 )+
coord_sf ()+
theme_minimal ()+
theme (legend.position = "bottom" )+
labs (x= "Longitude" ,y= "Latitude" )
g3<- ggplot ()+
geom_raster (data= ortho_ras_ll_df, aes (x= x, y= y, fill= color)) +
scale_fill_binned (type = "viridis" )+
geom_sf (data= file_sh,alpha= 0.6 ,size= 2 ,col= "black" )+
geom_sf_label (data= file_sh,
aes (label= paste0 (NAME," \n " ,round (CN,0 ))),alpha= 0.2 )+
scale_fill_identity ()+
theme_minimal ()+
labs (x= "Longitude" ,y= "Latitude" )
tbl1<- file_sh %>%
as.data.frame () %>%
dplyr:: select (id,CN) %>%
dplyr:: filter (complete.cases (.)) %>%
dplyr:: arrange (id)
results<- list (g1,g2,g3,tbl1)
names (results)<- c ("g1" ,"g2" ,"g3" ,"tbl1" )
return (results)
}
```
# Golder Watersheds
```{r}
file_sh<- read_sf (here:: here ("data" ,"shp" ,"Golder_catchments_20190419_rk.shp" ))
# file_sh<-read_sf(here::here("data","shp","TSFarea_closure_v1.shp"))
#from UTM to lat and long
file_sh_ll<- st_transform (file_sh,st_crs (aoi_ras))
```
## AMC II
```{r,fig.height=9,fig.width=14,results="asis"}
golder_II_res<- gp_tbl_fn (cn_tbl = cn_II_tbl,cn_ras = cn_II_ras,file_sh = file_sh_ll,id = "Basin_ID" )
print (golder_II_res$ g1)
print (golder_II_res$ g2)
print (golder_II_res$ g3)
golder_II_res$ tbl1 %>%
arrange (Basin_ID) %>%
pander:: pandoc.table (style= "simple" )
```
## AMC III
```{r,results="asis",fig.height=9,fig.width=14}
golder_III_res<- gp_tbl_fn (cn_tbl = cn_III_tbl,cn_ras = cn_III_ras,file_sh = file_sh_ll,id = "Basin_ID" )
print (golder_III_res$ g1)
print (golder_III_res$ g2)
print (golder_III_res$ g3)
golder_III_res$ tbl1 %>%
arrange (Basin_ID) %>%
pander:: pandoc.table (style= "simple" )
```
## Synthesis
```{r,results="asis"}
golder_tbl<- tibble:: tribble (
~ Basin_ID, ~ GOLDER,
"TSF00" , 95L,
"TSF01" , 90L,
"TSF02" , 86L,
"TSF03" , 86L,
"TSF04" , 87L,
"TSF05" , 92L,
"TSF06" , 91L
)
plyr:: join_all (list (golder_tbl,golder_II_res$ tbl1 %>% dplyr:: rename (CN_AMCII= CN),
golder_III_res$ tbl1 %>% dplyr:: rename (CN_AMCIII= CN)
),match = "all" ) %>%
arrange (Basin_ID) %>%
pander:: pandoc.table (round= 0 ,missing= "-" )
```
# Closure
```{r}
file_sh<- read_sf (here:: here ("data" ,"shp" ,"TSFarea_closure_v1.shp" ))
#from UTM to lat and long
file_sh_ll<- st_transform (file_sh,st_crs (aoi_ras))
```
## AMC II
```{r,fig.height=9,fig.width=14,results="asis"}
closure_II_res<- gp_tbl_fn (cn_tbl = cn_II_tbl,cn_ras = cn_II_ras,file_sh = file_sh_ll,id = "NAME" )
print (closure_II_res$ g1)
print (closure_II_res$ g2)
print (closure_II_res$ g3)
closure_II_res$ tbl1 %>%
arrange (NAME) %>%
pander:: pandoc.table (style= "simple" )
```
## AMC III
```{r,results="asis",fig.height=9,fig.width=14}
closure_III_res<- gp_tbl_fn (cn_tbl = cn_III_tbl,cn_ras = cn_III_ras,file_sh = file_sh_ll,id = "NAME" )
print (closure_III_res$ g1)
print (closure_III_res$ g2)
print (closure_III_res$ g3)
closure_III_res$ tbl1 %>%
arrange (NAME) %>%
pander:: pandoc.table (style= "simple" )
```
## Synthesis
```{r,results="asis"}
plyr:: join (closure_II_res$ tbl1 %>% dplyr:: rename (CN_AMCII= CN),
closure_III_res$ tbl1 %>% dplyr:: rename (CN_AMCIII= CN)) %>%
arrange (NAME) %>%
pander:: pandoc.table (round= 0 ,style= "simple" )
```