function xml_iodef() {
cat <<EOF
<?xml version="1.0"?>
<simulation> 
	<context  id="xios">
		<variable_definition>
                <variable id="print_file" type="bool"> true </variable> 
       		<variable_group id="buffer">
         	</variable_group>

        	<variable_group id="parameters" >
        		<variable id="using_server" type="bool">false</variable>
        		<variable id="info_level" type="int">1</variable>
        	</variable_group>
		</variable_definition>
	</context>
        <context id="icosagcm" src="./icosa.xml" /> 
</simulation>
EOF
}

function xml_icosa() {
cat <<EOF
<context id="icosagcm" >
  <calendar type="d360" start_date="2000-01-01 00:00:00" time_origin="2000-01-01 00:00:00"/>
  
  <field_definition src="./field_def.xml" />

  <file_definition src="./filedef_dcmip2016.xml" /> 

  <file_definition type="one_file" par_access="collective">

    <file id="output" name="output" enabled=".FALSE."  output_freq="2h" sync_freq="2h" output_level="10" >
      <field_group group_ref="standard_output"/>
    </file>

    <file id="restart" name="restart" enabled="true">
       <field_group id="group_restart" operation="once"> 
          <field id="it_restart" name="iteration"  grid_ref="scalar" />
          <field id="q_restart" name="q"  grid_ref="grid_q" />
          <field_group domain_ref="i"> 
             <field id="ps_restart" name="ps" />
             <field id="phis_restart" name="phis" />
             <field_group axis_ref="lev" >
                <field id="theta_rhodz_restart" name="theta_rhodz" />
                <field id="u_restart" name="u" domain_ref="u"/>
                <field id="ulon_restart" name="ulon" />
                <field id="ulat_restart" name="ulat" />
             </field_group>
             <field_group axis_ref="levp1" >
                <field id="W_restart" name="W" />
                <field id="geopot_restart" name="geopot" />
             </field_group>
          </field_group>
       </field_group>
    </file>

    <file id="start" name="start" enabled="false" mode="read">
       <field_group id="group_start" operation="once"> 
          <field id="it_start" name="iteration"  grid_ref="scalar" enabled="false"/>
          <field id="q_start" name="q"  grid_ref="grid_q_read" />
          <field_group domain_ref="i"> 
             <field id="ps_start" name="ps" />
             <field id="phis_start" name="phis" />
             <field_group axis_ref="lev_read" >
                <field id="theta_rhodz_start" name="theta_rhodz" />
                <field id="u_start" name="u" domain_ref="u"/>
                <field id="ulon_start" name="ulon" />
                <field id="ulat_start" name="ulat" />
             </field_group>
             <field_group axis_ref="levp1" >
                <field id="W_start" name="W" />
                <field id="geopot_start" name="geopot" />
             </field_group>
          </field_group>
       </field_group>
    </file>

    
  </file_definition>
    
  <axis_definition>
    <axis id="lev" name="lev" positive="down" standard_name="atmopshere_hybrid_sigma_pressure_coordinate" long_name="hybrid level at midpoints" />
    <axis id="lev_read" />
    <axis id="levp1" name="ilev"  unit="level" positive="down" standard_name="atmopshere_hybride_sigma_pressure_coordinate" long_name="hybrid level at interface" />
    <axis id="lev_pressure" positive="down"  n_glo="2" unit="Pa" value="(0,1)[85000 50000]" >
      <interpolate_axis order="1" coordinate="p" />
    </axis>    
    <axis id="nq" name="nq" n_glo="5" value="(0,4)[1 2 3 4 5]"/>
  </axis_definition>    
  
  <domain_definition>
    
     <domain_group id="i"> 
       <domain id="i" name="mesh"/>     
     </domain_group> 
     
     
     <domain id="v" />
     <domain id="u" />
     
     <domain id="regular_half_degree" type="rectilinear" ni_glo="720" nj_glo="360" >
       <generate_rectilinear_domain />
       <interpolate_domain order="1"/>
     </domain> 
     
     <domain id="regular_one_degree" type="rectilinear" ni_glo="360" nj_glo="180" >
       <generate_rectilinear_domain />
       <interpolate_domain order="1"/>
     </domain> 
     
     <domain id="regular_two_degree" type="rectilinear" ni_glo="180" nj_glo="90" >
       <generate_rectilinear_domain />
       <interpolate_domain order="1"/>
     </domain> 
     
  </domain_definition>    
  
  <grid_definition>    
    <grid id="scalar">
      <scalar />
    </grid>
    
    <grid id="grid_q">
      <domain domain_ref="i" />
      <axis axis_ref="lev" />
      <axis axis_ref="nq" />
    </grid >

    <grid id="grid_q_read">
      <domain domain_ref="i" />
      <axis axis_ref="lev_read" />
      <axis axis_ref="nq" />
    </grid >
 
    <grid id="grid_q_regular">
      <domain domain_ref="regular_two_degree" />
      <axis axis_ref="lev" />
      <axis axis_ref="nq" />
    </grid >
  </grid_definition>   
  
</context>
EOF
}

function xml_field_def() {
    cat <<EOF
<field_definition level="1" enabled=".TRUE." operation="instant">
  
  <field_group id="standard_output"> 
    
    <field id="timestep" grid_ref="scalar" unit="s"/>
    <field id="preff" grid_ref="scalar"    unit="Pa"/>
    <field id="ap" axis_ref="levp1"   long_name="hybrid A coefficient at layer interface" />
    <field id="bp" axis_ref="levp1"  long_name="hybrid B coefficient at layer interface" />
    <field id="mid_ap" axis_ref="lev"   long_name="hybrid A coefficient at midpoints" />
    <field id="mid_bp" axis_ref="lev"  long_name="hybrid B coefficient at midpoints" />
    
    <field_group domain_ref="i">
      <field id="ps" />
      <field id="dps"  />
      <field id="Ai" />
      <field id="phis" />
      <field id="phi" />
      <field id="precl" />
      <field id="Q1_col_int" />     
      <field id="Q2_col_int" />     
      <field id="ps_init" />
      
      <field_group axis_ref="lev"> 
        <field id="mass" />
        <field id="p" />
        <field id="dmass"/>
        <field id="theta" />
        <field id="dyn_q" />
        <field id="pk"/>
        <field id="ulon"/>
        <field id="ulat"/>
        <field id="uz"/>
        <field id="omega"/>
        <field id="temp"/>
        
        <field id="mass_init" />
        <field id="p_init" />
        <field id="theta_init" />
        <field id="dyn_q_init" />
        <field id="ulon_init"/>
        <field id="ulat_init"/>
        <field id="uz_init"/>
        <field id="temp_init"/>
        
        <field id="mass_t"           long_name="Time-averaged air mass"   unit="kg/m2"/>
        <field id="massflux_lon"     long_name="Time-averaged air mass flux, zonal"   unit="kg/m/s"/>
        <field id="massflux_lat"     long_name="Time-averaged air mass flux, meridional"   unit="kg/m/s"/>

        <field id="ulon_t"           long_name="Time-averaged zonal momentum"   unit="kg/m/s"/>
        <field id="ulonflux_lon"     long_name="Time-averaged zonal momentum flux, zonal"   unit="kg/s2"/>
        <field id="ulonflux_lat"     long_name="Time-averaged zonal momentum flux, meridional"   unit="kg/s2"/>

        <field id="theta_t"          long_name="Time-averaged potential temperature"   unit="K.kg/m2"/>
        <field id="thetaflux_lon"    long_name="Time-averaged potential temperature flux, zonal"   unit="K.kg/m/s"/>
        <field id="thetaflux_lat"    long_name="Time-averaged potential temperature flux, meridional"   unit="K/kg/m/s"/>

        <field id="epot_t"           long_name="Time-averaged potential energy"   unit="J/m2"/>
        <field id="epotflux_lon"     long_name="Time-averaged potential energy flux, zonal"   unit="J/m/s"/>
        <field id="epotflux_lat"     long_name="Time-averaged potential energy flux, meridional"   unit="J/m/s"/>

        <field id="ekin_t"           long_name="Time-averaged kinetic energy"   unit="J/m2"/>
        <field id="ekinflux_lon"     long_name="Time-averaged kinetic energy flux, zonal"   unit="J/m/s"/>
        <field id="ekinflux_lat"     long_name="Time-averaged kinetic energy flux, meridional"   unit="J/m/s"/>
        
        <field id="enthalpy_t"       long_name="Time-averaged enthalpy"   unit="J/m2"/>
        <field id="enthalpyflux_lon" long_name="Time-averaged enthalpy flux, zonal"   unit="J/m/s"/>
        <field id="enthalpyflux_lat" long_name="Time-averaged enthalpy flux, meridional"   unit="J/m/s"/>

        <field id="dulon_diss1"/>
        <field id="dulon_diss2"/>
        <field id="dulat_diss1"/>
        <field id="dulat_diss2"/>

        <field id="dulon_phys"       long_name="Physics tendency for zonal velocity"   unit="m/s^2"/>
        <field id="dulat_phys"       long_name="Physics tendency for meridional velocity"   unit="m/s^2"/>

      </field_group>

      <field id="u850" />
      <field id="v850" />
      <field id="w850" />
      <field id="t850" />
      <field id="omega850"/>                  
      <field id="u500" />
      <field id="v500" />
      <field id="w500" />
      <field id="t500" />
      <field id="omega500"/>                  
      <field id="SST"/>
      
      <field id="ulon_850_500" field_ref="p" axis_ref="lev_pressure" />
      
      <field_group axis_ref="levp1"> 
        <field id="geopot"/>
        <field id="geopot_init"/>
      </field_group>
    </field_group> 

    <field_group grid_ref="grid_q">
      <field id="q_init" />
      <field id="q" />
      <field id="qmass_t" />
      <field id="qflux_lon" />
      <field id="qflux_lat" />
    </field_group> 

    <field_group domain_ref="v" axis_ref="lev">
      <field id="vort" />
      <field id="pv" />
    </field_group>
    
  </field_group>
  
  
</field_definition>

EOF
}

function xml_dcmip() { # EXPNAME OUTPUT_GRID OUTPUT_FREQ
EXPNAME=$1
OUTPUT_GRID=$2
OUTPUT_FREQ=$3
NBP=80 # FIXME
LLM=10 # FIXME
cat <<EOF
<file_definition > 

  <file id="output_dcmip2016_native" convention="CF" enabled="false" output_freq="$OUTPUT_FREQ" sync_freq="$OUTPUT_FREQ" output_level="10" timeseries="none" description="$EXPNAME" > 

<!-- FIXME
    <field_group id="dcmip2016_output_field_once" operation="once" freq_offset="0ts" ts_enabled="true">
      <field field_ref="timestep" name="mdt" />
      <field field_ref="preff" name="P0" long_name="reference pressure" />
      <field field_ref="ap" name="hyai"   />
      <field field_ref="bp" name="hybi"   />
      <field field_ref="mid_ap" name="hyam"   />
      <field field_ref="mid_bp" name="hybm"   />
    </field_group>
-->
    
    <field_group id="init_fields" operation="once" freq_offset="0ts" ts_enabled="true">
      <field field_ref="temp_init"   />
      <field field_ref="uz_init"     />
      <field field_ref="ulon_init"   />
      <field field_ref="ulat_init"   />
      <field field_ref="p_init"      />
      <field field_ref="ps_init"     />
      <field field_ref="mass_init"   />
      <field field_ref="geopot_init" />
    </field_group>
    
    <field_group id="dcmip2016_output_field" ts_enabled="true">
      <field field_ref="ps" name="PS"       standard_name="surface_pressure" long_name="Surface pressure"         unit="Pa" />
      <field field_ref="phis" operation="once" freq_offset="0ts" name="PHIS"   standard_name="surface_geopotential" long_name="Surface geopotential" unit="m2/m2"/>
      <field field_ref="geopot"  name="PHI"   standard_name="geopotential" long_name="geopotential" unit="m2/s2"/>
      <field field_ref="ulon" name="U"      standard_name="eastward_wind" long_name="Zonal wind"                  unit="m/s"/>
      <field field_ref="ulat" name="V"      standard_name="northward_wind" long_name="Meridional wind"            unit="m/s"/>
      <field field_ref="uz" name="W"        standard_name="upward_air_velocity" long_name="Vertical velocity"     unit="m/s"/>
      <field field_ref="omega" name="OMEGA"        standard_name="lagrangian_tendency_of_air_pressure" long_name="Vertical pressure velocity"     unit="Pa/s"/>
      <field field_ref="p"  name="P"        standard_name="air_pressure" long_name="Pressure"     unit="Pa"/>
      <field field_ref="temp"  name="T"        standard_name="air_temperature" long_name="Temperature"     unit="K"/>
      <field field_ref="precl"  name="PRECT"     standard_name="rainfall_rate"  long_name="Large Scale precipitation rate"  unit="m/s"/>
      <field field_ref="Q1_col_int"  name="Q1_col_int"      long_name="Single chlorine column integrated mixing ratio"  unit="kg/m2"/> 
      <field field_ref="Q2_col_int"  name="Q2_col_int"      long_name="Chlorine gas column integrated mixing ratio"  unit="kg/m2"/> 
      <field field_ref="u850" name="U850"       long_name="Zonal wind at 850 hPa"      unit="m/s"/>
      <field field_ref="v850" name="V850"       long_name="Meridional wind at 850 hPa"   unit="m/s"/>
      <field field_ref="w850" name="W850"       long_name="Vertical velocity at 850 hPa"     unit="m/s"/>
      <field field_ref="t850" name="T850"       long_name="Temperature at 850 hPa"   unit="K"/>
      <field field_ref="omega850" name="OMEGA850"   long_name="Vertical pressure velocity at 850 hPa"     unit="Pa/s"/>
      <field field_ref="u500" name="U500"       long_name="Zonal wind at 500 hPa"      unit="m/s"/>
      <field field_ref="v500" name="V500"       long_name="Meridional wind at 850 hPa"   unit="m/s"/>
      <field field_ref="w850" name="W500"       long_name="Vertical velocity at 500 hPa"     unit="m/s"/>
      <field field_ref="t500" name="T500"       long_name="Temperature at 850 hPa"   unit="K"/>
      <field field_ref="omega500" name="OMEGA500"   long_name="Vertical pressure velocity at 500 hPa"     unit="Pa/s"/>
      <field field_ref="SST" name="SST"       long_name="Sea surface temperature"   unit="K"/>
      
      <field field_ref="mass_t" name="mass_t"                   long_name="Time-averaged air mass"   unit="kg/m2"/>
      <field field_ref="massflux_lon" name="massflux_lon"       long_name="Time-averaged air mass flux, zonal"   unit="kg/m/s"/>
      <field field_ref="massflux_lat" name="massflux_lat"       long_name="Time-averaged air mass flux, meridional"   unit="kg/m/s"/>

      <field field_ref="epot_t" name="epot_t"                   long_name="Time-averaged potential energ"   unit="J/m2"/>
      <field field_ref="epotflux_lon" name="epotflux_lon"       long_name="Time-averaged potential energy flux, zonal"   unit="J/m/s"/>
      <field field_ref="epotflux_lat" name="epotflux_lat"       long_name="Time-averaged potential energy flux, meridional"   unit="J/m/s"/>

      <field field_ref="ekin_t" name="ekin_t"                   long_name="Time-averaged kinetic energ"   unit="J/m2"/>
      <field field_ref="ekinflux_lon" name="ekinflux_lon"       long_name="Time-averaged kinetic energy flux, zonal"   unit="J/m/s"/>
      <field field_ref="ekinflux_lat" name="ekinflux_lat"       long_name="Time-averaged kinetic energy flux, meridional"   unit="J/m/s"/>

      <field field_ref="enthalpy_t" name="enthalpy_t"                   long_name="Time-averaged enthalpy"   unit="J/m2"/>
      <field field_ref="enthalpyflux_lon" name="enthalpyflux_lon"       long_name="Time-averaged enthalpy flux, zonal"   unit="J/m/s"/>
      <field field_ref="enthalpyflux_lat" name="enthalpyflux_lat"       long_name="Time-averaged enthalpy flux, meridional"   unit="J/m/s"/>

    </field_group>

    <variable name="model" type="string" > dynamico </variable>
    <variable name="test_case" type="string" > $EXPNAME </variable>
    <variable name="horizontal_resolution" type="string" > native (grid points : 80x80x10) </variable>
    <variable name="levels" type="string" > L10 </variable>
    <variable name="grid" type="string" > hex </variable>
    <variable name="equation" type="string" > hydro </variable>
    <variable name="time_frequency" type="string" > $OUTPUT_FREQ </variable>    

  </file>


  <file id="output_dcmip2016_regular" enabled="true" output_freq="$OUTPUT_FREQ" sync_freq="$OUTPUT_FREQ" output_level="10" description="$EXPNAME" timeseries="none" > 
    
<!-- FIXME
    <field_group group_ref="dcmip2016_output_field_once"/>
-->
    <field_group group_ref="init_fields" domain_ref="regular_$OUTPUT_GRID" />
    
    <field_group group_ref="dcmip2016_output_field" domain_ref="regular_$OUTPUT_GRID" />

    <field_group id="transport" ts_enabled="true" grid_ref="grid_q_regular" >
          <field field_ref="q" name="q"  />
          <field field_ref="qmass_t" name="qmass_t"  long_name="Time-averaged tracer mass"   unit="kg/m2"/>
          <field field_ref="qflux_lon" name="qflux_lon"  long_name="Time-averaged tracer mass flux, zonal"   unit="kg/m/s"/>
          <field field_ref="qflux_lat" name="qflux_lat"  long_name="Time-averaged tracer mass flux, meridional"   unit="kg/m/s"/>
    </field_group>

    <variable name="model" type="string" > dynamico </variable>
    <variable name="test_case" type="string" > $EXPNAME </variable>
    <variable name="horizontal_resolution" type="string" > $OUTPUT_GRID interpolated (grid points : $NBP x $NBP x $LLM) </variable>
    <variable name="levels" type="string" > L30 </variable>
    <variable name="grid" type="string" > hex </variable>
    <variable name="equation" type="string" > hydro </variable>
    <variable name="time_frequency" type="string" > $OUTPUT_FREQ </variable>    

  </file>


</file_definition>
EOF
}
