EGRET icon indicating copy to clipboard operation
EGRET copied to clipboard

wBT saveOutput equivalent for GFN?

Open elp94 opened this issue 2 years ago • 9 comments

Hello! Is there a wBT saveOutput equivalent for using runPairs or runSeries for GFN? I'd like to save the formatted output from the console to a text file for later reference.

Thank you!

elp94 avatar May 03 '22 19:05 elp94

There isn't, but that's a good idea. I could probably take the code I'm pasting here and add it to EGRET.

Here's some code for the pairs you could use in the meantime:

print_pairs <- function(eList, pairResults){
  
  Other <- attr(pairResults, "Other")
  SampleBlocks <- attr(pairResults, "SampleBlocks") 
  yearPairInfo <- attr(pairResults, "yearPair")
  
  sample1EndDate <- SampleBlocks["SampleBlocks"]
  
  cat("\n  ", eList$INFO$shortName, "\n  ", eList$INFO$paramShortName)
  periodName <- setSeasonLabelByUser(eList$INFO$paStart, eList$INFO$paLong)
  cat("\n  ", periodName, "\n")
  if (Other$wall) 
    cat("\n Sample data set was partitioned with a wall right after ", 
        as.character(sample1EndDate), "\n")
  cat("\n Change estimates ", yearPairInfo[["year2"]], " minus ", yearPairInfo[["year1"]], "\n")
  totChange <- format(pairResults[1, 1], digits = 3)
  totChangePct_conc_f <- format(Other$PercentChangeConc[["Total Percent Change"]], digits = 2)
  cat("\n For concentration: total change is ", totChange, 
      "mg/L")
  cat("\n expressed as Percent Change is ", totChangePct_conc_f, "%")
  pctRS <- format(Other$PercentChangeConc[["CQTC Percent"]], digits = 2)
  pctFD <- format(Other$PercentChangeConc[["QTC Percent"]], digits = 2)
  cat("\n\n Concentration v. Q Trend Component ", pctRS, "%\n       Q Trend Component            ", 
      pctFD, "% \n\n")
  totChange <- format(pairResults[2, 1], digits = 3)
  totChangePct_flux_f <- format((Other$PercentChangeFlux[["Total Percent Change"]]), digits = 2)
  cat("\n For flux: total change is ", totChange, "million kg/year")
  cat("\n expressed as Percent Change is ", totChangePct_flux_f, "%")
  pctRS <- format(Other$PercentChangeFlux[["CQTC Percent"]], digits = 2)
  pctFD <- format(Other$PercentChangeFlux[["QTC Percent"]], digits = 2)
  cat("\n\n Concentration v. Q Trend Component ", pctRS, "%\n       Q Trend Component            ", 
      pctFD, "% \n\n")
  print(pairResults[,1:7], digits = 2)
}

After you copy/paste that function in your R console, you can then run:

sink("test.txt")
print_pairs(eList, pairOut)
sink()

# or just to see the output:
print_pairs(eList, pairOut)

The group function would be:

print_groups <- function(eList, groupResults){
  
  SampleBlocks <- attr(groupResults, "SampleBlocks")
  Other <- attr(groupResults, "Other") 
  sample1EndDate <- SampleBlocks["SampleBlocks"]
  
  groupInfo <- attr(groupResults, "groupInfo")
  dateInfo <- attr(groupResults, "dateInfo")
  
  cat("\n  ", eList$INFO$shortName, "\n  ", eList$INFO$paramShortName)
  periodName <- setSeasonLabelByUser(eList$INFO$paStart, eList$INFO$paLong)
  cat("\n  ", periodName, "\n")
  if (Other$wall) 
    cat("\n Sample data set was partitioned with a wall right after ", 
        as.character(sample1EndDate), "\n")
  cat("\n Change estimates for\n average of", groupInfo["group2firstYear"]," through",groupInfo["group2lastYear"],
      " minus average of", groupInfo["group1firstYear"]," through", groupInfo["group1lastYear"], "\n")
  totChange <- format(groupResults[1, 1], digits = 3)
  
  c22 <- groupResults[1, "x22"]
  c11 <- groupResults[1, "x11"]
  cRSpart <- groupResults[1, "CQTC"]
  cFDpart <- groupResults[1, "QTC"]
  f22 <- groupResults[2, "x22"]
  f11 <- groupResults[2, "x11"]
  fRSpart <- groupResults[2, "CQTC"]
  fFDpart <- groupResults[2, "QTC"]
  totChangePct <- format(100 * ((c22 - c11)/c11), digits = 2)
  cat("\n For concentration: total change is ", totChange, 
      "mg/L")
  cat("\n expressed as Percent Change is ", totChangePct, "%")
  pctRS <- format(100 * (cRSpart/c11), digits = 2)
  pctFD <- format(100 * (cFDpart/c11), digits = 2)
  cat("\n\n Concentration v. Q Trend Component ", pctRS, "%\n       Q Trend Component            ", 
      pctFD, "% \n\n")
  totChange <- format(groupResults[2, 1], digits = 3)
  totChangePct <- format(100 * ((f22 - f11)/f11), digits = 2)
  cat("\n For flux: total change is ", totChange, "million kg/year")
  cat("\n expressed as Percent Change is ", totChangePct, "%")
  pctRS <- format(100 * (fRSpart/f11), digits = 2)
  pctFD <- format(100 * (fFDpart/f11), digits = 2)
  cat("\n\n Concentration v. Q Trend Component ", pctRS, "%\n       Q Trend Component            ", 
      pctFD, "% \n\n")
  print(groupResults, digits = 2)
  
}

print_groups(eList, groupOut)
# or to save:
sink("output_groups.txt")
print_groups(eList, groupOut)
sink()

ldecicco-USGS avatar May 03 '22 20:05 ldecicco-USGS

This is great! Thank you so much. I'd love to see this feature added to EGRET for GFN.

After I ran the function and tried to run print_pairs(eList, pairOut), I got an error saying object 'pairOut' not found. Should pairOut in this line be called something else?

Thanks again!

elp94 avatar May 03 '22 21:05 elp94

pairOut would be what you get back from the runPairs function:

eList <- Choptank_eList
year1 <- 1985
year2 <- 2010

pairOut_1 <- runPairs(eList,
                      year1, year2,
                      windowSide = 0)

print_pairs(eList, pairOut_1)

ldecicco-USGS avatar May 03 '22 21:05 ldecicco-USGS

Oh, of course! I apologize for these dummy questions, but what does pairResults refer to in the print_pairs function? I had named the object returned by the runPairs function 'pairResults' which now seems to be causing me some confusion.

elp94 avatar May 03 '22 21:05 elp94

No problem! Does this work for you?

print_pairs(eList, pairResults)

If not, what is the error message you see?

ldecicco-USGS avatar May 03 '22 21:05 ldecicco-USGS

Yes and no. It doesn't produce any errors, but it also doesn't return anything.

elp94 avatar May 03 '22 21:05 elp94

Did you run the sink() command first? sink is a weird function, when you run it once, it starts putting all the console output into a file. To stop sending that output to the file, you need to run sink() again. So maybe try running:

sink() # to finish populating whatever was being saved before
print_pairs(eList, pairResults)

If that doesn't work...maybe restart RStudio? (after saving stuff)

ldecicco-USGS avatar May 03 '22 22:05 ldecicco-USGS

Rerunning sink() didn't do it, but closing and reopening R did! Yay! Thank you! Would it be possible to add additional code to the function that also pulls in the CIs, p values, likelihoods, and the wordsOut text like in the wBT output?

elp94 avatar May 03 '22 22:05 elp94

Those are only calculated when the runPairsBoot (or other "boot" function) is run, so it would need to be a little different. I'll think about how to implement it though.

ldecicco-USGS avatar May 20 '22 17:05 ldecicco-USGS