python读取气象nc数据
绝大多数nc的数据存在variable中,但也有部分nc数据variable是空的,数据存在其他地方。以下分别对这两类情况进行读取。
·
绝大多数nc的数据存在variable中,但也有部分nc数据variable是空的,数据存在其他地方。以下分别对这两类情况进行读取。
情况1:数据在variables中
import netCDF4 as nc
dataset = nc.Dataset('xxx.nc') # 读取数据
print(dataset.variables.keys()) # 输出所有变量
lon = dataset.variables['longitude'][:].data # 读取经度
lat = dataset.variables['latitude'][:].data # 读取维度
time = dataset.variables['time'] # 读取时间
real_time = nc.num2date(time, time.units).data # 转成时间格式
u10 = dataset.variables['u10'][:].data # 读取u10风速
情况2:数据不在variable中
参考:https://github.com/acgeospatial/Sentinel-5P/blob/master/Sentinel_5P.ipynb
import numpy as np
import netCDF4 as nc
import pandas as pd
from netCDF4 import Dataset
if __name__ == '__main__':
fh = Dataset('S5P_OFFL_L2__NO2____20220702T022827_20220702T040957_24441_02_020301_20220703T180630.nc', mode='r')
print(fh)
print(fh.file_format)
print(fh.groups)
print(fh.groups['PRODUCT'])
print(fh.groups['PRODUCT'].dimensions)
print(fh.groups['PRODUCT'].dimensions.keys())
print(fh.groups['PRODUCT'].dimensions['time'])
print(fh.groups['METADATA'])
print(fh.groups['PRODUCT'].variables)
print(fh.groups['PRODUCT'].variables.keys())
time = fh.groups['PRODUCT'].variables['time'][:].data
lon = fh.groups['PRODUCT'].variables['longitude'][:].data
lat = fh.groups['PRODUCT'].variables['latitude'][:].data
ntc = fh.groups['PRODUCT'].variables['nitrogendioxide_tropospheric_column'][:].data
reslut = np.zeros([np.size(lon, 1) * np.size(lon, 2), 3])
r = 0
for i in range(np.size(lon, 1)):
for j in range(np.size(lon, 2)):
reslut[r, 0:3] = lon[0, i, j], lat[0, i, j], ntc[0, i, j]
r += 1
reslut=np.delete(reslut, reslut[:,2]>99999999, 0)
np.savetxt('S5P_OFFL_L2__NO2____20220702T022827_20220702T040957_24441_02_020301_20220703T180630.txt', reslut,delimiter=',')
更多推荐
已为社区贡献2条内容
所有评论(0)