/*******************************************************************************
* Instrument: MCPL_merge
*
* %I
* Written by: Peter Willendrup <pkwi@fysik.dtu.dk>
* Date: 2019-03
* Origin: DTU
* %INSTRUMENT_SITE: Tools
*
* Merge up to 10 MCPL input files regardless of origin/header info
*
* %D
* 
* Example: Merge two input files
* mcrun MCPL_merge f0=my1.mcpl.gz f1=my2.mcpl.gz fout=output
*
* %P
* fout:       [str] Output mcpl filename
* f0:         [str] 1st mcpl input filename
* f1:         [str] 2nd mcpl input filename
* f2:         [str] 3rd mcpl input filename
* f3:         [str] 4th mcpl input filename
* f4:         [str] 5th mcpl input filename
* f5:         [str] 6th mcpl input filename
* f6:         [str] 7th mcpl input filename
* f7:         [str] 8th mcpl input filename
* f8:         [str] 9th mcpl input filename
* f9:         [str] 10th mcpl input filename
*
* %L
* <reference/HTML link>
* MCPL website at <a href="https://mctools.github.io/mcpl/">https://mctools.github.io/mcpl/</a>
* %E
*******************************************************************************/
DEFINE INSTRUMENT MCPL_merge(string fout="output", string f0="NULL", string f1="NULL", string f2="NULL", string f3="NULL", string f4="NULL", string f5="NULL", string f6="NULL", string f7="NULL", string f8="NULL", string f9="NULL")

DECLARE
%{
  long long unsigned int ncount_set;
%}

INITIALIZE
%{
  ncount_set=0;
%}

TRACE

COMPONENT Origin = Progress_bar()
AT (0, 0, 0) ABSOLUTE
EXTEND %{
  if (!ncount_set) {
    if (strcmp(MC_GETPAR(MCPLinput0,filename),"NULL")) ncount_set+= mcpl_hdr_nparticles(MC_GETPAR(MCPLinput0,inputfile));
    if (strcmp(MC_GETPAR(MCPLinput1,filename),"NULL")) ncount_set+= mcpl_hdr_nparticles(MC_GETPAR(MCPLinput0,inputfile));
    if (strcmp(MC_GETPAR(MCPLinput2,filename),"NULL")) ncount_set+= mcpl_hdr_nparticles(MC_GETPAR(MCPLinput0,inputfile));
    if (strcmp(MC_GETPAR(MCPLinput3,filename),"NULL")) ncount_set+= mcpl_hdr_nparticles(MC_GETPAR(MCPLinput0,inputfile));
    if (strcmp(MC_GETPAR(MCPLinput4,filename),"NULL")) ncount_set+= mcpl_hdr_nparticles(MC_GETPAR(MCPLinput0,inputfile));
    if (strcmp(MC_GETPAR(MCPLinput5,filename),"NULL")) ncount_set+= mcpl_hdr_nparticles(MC_GETPAR(MCPLinput0,inputfile));
    if (strcmp(MC_GETPAR(MCPLinput6,filename),"NULL")) ncount_set+= mcpl_hdr_nparticles(MC_GETPAR(MCPLinput0,inputfile));
    if (strcmp(MC_GETPAR(MCPLinput7,filename),"NULL")) ncount_set+= mcpl_hdr_nparticles(MC_GETPAR(MCPLinput0,inputfile));
    if (strcmp(MC_GETPAR(MCPLinput8,filename),"NULL")) ncount_set+= mcpl_hdr_nparticles(MC_GETPAR(MCPLinput0,inputfile));
    if (strcmp(MC_GETPAR(MCPLinput9,filename),"NULL")) ncount_set+= mcpl_hdr_nparticles(MC_GETPAR(MCPLinput0,inputfile));
    printf("\n\n ** NOTA BENE: Estimated maximal neutron count is %g **\n\n",(double)ncount_set);
  }
  mcset_ncount(ncount_set);
%}

/* read neutrons from an mcpl file*/

COMPONENT MCPLinput0 = MCPL_input(filename=f0,verbose=1)
AT(0,0,0) RELATIVE PREVIOUS
GROUP sources

COMPONENT MCPLinput1 = COPY(MCPLinput0)(filename=f1)
AT(0,0,0) RELATIVE PREVIOUS
GROUP sources

COMPONENT MCPLinput2 = COPY(MCPLinput0)(filename=f2)
AT(0,0,0) RELATIVE PREVIOUS
GROUP sources

COMPONENT MCPLinput3 = COPY(MCPLinput0)(filename=f3)
AT(0,0,0) RELATIVE PREVIOUS
GROUP sources

COMPONENT MCPLinput4 = COPY(MCPLinput0)(filename=f4)
AT(0,0,0) RELATIVE PREVIOUS
GROUP sources

COMPONENT MCPLinput5 = COPY(MCPLinput0)(filename=f5)
AT(0,0,0) RELATIVE PREVIOUS
GROUP sources

COMPONENT MCPLinput6 = COPY(MCPLinput0)(filename=f6)
AT(0,0,0) RELATIVE PREVIOUS
GROUP sources

COMPONENT MCPLinput7 = COPY(MCPLinput0)(filename=f7)
AT(0,0,0) RELATIVE PREVIOUS
GROUP sources

COMPONENT MCPLinput8 = COPY(MCPLinput0)(filename=f8)
AT(0,0,0) RELATIVE PREVIOUS
GROUP sources

COMPONENT MCPLinput9 = COPY(MCPLinput0)(filename=f9)
AT(0,0,0) RELATIVE PREVIOUS
GROUP sources

COMPONENT Calc = Arm()
AT(0,0,0) RELATIVE PREVIOUS
GROUP sources
EXTEND %{
  SCATTER;
  printf("Reached last particle!\n");
  mcset_ncount(mcget_run_num());
%}
  
COMPONENT MCPLoutput = MCPL_output(filename=fout)
AT (0,0,0) RELATIVE PREVIOUS
  
FINALLY
%{
%}

END
