echo "Stics4CarSolEl Job starting:"
date;

n_slots=${1};
csv_plan=${2};
meteo_archive=${4};
station_archive=${5};
sol_archive=${6};
sim_number=$(($(cat ${csv_plan} | wc -l) - 1));

b_size=1

echo "number of GALAXY_SLOTS: ${1}"

echo "expected simulations: ${sim_number}"
echo "cvle block size : ${b_size}"

if [ "$OMPI_COMM_WORLD_RANK" == "0" ]; then

    cp -r /data/vle-2001/pkgs/CarSolEl_data/data .
    rm -rf data/meteo
    rm -rf data/station
    rm -rf data/sol
    cd data
    unzip ${meteo_archive}
    unzip ${station_archive}
    unzip ${sol_archive}
    cd ..
fi

cvle -b $b_size -i ${2} -w work -P Stics4AgFlow -P CarSolEl_sim CarSolEl.vpz

if [ "$OMPI_COMM_WORLD_RANK" == "0" ]; then

    ls work_*/*_byUsm.txt | \
	xargs -t -i tail -n +2 {} | \
	sort -n -k 1 -t ';' > resWithoutHeader.csv

    head -1 work_0/1_byUsm.txt > header.cvs

    cat header.cvs resWithoutHeader.csv > res.csv

    mv res.csv ${3}

fi

echo "Stics4AgGlob Job ending:"
date;
