Skip to contents

Takes a directory of ERA5 .nc data as an argument and exports the data in CSV format. This function grabs each NetCDF file and runs netcdf_df_formatter() on it. It builds a list of variables across all data frames in the folder and joins data by time, filtering to return only full years of data.

Usage

netcdf_to_csv(site_folder = NULL, output_filepath = NULL, site_name = NULL)

Arguments

site_folder

(character) A folder for one site with NetCDF data. The NetCDF files can be of different variables and of different years so long as it is for one site.

output_filepath

(character) File path to where the output CSV should be written.

site_name

(character) Name of the site that will be concatenated onto CSV file name (e.g. Us_TaS).

Value

.csv file of NetCDF data within the site folder. The .csv file has the file name format: siteID_startYear_endYear_variableName.csv. For example, US-Ho1_2001_2020_tp_t2m.csv. SiteID is determined from lat and lon coordinates in df.sitemetadata. Each CSV file starts from the first hour of a year (e.g., 2000-01-01 00:00) and ends with the last hour of a year (e.g., 2020-12-31 23:00).

Examples

# Point to a folder containing ERA5 .nc files
site_folder <- system.file("extdata", package = "ERA5Flux")
# Specify a site name
site_name <- "US_TaS"
# Create a temporary directory to export our output to
output_filepath <- tempdir()

# Convert NetCDF data to a CSV file
netcdf_to_csv(site_folder, output_filepath, "US_TaS")
#> Saved: US_TaS2017_2019_d2m_ssrd_t2m.csv 

# Read the CSV back in
data <- read.csv(list.files(output_filepath, pattern = "US_TaS", full.names = TRUE))
head(data)
#>          time ssrd      d2m      t2m
#> 1 2.01701e+11    0 16.88320 23.95596
#> 2 2.01701e+11    0 16.95254 24.11782
#> 3 2.01701e+11    0 17.30703 24.22744
#> 4 2.01701e+11    0 17.74941 24.02017
#> 5 2.01701e+11    0 18.20376 23.94253
#> 6 2.01701e+11    0 18.32949 23.63198