#!/bin/bash
# Job name
# @ job_name = ExtractForcing
# Standard output file name
# @ output = ExtractForcing.000001
# Error output file name
# @ error = ExtractForcing.000001
# Job type
# @ job_type = serial
# Specific option for OpenMP parallelization: Number of OpenMP threads per MPI task
# Memory : as_limit=3.5gb max per process per core. With 4 threads per process use max as_limit=14gb
# @ as_limit = 15gb
# Maximum CPU time per task hh:mm:ss
# @ wall_clock_limit = 20:00:00
# End of the header options
# @ queue
#
######################################################################################################
#
# Functions
#
######################################################################################################
function concat2dfields {
infile=$1
outfile=$2
#
echo "==> Working on ${infile}"
#
# LWdown:long_name = "Mean surface incident longwave" ;
echo "==> LWdown:long_name = Mean surface incident longwave"
ncks -h -v rlds ${infile} LWdown.nc
ncrename -h -O -v rlds,LWdown LWdown.nc
ncatted -h -a cell_methods,LWdown,o,c,"Time: mean(center)" LWdown.nc
# SWdown:long_name = "Mean surface incident shortwave" ;
echo "==> SWdown:long_name = Mean surface incident shortwave"
ncks -h -v rsds ${infile} SWdown.nc
ncrename -h -O -v rsds,SWdown SWdown.nc
ncatted -h -a cell_methods,SWdown,o,c,"Time: mean(center)" SWdown.nc
# PSurf:long_name = "Surface pressure at time stamp" ;
echo "==> PSurf:long_name = Surface pressure at time stamp"
ncks -h -v ps ${infile} PSurf.nc
ncrename -h -O -v ps,PSurf PSurf.nc
ncatted -h -a cell_methods,PSurf,o,c,"Time: instantaneous" PSurf.nc
# Rainf:long_name = "Mean rainfall rate" ;
# Snowf:long_name = "Mean snowfall rate" ;
echo "==> Snowf:long_name = Mean snowfall rate"
echo "==> Rainf:long_name = Mean rainfall rate"
ncap2 -h -v -s 'Snowf=pr*SR;Rainf=pr-(pr*SR)' ${infile} Precipitation.nc
ncks -h -v Snowf Precipitation.nc Snowf.nc
# Ensure we do not have negative values
ncatted -h -a long_name,Snowf,o,c,"Mean snowfall rate" Snowf.nc
ncatted -h -a cell_methods,Snowf,o,c,"Time: mean(center)" Snowf.nc
ncks -h -v Rainf Precipitation.nc Rainf.nc
# Ensure we do not have negative values
ncatted -h -a long_name,Rainf,o,c,"Mean rainfall rate" Rainf.nc
ncatted -h -a cell_methods,Rainf,o,c,"Time: mean(center)" Rainf.nc
/bin/rm -rf Precipitation.nc Snowfa.nc Rainfa.nc
# contfrac
ncks -h -v LANDMASK geo_em.d01.nc contfrac.nc
ncrename -h -O -v LANDMASK,contfrac -d south_north,y_grid_M -d west_east,x_grid_M contfrac.nc
#
# Merge these variables into the forcing :
#
echo "==> Concatenate Surface fields"
ncwa -a Time contfrac.nc ${outfile}
ncks -h -A -v LWdown LWdown.nc ${outfile}
ncks -h -A -v SWdown SWdown.nc ${outfile}
ncks -h -A -v PSurf PSurf.nc ${outfile}
ncks -h -A -v Rainf Rainf.nc ${outfile}
ncks -h -A -v Snowf Snowf.nc ${outfile}
#
echo "==> Clean-up 2D"
/bin/rm -rf contfrac.nc LWdown.nc SWdown.nc PSurf.nc Rainf.nc Snowf.nc
}
function add3dfields {
#
infile=$1
outfile=$2
# Levels:long_name = "relative height at the first vertical level over surface on the Mass grid" ;
echo "==> Compute first level height"
ncap2 -h -v -s 'ZZ0=(PHB(0,:,:)+PH(:,0,:,:))/9.81;ZZ1=(PHB(1,:,:)+PH(:,1,:,:))/9.81' ${infile} ZZ.nc
ncap2 -h -v -s 'Levels=0.5*(ZZ1-ZZ0)' ZZ.nc Levels.nc
ncatted -h -a long_name,Levels,o,c,"Height of lowest atmospheric level" Levels.nc
ncatted -h -a units,Levels,o,c,"m" Levels.nc
/bin/rm -rf ZZ.nc
# Compute conversion coefficient for temperature
echo "==> Conversion for temperature (with P in Pa)"
ncap2 -h -v -s 'cf=((PB(0,:,:)+P(:,0,:,:))*0.01/1000.0)^0.2854' ${infile} cf.nc
#
#
# Tair:long_name = "air temperature" ;
echo "==> Tair:long_name = air temperature"
ncks -h -A -v ta ${infile} cf.nc
ncap2 -h -v -s 'Tair=ta(:,0,:,:)*cf' cf.nc Tair.nc
/bin/rm -rf cf.nc
ncatted -h -a long_name,Tair,o,c,"Near Surface Air Temperature" Tair.nc
ncatted -h -a cell_methods,Tair,o,c,"Time: instantaneous" Tair.nc
#
echo "==> Slicing the 3D fields"
ncap2 -h -v -s 'Qair=hus(:,0,:,:);WindX=Um(:,0,:,:);WindY=Vm(:,0,:,:)' ${infile} tmp.nc
ncks -h -A -v SINALPHA,COSALPHA ${infile} tmp.nc
echo "==> Qair:long_name = Near surface specific humidity"
ncks -h -v Qair tmp.nc Qair.nc
ncatted -h -a long_name,Qair,o,c,"Near Surface Specific Air Humidity" Qair.nc
ncatted -h -a cell_methods,Qair,o,c,"Time: instantaneous" Qair.nc
echo "==> Wind_E:long_name = Near surface eastward wind speed"
ncap2 -h -v -s 'Wind_E=WindX*COSALPHA - WindY*SINALPHA' tmp.nc Wind_E.nc
ncatted -h -a long_name,Wind_E,o,c,"Near Surface Eastward wind" Wind_E.nc
ncatted -h -a cell_methods,Wind_E,o,c,"Time: instantaneous" Wind_E.nc
echo "==> Wind_N:long_name = Near surface northward wind speed"
ncap2 -h -v -s 'Wind_N=WindX*SINALPHA + WindY*COSALPHA' tmp.nc Wind_N.nc
ncatted -h -a long_name,Wind_N,o,c,"Near Surface Northward wind" Wind_N.nc
ncatted -h -a cell_methods,Wind_N,o,c,"Time: instantaneous" Wind_N.nc
#
# Merge 3D var into the forcing file
#
echo "==> Add atmospheric fields"
ncks -A -v Levels Levels.nc ${outfile}
ncks -A -v Tair Tair.nc ${outfile}
ncks -A -v Qair Qair.nc ${outfile}
ncks -A -v Wind_E Wind_E.nc ${outfile}
ncks -A -v Wind_N Wind_N.nc ${outfile}
#
echo "==> Clean-up 3D"
/bin/rm -rf Levels.nc Tair.nc Qair.nc Wind_E.nc Wind_N.nc tmp.nc
}
function rshfilesize {
TEST=$(rsh ergon "test -e $1 && echo \"0\" || echo \"1\"")
if [ -eq 0 ] ; then
filesz=$(rsh ergon stat --printf="%s" $1)
else
filesz=0
fi
echo
}
function cleanupfile {
infile=$1
# Delete unneeded variables
ncrename -h -O -d time_counter,Time
ncrename -h -O -v time_centered,Time
ncrename -h -O -d y_grid_M,south_north -d x_grid_M,west_east
ncks -h -x -v time_counter,time_counter_bounds,time_instant tmp.nc
/bin/mv tmp.nc
# Correct coordinates
vars="LWdown SWdown PSurf Levels Tair Qair Wind_E Wind_N Rainf Snowf"
for v in ; do
ncatted -h -a coordinates,,o,c,"Time nav_lat_grid_M nav_lon_grid_M"
done
ncatted -h -O -a history,global,o,c,'Built by ExtractForcing.sh'
/bin/rm -rf tmp.nc
}
#
######################################################################################################
#
# Main script
#
######################################################################################################
#
cd
#
EXP="MEDCORDEX-A"
FPATH="IGCM_OUT/RegIPSL/PROD/${EXP}/WRFORCH/ATM/Output/HF/"
module load nco/4.4.6
module load cdo/1.5.9
module load python/2.7.10
#
# Clean-up
#
/bin/rm -rf *.nc *.tmp
mfget ${FPATH}/${FILE}
mfget ../rron960/rron600/MORCE2/FICHIERS/WRF-GEOGRID/GEOGRID-MORCE-STREAM2/geo_em.d01.nc
ls -l
#
cp ~/NetCDFTools/rebasetime.py .
#
# Using only NCO tools we extract from a WRF simulation (using XIOS output) the forcing data needed
# for ORCHIDEE. The 3hourly file will be the basis for the extraction.
#
EXISTS=($(rsh ergon ls "WRF_Forcing/${EXP}"))
#
for YEAR in 0115eq 1979 19990032; do
#
RESFILE=flux_${YEAR}.nc
fsz=$(rshfilesize "WRF_Forcing/${EXP}/${RESFILE}")
#
# Only do the work if the file is too small
#
if [ ${fsz} -lt 8000000000 ] ; then
#
# Get list of files to treat
#
LIST=($(rsh ergon ls "/*_*_*.nc"))
#
if [ ${#LIST[@]} -eq 4 ] ; then
for ff in ${LIST[*]} ; do
#
mfget
FILE=$(basename )
EXT=$(echo | cut -d '_' -f 2-3)
#
# Uncompress
ncks --fl_fmt=classic ${FILE} workfile.nc
forcingfile="forcing_${EXT}.nc"
#
concat2dfields workfile.nc ${forcingfile}
add3dfields workfile.nc ${forcingfile}
#
/bin/rm workfile.nc
#
done
#
# Transfer the forcing file to the mass store.
#
ncrcat -h forcing_${YEAR}*.nc ${RESFILE}
cleanupfile ${RESFILE}
python rebasetime.py -d 1979-01-01 ${RESFILE}
mfput ${RESFILE} WRF_Forcing/${EXP}
/bin/rm forcing_1979*.nc ${RESFILE}
#
# Should the geo_em file not already be there, transfer it.
#
fsz=$(rshfilesize "WRF_Forcing/${EXP}/geo_em.d01.nc")
if [ ${fsz} -lt 10 ] ; then
mfput geo_em.d01.nc WRF_Forcing/${EXP}
fi
else
echo " does not have enough semesters for building forcing"
fi
fi
done