Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rain imputation in micro_global errors where months have no rain days #22

Closed
goldingn opened this issue Aug 18, 2023 · 5 comments
Closed

Comments

@goldingn
Copy link
Contributor

I'm running some microclimate models for dry places, and some spots have no rainy days in some months in the micro_global() source data. Niamey, Niger is one example. This leads to divide-by-zeros putting NAs in the rainfall data, when used with daily predictions and rainfrac=0, and then the fortran bit errors:

library(NicheMapR)
micro <- micro_global(
  loc = c(2.0840132, 13.5127664),
  timeinterval = 365,
  rainfrac = 0
)
#> If program is crashing, try run.gads = 2.
#> extracting climate data
#> extracting soil moisture data
#> running microclimate model for 365 days by 1 years at site long 2.0840132 lat 13.5127664
#> Note: the output column `SOLR` in metout and shadmet is for unshaded horizontal plane solar radiation
#> Error in microclimate(micro): NA/NaN/Inf in foreign function call (arg 24)

Created on 2023-08-18 with reprex v2.0.2

The offending lines are here (but possibly duplicated in other parts of the package?), and could be patched with some error handling

NicheMapR/R/micro_global.R

Lines 915 to 919 in 329eb11

RAINFALL1[m]<-(RAINFALL[i]*(1-rainfrac)*rainmult)/RAINYDAYS[i] # make remaining rain fall evenly over the remaining number of rainy days for the month, starting at the beginning of the month
}
}else{
if(rainfrac==0){
RAINFALL1[m]<-(RAINFALL[i]*rainmult)/RAINYDAYS[i]

@mrke
Copy link
Owner

mrke commented Aug 18, 2023 via email

@goldingn
Copy link
Contributor Author

Awesome! So speedy, thanks Mike!

@goldingn
Copy link
Contributor Author

Actually, I'm getting another related error now, where it's infilling Inf for the divide by 0. Previously it was 0 rainfall over 0 rainy days (0/0 = NA), but now I have non-zero rainfall on zero rainy days (1/0 = Inf).
A quick fix would be to change this line:

RAINFALL[is.na(RAINFALL)] <- 0

to:
RAINFALL[!is.finite(RAINFALL)] <- 0

That makes the asumption that RAINYDAYS is correctly zero and RAINFALL is erroneously nonzero for these months.

@goldingn
Copy link
Contributor Author

reprex on new coords for completeness:

library(NicheMapR)
micro <- micro_global(
  loc = c(73.0,  22.9),
  timeinterval = 365,
  rainfrac = 0
)
#> If program is crashing, try run.gads = 2.
#> extracting climate data
#> extracting soil moisture data
#> running microclimate model for 365 days by 1 years at site long 73 lat 22.9
#> Note: the output column `SOLR` in metout and shadmet is for unshaded horizontal plane solar radiation
#> Error in microclimate(micro): NA/NaN/Inf in foreign function call (arg 24)

Created on 2023-08-18 with reprex v2.0.2

@mrke
Copy link
Owner

mrke commented Aug 18, 2023 via email

@mrke mrke closed this as completed Dec 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants