Skip to content

Instantly share code, notes, and snippets.

@sadatnfs
Last active January 6, 2023 10:49
Show Gist options
  • Save sadatnfs/8e73d23e375f361ecab845c5df8c488f to your computer and use it in GitHub Desktop.
Save sadatnfs/8e73d23e375f361ecab845c5df8c488f to your computer and use it in GitHub Desktop.
Extracting an xarray based netcdf file to use in R
require(ncdf4)
require(ncdf4.helpers)
require(data.table)
## Get the name of the value vars in the nc file
get_nc_value_name <- function(nc_file) {
## Get names
nc_obj <- nc_open(nc_file)
name<-names(nc_obj$var)
## Close file
nc_close(nc_obj)
## Return the name
return(name)
}
## Once we have the name of the variable we want to extract, we pass it onto this function to return the full dataset
xarray_nc_to_R <- function(nc_file, dimname, start=NA, count=NA, df_return = T) {
## Open the file and show the attribuets
ncin <- nc_open(nc_file)
print(ncin)
## Get the full array, using the variable name we want
Rarray <- ncvar_get(ncin, dimname, start = start, count = count, collapse_degen=F)
## Get the fillvalue info
fillvalue <- ncatt_get(ncin,dimname,"_FillValue")
## Get the dimension names in the right order
array_dim <- ncdf4.helpers::nc.get.dim.names(ncin, dimname)
## Close the file
nc_close(ncin)
## Get all of the dimension information in the order specified
array_dim_list <- list()
for(i in array_dim) {
array_dim_list[[i]] <- ncin$dim[[i]]$vals
}
## Fill in NaNs with NA
Rarray[Rarray==fillvalue$value] <- NA
## Assign the dimension labels to the R array
for(i in 1:length(array_dim_list)) {
dimnames(Rarray)[[i]] <- array_dim_list[[i]]
}
## Attach the dimension name to the array
names(attributes(Rarray)$dimnames) <- array_dim
if(df_return) {
return(data.frame(reshape2::melt(Rarray)))
} else {
return(Rarray)
}
}
### Testing with Population data from FBD
xarray_data <- "REDACTED"
dimname = get_nc_value_name(xarray_data)
## We'll extract the 'population' column and convert to a DT
## NOTE: We are pulling in the full xarray. If you wanted to slice, then you'd have
## to define the start and count variables... TBV
pop_data <- data.table(xarray_nc_to_R(nc_file = pop_file_2, dimname))
print(pop_data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment