maximusdooku
maximusdooku

Reputation: 5542

How can I reduce the dimension of the netCDF file and change data?

dimensions:
    i1 = 3 ;
    x = 11 ;
    s1 = 1 ;
    mid1 = 8 ;
    mid2 = 8 ;
variables:
    double Height(i1,x) ;
    double Temp(s1, x) ;
    short Soil(s1, x) ;
    double Liq(mid1, x) ;

I have a netCDF file on which I want to reduce the size of one of the dimensions mid1 and replace values:

icond <- ncdf4::nc_open('dat.nc)

#New dimensions for new file
idim <- icond$dim[['i1']]
xdim <- icond$dim[['x']]
s1dim <- icond$dim[['s1']]
mid1dim <- ncdim_def("mid1", "", 1:3) #3 layers
mid2dim <- icond$dim[['mid2']]

mv <- -9999

#Get variable data
Liqxdat <- ncvar_get(icond, 'Liq')[,1:3] #3 Layers 

#Define new variable
Liqx = ncvar_def( "Liq", "units", list(mid1dim, i1), mv, prec="double")

#Create netCDF file
nc =  nc_create("test.nc", list(Height, Temp, Soil, Liqx)

#Write data to the NetCDF file
ncvar_put(nc, Liqx, Liqxdat)

But this is not giving me any data in the output file.

Upvotes: 0

Views: 702

Answers (1)

RFelber
RFelber

Reputation: 164

Unfortunately I cannot run your code. So I just can guess why it's not working.

  1. There is a closing bracket missing on the line nc = nc_create(..).
  2. Height, Temp, and Soil are not defined.
  3. With nc_create you create an new file, but you do not add data to an existing file, is that what you want? If you want to add a new variable you have to use ncvar_add()

Upvotes: 1

Related Questions