#!/bin/bash
#
##################################################################
##################################################################
#
# To generate XML metadata files for multi-yrear periods 4Dncfiles.
# run by "divadoxml" only
###################################################################
###################################################################
#
var=$1
#
WDIR=$(pwd)
DIVADIR="../../DIVA3D"
mkdir -p ./output/3Danalysis/MetaDataXMLs
#
FILENAME=./input/general_info
if [ -f "$FILENAME" ]
then
{
read comment
read organisation_name
read comment
read org_email
read comment
read author_org_name
read comment
read author_email
} < $FILENAME
else
echo Could not find $FILENAME
echo Please generate information file $FILENAME first
exit
fi
#
INSTITUT_name="'"${organisation_name}"'"
INSTITUT_email="'"${org_email}"'"
PRODUCTION_name="'"${author_org_name}"'"
PRODUCTION_email="'"${author_email}"'"
#
organisation_name=${organisation_name//\//\\\/}
org_email=${org_email//\//\\\/}
author_org_name=${author_org_name//\//\\\/}
author_email=${author_email//\//\\\/}
#
nblns=`cat ./yearlist |wc -l`
Fileinf=./yearlist
{
read year
year1=${year:0:4}
year2=${year:4:4}
ll=2

while [ $ll -le $nblns ]
do
read year
year2=${year:4:4}
let ll=$ll+1
done
} < $Fileinf

nblns=`cat ./monthlist |wc -l`
Fileinf=./monthlist
{
read month
month1=${month:0:2}
month2=${month:2:2}
ll=2

while [ $ll -le $nblns ]
do
read month
month2=${month:2:2}
let ll=$ll+1
done
} < $Fileinf
#

#########################################################
for var in `cat varlist`
#########################################################
do
echo variable $var

year='-9999'

rm -rf ${DIVADIR}/divastripped/output/*
rm -rf ${DIVADIR}/divastripped/divawork/*
rm -rf ${DIVADIR}/divastripped/3DWORK/*


cp ./output/3Danalysis/$var'.4Danl.nc' ${DIVADIR}/divastripped/output/.


echo  $var   > ${DIVADIR}/divastripped/3DWORK/md.info
echo  $year >> ${DIVADIR}/divastripped/3DWORK/md.info

if [ "$#" == "2" ];then
cp './output/3Danalysis/'$var'.4Danl.nc' './output/3Danalysis/OLD_'$var'.4Danl.nc'
echo  $INSTITUT_name  > ${DIVADIR}/divastripped/3DWORK/glatt.info
echo  $INSTITUT_email >> ${DIVADIR}/divastripped/3DWORK/glatt.info
echo  $PRODUCTION_name >> ${DIVADIR}/divastripped/3DWORK/glatt.info
echo  $PRODUCTION_email >> ${DIVADIR}/divastripped/3DWORK/glatt.info
fi

cd ${DIVADIR}/divastripped/3DWORK/

../../bin/diva4Drdmd.a

cd ../

if [ "$#" == "2" ];then
cp './output/'$var'.4Danl.nc' ${WDIR}'/output/3Danalysis/'$var'.4Danl.nc'
fi
cp -v ./output/$var'.4DNCMetainfo' ${WDIR}/output/3Danalysis/.
cp -v ./output/$var'.Metainfo.txt' ${WDIR}/output/3Danalysis/.

cd ${WDIR}/

FILENAME=./output/3Danalysis/$var'.4DNCMetainfo'
if [ -f "$FILENAME" ]
then
{
read NXcolumn
read NYlines
read Sunit
read var_units
read SouthBL
read NorthBL
read WestBL
read EastBL
read deltaX
read deltaY
read vertlevs
read mindep
read maxdep
} < $FILENAME
else
echo Could not find $FILENAME
echo Please generate information file $FILENAME first
exit
fi

#
today=`date --rfc-3339='date'`
#
resolt=$deltaX'X'$deltaY

############################################################
FILENAME=./$var'.SDNconv'

if [ -f "$FILENAME" ]
then
{
read comment
read prodLocShn
read comment
read prodPar
read comment
read Thumbnailfilename
read comment
read url_compliment
read comment
read Geo_area
read comment
read Time_units
} < $FILENAME
else
echo Could not find $FILENAME
echo Please provide variable SDN coventions file $FILENAME 
exit
fi

prodLocShn=${prodLocShn//\//\\\/}
prodPar=${prodPar//\//\\\/}
Thumbnailfilename=${Thumbnailfilename//\//\\\/}
url_compliment=${url_compliment//\//\\\/}

######
#
varlongname=${prodPar}
sea_area=${Geo_area}
NO='NO'
UseLim1='SeaDataNet licence'
UseLim2='Distributed under SeaDataNet licence'
#
#############

File=sextan.tmplt
##XMLtempl
##File=$XMLFile
NewFile='./output/3Danalysis/'$var'.xml'
###'./output/3Danalysis/MetaDataXMLs/'$prodLocShn.xml'

sed \
-e s/"SDN:PRODUCT:EDMO id:MOstr_SDN_Local_Product_ID_endMO"/"SDN:PRODUCT:EDMO id:${prodLocShn}"/ \
-e s/"SDN:EDMO::EDMO id = SDN:EDMO name_ccmd"/"${author_org_name}"/ \
-e s/"MOstr_ccmetadata@e-mail.adress_endMO"/"${author_email}"/ \
-e s/"2012-06-00"/"${today}"/ \
-e s/"9991"/"${NXcolumn}"/ \
-e s/"9992"/"${NYlines}"/ \
-e s/"MOstr_time_resolution_endMO"/"${Time_units}"/ \
-e s/"2012-06-20"/"${today}"/ \
-e s/"2012-06-21"/"${today}"/ \
-e s/"MOstr_SDN_Local_Product_ID_endMO"/"${prodLocShn}"/ \
-e s/"MOstr_Short_description_endMO"/" Analyses of ${varlongname} data in ${sea_area} on bases of ${Time_units}s between ${year1} and ${year2}. Units=${var_units}. Time units: ${Time_units}. Depth range: ${vertlevs}"/ \
-e s/"SDN:EDMO::EDMO id = SDN:EDMO name_org"/"${organisation_name}"/ \
-e s/"MOstr_organisation@e-mail.adress_endMO"/"${org_email}"/ \
-e s/"SDN:EDMO::EDMO id = SDN:EDMO name_dhc"/"SDN:EDMO::EDMO xxx = SDN:EDMO University of Liege, GeoHydrodynamics and Environment Research"/ \
-e s/"MOstr_datacenter@e-mail.adress_endMO"/"a.barth@ulg.ac.be"/ \
-e s/"MOstr_Thumbnail_name_endMO"/"${Thumbnailfilename}"/ \
-e s/"SDN:P021:68:NTRA = Nitrate concentration parameters in the water column"/"${prodPar}"/ \
-e s/"MOstr_Use_Limitation_for_what2_endMO"/"${UseLim2}"/ \
-e s/"Mostr_Other_restrictions_and_leagal_prerequisities_endMO"/"${NO}"/ \
-e s/"MOsrt_Not_to_be_used_for_what_endMO"/"${UseLim1}"/ \
-e s/"9993"/"${deltaX}"/ \
-e s/"MOstr_Sunit_endMO"/"${Sunit}"/ \
-e s/"Mostr_deltaXxdeltaY_endMO"/"${deltaX}X${deltaY}"/ \
-e s/"yyy1-m1"/"${year1}-${month1}"/ \
-e s/"yyy2-m2"/"${year2}-${month1}"/ \
-e s/"-180WB"/"${WestBL}"/ \
-e s/"180EB"/"${EastBL}"/ \
-e s/"-90SB"/"${SouthBL}"/ \
-e s/"90NB"/"${NorthBL}"/ \
-e s/"SDN:C16:8:ZZ = Unknown"/"${Geo_area}"/ \
-e s/"MOstr_distrib_transfer_format_endMO"/"Climate and Forecast NetCDF"/ \
-e s/"Mostr_version_distrb_format_endMO"/"DIVA 4D-Climatology"/ \
-e s/"MOstr_descript_specification_format_endMO"/"Diva 4D Climatology CF netcdf file contains the diva analysis of the variable and a set of variable related information fields: relative error and error standard deviation, variable masked relative error thresholds .3 and .5, and deepest values. It contains also fields of information about data distribution, outliers, correlation length and signal to noise ratio parameters."/ \
-e s/"SDN:EDMO::EDMO id = SDN:EDMO name_ddc"/"SDN:EDMO::EDMO xxx = SDN:EDMO University of Liege, GeoHydrodynamics and Environment Research"/ \
-e s/"MOstr_Dataset_size_endMO"/"${NO}"/ \
-e s/"MOstr_URL_Accessing_website_endMO"/"http:\/\/gher-diva.phys.ulg.ac.be\/data\/emodnet-domains\/${url_compliment}"/ \
-e s/"MOstr_name_resource_endMO"/"EMODNET Data Products Viewing and Downloading Service"/ \
-e s/"Mostr_description_access_web_endMO"/"OPENDAP download"/ \
-e s/"9998"/"${mindep}"/ \
-e s/"9999"/"${maxdep}"/ \
-e s/"MOstr_Hor_accuracy_description_endMO"/"${NO}"/ \
-e s/"MOstr_Measure_description_endMO"/"refer to autors"/ \
-e s/"MOstr_Evaluation_method_description_endMO"/"${NO}"/ \
-e s/"DIVA software"/" DIVA (Data Interpolating Variational Analyses) software "/ \
-e s/"MOstr_Description_of_data_processing_:_endMO"/" DIVA (Data Interpolating Variational Analyses) allows a spatial interpolation of data (analysis) in an optimal way, it takes into account coastlines, sub-basins, advection and detrending. Calculations are highly optimized and rely on a finite element resolution. Quality control of data can be performed and error fields can be calculated. 3D and 4D extensions are included with emphasis on direct computations of climatologies from ODV spreadsheet files.}"/ \
-e s/"MOstr_Description_of_processed_data_sources :_endMO"/"refer to autors"/ \
  <$File>$NewFile

done
## 
