[MITgcm-support] Read NetCDF data (Internal Waves)

Ryan Abernathey ryan.abernathey at gmail.com
Fri Jun 3 16:04:38 EDT 2016


For reading and writing netcdf in Python, it is almost always easier to go through xarray
http://xarray.pydata.org/en/stable/
rather than using the netcdf4 module directly. I can't recommend this package more highly. 

-Ryan

> On Jun 3, 2016, at 3:10 PM, Alejandro Jiménez Rico <paulexca at hotmail.com> wrote:
> 
> You are right. But it still does not work. Thanks to that answer, I wrote this:
> 
> import netCDF4
> import numpy as np
> 
> ncfile = netCDF4.Dataset('/path/to/file/datatoedit.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,:])
> 
> # Write out u_center and v_center into a new netCDF file
> ncfile_out = netCDF4.Dataset('./dataedited.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()
> 
> It tell me an error at lines 22 and 23 about dimensions. I want now to introduce the time dependence here, but I don't know how to do it.
> 
> 
> > From: martin.losch at awi.de
> > Date: Thu, 2 Jun 2016 13:54:14 +0200
> > To: mitgcm-support at mitgcm.org
> > Subject: Re: [MITgcm-support] Read NetCDF data (Internal Waves)
> > 
> > Alejandro
> > > On 01 Jun 2016, at 13:09, Alejandro Jiménez Rico <paulexca at hotmail.com> wrote:
> > > 
> > > Now I am trying to interpole all U and V variables. I just need to export all matrix and put all values on the same coordinates. How can I do this?
> > 
> > This question was already answered a couple of days ago:
> > http://mitgcm.org/pipermail/mitgcm-support/2016-May/010438.html
> > 
> > M.
> > 
> > 
> > _______________________________________________
> > MITgcm-support mailing list
> > MITgcm-support at mitgcm.org
> > http://mitgcm.org/mailman/listinfo/mitgcm-support
> _______________________________________________
> MITgcm-support mailing list
> MITgcm-support at mitgcm.org
> http://mitgcm.org/mailman/listinfo/mitgcm-support
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mitgcm.org/pipermail/mitgcm-support/attachments/20160603/dbd66432/attachment-0001.htm>


More information about the MITgcm-support mailing list