pschmidtwalter / LWFBrook90R

Run the LWF-Brook90 hydrological model within R.
https://pschmidtwalter.github.io/LWFBrook90R/
11 stars 7 forks source link

date inheritance problem #55

Closed HammadAli-WD closed 3 years ago

HammadAli-WD commented 3 years ago

I am facing this problem and do not know how to solve it. Error in runLWFB90(options.b90 = options.b90, param.b90 = param.b90, climate = slb1_meteo, : inherits(climate$dates, "Date") is not TRUE

pschmidtwalter commented 3 years ago

I guess you need to set up proper input objects using ´set_optionsLWFB90()` etc, but I cant tell from your example. Please provide a reproducible example!

HammadAli-WD commented 3 years ago

Respected Sir, I am using the following script and facing the problem of inherits

library(LWFBrook90R) library(ggplot2) library(data.table)

meteo <- "britz_meteo1.csv" slb1_meteo = read.table(meteo, header = TRUE, sep = ",") soil<- "britz_soil.csv" slb1_soil = read.table(soil,header = TRUE, sep = ",")

slb1_soil <- load("britz_soil.rda")

data("britz_meteo.rda")

options.b90 <- setoptions_LWFB90() param.b90 <- setparam_LWFB90()

Set start and end dates in model control options:

options.b90$startdate <- as.Date("2019-01-01") options.b90$enddate <- as.Date("2019-12-31")

soil <- cbind(slb1_soil, hydpar_wessolek_mvg(slb1_soil$texture))

output <- setoutput_LWFB90() output results_slb1_default <- runLWFB90( options.b90 = options.b90, param.b90 = param.b90, climate = slb1_meteo, soil = soil, output = output)

pschmidtwalter commented 3 years ago

Dear Mr Hammad, when you read in your climate data from file, the date-column will be read in as Character variable. You have to convert it to a Date variable:

meteo <- read.table("britz_meteo1.csv",header = TRUE, sep = ",")
meteo$dates <- as.Date(meteo$dates)
HammadAli-WD commented 3 years ago

Respected Sir, Thanks a lot for your support. Now its giving me this error. Error in r_lwfbrook90(siteparam = data.frame(simyears[1], as.integer(format(options.b90$startdate, : NA/NaN/Inf in foreign function call (arg 5)

Even I am using NaRV.omit(meteo) for removal of na/nan/inf , and na.omit(), it gives me same error

Thanks

pschmidtwalter commented 3 years ago

Sorry, without any data and code for reproduction, I can only guess the problem here: Your climate data has to be a complete series, no data gaps in any of the variables are allowed.

HammadAli-WD commented 3 years ago

Respected Sir, Thanks for your precious guidance. The data has 0 values for some of the precipitation and negative values for temperature. firstly we want to analyze only for 2019 starting from 1/1/2019 and ends at 1/3/2020.

HammadAli-WD commented 3 years ago

The problem is solved, we have some errors in data, but I have another question. I am not able to update the current version and can only able to run the model using your provided Appendix A. Now, the problem is in assigning the dates using dates <- with(results_slb1_default $daily_output, as.Date(paste(yr, mo, da, sep = "-"))) it shows the error of yr object not found. the daily_output is also not available in results_slb1_default. The dates are available in model_input and then standprop_daily variables of results_slb1_default. Please guide me in this regard. Thanks

pschmidtwalter commented 3 years ago

I cant help you if dont post a reproducible example including sample data. Please include code in any inquiries posted here. Call run_LWFB90() with output = -1, then daily_output should exist as an entry of the return value.

HammadAli-WD commented 3 years ago

Respected Sir, My script is as follow, even with output = -1; i am getting the error : Error in paste(yr, mo, da, sep = "-") : object 'yr' not found

install.packages("data.table")

install.packages("ggplot2")

install.packages("hydroGOF")

install.packages("BayesianTools")

x <- model.matrix( ~ ., Data)

setwd("C:/Data/Research/NewProject/BrookModel_Example/Analysis") directory <- getwd()

library(LWFBrook90R) library(ggplot2) library(data.table)

require(IDPmisc)

meteo <- "britz_meteo1.csv"

slb1_meteo = read.table(meteo, header = TRUE, sep = ",")

slb1_meteo$dates <- as.Date(slb1_meteo$dates)

slb1_meteo <- read.table("britz_meteo.csv",header = TRUE, sep = ",") slb1_meteo$dates <- as.Date(slb1_meteo$dates) typeof(slb1_meteo$globrad) slb1_meteo[is.na(slb1_meteo)] = 0 is.na(slb1_meteo) slb1_meteo

na.omit(slb1_meteo)

NaRV.omit(slb1_meteo)

is.na(slb1_meteo)

slb1_meteo$tmin <- as.double(slb1_meteo$tmin) slb1_meteo$tmax <- as.double(slb1_meteo$tmax) slb1_meteo$tmean <- as.double(slb1_meteo$tmean) slb1_meteo$prec <- as.double(slb1_meteo$prec) slb1_meteo$relhum <- as.double(slb1_meteo$relhum) slb1_meteo$globrad <- as.double(slb1_meteo$globrad) slb1_meteo$wind <- as.double(slb1_meteo$wind) slb1_meteo$vappres <- as.double(slb1_meteo$vappres)

soil<- "britz_soil_1.csv" slb1_soil = read.table(soil,header = TRUE, sep = ",")

NaRV.omit(slb1_soil)

slb1_soil[is.na(slb1_soil)] = 0 is.na(slb1_soil) slb1_soil

slb1_soil$gravel <- as.double(slb1_soil$gravel) typeof(slb1_soil$c_org)

slb1_soil <- load("britz_soil.rda")

data("britz_meteo.rda")

options.b90 <- setoptions_LWFB90() param.b90 <- setparam_LWFB90()

Set start and end dates in model control options:

options.b90$startdate <- as.Date("2019-01-02") options.b90$enddate <- as.Date("2020-01-03")

typeof(options.b90$startdate)

soil <- cbind(slb1_soil, hydpar_wessolek_mvg(slb1_soil$texture))

output <- setoutput_LWFB90() output

dates <- slb1_meteo$dates

dates

results_slb1_default <- runLWFB90(project.dir = "testrun1/", options.b90 = options.b90, param.b90 = param.b90, climate = slb1_meteo, soil = soil, output = -1, verbose = TRUE)

> Creating stand properties from parameters...

> Standproperties created succesfully

> Running model...

> Simulation successful! Duration: 4.63 seconds

> Reading output...

> Finished!

dates <- with(results_slb1_default$daily_output, as.Date(paste(yr, mo, da, sep = "-")))

oldpar <- par(no.readonly = TRUE) plot(dates,results_slb1_default$daily_output$tran, col = "green", type = 'l', ylim = c(0,5), xlab = "", ylab = "Transpiration [mm]") par(new=TRUE) plot(dates, results_slb1_default$daily_output$swat, type = "l", col = "blue", ylim = c(200,500), yaxt = "n", xaxt = "n", ylab = "", xlab = "")

axis(4,pretty(c(200,500))) mtext("Soil water storage [mm]", side = 4, line =3) legend("left", legend = c("tran", "swat"), col = c("green", "blue"), lty = 1, bty = "n")

pschmidtwalter commented 3 years ago

I cant follow you like this. Please upgrade to the most recent released version using install.packages("LWFBrook90R") and check out the manual with vignette('intro_LWFB90'). There, you can reproduce the examples step by step and try to understand the model, and replace the input with your data.

This passage here looks like you are using an older version (< v0.4.0):

results_slb1_default <- runLWFB90(project.dir = "testrun1/",
options.b90 = options.b90,
param.b90 = param.b90,
climate = slb1_meteo,
soil = soil,
output = -1,
verbose = TRUE)
#> Creating stand properties from parameters...
#> Standproperties created succesfully
#> Running model...
#> Simulation successful! Duration: 4.63 seconds
#> Reading output...
#> Finished!

If so, runLWFB90 will not include a daily_output item in the return value (results_slb1_default), so you get an error when you try to access results_slb1_default$daily_output.