在气象数据分析领域,高效处理与分析海量的模拟数据是至关重要的。WRF(Weather Research and Forecasting Model)作为一种广泛应用的数值天气预报模型,每天为我们生成庞大的地球系统数据集,这些数据涵盖了从大气温度、湿度到风场的方方面面。然而,单独的WRF输出文件通常只包含特定时间段或空间区域的信息,对于跨时间尺度的长期趋势分析或是大范围空间特征的研究来说,原始数据的分散状态无疑增加了研究的复杂度。
幸运的是,我们拥有强大的工具——NCO(NetCDF Operators),它专为简化和加速基于NetCDF格式数据集的操作而设计。NCO提供了一套全面的命令行工具,使得合并多个WRF输出文件、计算不同模拟结果间的差异等任务变得轻而易举。通过NCO,我们可以轻松地将WRF模型生成的一个个“时间切片”拼接成连续的时间序列,从而观察气候模式的变化;或者对比不同模拟情景下的数据,深入探究各种气象现象的差异与联系。
本篇将以实际操作为例,介绍如何利用NCO的ncrcat和ncdiff/ncbo命令来合并WRF模拟的多个文件,并展示如何进行模拟结果之间的差异分析。这不仅对理解气候变化趋势、极端天气事件的形成机制有着重要意义,也为预报员提供了宝贵的辅助信息,帮助他们在复杂多变的气象环境中做出更加准确的预测。
nco学习网站:
需要查看参数时直接命令行键入函数名即可
!ncbo
ncbo Command line options cheatsheet (full details at http://nco.sf.net/nco.html#ncbo):
ncbo [-3] [-4] [-5] [-6] [-7] [-A] [--bfr byt] [-C] [-c] [--cmp sng] [--cnk_byt byt] [--cnk_csh byt] [--cnk_dmn nm,lmn] [--cnk_map map] [--cnk_min byt] [--cnk_plc plc] [--cnk_scl sz] [-D dbg_lvl] [-d ...] [-F] [--fl_fmt fmt] [-G grp:lvl] [-g ...] [--glb ...] [-H] [-h] [--hdf] [--hdr_pad nbr] [--hpss] [-L lvl] [-l path] [--msa] [-n ...] [--no_cll_msr] [--no_frm_trm] [--no_tmp_fl] [-O] [-o out.nc] [-p path] [-R] [-r] [--ram_all] [-t thr_nbr] [--uio] [--unn] [-v ...] [-X box] [-x] [-y op_typ] in1.nc in2.nc [out.nc]
-3, --3, classic Output file in netCDF3 CLASSIC (32-bit offset) storage format
-4, --4, netcdf4 Output file in netCDF4 (HDF5) storage format
-5, --5, 64bit_data Output file in netCDF3 64-bit data (i.e., CDF5, PnetCDF) storage format
-6, --6, 64, 64bit_offset Output file in netCDF3 64-bit offset storage format
-7, --7, netcdf4_classic Output file in netCDF4 CLASSIC format (3+4=7)
-A, --apn, append Append to existing output file, if any
--bfr_sz, buffer_size sz Buffer size to open files with
-C, --no_crd, xcl_ass_var Exclude coordinates, CF-associated variables (ancillary, bounds, ...)
-c, --crd, xtr_ass_var Extract coordinates, CF-associated variables (ancillary, bounds, ...)
--cmp cmp_sng Compression string (e.g., 'gbr,3|zstd,1')
--cnk_byt, chunk_byte sz_byt Chunksize in bytes
--cnk_csh, chunk_cache sz_byt Chunk cache size in bytes
--cnk_dmn, chunk_dimension nm,sz_lmn Chunksize of dimension nm (in elements not bytes)
--cnk_map, chunk_map map Chunking map [dmn,lfp,nc4,nco,prd,rd1,rew,scl,xpl,xst]
--cnk_min, chunk_min sz_byt Minimum size [B] of variable to chunk
--cnk_plc, chunk_policy plc Chunking policy [all,g2d,g3d,xpl,xst,uck]
--cnk_scl, chunk_scalar sz_lmn Chunksize scalar (in elements not bytes) (for all dimensions)
-D, --dbg_lvl, debug-level lvl Debug-level is lvl
-d, --dmn, dimension dim,[min][,[max]][,[stride]] Dimension's limits and stride in hyperslab
-F, --ftn, fortran Fortran indexing conventions (1-based) for I/O
--fl_fmt, file_format fmt File format for output [classic,64bit_offset,64bit_data,netcdf4,netcdf4_classic]
-G, --gpe [grp_nm][:[lvl]] Group Path Editing path, levels to replace
-g, --grp grp1[,grp2[...]] Group(s) to process (regular expressions supported)
--glb_att_add nm=val Global attribute to add
-H, --fl_lst_in, file_list Do not create "input_file_list" global attribute that stores stdin filenames
-h, --hst, history Do not append to "history" global attribute
--hdf_upk, hdf_unpack HDF unpack convention: unpacked=scale_factor*(packed-add_offset)
--hdr_pad, header_pad Pad output header with nbr bytes
--hpss, hpss_try Search for unfound files on HPSS with 'hsi get ...'
-L, --dfl_lvl, deflate lvl Lempel-Ziv deflation/compression (lvl=0..9) for netCDF4 output
-l, --lcl, local path Local storage path for remotely-retrieved files
--msa, msa_usr_rdr Multi-Slab-Algorithm output in User-Order
-n, --nintap nbr_files,[nbr_numeric_chars[,increment]] NINTAP-style abbreviation of file list
--no_cll_msr Do not extract cell_measures variables
--no_frm_trm Do not extract formula_terms variables
--no_tmp_fl Do not write output to temporary file
-O, --ovr, overwrite Overwrite existing output file, if any
-o, --output, fl_out Output file name (or use last positional argument)
-p, --pth, path path Path prefix for all input filenames
-R, --rtn, retain Retain remotely-retrieved files after use
-r, --revision, version Compile-time configuration and/or program version
--ram_all, diskless_all Open netCDF3 files and create output files in RAM
-t, --thr_nbr, threads, omp_num_threads thr_nbr Thread number for OpenMP
--uio, share_all Unbuffered I/O to read/write netCDF3 file(s)
--unn, union Select union of specified groups and variables
-v, --variable var1[,var2[...]] Variable(s) to process (regular expressions supported)
-X, --auxiliary lon_min,lon_max,lat_min,lat_max Auxiliary coordinate bounding box
-x, --xcl, exclude Extract all variables EXCEPT those specified with -v
-y, --op_typ, operation op_typ Binary arithmetic operation: add,sbt,mlt,dvd (+,-,*,/)
in1.nc in2.nc Input file names (or use stdin)
[out.nc] Output file name (or use -o option)
Eight ways to find more help on ncbo and/or NCO:
1. Examples: http://nco.sf.net/nco.html#xmp_ncbo
2. Ref. manual: http://nco.sf.net/nco.html#ncbo
3. User Guide: http://nco.sf.net#RTFM
4. Manual pages: 'man ncbo', 'man nco', ...
5. Homepage: http://nco.sf.net
6. Code: http://github.com/nco/nco
7. Help Forum: http://sf.net/p/nco/discussion/9830
8. Publications: http://nco.sf.net#pub
Post questions, suggestions, patches at http://sf.net/projects/nco
命令格式为ncrcat 文件1 文件2 out.nc
随意选择两个时次的wrfout进行合并
!ncrcat /home/mw/input/wrfout3385/wrfout_d02_2022-07-14_0600.nc /home/mw/input/wrfout3385/wrfout_d02_2022-07-14_0700.nc out.nc
import xarray as xr
ds = xr.open_dataset("/home/mw/project/out.nc")
ds
<xarray.Dataset> Size: 102MB
Dimensions: (Time: 2, south_north: 90, west_east: 90,
bottom_top: 49, bottom_top_stag: 50,
soil_layers_stag: 4, west_east_stag: 91,
south_north_stag: 91, seed_dim_stag: 8)
Coordinates:
XLAT (Time, south_north, west_east) float32 65kB ...
XLONG (Time, south_north, west_east) float32 65kB ...
XTIME (Time) datetime64[ns] 16B ...
XLAT_U (Time, south_north, west_east_stag) float32 66kB ...
XLONG_U (Time, south_north, west_east_stag) float32 66kB ...
XLAT_V (Time, south_north_stag, west_east) float32 66kB ...
XLONG_V (Time, south_north_stag, west_east) float32 66kB ...
Dimensions without coordinates: Time, south_north, west_east, bottom_top,
bottom_top_stag, soil_layers_stag,
west_east_stag, south_north_stag, seed_dim_stag
Data variables: (12/213)
Times (Time) |S19 38B ...
LU_INDEX (Time, south_north, west_east) float32 65kB ...
ZNU (Time, bottom_top) float32 392B ...
ZNW (Time, bottom_top_stag) float32 400B ...
ZS (Time, soil_layers_stag) float32 32B ...
DZS (Time, soil_layers_stag) float32 32B ...
... ...
PCB (Time, south_north, west_east) float32 65kB ...
PC (Time, south_north, west_east) float32 65kB ...
LANDMASK (Time, south_north, west_east) float32 65kB ...
LAKEMASK (Time, south_north, west_east) float32 65kB ...
SST (Time, south_north, west_east) float32 65kB ...
SST_INPUT (Time, south_north, west_east) float32 65kB ...
Attributes: (12/136)
TITLE: OUTPUT FROM WRF V4.4.2 MODEL
START_DATE: 2022-07-14_00:00:00
SIMULATION_START_DATE: 2022-07-14_00:00:00
WEST-EAST_GRID_DIMENSION: 91
SOUTH-NORTH_GRID_DIMENSION: 91
BOTTOM-TOP_GRID_DIMENSION: 50
... ...
ISURBAN: 13
ISOILWATER: 14
HYBRID_OPT: 2
ETAC: 0.2
history: Wed Jun 19 14:01:23 2024: ncrcat /home/m...
NCO: netCDF Operators version 5.1.5 (Homepage...
合并成功,为了内存考虑暂且合并两个,可以使用通配符批量合并
使用台风个例的两个时间点的wrfout文件作差,并查看作差变量
命令格式也是ncdiff 文件1 文件2 out.nc
!ncdiff /home/mw/input/typhoon9537/wrfout_d01_2019-08-09_06_00_00 /home/mw/input/typhoon9537/wrfout_d01_2019-08-08_19_00_00 diff.nc
看看作差前后的OLR以及输出的文件
import xarray as xr
ds0 = xr.open_dataset("/home/mw/input/typhoon9537/wrfout_d01_2019-08-09_06_00_00")
ds0
<xarray.Dataset> Size: 589MB
Dimensions: (Time: 1, south_north: 437, west_east: 447,
bottom_top: 34, bottom_top_stag: 35,
soil_layers_stag: 4, west_east_stag: 448,
south_north_stag: 438, seed_dim_stag: 2)
Coordinates:
XLAT (Time, south_north, west_east) float32 781kB ...
XLONG (Time, south_north, west_east) float32 781kB ...
XTIME (Time) datetime64[ns] 8B ...
XLAT_U (Time, south_north, west_east_stag) float32 783kB ...
XLONG_U (Time, south_north, west_east_stag) float32 783kB ...
XLAT_V (Time, south_north_stag, west_east) float32 783kB ...
XLONG_V (Time, south_north_stag, west_east) float32 783kB ...
Dimensions without coordinates: Time, south_north, west_east, bottom_top,
bottom_top_stag, soil_layers_stag,
west_east_stag, south_north_stag, seed_dim_stag
Data variables: (12/159)
Times (Time) |S19 19B ...
LU_INDEX (Time, south_north, west_east) float32 781kB ...
ZNU (Time, bottom_top) float32 136B ...
ZNW (Time, bottom_top_stag) float32 140B ...
ZS (Time, soil_layers_stag) float32 16B ...
DZS (Time, soil_layers_stag) float32 16B ...
... ...
PCB (Time, south_north, west_east) float32 781kB ...
PC (Time, south_north, west_east) float32 781kB ...
LANDMASK (Time, south_north, west_east) float32 781kB ...
LAKEMASK (Time, south_north, west_east) float32 781kB ...
SST (Time, south_north, west_east) float32 781kB ...
SST_INPUT (Time, south_north, west_east) float32 781kB ...
Attributes: (12/134)
TITLE: OUTPUT FROM WRF V4.4.2 MODEL
START_DATE: 2019-08-08_18:00:00
SIMULATION_START_DATE: 2019-08-08_18:00:00
WEST-EAST_GRID_DIMENSION: 448
SOUTH-NORTH_GRID_DIMENSION: 438
BOTTOM-TOP_GRID_DIMENSION: 35
... ...
ISLAKE: 21
ISICE: 15
ISURBAN: 13
ISOILWATER: 14
HYBRID_OPT: 2
ETAC: 0.2
ds0.OLR.plot()
<matplotlib.collections.QuadMesh at 0x7fccbbe72760>

image
ds1 = xr.open_dataset("/home/mw/input/typhoon9537/wrfout_d01_2019-08-08_19_00_00")
ds1.OLR.plot()
<matplotlib.collections.QuadMesh at 0x7f1fdfe0efa0>

image
diff = xr.open_dataset("/home/mw/project/diff.nc")
diff.OLR.plot()
<matplotlib.collections.QuadMesh at 0x7f2045d01310>

image
使用ncbo命令执行加减乘除等二进制算术运算时,可以通过指定-y或--op_typ选项后面跟着相应的运算符来完成。下面是不同运算的操作方法:
加法 (Addition): 使用 add 或 + 作为运算类型。
Bash
ncbo -y add file1.nc file2.nc output.nc
减法 (Subtraction): 使用 sbt、-、dff、diff、sub、subtract 或 subtraction 中的任意一个作为运算类型。
Bash
ncbo -y sbt file1.nc file2.nc output.nc
乘法 (Multiplication): 使用 mlt、*、mult、multiply 或 multiplication 中的任意一个作为运算类型。
Bash
ncbo -y mlt file1.nc file2.nc output.nc
除法 (Division): 使用 dvd、/、divide 或 division 中的任意一个作为运算类型。
Bash
ncbo -y dvd file1.nc file2.nc output.nc
在这些命令中,file1.nc 和 file2.nc 是输入的NetCDF文件,而output.nc是输出文件,将会存储运算的结果。请注意,执行这些操作前,确保两个输入文件中对应变量的维度和结构相匹配
!ncbo -O /home/mw/input/typhoon9537/wrfout_d01_2019-08-09_06_00_00 /home/mw/input/typhoon9537/wrfout_d01_2019-08-08_19_00_00 bias.nc
-O是覆盖文件,结构为 ncbo -O 文件1 文件2 out.nc
import xarray as xr
ds1 = xr.open_dataset("/home/mw/project/bias.nc")
ds1
<xarray.Dataset> Size: 589MB
Dimensions: (Time: 1, south_north: 437, west_east: 447,
bottom_top_stag: 35, bottom_top: 34,
soil_layers_stag: 4, seed_dim_stag: 2,
west_east_stag: 448, south_north_stag: 438)
Coordinates:
XLAT (Time, south_north, west_east) float32 781kB ...
XLAT_U (Time, south_north, west_east_stag) float32 783kB ...
XLAT_V (Time, south_north_stag, west_east) float32 783kB ...
XLONG (Time, south_north, west_east) float32 781kB ...
XLONG_U (Time, south_north, west_east_stag) float32 783kB ...
XLONG_V (Time, south_north_stag, west_east) float32 783kB ...
XTIME (Time) datetime64[ns] 8B ...
Dimensions without coordinates: Time, south_north, west_east, bottom_top_stag,
bottom_top, soil_layers_stag, seed_dim_stag,
west_east_stag, south_north_stag
Data variables: (12/159)
ACGRDFLX (Time, south_north, west_east) float32 781kB ...
ACHFX (Time, south_north, west_east) float32 781kB ...
ACLHF (Time, south_north, west_east) float32 781kB ...
ACSNOM (Time, south_north, west_east) float32 781kB ...
ALBBCK (Time, south_north, west_east) float32 781kB ...
ALBEDO (Time, south_north, west_east) float32 781kB ...
... ...
XICEM (Time, south_north, west_east) float32 781kB ...
XLAND (Time, south_north, west_east) float32 781kB ...
ZETATOP (Time) float32 4B ...
ZNU (Time, bottom_top) float32 136B ...
ZNW (Time, bottom_top_stag) float32 140B ...
ZS (Time, soil_layers_stag) float32 16B ...
Attributes: (12/136)
TITLE: OUTPUT FROM WRF V4.4.2 MODEL
START_DATE: 2019-08-08_18:00:00
SIMULATION_START_DATE: 2019-08-08_18:00:00
WEST-EAST_GRID_DIMENSION: 448
SOUTH-NORTH_GRID_DIMENSION: 438
BOTTOM-TOP_GRID_DIMENSION: 35
... ...
ISURBAN: 13
ISOILWATER: 14
HYBRID_OPT: 2
ETAC: 0.2
history: Wed Jun 19 14:34:51 2024: ncbo -O /home/...
NCO: netCDF Operators version 5.1.5 (Homepage...
ds1.OLR.plot()
<matplotlib.collections.QuadMesh at 0x7fcc635e3490>

image
到这里,小朋友的问号应该很多了,小小说明一下
ncbo ncbo是一个通用的二进制操作命令,它允许用户执行加、减、乘、除等多种二进制算术运算。用户需要指定运算类型(如加法、减法等)以及其他可能的选项。例如,若要执行两个文件的乘法操作,可以使用ncbo --op_typ=mlt file1.nc file2.nc output.nc。ncbo的一个关键特性是支持组广播(group broadcasting),这意味着它可以自动处理输入文件中的组结构,使得不同组内的变量能够正确地对应并进行运算。
ncdiff
ncdiff是ncbo的一个特例,其主要用途是计算两个文件之间的差异,即默认执行减法操作。当你使用ncdiff file1.nc file2.nc output.nc时,它等效于
ncbo --op_typ=- file1.nc file2.nc output.nc
即输出文件中的每个对应变量值为file1.nc中的值减去file2.nc中的值。ncdiff简化了减法操作的调用,用户无需显式指定运算类型,非常适合快速计算两个时间步或实验间的差异。
主要区别 默认操作: ncdiff专门用于计算差异(减法),而ncbo则是一个更通用的工具,支持多种算术运算,需要用户明确指定运算类型。 便捷性: 对于简单的减法操作,ncdiff提供了一个更简洁的接口,用户不需要记忆或输入额外的运算类型参数。 功能范围: 虽然ncdiff可以视为ncbo的一个子集,专注于减法,但ncbo支持加、减、乘、除等操作,提供了更广泛的功能。 组广播: 特别地,ncbo支持的组广播特性使得它在处理含有复杂结构(如多组数据)的NetCDF文件时更为灵活和强大。
当然,ncbo的命令中我没有输入任何加减乘除的参数,输出的依然是减法的结果,为什么? 当ncbo命令只提供两个输入文件和一个输出文件时,它默认执行的是减法操作。 这是因为在NCO的文档中提到,ncbo命令如果没有明确指定操作类型,默认执行的是减法(即第二个文件的数据从第一个文件的数据中减去)。 因此,这条命令实际上执行的是将第二个时间点的数据从第一个时间点的数据中减去,得到的差异存储在bias.nc文件中。
将风变量打包成一个文件
!ncks -v U,V,W /home/mw/input/typhoon9537/wrfout_d01_2019-08-08_19_00_00 uvw.nc
uvw = xr.open_dataset('/home/mw/project/uvw.nc')
uvw
<xarray.Dataset> Size: 81MB
Dimensions: (Time: 1, bottom_top: 34, south_north: 437, west_east_stag: 448,
south_north_stag: 438, west_east: 447, bottom_top_stag: 35)
Dimensions without coordinates: Time, bottom_top, south_north, west_east_stag,
south_north_stag, west_east, bottom_top_stag
Data variables:
U (Time, bottom_top, south_north, west_east_stag) float32 27MB ...
V (Time, bottom_top, south_north_stag, west_east) float32 27MB ...
W (Time, bottom_top_stag, south_north, west_east) float32 27MB ...
Attributes: (12/136)
TITLE: OUTPUT FROM WRF V4.4.2 MODEL
START_DATE: 2019-08-08_18:00:00
SIMULATION_START_DATE: 2019-08-08_18:00:00
WEST-EAST_GRID_DIMENSION: 448
SOUTH-NORTH_GRID_DIMENSION: 438
BOTTOM-TOP_GRID_DIMENSION: 35
... ...
ISURBAN: 13
ISOILWATER: 14
HYBRID_OPT: 2
ETAC: 0.2
history: Wed Jun 19 14:50:06 2024: ncks -v U,V,W ...
NCO: netCDF Operators version 5.1.5 (Homepage...
对时间和区域进行切割
例如选择第一时间和 格点y [10,50] x[50,80]的区域
!ncks -d Time,0 -d south_north,10,50 -d west_east,50,80 /home/mw/project/out.nc sub.nc
sub = xr.open_dataset('/home/mw/project/sub.nc')
sub
<xarray.Dataset> Size: 9MB
Dimensions: (Time: 1, south_north: 41, west_east: 31,
bottom_top_stag: 50, bottom_top: 49,
soil_layers_stag: 4, seed_dim_stag: 8,
west_east_stag: 91, south_north_stag: 91)
Coordinates:
XLAT (Time, south_north, west_east) float32 5kB ...
XLAT_U (Time, south_north, west_east_stag) float32 15kB ...
XLAT_V (Time, south_north_stag, west_east) float32 11kB ...
XLONG (Time, south_north, west_east) float32 5kB ...
XLONG_U (Time, south_north, west_east_stag) float32 15kB ...
XLONG_V (Time, south_north_stag, west_east) float32 11kB ...
XTIME (Time) datetime64[ns] 8B ...
Dimensions without coordinates: Time, south_north, west_east, bottom_top_stag,
bottom_top, soil_layers_stag, seed_dim_stag,
west_east_stag, south_north_stag
Data variables: (12/213)
ACGRDFLX (Time, south_north, west_east) float32 5kB ...
ACHFX (Time, south_north, west_east) float32 5kB ...
ACLHF (Time, south_north, west_east) float32 5kB ...
ACLWDNB (Time, south_north, west_east) float32 5kB ...
ACLWDNBC (Time, south_north, west_east) float32 5kB ...
ACLWDNT (Time, south_north, west_east) float32 5kB ...
... ...
XICEM (Time, south_north, west_east) float32 5kB ...
XLAND (Time, south_north, west_east) float32 5kB ...
ZETATOP (Time) float32 4B ...
ZNU (Time, bottom_top) float32 196B ...
ZNW (Time, bottom_top_stag) float32 200B ...
ZS (Time, soil_layers_stag) float32 16B ...
Attributes: (12/136)
TITLE: OUTPUT FROM WRF V4.4.2 MODEL
START_DATE: 2022-07-14_00:00:00
SIMULATION_START_DATE: 2022-07-14_00:00:00
WEST-EAST_GRID_DIMENSION: 91
SOUTH-NORTH_GRID_DIMENSION: 91
BOTTOM-TOP_GRID_DIMENSION: 50
... ...
ISURBAN: 13
ISOILWATER: 14
HYBRID_OPT: 2
ETAC: 0.2
history: Wed Jun 19 14:54:18 2024: ncks -d Time,0...
NCO: netCDF Operators version 5.1.5 (Homepage...
out = xr.open_dataset('/home/mw/project/out.nc')
out.OLR[0].plot()
<matplotlib.collections.QuadMesh at 0x7fccbbd9dac0>

image
sub.OLR.plot()
<matplotlib.collections.QuadMesh at 0x7fccbac74190>

image
!cdo -f nc -sellonlatbox,116,130,21,32 -random,r500x400 template.nc
[32mcdo(1) random: [0mProcess started
[32mcdo(1) random: [0m
[32mcdo sellonlatbox: [0mProcessed 200000 values from 1 variable over 1 timestep [0.01s 323MB]
!ncremap -i /home/mw/project/bias.nc -v OLR -d /home/mw/project/template.nc -o resample.nc
Input #00: /home/mw/project/bias.nc
ncks: ERROR nco_grd_nfr() reports an identified grid variable (XLAT with rank 3 and/or XLONG with rank 3) has rank greater than two---grid variables currently must have rank 1 or 2.
HINT: If grid variables do not vary in time, then temporally average them (with, e.g., ncwa -a time in.nc out.nc) prior to inferring grid
ncremap: ERROR Failed to infer source grid. Debug this:
ncks -O --no_tmp_fl --hdr_pad=10000 --gaa remap_script=ncremap --gaa remap_hostname=klab --gaa remap_version=5.1.5 --rgr infer --rgr hnt_src="/tmp/ncremap_tmp_hnt_src.txt.pid649" --rgr scrip="/tmp/ncremap_tmp_grd_src.nc.pid649" "/home/mw/project/bias.nc" "/tmp/ncremap_tmp_out.nc.pid649"
看起来是时间维度出错了,搞他
!ncwa -O -a Time bias.nc bias_notime.nc
import xarray as xr
notime = xr.open_dataset('/home/mw/project/bias_notime.nc')
notime
<xarray.Dataset> Size: 589MB
Dimensions: (south_north: 437, west_east: 447,
bottom_top_stag: 35, bottom_top: 34,
soil_layers_stag: 4, seed_dim_stag: 2,
west_east_stag: 448, south_north_stag: 438)
Coordinates:
XLAT (south_north, west_east) float32 781kB ...
XLAT_U (south_north, west_east_stag) float32 783kB ...
XLAT_V (south_north_stag, west_east) float32 783kB ...
XLONG (south_north, west_east) float32 781kB ...
XLONG_U (south_north, west_east_stag) float32 783kB ...
XLONG_V (south_north_stag, west_east) float32 783kB ...
XTIME datetime64[ns] 8B ...
Dimensions without coordinates: south_north, west_east, bottom_top_stag,
bottom_top, soil_layers_stag, seed_dim_stag,
west_east_stag, south_north_stag
Data variables: (12/159)
ACGRDFLX (south_north, west_east) float32 781kB ...
ACHFX (south_north, west_east) float32 781kB ...
ACLHF (south_north, west_east) float32 781kB ...
ACSNOM (south_north, west_east) float32 781kB ...
ALBBCK (south_north, west_east) float32 781kB ...
ALBEDO (south_north, west_east) float32 781kB ...
... ...
XICEM (south_north, west_east) float32 781kB ...
XLAND (south_north, west_east) float32 781kB ...
ZETATOP float32 4B ...
ZNU (bottom_top) float32 136B ...
ZNW (bottom_top_stag) float32 140B ...
ZS (soil_layers_stag) float32 16B ...
Attributes: (12/136)
TITLE: OUTPUT FROM WRF V4.4.2 MODEL
START_DATE: 2019-08-08_18:00:00
SIMULATION_START_DATE: 2019-08-08_18:00:00
WEST-EAST_GRID_DIMENSION: 448
SOUTH-NORTH_GRID_DIMENSION: 438
BOTTOM-TOP_GRID_DIMENSION: 35
... ...
ISURBAN: 13
ISOILWATER: 14
HYBRID_OPT: 2
ETAC: 0.2
history: Wed Jun 19 15:45:26 2024: ncwa -O -a Tim...
NCO: netCDF Operators version 5.1.5 (Homepage...
第一种 使用现有文件网格插值
!ncremap -i bias_notime.nc -v OLR -d template.nc -o resample.nc
Input #00: /home/mw/project/bias_notime.nc
Grid(src): /tmp/ncremap_tmp_grd_src.nc.pid824
Grid(dst): /tmp/ncremap_tmp_grd_dst.nc.pid824
Map/Wgt : /tmp/ncremap_tmp_map_nco_nco_con.nc.pid824
第二种 自己设置网格插值
参数一 #latlon=500,400 网格分辨率 参数二 snwe经纬度范围
!ncremap -i bias_notime.nc -v OLR -G snwe=21,32,116,130#latlon=500,400 -o resample2.nc
Input #00: /home/mw/project/bias_notime.nc
Grid(src): /tmp/ncremap_tmp_grd_src.nc.pid3255
Grid(dst): /tmp/ncremap_tmp_grd_dst.nc.pid3255
Map/Wgt : /tmp/ncremap_tmp_map_nco_nco_con.nc.pid3255
import xarray as xr
resample = xr.open_dataset('/home/mw/project/resample2.nc')
resample.lon.data.shape
(400,)
resample.OLR.plot()
<matplotlib.collections.QuadMesh at 0x7fe5232b0250>

image
!ncremap -i bias_notime.nc -v OLR -G latlon=549,679#snwe=21,32,116,130 -o notime.nc
Input #00: /home/mw/project/bias_notime.nc
Grid(src): /tmp/ncremap_tmp_grd_src.nc.pid3088
Grid(dst): /tmp/ncremap_tmp_grd_dst.nc.pid3088
Map/Wgt : /tmp/ncremap_tmp_map_nco_nco_con.nc.pid3088
import xarray as xr
resample = xr.open_dataset('/home/mw/project/notime.nc')
resample.lon.data.shape
(679,)
resample.OLR.plot()
<matplotlib.collections.QuadMesh at 0x7fe52334d640>

image
!ncremap -a nco_idw -i bias_notime.nc -v OLR -G latlon=500,400#snwe=21,32,116,130 -o notime.nc
Input #00: /home/mw/project/bias_notime.nc
Grid(src): /tmp/ncremap_tmp_grd_src.nc.pid3422
Grid(dst): /tmp/ncremap_tmp_grd_dst.nc.pid3422
Map/Wgt : /tmp/ncremap_tmp_map_nco_nco_idw.nc.pid3422
import xarray as xr
resample = xr.open_dataset('/home/mw/project/notime.nc')
resample.OLR.plot()

image
学习各种高度集成的软件可以省去许多编程的功夫,许多同行碰到nco、cdo这种软件时当真大呼神器,大有相见恨晚之感
特别是nco对于wrf应该是做了非常多的适配,例如wrf合并作差等功能,cdo是无法完成的
参考链接: