replace data data in file1 from file2 with a condition
Added by Sagar Parajuli about 1 year ago
I have two files LAI_2020_all_regrid_time_assigned.nc and geo_em.d03_org_LAI12M.nc (attached) with identical grids and the variable LAI12M present in both of them. The variable LAI12M in the first file LAI_2020_all_regrid_time_assigned.nc has _FillValue and missing_value both set to 9.96920996838687e+36. I want to replace all the grids that are assigned _FillValue or missing_value in file LAI_2020_all_regrid_time_assigned.nc with the corresponding grid values from file geo_em.d03_org_LAI12M.nc. Looking from the documentation, it seems that this is possible using 'ifthen' or 'ifnotthen' but I have never used these commands before. Could anyone help please?
Replies (1)
RE: replace data data in file1 from file2 with a condition - Added by Karin Meier-Fleischer about 1 year ago
Hi Sagar,
the files are not CF-convention conform which makes it a lot harder to come to a result. There are multiple problems like multiple time entrys but all the same time 0000-00-00 00:00:00 and the grid information in file geo_em.d03_org_LAI12M.nc is missing.
Use CDO and NCO to do all the needed corrections. But I recommend to do it when you create the data files.
Delete the not needed one element dimension Time/Times:
cdo --reduce_dim -setgrid,LAI_2020_all_regrid_time_assigned.nc geo_em.d03_org_LAI12M.nc g.nc cdo --reduce_dim -copy LAI_2020_all_regrid_time_assigned.nc l.nc
Rename month to time:
ncrename -O -d month,time g.nc gg.nc ncrename -O -d month,time l.nc ll.nc
Make time dimension unlimited:
ncks -O --mk_rec_dmn time gg.nc gg2.nc ncks -O --mk_rec_dmn time ll.nc ll2.nc
Create a correct monthly time array:
ncap2 -O -s 'time[time]=array(0,1,$time)' gg2.nc ggg.nc ncap2 -O -s 'time[time]=array(0,1,$time)' ll2.nc lll.nc
Add units attribute to the time dimension variable:
ncatted -O -a units,time,c,c,"month since 2020-01-01" ggg.nc ncatted -O -a units,time,c,c,"month since 2020-01-01" lll.nc
Set missing value to a constant, create mask, use data to fill empty cells:
cdo -ifthenelse -gtc,-9999 -setmisstoc,-9999 lll.nc lll.nc ggg.nc outfile.nc