如何将hdf中的网格化卫星测绘数据转换为可从NASA GPM卫星获得的netcdf4?

时间:2019-11-15 07:05:29

标签: python netcdf hdf satellite nco

我正在尝试访问https://disc.gsfc.nasa.gov/datasets?keywords=LATENT%20HEATING&page=1的hdf中可用的栅格化卫星测绘数据。您可以看到我试图使用的任何3级数据。我熟悉诸如雪貂之类的netcdf可视化库以及诸如cdo和nco之类的netcdf操作软件。我想将这条网格数据转换为netcdf,以便使用cdo和nco进行平滑分析。

2 个答案:

答案 0 :(得分:3)

从此处发布的答案中提取答案:Convert hdf5 to netcdf4 in bash, R, python or NCL?

最简单的方法是:

nccopy in.h5 out.nc

答案 1 :(得分:0)

我从您共享的链接中下载了文件3B-ORBIT.GPM.DPRGMI.3GCSHv6-0.20190626-S064516-E081748.030252.V06A.HDF5,并创建了一个将文件名作为参数并生成netcdf的程序文件包含变量latentHeating,surfacePrecipRate和stratiformFraction。让我澄清一下,该文件是栅格化的卫星测绘数据。您可能需要做其他事情才能从像素级扫描数据中制作出网格数据

import h5py
import numpy as np
import datetime as dt
import xarray as xr
import sys

input_file = sys.argv[1]
f = h5py.File(input_file, 'r')

yy = np.max(f['Grid']['GridTime']['Year'][:])
mm = np.max(f['Grid']['GridTime']['Month'][:])
dd = np.max(f['Grid']['GridTime']['DayOfMonth'][:])

time_ = dt.datetime(yy, mm, dd)
lat = np.linspace(-67+0.25/2,67-0.25/2,536)
lon = np.linspace(-180+0.25/2,180-0.25/2,1440)
lev = np.arange(0.125,0.125+0.25*80,0.25)

lh = f['Grid']['latentHeating'][:]
spr = f['Grid']['surfacePrecipRate'][:]
stratfrac = f['Grid']['stratiformFraction'][:]
lh[lh==-9999.9] = np.nan
spr[spr==-9999.9] = np.nan
stratfrac[stratfrac==-9999.9] = np.nan

stratfrac = stratfrac.T
spr = spr.T
lh = lh.swapaxes(1,2)
lh = lh[np.newaxis,...]

stratfrac = stratfrac[np.newaxis,...]
spr = spr[np.newaxis,...]

df = xr.Dataset(
        data_vars={'latentHeating':    (('time', 'level', 'latitude', 
'longitude' ), lh),
                   'surfacePrecipRate':    (('time', 'latitude', 
'longitude' ), spr),
                   'stratiformFraction':    (('time', 'latitude', 
'longitude'), stratfrac)},
        coords={ 'time':np.atleast_1d((time_- 
dt.datetime(1,1,1)).days+2),
            'level': lev,
            'longitude': lon,
            'latitude': lat,
        })
df.latentHeating.attrs['long_name'] = 'Latent Heating'
df.surfacePrecipRate.attrs['long_name'] = 'Surface Precipitation Rate'
df.stratiformFraction.attrs['long_name'] = 'Stratiform Fraction'

df.time.attrs['units'] = 'days since 01-01-01 00:00:00'
df.time.attrs['standard_name'] = 'time'
df.time.attrs['long_name'] = 'Year AD'
df.time.attrs['calendar'] = 'standard'

df.longitude.attrs['standard_name'] = 'longitude'
df.longitude.attrs['long_name'] = 'longitude'
df.longitude.attrs['units'] = 'degrees_east'
df.longitude.attrs['axis'] = 'X'

df.latitude.attrs['standard_name'] = 'latitude'
df.latitude.attrs['long_name'] = 'latitude'
df.latitude.attrs['units'] = 'degrees_north'
df.latitude.attrs['axis'] = 'Y'

df.level.attrs['standard_name'] = 'air_pressure'
df.level.attrs['long_name'] = 'pressure_level'
df.level.attrs['units'] = 'km' 
df.level.attrs['axis'] = 'Z'
df.level.attrs['positive'] = 'up'
df.to_netcdf(input_file[:-4]+'nc')

您可以将其另存为python文件,然后将hdf文件作为参数传递。