% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/201_nahu_ylt_surgery.R
\name{nahu_ylt_surgery}
\alias{nahu_ylt_surgery}
\title{An example workflow for applying \code{ylt_surgery_groups} or \code{ylt_surgery_groups} to NAHU.}
\usage{
nahu_ylt_surgery(
  type_of_analysis,
  ipfilename,
  settingsfilename,
  ylt_opfilename,
  results_opfilename,
  nyearsinylt,
  rcp,
  baseyear1,
  baseyear2,
  targetyear,
  k2020settings,
  frequnc,
  quantile,
  units,
  randomseed,
  manual,
  manualadjustments,
  mincat,
  maxcat,
  test = FALSE,
  verbose = FALSE
)
}
\arguments{
\item{type_of_analysis}{"groups" or "notgroups"}

\item{ipfilename}{ipfilename}

\item{settingsfilename}{settingsfilename}

\item{ylt_opfilename}{ylt_opfilename}

\item{results_opfilename}{results_opfilename}

\item{nyearsinylt}{nyearsinylt}

\item{rcp}{rcp}

\item{baseyear1}{baseyear1}

\item{baseyear2}{baseyear2}

\item{targetyear}{targetyear}

\item{k2020settings}{k2020settings}

\item{frequnc}{frequnc}

\item{quantile}{quantile}

\item{units}{units}

\item{randomseed}{randomseed}

\item{manual}{manual}

\item{manualadjustments}{manualadjustments}

\item{mincat}{The minimum cat that is counted}

\item{maxcat}{The maximum cat that is counted}

\item{test}{test}

\item{verbose}{verbose}
}
\value{
Returns the two YLTs (each consisting of a long and short ylt),
and writes the new longylt to a csv file.
}
\description{
Shows how to take a YLT (that must have wspd, lat and lon columns) and apply climate
change adjustments.
}
\details{
The sections in the code are as follows:

1-writes out the settings to a csv file, for future reference

2-reads an input longylt from the file ipfilename

3-makes some input checks, to make sure the right columns are available in the input file

4-uses the wspd column to add a cat column to the longylt (from 1 to 7, which is cat -1 to 5)
(change this line if you want to make adjustments based on some other feature of the events)

5-uses the lon lat columns to add global region as a column to the longylt (regions from 1 to 18, from the Jewson BAMS paper)
(change this line if you want to use different regions)

6-looks at some diagnostics on the input ylt

7-sets up the GMST scenarios
(change this line if you want to use different GMST scenarios)

8-sets up the rates changes from the BAMS article supporting material on zenodo
(change this line if you want to use different rate changes)

9-does the temporal interpolation based on the input options, GMST scenarios, and zenodo rates

10-converts the adjustments from “by reg-cat” to “by-event”, so they are ready to apply to the ylt

11-defines groups in a certain way...currently just by cat)
(change this line if you want to define groups differently)

12-actually does the adjustment, and makes a new ylt

13-makes joint ylts from the input and results

14-looks at some diagnostics for the new ylt

15-looks at AAE, AAL change diagnostics

16-looks at AAE, AAL change diagnostics, now by cat

17-writes out the new longylt to a csv file

18-returns
}
\examples{
#
# This long example gives a complete end-to-end example of how to adjust a
# hurricane ylt for climate change.
# It uses either surgery, or surgery with groups (according to the settings below).
#
# Here's roughly what it does:
# -creates a ylt
# -sets up some parameters that define climate change adjustments
#  (years, scenarios, etc)
# -sets up some parameters that define what outputs to look at
# -and then adjusts the ylt nylt times, so we can see the variability in the
#  effect of the adjustments
# -it also process the adjusted ylts, and processes and prints the results in
#  nice format to screen and files
#
# 1 make the input ylt csv file:
# -3 years, 4 events
# -wspd, lats and lons will be used to generate appropriate rate adjustments
#
library(pracma)
tic()
message("1. make the input ylt csv file")
year=c(1,1,2,3)
evid=c(12,34,56,78)
loss=c(10,20,30,40)
wspd=c(60,90,120,150)
lflat=c(34.779,29.855,28.891,28.83)
lflon=c(-76.596,-84.174,-95.425,-95.515)
df=data.frame(year,evid,loss,wspd,lflat,lflon)
ipfilename=tempfile(fileext=".csv")
write.csv(df,file=ipfilename,row.names=FALSE)
#
# 2 specify the filenames, and nyearsinylt
#
message("2. specify the filenames etc")
results_Nx_opfilename			=tempfile(fileext=".csv")
settingsfilename					=tempfile(fileext=".csv")
# base only filenames
ylt_opfilename_base				="ylt_op"
results_1x_opfilename_base="results"
nyearsinylt=4
# the nyearsinylt variable forces the code to understand that the ylt represents
# e.g. 50k, even if year 50k is missing, which it might be in some ylts,
# if there are no events in that year
#
# 3 set the other settings (scenarios, etc)
#
message("3. adjustment settings")
type_of_analysis="groups"
type_of_analysis="notgroups"
nylt=1
rcp=8
baseyear1=1900
baseyear2=2022
targetyear=2099
k2020settings="Linear and Landfall"
frequnc="Use distribution"
quantile=90
units="mph"
randomseed=0
manual=FALSE
mn=c(1,1,1.1,1.2,1.3,1.4,1.5)#note that it has 7 values for cat -1 to cat 5
sd=c(0,0,0,0,0,0,0)
manualadjustments=list(mn=mn,sd=sd)
#
# 4 output analyis settings
#
message("4. output analysis settings")
onemillion=1000000
lossthresholds=c(0,30)*onemillion
maxnumberevents=12
countequalto=TRUE
rps=c(2,5)
rpi=pmax(round(nyearsinylt/rps),1)
probs=round(100/rps,digits=1)
nrp=length(rps)
mincat=0
maxcat=5
ncat=1+maxcat-mincat
ncatp1=ncat+1 #since I've added an extra row at the end for the total
nbs=100
nlossthresholds=length(lossthresholds)
#
# 5 output analysis arrays
#
message("5. output analysis arrays")
aal	=matrix(0,2,nylt)
pcaal=matrix(0,2,nylt)

aalbc	=array(0,c(2,nylt,ncatp1))
pcaalbc=matrix(0,nylt,ncatp1)

aae	=matrix(0,2,nylt)
pcaae=matrix(0,2,nylt)

aaebc	=array(0,c(2,nylt,ncatp1))
pcaaebc=matrix(0,nylt,ncatp1)

nbc	=array(0,c(2,nylt,ncatp1))

aep	=array(0,c(2,nylt,nrp))
daep=matrix(0,nylt,nrp)

oep	=array(0,c(2,nylt,nrp))
doep=matrix(0,nylt,nrp)

thresholdcounts	=array(0,c(2,nylt,nlossthresholds,(maxnumberevents+1)))
#+1 to cope with 0
#
# 6 loop to generate the new ylts
#
message("6. loop to generate the new ylts")
for (iylt in 1:nylt){
	cat("running version:",iylt,"\n")
	randomseed=iylt

# the new two lines have two options
	ylt_opfilename				=tempfile(fileext=".csv")
	results_1x_opfilename	=tempfile(fileext=".csv")

	cat("...calling nahu_ylt_surgery\n")
# this is where the actual adjustment takes place, given all the settings
	bothylts=nahu_ylt_surgery(type_of_analysis=type_of_analysis,
		ipfilename,settingsfilename,ylt_opfilename,results_opfilename="",nyearsinylt,
		rcp,baseyear1,baseyear2,targetyear,k2020settings,frequnc,quantile,units,randomseed,
		manual,manualadjustments,mincat,maxcat,
		test=FALSE,verbose=TRUE)
	cat("...back from nahu_ylt_surgery\n")

# for each iteration, extract and store what we need for the output file

# 2 extra ylts, make short ylts
	longylt1=bothylts$longylt1
	longylt2=bothylts$longylt2
	shortylt1=ylt_long2short(longylt1)
	shortylt2=ylt_long2short(longylt2)

# 1 bootstrap uncertainty on base
	if(iylt==1){
		bsaepsd=bootstrap_ep_uncertainty(shortylt1$loss_in_year,nbs,rps)
		bsoepsd=bootstrap_ep_uncertainty(shortylt1$max_loss_in_year,nbs,rps)
	}


# 3 aae and aal diagnostics
# -if there are 7 cats in the file, returns 7 cats
	aae[1,iylt]=ylt_diagnostics_aaeaal(longylt1)$AAE
	aae[2,iylt]=ylt_diagnostics_aaeaal(longylt2)$AAE
	pcaae[iylt]=round(100*(aae[2,iylt]-aae[1,iylt])/aae[1,iylt],digits=2)
	aal[1,iylt]=ylt_diagnostics_aaeaal(longylt1)$AAL
	aal[2,iylt]=ylt_diagnostics_aaeaal(longylt2)$AAL
	pcaal[iylt]=round(100*(aal[2,iylt]-aal[1,iylt])/aal[1,iylt],digits=2)

# 4 aae and aal diagnostics by cat
	aaeaalbc1=ylt_diagnostics_aaeaal_by_catf(longylt1,mincat=mincat,maxcat=maxcat)
	aaeaalbc2=ylt_diagnostics_aaeaal_by_catf(longylt2,mincat=mincat,maxcat=maxcat)
# aae by cat
	aaebc[1,iylt,]	=round(aaeaalbc1$AAEbycat,digits=2)
	aaebc[2,iylt,]	=round(aaeaalbc2$AAEbycat,digits=2)
	nbc[1,iylt,]	=aaeaalbc1$Nbycat
	nbc[2,iylt,]	=aaeaalbc2$Nbycat
	pcaaebc[iylt,]=round(100*(aaeaalbc2$AAEbycat-aaeaalbc1$AAEbycat)/aaeaalbc1$AAEbycat,
		digits=1)
# aal by cat
	aalbc[1,iylt,]=round(aaeaalbc1$AALbycat,digits=0)
	aalbc[2,iylt,]=round(aaeaalbc2$AALbycat,digits=0)
	pcaalbc[iylt,]=round(100*(aaeaalbc2$AALbycat-aaeaalbc1$AALbycat)/aaeaalbc1$AALbycat,
		digits=1)

# 5 calculate AEPs
	aep[,iylt,]=make_2_ep_by_sorting(shortylt1$loss_in_year,shortylt2$loss_in_year,rps)
	daep[iylt,]=round(100*(aep[2,iylt,]-aep[1,iylt,])/aep[1,iylt,],digits=1)

# 6 calculate OEPs
	oep[,iylt,]=make_2_ep_by_sorting(shortylt1$max_loss_in_year,shortylt2$max_loss_in_year,
		rps)
	doep[iylt,]=round(100*(oep[2,iylt,]-oep[1,iylt,])/oep[1,iylt,],digits=1)

# 7 number of years with n events greater than x loss
	m30=30000000
	thresholdcounts[1,iylt,,]=ylt_diagnostics_loss_thresholds(longylt1,lossthresholds,
		maxnumberevents,countequalto)
	thresholdcounts[2,iylt,,]=ylt_diagnostics_loss_thresholds(longylt2,lossthresholds,
		maxnumberevents,countequalto)

# write results for each ylt separately, just in case you need that
con=file(results_1x_opfilename)
sink(con,append=TRUE)
sink(con,append=TRUE,type="message")
	writeNresults2csv(nyearsinylt,mincat,maxcat,nrp,nylt,maxnumberevents,bsaepsd,bsoepsd,
		aal,pcaal,aalbc,pcaalbc,
		aae,pcaae,aaebc,pcaaebc,
		nbc,
		probs,rps,aep,daep,oep,doep,lossthresholds,thresholdcounts,justone=TRUE,iylt)
closeAllConnections()

}

# write results for all ylts together into one file...this is the main output
con=file(results_Nx_opfilename)
sink(con,append=TRUE)
sink(con,append=TRUE,type="message")
	writeNresults2csv(nyearsinylt,mincat,maxcat,nrp,nylt,maxnumberevents,bsaepsd,bsoepsd,
		aal,pcaal,aalbc,pcaalbc,
		aae,pcaae,aaebc,pcaaebc,
		nbc,
		probs,rps,aep,daep,oep,doep,lossthresholds,thresholdcounts,justone=FALSE,iylt=999)
closeAllConnections()

toc()

}
\author{
Stephen Jewson \email{stephen.jewson@gmail.com}
}
