1

I'll explain a bit my situation: I am trying to edit some variables of a netcdf file and write out a new netcdf file that shows me the new edited variables. My problem is that variables are time-dependent and I don't know how to include this in my code:

import netCDF4 import numpy as np ncfile = netCDF4.Dataset('toread.nc', 'r') u = ncfile.variables['u'][:,:,:] v = ncfile.variables['v'][:,:,:] nx = np.shape(u)[0] - 1 ny = np.shape(v)[1] - 1 nz = np.shape(u)[2] u_center = 0.5 * (u[0:nx,:,:] + u[1:nx+1,:,:]) v_center = 0.5 * (v[:,0:ny,:] + v[:,1:ny+1,:]) ncfile_out = netCDF4.Dataset('./output.nc', 'w') ncfile_out.createDimension('longitude', nx) ncfile_out.createDimension('latitude', ny) ncfile_out.createDimension('level', nz) u_out = ncfile_out.createVariable('u_center', 'f4', ('longitude', 'latitude', 'level')) v_out = ncfile_out.createVariable('v_center', 'f4', ('longitude', 'latitude', 'level')) u_out[:,:,:] = u_center[:,:,:] v_out[:,:,:] = v_center[:,:,:] ncfile_out.close() 

I tried to compile this but it showed a Value Error talking about a dimension problem. I think my variables are 4D including time-dependence but I don't know how to define it and finish my code.

ncdump info:

 netcdf state.global { dimensions: T = UNLIMITED ; // (10001 currently) Xp1 = 61 ; Y = 1 ; Z = 20 ; X = 60 ; Yp1 = 2 ; Zl = 20 ; variables: double Xp1(Xp1) ; Xp1:long_name = "X-Coordinate of cell corner" ; Xp1:units = "meters" ; double Y(Y) ; Y:long_name = "Y-Coordinate of cell center" ; Y:units = "meters" ; double Z(Z) ; Z:long_name = "vertical coordinate of cell center" ; Z:units = "meters" ; Z:positive = "up" ; double X(X) ; X:long_name = "X-coordinate of cell center" ; X:units = "meters" ; double Yp1(Yp1) ; Yp1:long_name = "Y-Coordinate of cell corner" ; Yp1:units = "meters" ; double Zl(Zl) ; Zl:long_name = "vertical coordinate of upper cell interface" ; Zl:units = "meters" ; Zl:positive = "up" ; double T(T) ; T:long_name = "model_time" ; T:units = "s" ; int iter(T) ; iter:long_name = "iteration_count" ; double U(T, Z, Y, Xp1) ; U:units = "m/s" ; U:coordinates = "XU YU RC iter" ; double V(T, Z, Yp1, X) ; V:units = "m/s" ; V:coordinates = "XV YV RC iter" ; double Temp(T, Z, Y, X) ; Temp:units = "degC" ; Temp:long_name = "potential_temperature" ; Temp:coordinates = "XC YC RC iter" ; double S(T, Z, Y, X) ; S:long_name = "salinity" ; S:coordinates = "XC YC RC iter" ; double Eta(T, Y, X) ; Eta:long_name = "free-surface_r-anomaly" ; Eta:units = "m" ; Eta:coordinates = "XC YC iter" ; double W(T, Zl, Y, X) ; W:units = "m/s" ; W:coordinates = "XC YC RC iter" ; // global attributes: :MITgcm_version = "****************" ; :build_user = "************" ; :build_host = "**************" ; :build_date = "*******************" ; :MITgcm_URL = "***************" ; :MITgcm_tag_id = "*******************" ; :MITgcm_mnc_ver = 0.9 ; :sNx = 30 ; :sNy = 1 ; :OLx = 2 ; :OLy = 2 ; :nSx = 2 ; :nSy = 1 ; :nPx = 1 ; :nPy = 1 ; :Nx = 60 ; :Ny = 1 ; :Nr = 20 ; } 

1 Answer 1

2

You have to create another dimension for time:

ncfile_out.createDimension('time', nt) # to make time dimension unlimited put None instead of nt v_out = ncfile_out.createVariable('v_center', 'f4', ('time', 'longitude', 'latitude', 'level')) 

Then add additional array to store time values and fill it:

time = ncfile_out.createVariable('Time', 'i4', 'time') 

Aditional information you may find here: http://pyhogs.github.io/intro_netcdf4.html

Sign up to request clarification or add additional context in comments.

7 Comments

I'll try it. What is the difference between ('Time') and ('time')?
I guess, it does not matter because it's just a name. Problem is your array have 3 dimensions, but you need to write 4d array.
How? Could you post the necessary changes?
Like here: newu = np.zeros((nx,ny,nz,2), dtype=np.float32); newu[:,:,:,0] = u_center[:,:,:]
So should I change v_out[:,:,:] = v_center[:,:,:] by v_out[:,:,:,:]=v_center[:,:,:,:] ?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.