GLEON / LakeMetabolizer

Collection of Lake Metabolism Functions
16 stars 10 forks source link

Error calculating lwnet #143

Closed RFlazcano closed 3 years ago

RFlazcano commented 3 years ago

I am trying to calculate lwnet but keep encountering this error:
Error in SatVapor/1000 : non-numeric argument to binary operator

I have been going off the demo so I've defined all the arguments in the calculation as well input my data frame as a complete time-series with all my variables as numeric except for the datetime.

hdugan commented 3 years ago

This is hard to troubleshoot. Are the demo examples working for you?

Can you give an example of your data and error? Something like:

# install.packages('LakeMetabolizer')
library(LakeMetabolizer)
#> Loading required package: rLakeAnalyzer

## Base example
dateTime <- as.POSIXct("2013-12-30 23:00")
Uz <- 3
airT <- 20
RH <- 90
sw <- 800
wndZ <- 2
Kd <- 2
lat <- 54
lake.area <- 5000 
atm.press <- 1013
Ts <- 22
calc.lw.net.base(dateTime,sw,Ts,lat,atm.press,airT,RH)
#> [1] -52.64892

Created on 2021-10-10 by the reprex package (v0.3.0)

RFlazcano commented 3 years ago

Yes, the demo examples work for me.

I first tried running the code with data columns as: datetime, doobs, wtr_1, par, wnd, z.mix, airt, rh When organized like that I can calculate lwnet if I calculate o2.sat before but then I run into this error :

data <- A_QC

corral.area <- 75.5 atm.press <- 961.87 elevation <- 396.72 latitude <- 49.71 longitude <- -93.7766 Kd <- 0.00951 wnd.z = 10 o2.sat <- o2.at.sat(data[c("datetime", "wtr")], baro = atm.press, altitude = elevation, salinity =0.01, model = "garcia-benson") data <- merge(o2.sat, data) lwnet <- calc.lw.net(data, latitude, atm.press) data = merge(data, lwnet) k600.macintyre <- k.macIntyre(data, wnd.z = wnd.z, Kd = Kd, atm.press = atm.press) Error in get.Ts(ts.data) : no matches for water temperatures within depth range 0 to 1 In addition: Warning message: In get.offsets(wtr) : Problem determining variable depths from column names. Please use the 'var_#.#' format for your data.frame header

So then I tried running the same code with but with including the depth of the water temp measurement, my data columns were: datetime, doobs, wtr, par, wnd, z.mix, airt, rh:

data <- A_QC

corral.area <- 75.5 atm.press <- 961.87 elevation <- 396.72 latitude <- 49.71 longitude <- -93.7766 Kd <- 0.00951 wnd.z = 10 o2.sat <- o2.at.sat(data[c("datetime", "wtr_1")], baro = atm.press, altitude = elevation, salinity =0.01, model = "garcia-benson") Error in data.frame(datetime = ts.data$datetime, do.sat = dosat) : arguments imply differing number of rows: 6494, 0 In addition: Warning message: Unknown or uninitialised column: wtr.

It seems that I can calculate lwnet when I calculate o2.sat first but then I run into an error with calculating k because wtr doesn't have a variable depth. But if I include the depth o2.sat won't run without errors.

The last code I tried was where I did not calculate o2.sat

data <- A_QC

corral.area <- 75.5 atm.press <- 961.87 elevation <- 396.72 latitude <- 49.71 longitude <- -93.7766 Kd <- 0.00951 wnd.z = 10 lwnet <- calc.lw.net(ts.data, latitude, atm.press) Error in SatVapor/1000 : non-numeric argument to binary operator

I've attached my data file. I am not sure if I am just defining my variables incorrectly. I can run the demo without errors and tried to organize my data and define my variables similarly so I am not sure why I am getting these errors. Water temp was only taken at 1 m depth so could it be that I do not have enough temperatures at more than one depth that is causing this?

A_QC.csv

hdugan commented 3 years ago

I think part of the problem might just be switching between the column header of wtr and wtr_1. Also, make sure that you have the latest LakeMetabolizer version installed from GitHub (it has a few updates which might be part of your problem).

Anyway, this worked for me, so hopefully it works for you:

devtools::install_github('GLEON/LakeMetabolizer') # Make sure newest version is installed

library(tidyverse)
library(LakeMetabolizer)
library(lubridate)

data = read_csv('A_QC.csv') %>% 
  rename(wtr = wtr_1) %>% #remove depth from column header
  mutate(datetime = ymd_hms(datetime))

corral.area <- 75.5
atm.press <- 961.87
elevation <- 396.72
latitude <- 49.71
longitude <- -93.7766
Kd <- 0.00951
wnd.z = 10

o2.sat <- o2.at.sat(data[c("datetime", "wtr")], baro = atm.press, altitude = elevation, salinity =0.01, model = "garcia-benson")
data <- merge(o2.sat, data)

lwnet <- calc.lw.net(data, latitude, atm.press)
data = merge(data, lwnet)

data = data %>% rename(wtr_1 = wtr) # Add depth to column header 
k600.macintyre <- k.macIntyre(ts.data = data, wnd.z = wnd.z, Kd = Kd, atm.press = atm.press)
RFlazcano commented 3 years ago

Yes, Thank you! This works now