weatherdatagrab

Extract 3 sets of data from 2 different sources to obtain weather and tide data for a Tableau dashboard

Licence for data sources are given at end of document.

  1. Extract a current forcast for a location from open-meteo.com
  2. Extract a 7 day forecast for a location from open-meteo.com
  3. Extact tidal information for a location from UK Hydrographic office

The 3 csv files can then be fed into a Tablea dashboard.

These API requests can be set to run automaticaly by creating a script file with the code chunks and then running it automatically in windows scheduler to obtain fresh data as required.



#### Step 1.

API request for given site Lat Long to obtain current weather data and save to csv file

library(httr)
library(jsonlite)
library(dplyr)

# API request setup
api_url <- "https://api.open-meteo.com/v1/forecast"
params <- list(
  latitude = 50.3577,
  longitude = -4.6934,
  current = paste(
    "temperature_2m",
    "relative_humidity_2m",
    "apparent_temperature",
    "is_day",
    "wind_speed_10m",
    "wind_direction_10m",
    "wind_gusts_10m",
    "precipitation",
    "rain",
    "showers",
    "snowfall",
    "weather_code",
    "pressure_msl",
    "cloud_cover",
    "surface_pressure",
    sep = ","
  ),
  forecast_days = 1
)

# Make the API call
response <- GET(api_url, query = params)
data <- content(response, as = "parsed", simplifyVector = TRUE)

# Extract current data
current_data <- data$current
current_df <- data.frame(
  time = current_data$time,
  temperature_2m = current_data$temperature_2m,
  relative_humidity_2m = current_data$relative_humidity_2m,
  apparent_temperature = current_data$apparent_temperature,
  is_day = current_data$is_day,
  wind_speed_10m = current_data$wind_speed_10m,
  wind_direction_10m = current_data$wind_direction_10m,
  wind_gusts_10m = current_data$wind_gusts_10m,
  precipitation = current_data$precipitation,
  rain = current_data$rain,
  showers = current_data$showers,
  snowfall = current_data$snowfall,
  weather_code = current_data$weather_code,
  pressure_msl = current_data$pressure_msl,
  cloud_cover = current_data$cloud_cover,
  surface_pressure = current_data$surface_pressure
)

# Save current data to CSV for Tableau
write.csv(current_df, "current_weather_data.csv", row.names = FALSE)
print("Data saved to current_weather_data.csv")
## [1] "Data saved to current_weather_data.csv"
# Display the first rows of the dataframe
head(current_df)
##               time temperature_2m relative_humidity_2m apparent_temperature
## 1 2025-03-27T15:45             11                   94                 10.1
##   is_day wind_speed_10m wind_direction_10m wind_gusts_10m precipitation rain
## 1      1             14                179           20.9             0    0
##   showers snowfall weather_code pressure_msl cloud_cover surface_pressure
## 1       0        0            3         1020         100           1015.6



Step 2

API request to obtain 7 day forecast for given site Lat Long and save to csv file.

#library(httr)
#library(jsonlite)
#library(dplyr)

# API request setup
api_url <- "https://api.open-meteo.com/v1/forecast"
params <- list(
  latitude = 50.3577,
  longitude = -4.6934,
  daily = paste(
    "sunset",
    "sunrise",
    sep = ","
  ),
  hourly = paste(
    "temperature_2m",
    "relative_humidity_2m",
    "apparent_temperature",
    "precipitation_probability",
    "precipitation",
    "rain",
    "snowfall",
    "weather_code",
    "pressure_msl",
    "surface_pressure",
    "cloud_cover",
    "visibility",
    "wind_speed_10m",
    "wind_direction_10m",
    "wind_gusts_10m",
    "uv_index",
    "is_day",
    "dew_point_2m",
    "showers",
    sep = ","
  ),
  models = "best_match",
  timezone = "Europe/London",
  past_days = 2
)

# Make the API call and parse the JSON response
response <- GET(api_url, query = params)
data <- content(response, as = "parsed", simplifyVector = TRUE)

# Process hourly data and rename columns for Tableau
hourly <- data$hourly
hourly_df <- data.frame(
  "Forecast Time" = as.POSIXct(hourly$time, format = "%Y-%m-%dT%H:%M", tz = "UTC"),
  "Forecast Temperature 2M" = hourly$temperature_2m,
  "Forecast Relative Humidity 2M" = hourly$relative_humidity_2m,
  "Forecast Apparent Temperature" = hourly$apparent_temperature,
  "Forecast Precipitation Probability" = hourly$precipitation_probability,
  "Forecast Precipitation" = hourly$precipitation,
  "Forecast Rain" = hourly$rain,
  "Forecast Snowfall" = hourly$snowfall,
  "forecast_weather_code" = hourly$weather_code,
  "Forecast Pressure MSL" = hourly$pressure_msl, 
  "Forecast Surface Pressure" = hourly$surface_pressure,
  "Forecast Cloud Cover" = hourly$cloud_cover,
  "Forecast Visibility" = hourly$visibility,
  "Forecast Wind Speed 10M" = hourly$wind_speed_10m,
  "Forecast Wind Direction 10M" = hourly$wind_direction_10m,
  "Forecast Wind Gusts 10M" = hourly$wind_gusts_10m,
  "Forecast UV Index" = hourly$uv_index,
  "Forecast Is Day" = hourly$is_day,
  "Dew Point 2M" = hourly$dew_point_2m,  
  "Forecast Showers" = hourly$showers
)

# Process is day/night data
daily <- data$daily
daily_df <- data.frame(
  date = as.POSIXct(daily$time, format = "%Y-%m-%d", tz = "UTC"),
  sunset = daily$sunset,
  sunrise = daily$sunrise
)

# Print a preview of data
print(head(hourly_df))
##         Forecast.Time Forecast.Temperature.2M Forecast.Relative.Humidity.2M
## 1 2025-03-25 00:00:00                     5.2                            95
## 2 2025-03-25 01:00:00                     5.0                            96
## 3 2025-03-25 02:00:00                     5.7                            92
## 4 2025-03-25 03:00:00                     5.3                            91
## 5 2025-03-25 04:00:00                     5.0                            91
## 6 2025-03-25 05:00:00                     4.9                            92
##   Forecast.Apparent.Temperature Forecast.Precipitation.Probability
## 1                           3.9                                  0
## 2                           4.3                                  0
## 3                           3.1                                  0
## 4                           3.6                                  0
## 5                           3.1                                  0
## 6                           2.7                                  0
##   Forecast.Precipitation Forecast.Rain Forecast.Snowfall forecast_weather_code
## 1                      0             0                 0                     3
## 2                      0             0                 0                     3
## 3                      0             0                 0                     3
## 4                      0             0                 0                     3
## 5                      0             0                 0                     3
## 6                      0             0                 0                     3
##   Forecast.Pressure.MSL Forecast.Surface.Pressure Forecast.Cloud.Cover
## 1                1023.8                    1019.3                  100
## 2                1023.9                    1019.4                  100
## 3                1023.8                    1019.3                   99
## 4                1023.8                    1019.3                  100
## 5                1024.0                    1019.5                  100
## 6                1023.8                    1019.3                  100
##   Forecast.Visibility Forecast.Wind.Speed.10M Forecast.Wind.Direction.10M
## 1               24140                     5.4                         360
## 2               24140                     7.6                         335
## 3               24140                     8.9                         328
## 4               24140                     5.8                         353
## 5               24140                     4.7                          23
## 6               24140                     3.8                          17
##   Forecast.Wind.Gusts.10M Forecast.UV.Index Forecast.Is.Day Dew.Point.2M
## 1                    11.2                 0               0          4.5
## 2                    10.1                 0               0          4.4
## 3                    11.5                 0               0          4.5
## 4                    10.8                 0               0          4.0
## 5                     5.8                 0               0          3.6
## 6                    10.8                 0               0          3.7
##   Forecast.Showers
## 1                0
## 2                0
## 3                0
## 4                0
## 5                0
## 6                0
print(head(daily_df))
##         date           sunset          sunrise
## 1 2025-03-25 2025-03-25T18:39 2025-03-25T06:09
## 2 2025-03-26 2025-03-26T18:41 2025-03-26T06:07
## 3 2025-03-27 2025-03-27T18:42 2025-03-27T06:05
## 4 2025-03-28 2025-03-28T18:44 2025-03-28T06:03
## 5 2025-03-29 2025-03-29T18:46 2025-03-29T06:00
## 6 2025-03-30 2025-03-30T18:47 2025-03-30T05:58
# Save to CSV for Tableau
write.csv(hourly_df, "forecast_hourly_weather_data.csv", row.names = FALSE)
write.csv(daily_df, "daily_sunrise_sunset_data.csv", row.names = FALSE)

print("Data saved to hourly_weather_data.csv and daily_weather_data.csv")
## [1] "Data saved to hourly_weather_data.csv and daily_weather_data.csv"



#### Step 3

API request to UK Hydro office to obtain tide informaton for local site and save to csv file

#library(httr)
#library(jsonlite)

# API URL
url <- "https://admiraltyapi.azure-api.net/uktidalapi/api/V1/Stations/0008/TidalEvents"

# Rquest headers
headers <- add_headers(
  `Cache-Control` = "no-cache",
  `Ocp-Apim-Subscription-Key` = "810112e61b6543129fd9739f96965786"
)

# API call
response <- GET(url, headers)

# Check the response status code
status_code <- status_code(response)
print(paste("Response Code:", status_code))
## [1] "Response Code: 200"
# Parse and save data if the request is successful
if (status_code == 200) {
  # Parse the JSON content
  tidal_data <- content(response, as = "parsed", simplifyVector = FALSE)
  
  # Ensure `tidal_data` is treated as a list of events
  tidal_df <- data.frame(
    DateTime = sapply(tidal_data, function(x) x$DateTime),
    EventType = sapply(tidal_data, function(x) x$EventType),
    Height = sapply(tidal_data, function(x) x$Height)
  )
  
  # Save to CSV for Tableau
  write.csv(tidal_df, "tidal_events_data.csv", row.names = FALSE)
  print("Tidal data saved to tidal_events_data.csv")
  
  # Preview the data
  print(head(tidal_df))
} else {
  # Print error message if the request fails
  print("Failed to fetch data. Please check API key and parameters.")
}
## [1] "Tidal data saved to tidal_events_data.csv"
##                  DateTime EventType   Height
## 1 2025-03-27T03:42:43.333 HighWater 4.941922
## 2 2025-03-27T10:10:24.167  LowWater 0.895907
## 3     2025-03-27T16:16:15 HighWater 4.895074
## 4     2025-03-27T22:33:05  LowWater 0.858783
## 5   2025-03-28T04:31:02.5 HighWater 5.263757
## 6 2025-03-28T11:00:41.667  LowWater 0.473054

Data licences

https://open-meteo.com/en/license
http://www.ukho.gov.uk/copyright/index.aspx#Noncommercialpurpose

#citation(ggplot) #citation(httr) #citation(jsonlite) #citation(dplyr)