Ostrich calibration using R-driver script to calculate objective function

Ask questions or report problems
sgronsdahl
Posts: 7
Joined: Sun Sep 22, 2019 9:13 pm

Ostrich calibration using R-driver script to calculate objective function

Postby sgronsdahl » Sun Sep 22, 2019 9:28 pm

Hi there,

I am currently trying to set up a model calibration using Ostrich. I have developed an R-script that calls the model from the command line, reads in the simulated hydrographs, reads in the observed hydrographs, and compute a user-defined objective function.

The script runs completely as intended in R, but it stops part-way when I try to call it from the command line. After executing the Raven model it requires that I manually hit the enter button to execute the remainder of the script. This is prohibiting me from using this setup in Ostrich.
Capture.JPG

Any advice on how to solve this issue would be greatly appreciated.

Thanks,
Stefan
You do not have the required permissions to view the files attached to this post.

rchlumsk
Posts: 156
Joined: Fri Sep 16, 2016 3:18 pm

Re: Ostrich calibration using R-driver script to calculate objective function

Postby rchlumsk » Sun Sep 22, 2019 10:26 pm

Hi Stefan

I have also used batch files to run Raven and R and have managed to get it to work without the wait for input. I am unable to recreate your problem, but if you can run a test to confirm (maybe with some additional printouts, echo function in DOS and simple print or cat function in R) that the R script is not run or started before the 'Press the ENTER key to continue' button appears, that would be helpful. I suspect that this is likely not an R script issue but something with the .bat command. Please reply back with some additional insight, the version of Raven you are using (just in case), and what your .bat file looks like.

Update
Sample .bat file to run Raven.exe and R which seems to work for me. Take a look at the post (https://www.r-bloggers.com/how-to-run-r-from-the-task-scheduler/) here to associate the Rscript.exe in the Windows PATH variable.

Code: Select all

@echo off

REM test R script file
Rscript testR.R
echo "R script run"

REM Raven.exe Irondequoit -o ./output/

echo "bat file run"


Cheers,
Rob
Robert Chlumsky
rchlumsk@uwaterloo.ca

sgronsdahl
Posts: 7
Joined: Sun Sep 22, 2019 9:13 pm

Re: Ostrich calibration using R-driver script to calculate objective function

Postby sgronsdahl » Sun Sep 22, 2019 11:40 pm

Hi Rob, thanks for you quick response. I'm not certain I fully understand what you mean when you ask if the R script is run or started before the 'Press the ENTER key to continue' prompt. I currently have it set up so there is only one R script that runs raven, reads the hydrographs, and calculates the objective function. I am using Raven 2.9.2.

Here is my batch file:
batch.JPG


The batch file successfully begins the R-script

The first thing this script does is run Raven from the command line:
Capture.JPG


However, once the model has executed, it does not run the remainder of the code - the script just sort of stalls.

Unfortunately my knowledge of DOS commands is limited.

Best,
Stefan
You do not have the required permissions to view the files attached to this post.

rchlumsk
Posts: 156
Joined: Fri Sep 16, 2016 3:18 pm

Re: Ostrich calibration using R-driver script to calculate objective function

Postby rchlumsk » Mon Sep 23, 2019 9:03 am

Hi Stefan

My setup is typically to have multiple R files for analysis and run Raven from DOS commands, but this works equally well. I just went through and recreated your setup with my own test case, using Rscript to run R commands and the Irond model from the Raven tutorial files with Raven v2.9.2 on Windows 64bit. However, I am not able to recreate your error on my machine. Here are my .bat and .R files, see if you can spot anything that helps you spot a difference that may be causing this. I also checked the .rvi file in case there was some specific Raven command to not wait for input when done but I don't believe there is.

Note - I added the :SilentMode command to the rvi file to reduce the output to the console. But this setup works with or without that command in the rvi file.

test.bat

Code: Select all

PUSHD C:\test
Rscript testR2.R
POPD


testR2.R

Code: Select all

ravendir <- "C:/test/Raven.exe"
indir <- "C:/test/Irond/Irond"
RavenCMD <- paste(shQuote(ravendir), shQuote(indir))
RavenCMD

system(RavenCMD)
cat("test message!")


Output from Command Line:

Code: Select all

C:\test>test.bat

C:\test>PUSHD C:\test

C:\test>Rscript testR2.R
[1] "\"C:/test/Raven.exe\" \"C:/test/Irond/Irond\""
============================================================
                        RAVEN
 a robust semi-distributed hydrological modelling framework
    Copyright 2008-2019, the Raven Development Team
                    Version 2.9.2
                BuildDate May 30 2019
============================================================


===============Exiting Gracefully==============
Exiting Gracefully: Successful Simulation
===============================================
===============================================
[1] 0
test message!
C:\test>POPD


If you recreate this setup and use the same .bat and setup as I have below with the Irond model, then it must be a difference in system settings on the machines we are running. You will notice that in the R output, I receive a

Code: Select all

[1] 0
output from the [code]system()[\code] command in R, which is obviously not happening in yours. The test message also prints out, indicating that the R script continues to run after the Raven run.

Let me know if you are able to run it successfully with the Irond model and this setup or if there is some other factor. If this is some other factor I am not sure how much I will be able to help further, but at least it narrows down the problem. Hope that helps.

Cheers,
Rob
Robert Chlumsky
rchlumsk@uwaterloo.ca

sgronsdahl
Posts: 7
Joined: Sun Sep 22, 2019 9:13 pm

Re: Ostrich calibration using R-driver script to calculate objective function

Postby sgronsdahl » Mon Sep 23, 2019 12:50 pm

Hi Rob,

I tried setting it up with the Irond model as you indicated and it worked as it should have. I then opened up my input files to see if there was anything unusual. I based my model off of a model template that I received from someone else. There is a section of code within the .rvi file that seems to have been the culprit. I commented these lines out and its working!

Capture.JPG


Thanks again for your help with everything.

Stefan
You do not have the required permissions to view the files attached to this post.

rchlumsk
Posts: 156
Joined: Fri Sep 16, 2016 3:18 pm

Re: Ostrich calibration using R-driver script to calculate objective function

Postby rchlumsk » Mon Sep 23, 2019 3:01 pm

Hi Stefan

That makes sense, it is likely connected to the :EndPause option only, you might be able to uncomment the rest (depending on the options and setup you want to have, some of those will impact the results). Should have thought of the EndPause command sooner, but glad it is working now in any case.

Cheers,
Rob
Robert Chlumsky
rchlumsk@uwaterloo.ca


Return to “Help & Support”