#!/bin/ksh
#-----------------------------------------------------------------
function ICO_Initialize
{
    IGCM_debug_PushStack "ICO_Initialize"

    ##- Horizontal resolution read from dynamic.card
    if [ ! X${dynamico_UserChoices_RESOL_NBP} = X ] ; then
	RESOL_NBP=${dynamico_UserChoices_RESOL_NBP}
    else
	# Exit
	IGCM_debug_Exit "RESOL_NBP must be set in dynamico.card UserSection"
    IGCM_debug_Verif_Exit
    fi

    ##- Horizontal resolution read from dynamico.card
    if [ ! X${dynamico_UserChoices_RESOL_ATM_Z} = X ] ; then
	RESOL_ATM_Z=${dynamico_UserChoices_RESOL_ATM_Z}
    else
        # RESOL_ATM_Z is not set in dynamico.card but this is needed
	IGCM_debug_Exit "RESOL_ATM_Z must be set in dynamico.card UserSection"
	IGCM_debug_Verif_Exit
    fi

    IGCM_debug_PopStack "ICO_Initialize"
}

#-----------------------------------------------------------------
function ICO_Update
{
    IGCM_debug_PushStack "ICO_Update"

    IGCM_comp_modifyDefFile blocker run_dynamico.def run_length $(( ${PeriodLengthInDays} * 86400 ))

    # Change run_dynamico.def according to UserChoices options set in dynmaico.card
    
    # Set create_etat0_limit=y or n and 
    if [ X${dynamico_UserChoices_create_etat0_limit} = Xy ] ; then 
	# create_etat0_limit is set in dynamcio.card to y
	# Activate create_etat0_limit=y in run_dynamico.def
	IGCM_comp_modifyDefFile nonblocker run_dynamico.def create_etat0_limit y

	# Use iflag_phys=101 or higher to activate aquaplanet mode
	if [ X${dynamico_UserChoices_iflag_phys} = X ] || [ X${dynamico_UserChoices_iflag_phys} = X1 ]; then 
            # iflag_phys is not set or iflag_phys=1 => run is not an aquaplanet
	    IGCM_comp_modifyDefFile nonblocker run_dynamico.def etat0              database
	    IGCM_comp_modifyDefFile nonblocker run_dynamico.def iflag_phys         1
	else
	    # This is an aquaplanet
	    IGCM_comp_modifyDefFile nonblocker run_dynamico.def etat0              held_suarez
	    IGCM_comp_modifyDefFile nonblocker run_dynamico.def iflag_phys         ${dynamico_UserChoices_iflag_phys}
	fi
    else
	# Deactivate create_etat0_limit to run a the GCM (aquaplanet or not)
	IGCM_comp_modifyDefFile nonblocker run_dynamico.def create_etat0_limit   n
	IGCM_comp_modifyDefFile nonblocker run_dynamico.def etat0                start_file

	# Set iflag_phys even though it only will be read but not used for the case create_etat0_limit=n
	if [ X${dynamico_UserChoices_iflag_phys} = X ] ||  X${dynamico_UserChoices_iflag_phys} = X1 ]; then 
            # iflag_phys is not set or iflag_phys=1 => run is not an aquaplanet
	    IGCM_comp_modifyDefFile nonblocker run_dynamico.def iflag_phys           1
	else
	    IGCM_comp_modifyDefFile nonblocker run_dynamico.def iflag_phys         ${dynamico_UserChoices_iflag_phys}
	fi
    fi 


    # Add include of context xml files for DYNAMICO in iodef.xml
    # In iodef.xml add on the next line after "COMPONENT CONTEXT"
    echo '<context id="icosagcm" src="./context_dynamico.xml"/>' >> add.tmp

    # Add inclusion of file context_input_dynamico.xml if this file exists
    if [ -f context_input_dynamico.xml ] ; then
        echo '<context id="icosagcm" src="./context_input_dynamico.xml"/>' >> add.tmp
    fi

    # Include xml files for output configuration if running with workflow CMIP6
    if [ X"$( echo ${config_UserChoices_ExpType} | grep CMIP6 )" != "X" ] ; then
      if [ -f ping_dynamico.xml ] ; then
        echo '<context id="icosagcm" src="./ping_dynamico.xml"/>' >> add.tmp
        echo '<context id="icosagcm" src="./dr2xml_dynamico.xml"/>' >> add.tmp
      fi
    fi
    cp iodef.xml iodef.xml.tmp
    sed -e "/COMPONENT CONTEXT/r add.tmp" iodef.xml.tmp > iodef.xml
    rm iodef.xml.tmp add.tmp

    # Add a Long Name as global attribute in output files if LongName is set in config.card
    if [ ! "X${config_UserChoices_LongName}" = "X" ] ; then
      listfile=$(ls file_def*dynamico.xml)
      echo "<variable id=\"LongName\" type=\"string\">${config_UserChoices_LongName}</variable>" > add.tmp
      for file in ${listfile} ; do
          cp ${file} ${file}.tmp
          sed -e "/<file id/r add.tmp" \
          ${file}.tmp > ${file}
          rm ${file}.tmp 
      done
      rm add.tmp
    fi


    # Compression level (if CompressionLevel is not empty)
    if [ ! "X${config_UserChoices_CompressionLevel}" = "X" ] ; then
	echo "NetCDF output files compression level is " ${config_UserChoices_CompressionLevel}
	listfile=$(ls file_def*dynamico.xml)
	for file in ${listfile} ; do
            sed -i -e "s/\(compression_level=\"\)[^\"]*\(\"\)/\1${config_UserChoices_CompressionLevel}\2/" ${file}
	done
    fi

    IGCM_debug_PopStack "ICO_Update"
}

#-----------------------------------
function ICO_Finalize
{
    IGCM_debug_PushStack "ICO_Finalize"

    IGCM_debug_PopStack "ICO_Finalize"
}

