Prepare Climate Data

日本語版はこちら (Japanese version)

This chapter creates climate variables from Average Value Mesh Data. The data source is NLNI’s Average Value Mesh Data.

Download Average Value Mesh Data

Download required data from:

Data files are distributed as ZIP files. Extract them after download.

Store downloaded data in any folder. In this project, the path is set through the PROJECT_DATA_DIR environment variable.

.Renviron
PROJECT_DATA_DIR=C:/Users/Username/Dropbox/GitHub/japanese_climate_calculation
NoteTerms of use

Please check the NLNI terms of use here.

Load libraries

Set the data directory

data_dir <- Sys.getenv("PROJECT_DATA_DIR")
# If the environment variable is not set:
# data_dir <- "data"

Extract ZIP files

zip_output_dir <- file.path("data", "climate_mesh_data")
dir.create(zip_output_dir, showWarnings = FALSE, recursive = TRUE)

zip_files <- list.files(
  file.path(data_dir, "climate_mesh_data_zip"),
  pattern = "^G02-22_.*\\.zip$",
  full.names = TRUE
)
file_names <- tools::file_path_sans_ext(basename(zip_files))
lapply(zip_files, unzip, overwrite = TRUE, exdir = zip_output_dir)

Read shapefiles

data_list <- list()
data_paths <- list.files(
  zip_output_dir,
  pattern = "^G02-22_.*\\.shp$",
  full.names = TRUE,
  recursive = TRUE
)

for (i in seq_along(data_paths)) {
  path <- data_paths[i]
  name <- file_path_sans_ext(basename(path))
  data_list[[name]] <- st_read(path)
}
sf_climate <- do.call(rbind, data_list)

df_label <- read.csv(
  file.path(data_dir, "manual/mesh_climate_data_labels.csv"),
  fileEncoding = "UTF-8"
)
names(sf_climate) <- make.names(c(df_label$name_en, "geometry"))

Convert values

Values are scaled by 10 in the source data; divide by 10 to recover original units. Missing values are encoded as 999999.

col_index <- c(
  grep("precipitation", names(sf_climate)),
  grep("temperature", names(sf_climate)),
  grep("sunshine_duration", names(sf_climate)),
  grep("global_solar_radiation", names(sf_climate))
)
indices_snow <- grep("maximum_snow_depth", names(sf_climate))

NA_value <- 999999
df <- st_drop_geometry(sf_climate)
df[, col_index][df[, col_index] == NA_value] <- NA
df[, col_index] <- df[, col_index] * 0.1
df[, indices_snow][df[, indices_snow] == NA_value] <- 0
sf_climate <- st_set_geometry(df, st_geometry(sf_climate))

Extract longitude and latitude

sf_climate$longitude <- st_coordinates(st_centroid(sf_climate))[, 1]
sf_climate$latitude <- st_coordinates(st_centroid(sf_climate))[, 2]

Save data

save_dir <- file.path(data_dir, "climate_mesh_data_joined")
dir.create(save_dir, showWarnings = FALSE, recursive = TRUE)
saveRDS(sf_climate, file.path(save_dir, "climate_mesh_data.rds"))

Save as CSV

df_climate <- st_drop_geometry(sf_climate)
write.csv(
  df_climate,
  file.path(save_dir, "climate_mesh_data.csv"),
  row.names = FALSE,
  fileEncoding = "UTF-8"
)