1

I have a few lists in Python that I am trying to write to NetCDF files, but I am unsure how to get it to work.

An example of how the lists are structured is:

Each list consists of a number of lists itself, say 17, each having 3 elements. The first element species a year, so just an integer or a string, the second element consists of 36 arrays, and the third element consists of 5 arrays.

Based on other guides, I tried to write the code in this manner:

fn = 'mean_yearly_list.nc'
ds = nc.Dataset(fn, 'w', format='NETCDF4')

year = ds.createDimension('date', None)
lat = ds.createDimension('lat', 36)
clim = ds.createDimension('clim', 5)

years = ds.createVariable('date', 'f4', ('year',))
lats = ds.createVariable('lat', 'f4', ('lat',))
clims = ds.createVariable('clim', 'f4', ('clim',))
value1 = ds.createVariable('value_lat', 'f4', ('years', 'lat',))
value2 = ds.createVariable('value_clim', 'f4', ('years', 'clim',))

But now I am unsure of how to exactly write my lists/arrays to these variables in the NetCDF file. Does anyone know how this is easiest done? Thanks for any help!

1 Answer 1

1

Let us assume you have variables/lists with values as yeardata, latdata, climdata,value1data, value2data. Then all you have to do is:

years[:] = yeardata;
lats[:] = latdata;
clims[:] = climdata;
value1[:] = value1data;
value2[:] = value2data;

ds.close()

As you can already assume, the dimensions of the data has to match with the dimensions in the netCDF file.

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

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.