1

I was able to figure out the lat, lon dimension declaration as indicated in the following code. I think I am close to obtain the netCDF file. But, I get an error.

import numpy as np
import netCDF4
import os

# load the data

path='C:\Users\.spyder2'
os.chdir(path)
# this load the file into a Nx3 array (three columns)
data = np.loadtxt('TRMM_1998_01_0100_newntcl.csv', delimiter=',')


# create a netcdf Data object

with netCDF4.Dataset('TEST_file.nc', mode="w", format='NETCDF4') as ds:
    # some file-level meta-data attributes:
    ds.Conventions = "CF-1.6" 
    ds.title = 'Non TC precipitation'
    ds.institution = 'USU'

    lat = data[:,0] # the first column 
    lon = data[:,1] # the second column 
    precip = data[:,2] # the third column 

    nlat = lat.reshape( (161, 321) )
    nlon = lon.reshape( (161, 321) )

    # time = ds.createDimension('time', 0)
    ds.createDimension('latitude', 161)
    ds.createDimension('longitude', 321)


    precip = ds.createVariable('precip', 'f4', ('latitude', 'longitude'))
    precip[:] = data[:,:]
    ## adds some attributes
    precip.units = 'mm'
    precip.long_name = 'Precipitation'

Error:

Traceback (most recent call last):

  File "<ipython-input-101-483efc7d87e2>", line 42, in <module>
    precip[:] = data[:,:]

  File "netCDF4.pyx", line 3066, in netCDF4.Variable.__setitem__ (netCDF4.c:38720)

  File "netCDF4.pyx", line 3177, in netCDF4.Variable._put (netCDF4.c:39523)

IndexError: size of data array does not conform to slice

I appreciate if you can clarify here a bit or provide me some clues of what's happening to correct it.

Thanks in advance,

3 Answers 3

2

Creating dimensions is done with no assignment to a variable. Just remove the lat= and lon= when defining dimensions and then you can reference the dimensions while creating a variable.

ds.createDimension('latitude', 161)
ds.createDimension('longitude', 321)

precip = ds.createVariable('precip, 'f4', ('latitude', 'longitude',))

Also, be careful with scoping issues as you re-use lat several times in the script. Best to use unique names when dealing with the actual data, named dimensions, and dimension sizes. I usually do something like lat_arr (for the data), nlat (for dimension size), and lat or latitude for the dimension name.

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

2 Comments

Thanks. I did the revisions and now I am getting a different error. Please see the code and error above.
precip is 2D, so it needs to be precip[:,:] = data[:,:]
1

That error "size of data array does not conform to slice" indicates that precip[:] and data[:,:] do not have the same number of items. At that point in the code, it looks like precip should be a variable of size 161x321. You are trying to assign it data[:,:], which looks like all 3 columns of information.

I think what you really want is: precip[:] = data[:,2]

Comments

0

Don't think this is still relevant for op, but I tried the following. I adapted op's code from mine, which I just managed to get to work (python 3.7; jupyter notebook).

...
lon = ds.createVariable('longitude', float, ('longitude',), zlib=True)
lat = ds.createVariable('latittude', float, ('latitude',), zlib=True)
precip = ds.createVariable('precip', float, ('latitude', 'longitude'), zlib=True)

precip[:] = data
lon[:] = lon
lat[:] = lat
...

Make sure your data has the same dimensions (i.e. (161, 321)) as your lat and lon.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.