Assigning to part of a NetCDF4 Variable

26 views
Skip to first unread message

Matthew Norman

unread,
Jun 5, 2013, 5:18:23 PM6/5/13
to netcdf4...@googlegroups.com
I want to assign data to part of a netCDF4 Variable, but what I'm doing below does not seem to be working. What ends up in the file is not correct based on the binary data I'm reading in. Do you see any obvious errors, particularly in the assigning of data to the netcdf Varaible at the very en?

Thanks,

-Mtt



from Numeric import *
from struct import unpack
from netCDF4 import Dataset

x0 = -645*1000
y0 = -3370*1000
dx = 500
dy = 500
nx = 3010
ny = 5460
nt = 2
filebase = [ './2007/greenland_vel_mosaic500_2007_2008', \
             './2008/greenland_vel_mosaic500_2008_2009' ]

print('Creating netCDF file')
nc = Dataset('greenland_vel_mosaic500.nc', 'w', format='NETCDF4')
x_d  = nc.createDimension('x', nx)
y_d  = nc.createDimension('y', ny)
t_d  = nc.createDimension('t', nt)
x_v  = nc.createVariable('x' ,'f4',('x',))
y_v  = nc.createVariable('y' ,'f4',('y',))
t_v  = nc.createVariable('t' ,'f4',('t',))
vx_v = nc.createVariable('vx','f4',('t','y','x',))
vy_v = nc.createVariable('vy','f4',('t','y','x',))
vm_v = nc.createVariable('vm','f4',('t','y','x',))
vx_v.missing_value = -2.e9
vy_v.missing_value = -2.e9
vm_v.missing_value = -2.e9
x_v[:] = [i for i in range(x0,x0+nx*dx,dx)]
y_v[:] = [i for i in range(y0,y0+ny*dy,dy)]
t_v[:] = [2007.,2008.]

for n in range(nt) :
  print('Reading vx and vy for file: ',n)
  f = open(filebase[n]+'.vx','rb')
  vx_bin = f.read()
  f.close()
  f = open(filebase[n]+'.vy','rb')
  vy_bin = f.read()
  f.close()
  vx = [[0. for i in range(nx)] for j in range(ny)]
  vy = [[0. for i in range(nx)] for j in range(ny)]
  vm = [[0. for i in range(nx)] for j in range(ny)]
  for j in range(ny) :
    if (j%200 == 0) :
      print("Percentage complete: ",float(j)/ny*100.)
    for i in range(nx) :
      ind = j*nx+i
      beg =  ind   *4
      end = (ind+1)*4
      vx[j][i] = float( unpack('>f',vx_bin[beg:end])[0] )
      vy[j][i] = float( unpack('>f',vy_bin[beg:end])[0] )
      if (vx[j][i] != -2.e9) :
        vm[j][i] = sqrt( vx[j][i]**2 + vy[j][i]**2 )
      else :
        vm[j][i] = -2.e9

  print('writing vx, vy, and vm to file')
  vx_v[n,:,:] = vx
  vy_v[n,:,:] = vy
  vm_v[n,:,:] = vm

nc.close()

Гузий Саша

unread,
Aug 14, 2013, 2:06:10 PM8/14/13
to netcdf4...@googlegroups.com


On Wednesday, 5 June 2013 17:18:23 UTC-4, Matthew Norman wrote:
I want to assign data to part of a netCDF4 Variable, but what I'm doing below does not seem to be working. What ends up in the file is not correct based on the binary data I'm reading in. Do you see any obvious errors, particularly in the assigning of data to the netcdf Varaible at the very en?

Thanks,

-Mtt

Hi Norman:

it would be easier to look at your problem if we had a script converting 1 binary file to a netcdf, and the input binary file.

Cheers
--
Sasha
Reply all
Reply to author
Forward
0 new messages