-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwetVpre-diag.R
More file actions
131 lines (81 loc) · 2.18 KB
/
Copy pathwetVpre-diag.R
File metadata and controls
131 lines (81 loc) · 2.18 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# -------------------------------------------------------------------------------
# R script to calculate nonlinear relationship between precip and wetdays
library(ncdf4)
library(minpack.lm)
library(qpcR)
eps <- .Machine$double.xmin
# -------------------------------------------------------------------------------
# file names
args <- commandArgs(trailingOnly <- TRUE)
mon <- 1 # as.numeric(args[1])
# open the input file
ifid <- nc_open("/Users/jkaplan/nosync/datasets/wetfrac30m_t0.2_202512.nc",write=FALSE)
# get dimension lengths
lon <- ncvar_get(ifid,"lon")
lat <- ncvar_get(ifid,"lat")
time <- ncvar_get(ifid,"time")
xlen <- length(lon)
ylen <- length(lat)
tlen <- length(time)
# get input data
pre <- ncvar_get(ifid,varid="pre")
wet <- ncvar_get(ifid,varid="wetf")
# close input files
nc_close(ifid)
# ----------------------
# calculate single pixel
# southern amazon
# x <- 238
# y <- 152
# california
# x <- 144
# y <- 249
# central amazon
# x <- 221
# y <- 174
# spain
# x <- 346
# y <- 255
# pixels with negative slope and significant p-value
# x <- 215
# y <- 129
# x <- 357
# y <- 230
# x <- 384
# y <- 232
x <- 194
y <- 233
# ---
t0 <- 1
t1 <- tlen
# ----------------------
ind_all <- pre[x,y,t0:t1]
dep_all <- wet[x,y,t0:t1]
didrain <- ind_all > 0.2
obs <- sum(didrain, na.rm = TRUE)
ind <- ind_all[didrain]
dep <- dep_all[didrain]
lmfit <- lm(dep ~ log(ind))
# rlmfit <- rlm(dep ~ log(ind))
coefs <- coef(lmfit)
# rng <- max(ind) - min(ind)
#
# ind_same <- abs(max(ind) - min(ind)) == 0
# dep_same <- abs(max(dep) - min(dep)) == 0
#
# fit <- try(nlsLM(dep ~ (1-exp(-a*indr))^b,start=list(a=4,b=1),lower=c(eps,eps),upper=c(100,3),control = nls.lm.control(maxiter = 100)),TRUE)
#
# coefs <- coef(fit)
slope <- coefs[2]
intercept <- coefs[1]
rmse <- RMSE(lmfit)
# ----------------------
# plot fit lines as a diagnostic
# two plots per page
par(mfrow=c(1,2))
plot(dep ~ ind, pch=19, cex=0.5, xlab="precipitation", ylab="wet fraction")
xvals <- seq(0,max(ind),length.out = 51)
lines(xvals,slope * log(xvals) + intercept,col="red")
plot(dep ~ log(ind), pch=19, cex=0.5, xlab="log(precipitation)", ylab="wet fraction")
abline(lmfit,col="red")
# ----------------------