-1

I resampled NetCDF data and organized in OrderedDict(This is the original organization of original data). But when I use the following script to write it in a new netcdf file:

ncout.createDimension('lat', len(y))
ncout.createDimension('lon', len(x))


# create latitude axis
lat = ncout.createVariable('lat', np.dtype('float64').char, ('lat'))
lat.standard_name = 'latitude'
lat.long_name = 'latitude'
lat.units = 'degrees_north'
lat.axis = 'Y'

# create longitude axis
lon = ncout.createVariable('lon', np.dtype('float64').char, ('lon'))
lon.standard_name = 'longitude'
lon.long_name = 'longitude'
lon.units = 'degrees_east'
lon.axis = 'X'

# create variable array
for key, value in variables.items():
    var = ncout.createVariable(key+'re', np.dtype('float32').char, ('lon', 'lat'))
    var.long_name =key
    var=value


# copy axis from the original dataset
#time= nc_file.time_coverage_end
lon = lons
lat= lats

The output value inside the new file becomes the same value: '9969209968386869046778552952102584320.000' I can not find where the problem is and how I can do that. I have made sure that the output of resample is correct. Could you please give me some advice?

A J A Y
  • 585
  • 1
  • 7
  • 22

1 Answers1

0

You need to write:

var[:]=value

Instead of

var=value

In the first case, you set the values of an existing NetCDF variable to value; in the second case you are just creating a new variable var (not related to the NetCDF file).

Bart
  • 9,825
  • 5
  • 47
  • 73
  • Thank you for your answer. But when I follow your method, it gave me a value error :shape mismatch: objects cannot be broadcast to a single shape – lifeodyssey Mar 04 '20 at 07:55
  • You will have to provide more info, preferably by updating your original question, preferably with the full code + traceback of the error. – Bart Mar 04 '20 at 09:29