DOI-USGS / lake-temperature-process-models

Creative Commons Zero v1.0 Universal
1 stars 4 forks source link

Need to programmatically pull GLM version #8

Closed hcorson-dosch-usgs closed 2 years ago

hcorson-dosch-usgs commented 2 years ago

We'd like to be able to programmatically extract the GLM version and include it in the diagnostic tble we return from our model runs.

the function GLM3r::glm_version() returns a message, and the value 0:


    -------------------------------------------------------
    |  General Lake Model (GLM)   Version 3.1.0a4         |
    -------------------------------------------------------

     glm built using MSC version 1916
--help  : show this blurb
--nml <nmlfile> : get parameters from nmlfile
--xdisp : display temp/salt and selected others in x-window
--xdisp <plotsfile> : like --xdisp, but use <plotsfile> instead of plots.nml
--saveall : save plots to png files
--save-all-in-one : save all plots to png file
--save-all-in-one <destfile> : save all plots to png file <destfile>
--quiet   : less messages
--quiet <level> : set quiet level (1-10)
[1] 0

Jordan did some digging, and realized we could set stdout=TRUE to have the output go to the assignment value. That gets us the following:

> out <- system2("C:/Users/hcorson-dosch/Documents/R/AquaticEcoDynamics/GLM/glm", wait = TRUE, stdout = TRUE, stderr = "", args = "--help")
> out
 [1] "     "                                                                       
 [2] "    -------------------------------------------------------"                 
 [3] "    |  General Lake Model (GLM)   Version 3.1.0a4         |"                 
 [4] "    -------------------------------------------------------"                 
 [5] "     "                                                                       
 [6] "     glm built using MSC version 1916"                                       
 [7] "--help  : show this blurb"                                                   
 [8] "--nml <nmlfile> : get parameters from nmlfile"                               
 [9] "--xdisp : display temp/salt and selected others in x-window"                 
[10] "--xdisp <plotsfile> : like --xdisp, but use <plotsfile> instead of plots.nml"
[11] "--saveall : save plots to png files"                                         
[12] "--save-all-in-one : save all plots to png file"                              
[13] "--save-all-in-one <destfile> : save all plots to png file <destfile>"        
[14] "--quiet   : less messages"                                                   
[15] "--quiet <level> : set quiet level (1-10)"                                    
> out[3]
[1] "    |  General Lake Model (GLM)   Version 3.1.0a4         |"

That almost gets us there, but there's two issues: 1) we'd need to parse that line with regex (not a big deal) 2) I'm having to manually provide my path to GLM, despite it being set with Sys.setenv(GLM_PATH = "C:/Users/hcorson-dosch/Documents/R/AquaticEcoDynamics/GLM/glm"), so we'd either need system2 to recognize that path or pass the path as an argument to our execution function (more of a pain).

In light of these issues, Jordan is recommending solving this issue at the GLM3r package level, by solving this issue he created and putting it in a PR to the package and then updating the package version.

jordansread commented 2 years ago

First part of this doesn't require us to make the decision as to where we solve the problem (this repo vs the GLM3r pkg itself). We need to figure out how to extract "3.1.0a4" from " | General Lake Model (GLM) Version 3.1.0a4 |"

After that we'll check back in with GLM3r devs/users to see if this feature would be useful to others and welcomed as an update to the pkg. I think it may be easier to do there because it already handles issues with different OS's and env variables.

hcorson-dosch-usgs commented 2 years ago

Oh ok - I can tackle that regex, then, if you like.

jordansread commented 2 years ago

Sorry, I was just stating how I'd approach the problem. I'm good with taking it on.

jordansread commented 2 years ago
stringr::str_extract(" | General Lake Model (GLM) Version 3.1.0a4 |", '(?<=Version\\ ).+?(?=\\ )')
[1] "3.1.0a4"

Seems to do it. That would be a solution for the pipeline since we don't care about the stringr dependency. But that won't work for GLM3r pkg since we don't want to add other dependencies without a really good reason.

strcapture('(?<=Version\\ ).+?(?=\\ )', " | General Lake Model (GLM) Version 3.1.0a4 |", proto = data.frame(version = character(0)), perl = TRUE)
Error in strcapture("(?<=Version\\ ).+?(?=\\ )", " | General Lake Model (GLM) Version 3.1.0a4 |",  : 
  The number of captures in 'pattern' != 'length(proto)'

Fails for me but not sure why it is capturing more or less than one thing

jordansread commented 2 years ago

In base R:

regmatches(" | General Lake Model (GLM) Version 3.1.0a4 |", regexec("(?<=Version\ ).+?(?=\ )", " | General Lake Model (GLM) Version 3.1.0a4 |", perl = TRUE))[[1]]
[1] "3.1.0a4"
jordansread commented 2 years ago

@hcorson-dosch this seems to be what we'd want: https://github.com/jread-usgs/GLM3r/commit/830d04ac5b29ca831cbea8ec98026190c331acd8

glm_version()

    -------------------------------------------------------
    |  General Lake Model (GLM)   Version 3.2.0a3         |
    -------------------------------------------------------

     glm built using gcc version 4.2.1
glm_version(as_char = TRUE)
[1] "3.2.0a3"
hcorson-dosch-usgs commented 2 years ago

Great! should I go ahead and add this into my PR?

jordansread commented 2 years ago

Sure - would you be adding the glm_version(as_char = TRUE) to your PR in that tibble and suggesting a GH install, or would you add the whole GLM3r change to the PR? I think I'd recommend the former, keeping this in GLM3r, and I can do a PR on that repo and also up the package version so it is clear what needs to be installed (and eventually, what needs to be in the shifter container). But let me know if you were thinking of pulling that whole change and backing code in here.

hcorson-dosch-usgs commented 2 years ago

Oh, gosh, I'm sorry. I was referring to adding the regex only, but I was forgetting my own note about the glm path 🤦‍♀️.

I can add either a) add glm_version(as_char = TRUE) and suggest a GH install, or b) leave my PR as is, and we can wait to add that to the tibble once the change is PR'd into GLM3r.

jordansread commented 2 years ago

I think keeping your PR as-is and leaving this issue open until we resolve it would be good. We'll get a sense if others are interested in this functionality soon

jordansread commented 2 years ago

Merged here https://github.com/GLEON/GLM3r/pull/24