#!/bin/bash
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# This script combines and formates the forcing shortwave and longwave 
# radiation fields. output_step12_0h.nc and output_step12_12h.nc as 
# described in README should be in the working directory 
#
# History : Virginie Guemas - Initial version                         - 2012 
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#                                Arguments
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
year='2013'
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ncap2 -O -s "ssrd=double(ssrd)" output_step12_12h.nc output_step12_12h.nc
ncap2 -O -s "strd=double(strd)" output_step12_12h.nc output_step12_12h.nc
ncap2 -O -s "ssrd=double(ssrd)" output_step12_0h.nc output_step12_0h.nc
ncap2 -O -s "strd=double(strd)" output_step12_0h.nc output_step12_0h.nc
ncatted -O -a add_offset,ssrd,d,d, output_step12_0h.nc output_step12_0h.nc
ncatted -O -a add_offset,strd,d,d, output_step12_0h.nc output_step12_0h.nc
ncatted -O -a scale_factor,strd,d,d, output_step12_0h.nc output_step12_0h.nc
ncatted -O -a scale_factor,ssrd,d,d, output_step12_0h.nc output_step12_0h.nc
ncatted -O -a add_offset,ssrd,d,d, output_step12_12h.nc output_step12_12h.nc
ncatted -O -a add_offset,strd,d,d, output_step12_12h.nc output_step12_12h.nc
ncatted -O -a scale_factor,strd,d,d, output_step12_12h.nc output_step12_12h.nc
ncatted -O -a scale_factor,ssrd,d,d, output_step12_12h.nc output_step12_12h.nc
cdo add output_step12_12h.nc output_step12_0h.nc output_ssrd_strd_int.nc
cdo divc,86400 output_ssrd_strd_int.nc  output_ssrd_strd.nc
ncks -v ssrd output_ssrd_strd.nc qsw_eraint.nc
ncrename -v ssrd,qsw qsw_eraint.nc
ncks -v strd output_ssrd_strd.nc qlw_eraint.nc
ncrename -v strd,qlw qlw_eraint.nc
cdo shifttime,-12h qlw_eraint.nc qlw_eraint_${year}.nc
cdo shifttime,-12h qsw_eraint.nc qsw_eraint_${year}.nc
rm -f output_step12_0h.nc output_step12_12h.nc output_ssrd_strd_int.nc output_ssrd_strd.nc qsw_eraint.nc qlw_eraint.nc
