Я пытаюсь записать данные в файл netcdf, и с этим примером кода я получаю следующую ошибку:
Traceback (most recent call last):
File "netCDF4\_netCDF4.pyx", line 4866, in netCDF4._netCDF4.Variable.__setitem__
ValueError: cannot reshape array of size 5 into shape (5,1,1,3)
Ниже приведен пример тестового кода, который вы можете попробовать сами;
from netCDF4 import Dataset
latitude = [21.1877]
longitude = [-68.95998]
depth_array = [2, 3.5, 5] # in meters
time_array = [19022040, 19022070, 19022100, 19022130, 19022160] # Minutes since 1970-01-01 00:00:00 UTC
pressure_array = [37.57, 37.573, 37.588, 37.597, 37.613]
temperature_array = [26.19, 26.2, 26.18, 26.19, 26.17]
current_speed_array = [[0.093, 0.109, 0.076, 0.045, 0.054], # 2M depth
[0.119, 0.041, 0.113, 0.12, 0.088], # 3.5M depth
[0.131, 0.116, 0.184, 0.095, 0.127]] # 5M depth
current_dir_array = [[63.98, 62.02, 335.81, 36.87, 70.56], # 2M depth
[21.18, 344.25, 3.04, 352.34, 344.23], # 3.5M depth
[346.28, 353.55, 8.44, 349.64, 339.71]] # 5M depth
dataset = Dataset("Test.nc", 'w', format="NETCDF4_CLASSIC")
# Creating Dimensions
dataset.createDimension('time', None)
dataset.createDimension('lat', 1)
dataset.createDimension('lon', 1)
dataset.createDimension('depth', len(depth_array))
# Creating Variables
times = dataset.createVariable("time", "f4", ('time',))
lats = dataset.createVariable("lat", 'd', ('lat',))
lons = dataset.createVariable("lon", 'd', ('lon',))
depths = dataset.createVariable("depth", "f8", ("depth",))
pressures = dataset.createVariable("pres", "f4", ("time", "lat", "lon", "depth",))
temperatures = dataset.createVariable("temp", "f4", ("time", "lat", "lon", "depth",))
current_speeds = dataset.createVariable("speed", "f8", ("time", "lat", "lon", "depth",))
current_directions = dataset.createVariable("dir", "f8", ("time", "lat", "lon", "depth",))
# Writing Data to Variables
times[:] = time_array
lats[:] = latitude
lons[:] = longitude
depths[:] = depth_array
pressures[:] = pressure_array
current_speeds[:] = current_speed_array
current_directions[:] = current_dir_array
dataset.close()