Creates a NetCDF file with an instance dimension, and any attributes from a data frame. Use to create the start of a NetCDF-DSG file. One character length dimension is created long enough to contain the longest provided character string. This function does not implement any CF convention attributes or standard names. Any columns of class date will be converted to character.

write_attribute_data(
  nc_file,
  att_data,
  instance_dim_name = "instance",
  units = rep("unknown", ncol(att_data)),
  overwrite = FALSE
)

Arguments

nc_file

character file path to the nc file to be created. If adding to a file, it must already have the named instance dimension.

att_data

data.frame with instances as columns and attributes as rows.

instance_dim_name

character name for the instance dimension. Defaults to "instance"

units

character vector with units for each column of att_data. Defaults to "unknown" for all.

overwrite

boolean overwrite existing file? Will append if FALSE.

Examples

sample_data <- sf::st_set_geometry(sf::read_sf(system.file("shape/nc.shp", 
                                                           package = "sf")), 
                                   NULL)
example_file <-write_attribute_data(tempfile(), sample_data,
                                    units = rep("unknown", ncol(sample_data)))

try({
  ncdump <- system(paste("ncdump -h", example_file), intern = TRUE)
  cat(ncdump ,sep = "\n")
}, silent = TRUE)
#> netcdf file5e50380b25dc {
#> dimensions:
#> 	instance = 100 ;
#> 	char = 12 ;
#> variables:
#> 	double AREA(instance) ;
#> 		AREA:units = "unknown" ;
#> 		AREA:missing_value = -9999.999 ;
#> 	double PERIMETER(instance) ;
#> 		PERIMETER:units = "unknown" ;
#> 		PERIMETER:missing_value = -9999.999 ;
#> 	double CNTY_(instance) ;
#> 		CNTY_:units = "unknown" ;
#> 		CNTY_:missing_value = -9999.999 ;
#> 	double CNTY_ID(instance) ;
#> 		CNTY_ID:units = "unknown" ;
#> 		CNTY_ID:missing_value = -9999.999 ;
#> 	char NAME(instance, char) ;
#> 		NAME:units = "unknown" ;
#> 	char FIPS(instance, char) ;
#> 		FIPS:units = "unknown" ;
#> 	double FIPSNO(instance) ;
#> 		FIPSNO:units = "unknown" ;
#> 		FIPSNO:missing_value = -9999.999 ;
#> 	int CRESS_ID(instance) ;
#> 		CRESS_ID:units = "unknown" ;
#> 		CRESS_ID:missing_value = -9999 ;
#> 	double BIR74(instance) ;
#> 		BIR74:units = "unknown" ;
#> 		BIR74:missing_value = -9999.999 ;
#> 	double SID74(instance) ;
#> 		SID74:units = "unknown" ;
#> 		SID74:missing_value = -9999.999 ;
#> 	double NWBIR74(instance) ;
#> 		NWBIR74:units = "unknown" ;
#> 		NWBIR74:missing_value = -9999.999 ;
#> 	double BIR79(instance) ;
#> 		BIR79:units = "unknown" ;
#> 		BIR79:missing_value = -9999.999 ;
#> 	double SID79(instance) ;
#> 		SID79:units = "unknown" ;
#> 		SID79:missing_value = -9999.999 ;
#> 	double NWBIR79(instance) ;
#> 		NWBIR79:units = "unknown" ;
#> 		NWBIR79:missing_value = -9999.999 ;
#> }