Project

General

Profile

Replacing a variable without changing the attributes

Added by Ankur Kumar over 5 years ago

I have two nc files viz. file1.nc and file2.nc
file1.nc contains 3 variable (var1,var2,var3) and file2.nc contains 1 variable (var2)

I want to replace var2 of file1.nc with var2 of file2.nc. How can I do this by keeping the same global and variable attributes as that of the previous variable.

I have tried using replace command in cdo, but it completely changes the variable attribute.
I want to keep everything (attribute as in the file1.nc). Just ONLY I want to change the data containing var2 in file1.nc with var2 of file2.nc.

file2.nc (44.2 KB) file2.nc containing 1 variable viz., var2
file1.nc (705 KB) file1.nc containing 3 variables viz., var1, var2, var3

Replies (1)

RE: Replacing a variable without changing the attributes - Added by Karin Meier-Fleischer over 5 years ago

Hi Ankur,

the only thing which is different between var2 in file1.nc and the output of 'cdo replace' is the added attribute code for each variable (which is not distracting). But if you want to get rid of it use ncatted from the NCO's.

Replace var2 from file2.nc in file1.nc and write it to file out.nc:

cdo replace file1.nc file2.nc out.nc

Delete the code attributes of the variables in file out.nc:

ncatted -a code,var1,d,, -a code,var2,d,, -a code,var3,d,, out.nc

-Karin

    (1-1/1)