Rain – Evapotranspiration = mm Water

[This article was first published on R Programming Archives - Mark Niemann-Ross, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

“Eeee-VAP-oooo-TRANS-PURR-ation,” I savor the word as I release it into our conversation. I’m still at the party with Marsha and Bob. We’re trying to determine why anyone (such as me) would want to use R on their Raspberry Pi.

Photo by Tima Miroshnichenko

“Big word,” says Bob. “What’s it mean?”

“Water evaporation from the earth and transpiration from plants,” I respond. “It’s a sum of the water escaping from my irrigation system. Look it up on Wikipedia.”

Marsha interrupts grumpy Bob; “So – That means, um…desired amount of water – rainfall + evapotranspiration equals the amount of water your irrigation system needs to supply.”

“Precisely,” I agree. “Until I found out about evapotranspiration, I was unsure how to account for temperature. I knew hot days would require more water because of increased evaporation; but was stumped how to translate temperature into increased inches of necessary water.”

“Never heard of it,” says Bob.

“Me neither,” I agree. “Evapotranspiration is handy, but doesn’t show up in all weather forecasts. Open-Meteo makes it available.”

“Say you’ve got seven days worth of this miracle number,” says Bob. “What does the R code look like?”

“I’m so glad you asked,” I reply. I’ve been using my tablet for a snack plate, so I wipe off the cheese dip and open up the github repository showing my irrigation code.

“Remember, I’m using a matrix named “waterByZone” to store a year’s worth of rainfall. I use the day of the year to point to today’s values.”

> yearDay <- as.POSIXlt(Sys.Date())$yday + 1
> yearDay
86

“I use a call to open-meteo to retrieve the values for rainfall and evapotranspiration. That’s all put together by a few calculations with the waterByZone information.”

# get the sum of rainfall for yesterday, today, and tomorrow
recentRainfall <- sum(waterByZone["rainfall", (yearDay - 1):(yearDay + 1)])
# get the sum of evapotranspiration for three days
recentEVOTRP <- sum(waterByZone["evapotranspiration", (yearDay - 1):(yearDay + 1)])
# neededInFront is amount of rain needed today
neededRain <- waterByZone["neededInFront", yearDay] - recentRainfall + recentEVOTRP
# needed rain is never less than zero, so check that limit
waterByZone["wateredInFront", yearDay] <- ifelse(neededRain <= 0, 0, neededRain)

If I had nothing else to do, I would rewrite this as:

  • C or C++
  • Python
  • An R pipeline using |> instead of %>% (I’m focused on Base R wherever possible)

…but I’ll leave that to you, intrepid readers.

What do you think?

MNR

The post Rain – Evapotranspiration = mm Water appeared first on Mark Niemann-Ross.

To leave a comment for the author, please follow the link and comment on their blog: R Programming Archives - Mark Niemann-Ross.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)