#!/bin/bash

in_di=''
out_di=''

res='1.0'

#VAR='nbp npp gpp rh ra cVeg cSoil landCoverFrac mrso cLitter evapotrans'
#VAR='npp arh'
VAR='nbp npp rh arh cVeg cSoil cLitter'
SIM='S0 S1 S2 S3'

MOD='CABLE-POP CLASSIC CLASSIC-N CLM5.0 DLEM IBIS ISAM ISBA-CTRIP JSBACH LPJ-GUESS LPJ LPX-Bern OCN ORCHIDEE ORCHIDEEv3 SDGVM VISIT'


for m in $MOD; do
for v in $VAR; do
for s in $SIM; do
INFILE=$in_di$m'/'$s'/'$m'_'$s'_'$v'.nc'
if [ -f "$INFILE" ]; then
OUTFILE=$out_di$m'/'$m'_'$s'_'$v'_'$res'.nc'
if [ ! -f "$OUTFILE" ]; then
echo "regridding $INFILE..."
echo "outfile = $OUTFILE"
cdo remapcon,r360x180 $INFILE $OUTFILE
fi
fi
done
done
done


MOD='JULES-ES-1.1'
for m in $MOD; do
for v in $VAR; do
for s in $SIM; do
#INFILE=$in_di$m'/'$s'/JULES-ES-1p1_'$s'_'$v'.nc'
INFILE=$in_di$m'/'$s'/JULES-ES-1p1_'$s'_'$v'*'
INFILE=$(ls $INFILE)
#echo $INFILE
#if [ -f "$INFILE" ]; then
OUTFILE=$out_di$m'/'$m'_'$s'_'$v'_'$res'.nc'
#echo $OUTFILE
if [ ! -f "$OUTFILE" ]; then
echo "regridding $INFILE..."
echo "outfile = $OUTFILE"
cdo remapcon,r360x180 $INFILE $OUTFILE
fi
done
done
done


MOD='YIBs'
for m in $MOD; do
for v in $VAR; do
for s in $SIM; do
INFILE=$in_di$m'/'$s'/*'$v'.nc'
INFILE=$(ls $INFILE)
#echo $INFILE
#if [ -f "$INFILE" ]; then
OUTFILE=$out_di$m'/'$m'_'$s'_'$v'_'$res'.nc'
#echo $OUTFILE
if [ ! -f "$OUTFILE" ]; then
echo "regridding $INFILE..."
echo "outfile = $OUTFILE"
cdo remapcon,r360x180 $INFILE $OUTFILE
fi
done
done
done
