#!/bin/csh -f
# splits the original soil - NOx landtype nc file into 24 layers and merges them together 
# into a new file, with each layer having its own variable name, i.e. the lon,lat,koppen
# array is split into 24 lon,lat,(lev=1) arrays.
# the input file must have recognizable vertical levels, and the data values must be float 
# not integer!!
# Christoph Keller, Nov 2013. 

# filename
set infile  = 'soilNOx.landtype.generic.025x025.orig.nc'
set outfile = 'soilNOx.landtype.generic.025x025.nc'

# select each "level" and write into own file. Change variable name
set i = 1
while ( $i <= 24 )
   set j = `printf "%02d\n" $i`
   cdo sellevidx,$i $infile tmp.nc
   cdo setlevel,1 tmp.nc tmp2.nc
   cdo chname,LANDFRAC,LANDFRAC_K$j tmp2.nc tmp$j.nc
   @ i++
end

# merge all files
rm -f tmp.nc
rm -f tmp2.nc
cdo merge tmp*.nc $outfile
rm -f tmp*.nc

# exit
unset i
unset j
unset infile
unset outfile

exit(0)

# eof
