简体   繁体   中英

How to write netCDF file with character entries in dimension variable using R

I am not able to figure this out myself and thus was hoping for some ideas from your side.

I am trying to create a netCDF-4 file in R using the netcdf4 library. The nc-file should contain a variable, let's say temperature (ta), which has 4 dimensions: longitude, latitude, time, and pressure level.

Because of certain reasons I would like the last dimension "pressure level" to consist of character entries, like "upper level", "lower level". Unfortunately, the function ncdim_def() only allows numerical values and not characters.

I have looked into many things including nco/cdo commands, but did not find a solution. Does anybody have an idea?

Any help would be much appreciated!

I found a workaround for my question. So what you can do is:

  • create an array with two lines; rbind("upper level", "lower level")
  • create a dimension for the ncols (dim_p1) and one for the nrows (dim_p2) of the array. IMPORTANT: use ncdim_def(...,create_dimvar=FALSE)
  • create a new variable with ncvar_def(...,dim=list(dim_p1,dim_p2), prec="char")
  • use ncvar_put() to write the array into the variable

Hope this helps.

You can code upper and lower levels in numeric form ie. 1 for upper and 0 for lower levels of pressure.

This may require decoding back to string at a later stage.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM