mrke / NicheMapR

R implementation of Niche Mapper software for biophysical modelling
GNU General Public License v3.0
64 stars 34 forks source link

Inconsistent output when running micro_terra() multiple times #31

Open aleixab opened 1 month ago

aleixab commented 1 month ago

We have encountered an issue where running the micro_terra() function multiple times produces inconsistent outputs.

For example, running:

out <- micro_terra()

> head(out$metout)
     DOY TIME     TALOC     TAREF     RHLOC       RH      VLOC      VREF SNOWMELT POOLDEP PCTWET ZEN SOLR     TSKYC DEW FROST  SNOWFALL SNOWDEP SNOWDENS
[1,]  15    0 -13.07920 -11.13238  92.20111 77.34608 0.3360625 1.6676484        0       0      0  90    0 -31.66775   0     0 0.3362066       0   0.2178
[2,]  15   60 -13.37859 -11.27048  97.23262 80.36256 0.2926318 1.4521318        0       0      0  90    0 -32.42503   0     0 0.0000000       0   0.2178
[3,]  15  120 -13.79528 -11.38341 100.00000 83.37904 0.2492012 1.2366151        0       0      0  90    0 -33.14764   0     0 0.0000000       0   0.2178
[4,]  15  180 -14.26848 -11.47576 100.00000 86.39552 0.2057705 1.0210985        0       0      0  90    0 -33.84072   0     0 0.0000000       0   0.2178
[5,]  15  240 -14.84673 -11.55127 100.00000 89.41200 0.1623399 0.8055819        0       0      0  90    0 -34.50852   0     0 0.0000000       0   0.2178
[6,]  15  300 -15.55614 -11.61302 100.00000 92.42848 0.1189092 0.5900652        0       0      0  90    0 -35.15461   0     0 0.0000000       0   0.2178
out1 <- micro_terra() # Second run produces incorrect output
> head(out1$metout)
     DOY TIME     TALOC TAREF     RHLOC        RH       VLOC      VREF SNOWMELT POOLDEP PCTWET ZEN SOLR     TSKYC DEW FROST SNOWFALL SNOWDEP SNOWDENS
[1,]  15    0 -45.02997   -40 100.00000 100.00000 0.02015188 0.1000000        0       0      0  90    0 -53.01595 NaN     0        0       0 0.218926
[2,]  15   60 -45.75550   -40  99.86214  99.86214 0.02018410 0.1001599        0       0      0  90    0 -54.16701 NaN     0        0       0 0.217800
[3,]  15  120 -46.30756   -40  99.72428  99.72428 0.02021633 0.1003198        0       0      0  90    0 -55.33693 NaN     0        0       0 0.217800
[4,]  15  180 -46.81351   -40  99.58641  99.58641 0.02024855 0.1004797        0       0      0  90    0 -56.52644 NaN     0        0       0 0.217800
[5,]  15  240 -47.30082   -40  99.44855  99.44855 0.02028078 0.1006396        0       0      0  90    0 -57.73630 NaN     0        0       0 0.217800
[6,]  15  300 -47.78594   -40  99.31069  99.31069 0.02031300 0.1007995        0       0      0  90    0 -58.96733 NaN     0        0       0 0.217800

The second execution returns an incorrect result, despite no changes in inputs or variables. This issue also occurs when running the example provided in the documentation multiple times.

Any guidance and/or interpretations would be appreciated. Thank you very much!

mrke commented 1 month ago

Did you use the very latest version? I think that was due to an issue that arose in the conversion from raster to terra (getting rid of rgdal). I can't reproduce it at my end.

aleixab commented 1 month ago

Thanks for the quick reply!

I have been using the latest version of NicheMapR (v3.3.2), but I haven't tried using the latest commit from the main branch yet.

Here are the versions of the packages and R that I am currently using:

> sessionInfo()
R version 4.4.1 (2024-06-14 ucrt)
Platform: x86_64-w64-mingw32/x64
Running under: Windows 10 x64 (build 19045)

Matrix products: default

locale:
[1] English_Belgium.utf8

time zone: Europe/Brussels
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] RNetCDF_2.9-2   geodata_0.6-2   ncdf4_1.23      NicheMapR_3.3.2 terra_1.7-83

Thanks!

mrke commented 1 month ago

Can you try the latest commit and see if you have the same problem?

aleixab commented 4 weeks ago

I ran it using both the newest release and the latest commit, but I keep getting the same output. I also tried running it on a different computer and encountered the same issue. I looked through the function source code but couldn't identify what is causing the problem. One thing I noticed is that some of the links were broken, though I’m not sure if this is expected.

Thank you very much in advance.

mrke commented 3 weeks ago

It took a while but I tracked down the issue. It was not specific to micro_terra but to when snow was falling on the first day and the default snow density function was in operation. There was a variable ('cursnow') used to calculate snow density that wasn't initialised properly causing the second run to not work properly. The behaviour depends on the compiler it seems - on my system it just resulted in very slightly different values for the first day but on you system it appears to have led to an NA value. The fix is pushed to github now, so see if it works for you.

aleixab commented 3 weeks ago

Thank you for checking and implementing the changes. Initially, I still encountered the same issue with the latest commit: after the first run, the values were below -40. However, after comparing results across different computers, we found that while most of them showed the same -40 issue, one computer produced the correct output.

The only difference we noted was in the computer’s language and region settings. When we switched to the United Kingdom region and English (UK) language, the code ran without issues. However, when set to Belgium and English (Belgium), for example, the problem persisted.

I'm not familiar with why this would cause an error in R in the first place, but I hope this insight will help you identify the issue or assist others facing the same problem.

Feel free to close this issue. Thanks again!