使用python将不规则间隔的地理参照2D数组写入netcdf

问题描述 投票:0回答:1

我有一个要写入netcdf文件的数据数组(1500 x 2500),但是纬度/经度网格不规则排列。我可以创建一个包含数据的netcdf文件,但是该文件似乎不可读和/或格式正确。例如,当我尝试使用NOAA天气和气候工具包读取该文件时,出现错误,指出该文件被扫描为“网格”文件,但未找到网格。这是我的代码:

from netCDF4 import Dataset

ndvi_nc = Dataset("ndvi_out.nc", "w", format="NETCDF4")

print(ndvi_nc.data_model)

time = ndvi_nc.createDimension("time", 1)
lat = ndvi_nc.createDimension("lat", 3750000)
lon = ndvi_nc.createDimension("lon", 3750000)
data = ndvi_nc.createDimension("data", 3750000)

print(lats.shape)
print(lons.shape)
print(NDVInew.shape)

times = ndvi_nc.createVariable("time","f8",("time",))
latitudes = ndvi_nc.createVariable("lat","f4",("lat",))
longitudes = ndvi_nc.createVariable("lon","f4",("lon",))
ndvi = ndvi_nc.createVariable("ndvi","f4",("data",))

ndvi_nc.description = "NDVI dataset"
ndvi_nc.source = "netCDF4 python module tutorial"
latitudes.units = "degrees north"
longitudes.units = "degrees west"
ndvi.units = "dimensionless"
times.units = "seconds since 1970-1-1"

flatlats = lats.flatten()
flatlons = lons.flatten()
flatndvi = NDVInew.flatten()
print(flatndvi.shape)

latitudes[:] = flatlats
longitudes[:] = flatlons
ndvi[:] = flatndvi
times = 1303939211

print(ndvi)
print(latitudes)
print(longitudes)

ndvi_nc.close()

这是输出:

NETCDF4
(1500, 2500)
(1500, 2500)
(1500, 2500)
(3750000,)
<class 'netCDF4._netCDF4.Variable'>
float32 ndvi(data)
    units: dimensionless
unlimited dimensions: 
current shape = (3750000,)
filling on, default _FillValue of 9.969209968386869e+36 used
<class 'netCDF4._netCDF4.Variable'>
float32 lat(lat)
    units: degrees north
unlimited dimensions: 
current shape = (3750000,)
filling on, default _FillValue of 9.969209968386869e+36 used
<class 'netCDF4._netCDF4.Variable'>
float32 lon(lon)
    units: degrees west
unlimited dimensions: 
current shape = (3750000,)
filling on, default _FillValue of 9.969209968386869e+36 used

是否有更好的方法将数据写入netcdf?

谢谢,

-马特

python grid output netcdf
1个回答
0
投票

您无需将所有内容弄平,直接将网格写入间隔不均匀的netCDF即可。创建两个尺寸x / y,然后lonlatndvi都是这些尺寸上的2D字段。所以这样的事情对我有用:

from netCDF4 import Dataset
ndvi_nc = Dataset("ndvi.nc", "w")

lats = np.random.rand(1500, 2500)
lons = np.random.rand(1500, 2500)
NDVInew = np.random.rand(1500, 2500)

time = ndvi_nc.createDimension("time", 1)
lat = ndvi_nc.createDimension("y", 1500)
lon = ndvi_nc.createDimension("x", 2500)

times = ndvi_nc.createVariable("time", "f8", ("time",))
latitudes = ndvi_nc.createVariable("lat", "f4", ("y", "x"))
longitudes = ndvi_nc.createVariable("lon", "f4", ("y", "x"))
ndvi = ndvi_nc.createVariable("ndvi", "f4", ("y", "x"))

ndvi_nc.description = "NDVI dataset"
ndvi_nc.source = "netCDF4 python module tutorial"

latitudes.units = "degrees north"
longitudes.units = "degrees west"
ndvi.units = "dimensionless"
times.units = "seconds since 1970-1-1"

flatlats = lats.flatten()
flatlons = lons.flatten()
flatndvi = NDVInew.flatten()

latitudes[:] = lats
longitudes[:] = lons
ndvi[:] = NDVInew
times[:] = 1303939211

ndvi_nc.close()
© www.soinside.com 2019 - 2024. All rights reserved.