NetCDF:如何 mask/filter 排除全球数据集中的非土地价值,最好使用 Python and/or NCO?

NetCDF: How to mask/filter out non-land values in global dataset, preferably using Python and/or NCO?

我有一个分辨率为 0.25 度的全球数据,我想对其进行屏蔽,使其仅包含陆​​地上的数据值。

数据涵盖经度维度的完整 360 度和纬度维度的 -60 到 60 度。

文件头以及摘要经纬度坐标值如下所列:

netcdf cmorph_global_daily {
dimensions:
    lat = UNLIMITED ; // (480 currently)
    lon = 1440 ;
    time = 7305 ;
variables:
    float lat(lat) ;
        lat:units = "degrees_north" ;
        lat:long_name = "Latitude" ;
    float lon(lon) ;
        lon:units = "degrees_east" ;
        lon:long_name = "Longitude" ;
    float prcp(lat, lon, time) ;
        prcp:_FillValue = NaNf ;
        prcp:units = "mm" ;
        prcp:standard_name = "precipitation" ;
        prcp:long_name = "Precipitation" ;
        prcp:description = "CMORPH Version 1.0BETA Version, daily precip from 00Z-24Z" ;
    int time(time) ;
        time:units = "days since 1900-01-01" ;
        time:long_name = "Time" ;
        time:calendar = "gregorian" ;

// global attributes:
        :history = "Mon Mar 26 10:44:42 2018: ncpdq -a lat,lon,time cmorph_adjusted_daily.nc latlontime/cmorph_adjusted_daily.nc\nThu Mar 15 10:21:10 2018: ncks -4 cmorph_adjusted_daily.nc cmorph_adjusted_daily.nc" ;
        :nco_openmp_thread_number = 1 ;
        :title = "CMORPH Version 1.0BETA Version, daily precip from 00Z-24Z" ;
        :NCO = "4.7.2" ;
data:

 lat = -59.875, -59.625, -59.375, -59.125, ..., 59.125, 59.375, 59.625, 59.875 ;
 lon = 0.125, 0.375, 0.625, 0.875, 1.125, ..., 359.125, 359.375, 359.625, 359.875 ;

我更愿意使用 Python/numpy and/or NCO 来执行此操作,因为这是我的典型工具集。

CDO 解决方案

# make landseamask on 0.25 grid with 1 for land and missing for sea
cdo -P 8 -f nc2 setctomiss,0 -gtc,0 -remapcon,r1440x720 -topo seamask.nc

# not sure if you need to do this but lets put your data on same grid
cdo remapcon,r1440x720 yourdata.nc yourdataremap.nc

# now mask the data
cdo mul yourdataremap.nc seamask.nc yourdatamasked.nc

希望有用...

编辑:您也可以通过这种方式直接在数据网格上制作 landsea 掩码:

# make landseamask on 0.25 grid with 1 for land and missing for sea
cdo -P 8 -f nc setctomiss,0 -gtc,0 -remapcon,yourdata.nc -topo seamask.nc

# now mask the data
cdo mul yourdataremap.nc seamask.nc yourdatamasked.nc

一旦你在同一个网格上有一个变量来屏蔽,你可以使用 ncap2 where,例如

ncap2 -s 'where(LANDMASK != 1) prcp=prcp@_FillValue' in.nc out.nc

如果掩码与数据位于不同的网格上,您可以使用 (在 Linux/Mac 上)ncremap 的掩码功能,例如,使用

之类的东西将您的数据重新映射到您的掩码(反之亦然)
ncremap --msk_dst=LANDMASK -d mask.nc prcp_in.nc prcp_out.nc