Project

General

Profile

Problem in merging two nc files

Added by javad Babagolii 6 months ago

I have two NetCDF files: W.nc and E.nc. The first file covers the region defined by coordinates [-10, -0.25, 29, 47], and the second file covers the region [0.25, 37, 29, 47]. These coordinates represent [west, east, south, north] respectively. Each file contains the variables: lat, lon, lwrad, lwrad_down, Pair, Qair, rain, swrad, Tair, time, Uwind, and Vwind. I am attempting to merge both files into a single output file named out.nc, merging them along the longitude axis.

In this case, collgrid is designed but the result is wrong. I attached the figures.

Are there any ideas?


Replies (7)

RE: Problem in merging two nc files - Added by Karin Meier-Fleischer 6 months ago

Without further information or files we can only guess what the problem is. The longitudes of the two files do not connect seamlessly, there is a gap.

RE: Problem in merging two nc files - Added by Karin Meier-Fleischer 6 months ago

I guess you have to remap the curvilinear grid to lonlat grid first, and then you have to expand the grids by one element right respectively left so that collgrid can be used to merge it onto one grid.

1. Create grid description file g1.txt for 199101W.nc

gridtype  = lonlat
gridsize  = 2880
xsize     = 40
ysize     = 72
xname     = lon
xlongname = "longitude" 
xunits    = "degree_east" 
yname     = lat
ylongname = "latitude" 
yunits    = "degree_north" 
xfirst    = -10.
xinc      = 0.25
yfirst    = 29.25
yinc      = 0.25

2. Create grid description file g2.txt for 199101E.nc

gridtype  = lonlat
gridsize  = 10656
xsize     = 148
ysize     = 72
xname     = lon
xlongname = "longitude" 
xunits    = "degree_east" 
yname     = lat
ylongname = "latitude" 
yunits    = "degree_north" 
xfirst     = 0.0
xinc       = 0.25
yfirst     = 29.25 
yinc       = 0.25

3. Remap both data files

cdo -remapbil,g1.txt 199101W.nc o1.nc
cdo -remapbil,g2.txt 199101E.nc o2.nc

4. Collect the files

cdo -collgrid o1.nc o2.nc outfile.nc

RE: Problem in merging two nc files - Added by javad Babagolii 6 months ago

Thanks a lot.

Here is the new image.

The only thing: Is it possible to interpolate for missing values in -0.25 and 0 along longitude?

mede.svg (176 KB) mede.svg

RE: Problem in merging two nc files - Added by Karin Meier-Fleischer 6 months ago

cdo -O -setmisstonn -collgrid o1.nc o2.nc outfile.nc

RE: Problem in merging two nc files - Added by javad Babagolii 6 months ago

Thanks a lot!

It works properly.

But, I have faced a problem.

In the initial file is some header and attributes, after merging the attributes will omit.

I will use this file for the ocean model as a forcing file, so I have faced the error:

GET_VARCOORDS - Cannot find "coordinates" attribute for variable:  Uwind
in file: /scratch/f85015jb/MedSea/MED/1991/199101.nc
This attribute is needed to interpolate input data
to model grid. Following CF compliance, we need:
float my_var(time, lat, lon) ;
my_var:long_name = "my variable long name" ;
my_var:units = "my variable units" ;
my_var:coordinates = "lon lat" ;
my_var:time = "my_var_time" ;

I need all of them exactly the same as the initial file. Is there any solution?

RE: Problem in merging two nc files - Added by Karin Meier-Fleischer 6 months ago

#!/usr/bin/env bash

file1=199101W.nc
file2=199101E.nc

cdo -remapbil,g1.txt $file1 o1.nc
cdo -remapbil,g2.txt $file2 o2.nc

cdo -O -setmisstonn -collgrid o1.nc o2.nc outfile.nc

ncdump -h $file1 | grep coordinates > coords.txt

while read -r line; do
    var=$(echo "${line}" | cut -d ':' -f 1)
    ncatted -O -a coordinates,${var},c,c,"lon lat time" outfile.nc
done < coords.txt
    (1-7/7)