I am having problems creating a NetCDF file from my data. The data.frame is massive (110142 rows) and looks something like this:
df <- data.frame(
lon = c(-11.0, -10.9, -10.8, -10.7, -10.6, -10.5, 30.6, 30.7, 30.8, 30.9, 31.0, 31.1),
lat = c(34, 34, 34, 34, 34, 34, 60, 60, 60, 60, 60, 60),
pr = c(NA, NA, NA, NA, NA, NA, 677.2349, 680.4251, 683.1752, 686.4608, 686.1793, 683.9277),
temp = c(NA, NA, NA, NA, NA, NA, 7.845285, 7.744238, 7.686366, 7.626490, 7.620506, NA),
suitability = c(NA, NA, NA, NA, NA, NA, 1, 1, 1, 1, 1, NA)
)
I want to save a NetCDF file to my working directory so I can further work with it and visualize it using different software.
This is what I have done so far:
# define dimensions
x <- ncdim_def("lon", "degreesE", df$lon)
y <- ncdim_def("lat", "degreesN", df$lat)
# Define variables in the NetCDF file
temp_def <- ncvar_def("temp", "degreesC", list(x,y), df$temp)
pr_def <- ncvar_def("pr", "mm/a", list(x,y), df$pr)
suitability_def <- ncvar_def("suitability", "suitability", list(x,y), df$suitability)
# Create the NetCDF file
nc <- nc_create("df_suitability.nc", list(temp_def, pr_def, suitability_def))
# Close the NetCDF file
nc_close(nc)
However, nc_create throws the following error:
Error in R_nc4_enddef: NetCDF: One or more variable sizes violate format constraints [1] "Error, nc_enddef returned an error! Error happened with filename= df_suitability.nc and vars:" [1] "1 : temp" [1] "2 : pr" [1] "3 : suitability" Error in nc_create("df_suitability.nc", list(temp_def, : fatal error in nc_create
After googling I still don't know what it could mean... Is my data.frame just too big? How else could I do it? I would really appreciate any help!!