Project

General

Profile

Merge two grib2 files, but end up with two sets of coordinates

Added by Jia Wang 2 months ago

I have two grib2 files: a.grib2 and b_org.grib2.

I interpolate b_org.grib2 to the grids defined in a.grib2, and then merge them

cdo -remapbil,a.grib2 b_org.grib2 b_new.grib2
cdo merge a.grib2 b_new.grib2 c.grib2

But in c.grib2, there are two sets of lat/lon coordinates. why, since they should have the same set of coordinates?


Replies (3)

RE: Merge two grib2 files, but end up with two sets of coordinates - Added by Karin Meier-Fleischer 2 months ago

Try

cdo -O -remapbil,a.grib2 -setzaxis,surface b_org.grib2 b_remap2.grib2

cdo -O -mergetime a.grib2 -selgrid,1 b_remap.grib2 a_b_merged2.grib2

RE: Merge two grib2 files, but end up with two sets of coordinates - Added by Jia Wang 2 months ago

Thanks Karin.

After using your method to do the merge, the variables in a.grib2 and b_remap.grib2 are combined into one variable with an additional time dimension, when I view the result using ncl_filedump.

The variable in a.grib2 is model forecast, and the variable in b.grib2 is observation.

How to separate the two variables in the merged file, while only have one set of lat-lon coordinates?

RE: Merge two grib2 files, but end up with two sets of coordinates - Added by Karin Meier-Fleischer 2 months ago

The problem are the different time stamps. I was not able to delete the time dimension but I'm not a GRIB expert.

If you can go with netcdf than the following works:

#-- get the time stamps
ta=$(cdo -s showtimestamp a.grib2 | sed -e 's/ //g' -e 's/\://g')
tb=$(cdo -s showtimestamp b_org.grib2 | sed -e 's/ //g' -e 's/\://g')

#-- get the variable names
va=$(cdo -s showvar a.grib2 | sed -e 's/ //g')
vb=$(cdo -s showvar b_org.grib2 | sed -e 's/ //g')

#-- just to keep the time stamp add it as variable attribute
cdo -f nc --reduce_dim -setattribute,${va}@time=${ta} -seltimestep,1 a.grib2  tmp_${va}_${ta}.nc
cdo -f nc --reduce_dim -setattribute,${vb}@time=${tb} -seltimestep,1 b_org.grib2  tmp_${vb}_${tb}.nc

#-- remap b to a grid
cdo -O -remapbil,tmp_${va}_${ta}.nc -setzaxis,surface tmp_${vb}_${tb}.nc  tmp_${vb}_${tb}_remap.nc

#-- merge the files
cdo -O -merge \
        tmp_${va}_${ta}.nc \
        tmp_${vb}_${tb}_remap.nc \
        ${va}_${ta}_${vb}_${tb}.nc
>> cdo sinfon prate_2023-01-01T010000_param1.6.209_2023-01-01T003000.nc

   File format : NetCDF2
    -1 : Institut Source   T Steptype Levels Num    Points Num Dtype : Parameter name
     1 : unknown  unknown  c instant       1   1   1905141   1  F32  : prate         
     2 : unknown  unknown  c instant       1   1   1905141   1  F32  : param1.6.209  
   Grid coordinates :
     1 : projection               : points=1905141 (1799x1059)
                          mapping : lambert_conformal_conic
                                x : 0 to 5394000 by 3000 m
                                y : 0 to 3174000 by 3000 m
   Vertical coordinates :
     1 : surface                  : levels=1
    (1-3/3)