#!/bin/bash
#
############################################################
############################################################
#
# To generate XML metadata files.
#
# You need to provide the following files:
#                    ./input/general_info
#                    ./varlist`
#                    ./yearlist
#                    ./monthlist
# for each var in varlist:
#                    ./$var'.SDNconv'
# and for each var in varlist and year in yearlist:
#                    ./output/3Danalysis/$var.$year.4Danl.nc
#                                    or
#                    ./output/3Danalysis/$var.4Danl.nc
############################################################
############################################################
#
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

if [ -f './output/3Danalysis/'$var'.4Danl.nc' ];then


echo will run on '$var'.4Danl.nc

if [ "$#" == "1" ];then
./dvdoyrsxml $var rwt
else
./dvdoyrsxml $var
fi

else
#########################################################
for year in `cat yearlist`
#########################################################
do
echo year $year

if [ -f './output/3Danalysis/'$var'.'$year'.4Danl.nc' ];then
cp ./output/3Danalysis/$var'.'$year'.4Danl.nc' ${DIVADIR}/divastripped/output/.

echo 'will run on '$var'.'$year'.4Danl.nc'

fi

year1=${year:0:4}
year2=${year:4:4}

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

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

if [ "$#" == "1" ];then

cp './output/3Danalysis/'$var'.'$year'.4Danl.nc' './output/3Danalysis/OLD_'$var'.'$year'.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 [ "$#" == "1" ];then
cp './output/'$var'.'$year'.4Danl.nc' ${WDIR}'/output/3Danalysis/'$var'.'$year'.4Danl.nc'

cp -v ./output/$var'.4DNCMetainfo' ${WDIR}/output/3Danalysis/.
cp -v ./output/$var'.Metainfo.txt' ${WDIR}/output/3Danalysis/.

fi

cd ${WDIR}/


 #datasetsize=`cat './output/3Danalysis/'$var'.'$year'.4Danl.nc' | stat -c %s ` 


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
var_units=${var_units//\//\\\/}
#
today=`date --rfc-3339='date'`
#
resolt=$deltaX'X'$deltaY

############################################################
FILENAME='./input/NCDFinfo'
if [ -f "$FILENAME" ]
then
{
read comment
read comment
read comment
read comment
read comment
read comment
read comment
read comment
read comment
read comment
read comment
read comment
read comment
read data_source_str
} < $FILENAME
else
echo Could not find $FILENAME
echo Please provide variable SDN coventions file $FILENAME 
exit
fi

FILENAME=./$var'.SDNconv'
if [ -f "$FILENAME" ]
then
{
read comment
read prodLocShn
read comment
read prodPar
read comment
read MesInst
read comment
read PosInst
read comment
read Thumbnailfilename
read comment
read url_compliment
read comment
read Geo_area
read comment
read Time_units
read comment
read UseLim2
} < $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//\//\\\/}
UseLim2=${UseLim2//\//\\\/}
MesInst=${MesInst//\//\\\/}
PosInst=${PosInst//\//\\\/}
Geo_area=${Geo_area//\//\\\/}
######
#
varlongname=${prodPar}
sea_area=${Geo_area}
NO='NO'
UseLim1='SeaDataNet licence'
#UseLim2='Distributed under SeaDataNet licence'
#
#############

 ll1=${#varlongname}
sh_varlongname=${varlongname:18:$ll1}
 ll2=${#sea_area}
sh_sea_area=${sea_area:16:$ll2}

File=sextan.tmplt
##XMLtempl
##File=$XMLFile
NewFile='./output/3Danalysis/'$var'.'$year'.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/"2013-01-10T00:00:00"/"${today}T00:00:00"/ \
-e s/"9991"/"${NXcolumn}"/ \
-e s/"9992"/"${NYlines}"/ \
-e s/"MOstr_Time_unit_endMO"/"Time"/ \
-e s/"MOstr_time_resolution_endMO"/"${Time_units}"/ \
-e s/"2013-01-09"/"${today}"/ \
-e s/"2012-01-10"/"${today}"/ \
-e s/"MOstr_SDN_Local_Product_ID_endMO"/"${prodLocShn}"/ \
-e s/"MOstr_SDN_Local_Product_ID_endMO"/"${prodLocShn}"/ \
-e s/"MOstr_Short_description_endMO"/"Analyses of ${sh_varlongname} data in ${sh_sea_area} on bases of ${Time_units} between ${year1} and ${year2}. Units=${var_units}. Time units: ${Time_units}. Depth range: ${vertlevs}"/ \
-e s/"SDN:LO56:12\/130 = CTD"/"Not relevant"/ \
-e s/"SDN:LO56:3:POS24 = dead reckoning"/"Not relevant"/ \
-e s/"MOstr_Description_of_data_sources_endMO"/"${data_source_str}"/ \
-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::1579 = SDN: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/"SDN:LO54:12:130 = CTD"/"${MesInst}"/ \
-e s/"SDN:LO56:3:357 = acoustic traking systems"/"${PosInst}"/ \
-e s/"MOstr_Use_Limitation_for_what_endMO"/"${UseLim2}"/ \
-e s/"Mostr_Other_restrictions_and_leagal_prerequisities_endMO"/"${NO}"/ \
-e s/"MOstr_Not_to_be_used_for_what_endMO"/"${UseLim1}"/ \
-e s/"MOstr_Sunit_endMO"/"${Sunit}"/ \
-e s/"Mostr_deltaXxdeltaY_endMO"/"${deltaX}X${deltaY}"/ \
-e s/"2010-01"/"${year1}-${month1}"/ \
-e s/"2012-12"/"${year2}-${month2}"/ \
-e s/"-180.00000"/"${WestBL}"/ \
-e s/"180.00000"/"${EastBL}"/ \
-e s/"-90.00000"/"${SouthBL}"/ \
-e s/"90.00000"/"${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::1579 = SDN: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_CRScode_endMO"/"WGS 84 (EPSG:4326)"/ \
-e s/"MOstr_Geodeticsys_endMO"/" "/ \
-e s/"MOstr_GeodeticsysDes1_endMO"/" "/ \
-e s/"MOstr_GeodeticsysDes2_endMO"/" "/ \
-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_Hor_accuracy_quantitave_endMO"/" Not defined"/ \
-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

fi

done
## 
