Evapotranspiration illustration

Rain – Evapotranspiration = mm Water

“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

How about some Tips & Tricks on Programming R?

Sign up to receive content in your inbox every month.

We don’t spam! Read our privacy policy for more info.