Problem constructiing nc file from workspace variables

Hello everyone. I am trying to save some variables that I have obtained into a .nc file following this example.

The variables in question are uwind_pp_save and uwind_pp_save, whose dimensions are 50361201*21. These variables represent ensemble wind forecast, each one with 50 members measured at 361 values of latitude, 201 of longitude and at 21 different days. I also have the vectors with the values of time, latitude and longitude.

I have written the following:

dimTime <- ncdim_def('time', units='hours since 1900-01-01 00:00:00.0', longname='time', calendar="gregorian", vals=times_used)
dimLat <- ncdim_def('latitude', units='degrees_north', longname='latitude', calendar="gregorian", vals=lat_values)
dimLon<- ncdim_def('longitude', units='degrees_east', longname='longitude', calendar="gregorian", vals=lon_values)
dimU<- ncdim_def('U_wind', units='m/s', longname='U component of wind', calendar="gregorian", vals=uwind_pp_save)
dimV<- ncdim_def('V_wind', units='m/s', longname='V component of wind', calendar="gregorian", vals=vwind_pp_save)
vars_to_be_saved <- list(dimTime, dimLat, dimLon, dimU, dimV)
outputfile <- 'postprocessed_ensemble.nc';
con <- nc_create(outputfile, vars_to_be_saved)

and I get the following error

Error in nc_create(outputfile, vars_to_be_saved) : 
  Error, second arg must either be a ncvar object (created by a call to ncvar_def()) or a list of ncvar objects

I have constructed the dimU, dimV... variables just like in the example and I don't know there the error lies.

Can someone help me?
Regards.
Jaime.

Hi Jaime,
Based on the error message, it looks like your object "vars_to_be_saved" needs to be an ncvar object, which you can create by calling the ncvar_def() function. Have you tried something like obj <- ncvar_def(vars_to_be_saved)?

That said, I'm not sure that that's right because I can't run your code--you haven't included everything necessary for a minimal reproducible example. When I pasted your code into my R session, I couldn't run it because 1) you're missing a library() call to the package ncdf4, which is necessary for some of the functions you're running, 2) the variables times_used and lat_values (and others, etc.) are not defined in your code. You probably defined them elsewhere, but to make it easier for us to help you, could you include the code necessary for those objects in your example?

I hope that helps! Happy to look more into this if you can post fully reproducible code.

Hello again. I have realized where the error is thanks to your previous comment. I have managed to obtain a code which runs without issue.

rm(list=ls())

library(MASS)
library(abind)
library(scoringRules)

library(ncdf4)
library(raster)

vector1 <- c(5, 9, 3) 
vector2 <- c(10, 11, 12, 13, 14, 15) 

result <- array(c(vector1, vector2),  dim = c(50, 361, 201, 21)) 

dimU<- ncdim_def('U_wind', units='m/s', longname='U component of wind', calendar="gregorian", vals=result)
dimV<- ncdim_def('V_wind', units='m/s', longname='V component of wind', calendar="gregorian", vals=result)

varU <- ncvar_def(name='U_wind_pp', units='m/s', dim=list(dimU), missval=NA, longname='U_wind', prec='double')
varV <- ncvar_def(name='V_wind_pp', units='m/s', dim=list(dimV), missval=NA, longname='V_wind', prec='double')


vars_to_be_saved <- list(varU, varV)
outputfile <- 'preprocessed_ensemble.nc'
con <- nc_create(outputfile, vars_to_be_saved)

However, now I cannot find where preprocessed_ensemble.nc is.

It looks like you need to run nc_close(con) after the nc_create command. I'm getting that from the nc_create documentation, here, that says " Keep in mind that the new file may not actually be written to disk until nc_close is called. Always call nc_close when you are done with your file, or before exiting R!"

When I did that, it worked for me (although the code took a really long time to run--not sure if that's normal?). If you still can't find the file after that, it's probably a working directory issue. For me, the file wrote to the root directory of my R project, so I assume yours would do the same, or write to the current working directory. If you want to write to a different directory, I think you could update outputfile and make it into a fuller file path instead of just a file name, although I'm not 100% sure.

I hope that helps! Let me know if you still have problems with it.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.