Search code examples
pythonnetcdfpython-xarrayncocdo-climate

Merging multiple Netcdf files according to the vertical level


I have 34 netCDF (nc) files containing latitude, longitude, and the data in every file. Every filename containing a number which corresponds to the Pressure level in hPa (starts from 1 to 34 for respective pressure level from (1000 hPa to 0.4 hPa). I want to join all files into a single nc file with this vertical level dimension information.

I tried to read whole files using xarray open_mfdataset but I cannot con_cat with the level dimension since it isn't in the files.

import xarray as xr
ds = xr.open_mfdataset('/media/MediaCentre/Dataset/d9/data*.nc',concat_dim='level')

The files do not have any information in the global attributes regarding the pressure. They are names sequentially: data1.nc, data2.nc, ... dataN.nc and correspond to the pressure levels (hPa) of: 1000 975 950 925 900 850 800 750 700 650 600 550 500 450 400 350 300 *250 200 150 100 70 50 40 30 20 15 10 7 5 3 2 1 0.4

How can I merge these together using python xarray, or cdo/nco?

Sample data are here https://www.dropbox.com/sh/linfn0721ze3j1f/AACwxTsQVNyiE7mF_gRbpRfra?dl=0


Solution

  • A different approach with NCO would first combine the levels with ncecat, e.g.,

    ncecat -u level in*.nc out1.nc
    

    and then add the level coordinate with ncap2, e.g.,

    ncap2 -O -s 'level[$level]={1000, 975, ... 0.4}' out1.nc out2.nc
    

    and then add the attributes with ncatted as Adrian shows.

    ncatted <Adrian's example> out2.nc out3.nc
    

    Good luck, Charlie