将变量添加到 Python 中的现有 netcdf4 文件
Add variable to exisiting netcdf4 file in Python
我有一张 netcdf 格式的 MODIS 卫星图像。我使用此图像中的某些变量作为模型的输入,创建了一个名为 npp
的 numpy 数组。该数组与原始文件 (888,1368)
的纬度和经度具有相同的维度。我想将 npp
作为新变量添加到原始文件中,但我不清楚我做错了什么?
import netCDF4 as nc
from netCDF4 import Dataset
# Load input file
file_input = nc.Dataset('A2018066.5d.L3m_OC.nc', 'w', format='NETCDF4')
# view dimensions
print(file_input.dimensions)
"OrderedDict([('lat', <class 'netCDF4._netCDF4.Dimension'>: name = 'lat', size = 888
), ('lon', <class 'netCDF4._netCDF4.Dimension'>: name = 'lon', size = 1368
), ('rgb', <class 'netCDF4._netCDF4.Dimension'>: name = 'rgb', size = 3
), ('eightbitcolor', <class 'netCDF4._netCDF4.Dimension'>: name = 'eightbitcolor', size = 256
)])"
# input file variables.keys
print(file_input.variables.keys())
"odict_keys(['aot_869', 'angstrom', 'Rrs_412', 'Rrs_443', 'Rrs_469', 'Rrs_488', 'Rrs_531', 'Rrs_547', 'Rrs_555', 'Rrs_645', 'Rrs_667', 'Rrs_678', 'chlor_a', 'chl_ocx', 'Kd_490', 'pic', 'poc', 'ipar', 'nflh', 'par', 'lat', 'lon', 'palette'])"
# add npp to input file
file_input.createDimension('latitude',888)
file_input.createDimension('longitude', 1368)
nppvariable = file_input.createVariable('npp', 'int16',('latitude', 'longitude'))
nppvariable[:] = npp[:,:]
但这似乎覆盖了所有现有变量,丢失了所有其他数据?
file_input.variables.keys()
"odict_keys(['npp'])```
抱歉,这是我第一次在 python 中处理 netcdf4,但是当我使用 createvariable()
而不是 npp
作为新变量添加时,为什么我会丢失所有其他变量到原始文件?我错过了一步吗?
写入模式 w
确实会覆盖您现有的 NetCDF 文件,并在原处创建一个全新的文件。
您正在寻找追加模式,a
或 r+
:
file_input = nc.Dataset('A2018066.5d.L3m_OC.nc', 'r+', format='NETCDF4')
https://unidata.github.io/netcdf4-python/netCDF4/index.html#netCDF4.Dataset
access mode. r
means read-only; no data can be modified. w
means write; a new file is created, an existing file with the same name is deleted. a
and r+
mean append (in analogy with serial files); an existing file is opened for reading and writing.
我有一张 netcdf 格式的 MODIS 卫星图像。我使用此图像中的某些变量作为模型的输入,创建了一个名为 npp
的 numpy 数组。该数组与原始文件 (888,1368)
的纬度和经度具有相同的维度。我想将 npp
作为新变量添加到原始文件中,但我不清楚我做错了什么?
import netCDF4 as nc
from netCDF4 import Dataset
# Load input file
file_input = nc.Dataset('A2018066.5d.L3m_OC.nc', 'w', format='NETCDF4')
# view dimensions
print(file_input.dimensions)
"OrderedDict([('lat', <class 'netCDF4._netCDF4.Dimension'>: name = 'lat', size = 888
), ('lon', <class 'netCDF4._netCDF4.Dimension'>: name = 'lon', size = 1368
), ('rgb', <class 'netCDF4._netCDF4.Dimension'>: name = 'rgb', size = 3
), ('eightbitcolor', <class 'netCDF4._netCDF4.Dimension'>: name = 'eightbitcolor', size = 256
)])"
# input file variables.keys
print(file_input.variables.keys())
"odict_keys(['aot_869', 'angstrom', 'Rrs_412', 'Rrs_443', 'Rrs_469', 'Rrs_488', 'Rrs_531', 'Rrs_547', 'Rrs_555', 'Rrs_645', 'Rrs_667', 'Rrs_678', 'chlor_a', 'chl_ocx', 'Kd_490', 'pic', 'poc', 'ipar', 'nflh', 'par', 'lat', 'lon', 'palette'])"
# add npp to input file
file_input.createDimension('latitude',888)
file_input.createDimension('longitude', 1368)
nppvariable = file_input.createVariable('npp', 'int16',('latitude', 'longitude'))
nppvariable[:] = npp[:,:]
但这似乎覆盖了所有现有变量,丢失了所有其他数据?
file_input.variables.keys()
"odict_keys(['npp'])```
抱歉,这是我第一次在 python 中处理 netcdf4,但是当我使用 createvariable()
而不是 npp
作为新变量添加时,为什么我会丢失所有其他变量到原始文件?我错过了一步吗?
写入模式 w
确实会覆盖您现有的 NetCDF 文件,并在原处创建一个全新的文件。
您正在寻找追加模式,a
或 r+
:
file_input = nc.Dataset('A2018066.5d.L3m_OC.nc', 'r+', format='NETCDF4')
https://unidata.github.io/netcdf4-python/netCDF4/index.html#netCDF4.Dataset
access mode.
r
means read-only; no data can be modified.w
means write; a new file is created, an existing file with the same name is deleted.a
andr+
mean append (in analogy with serial files); an existing file is opened for reading and writing.