#!/usr/bin/env perl
use strict;
use lib $ENV{ 'MAIN_CONF'}  ;
use main_config ;
my %commands = () ;

if ( @ARGV <= 0 ) { push ( @{$commands{'help'}}, ' ' ) ; } 
else { (%commands) = main_config::get_commands ( @ARGV ) ; }

my $conf_file ;
if ( -f "main_config.data" )
{    $conf_file = "main_config.data" ; }
else
{    $conf_file = join ('', $ENV{"MAIN_CONF"}, "main_config.data" ) ; }

my %my_main = main_config::read_main_config_file ( $conf_file ) ; 

if ( !exists( $my_main{"DM_MAP_IN"} ) ) { push ( @{$my_main{"DM_MAP_IN"}}, "2FOFC" ) } ; 

#####################################################################

my $MACRO_FILE = "phase_comb.cmds" ;
my $PROC_ERROR = "FALSE" ;
my $doit = "FALSE" ;

######################################

my $RESOL_MIN = $my_main{"DFR_RES_MIN"}[0] ;
my $RESOL_MAX = $my_main{"DFR_RES_MAX"}[0] ;
my $HLA = "HLA" ;
my $HLB = "HLB" ;
my $HLC = "HLC" ;

my $keyword ; 
foreach $keyword ( sort keys (%commands) ) 
{
    if ( $keyword eq 'h' || substr($keyword, 0,4) eq "help" ) { goto HELP }
    if ( $keyword eq 'a' || substr($keyword, 0,6) eq "hlabcd" ) { goto HLABCD }
    if ( $keyword eq 'f' || substr($keyword, 0,4) eq "fobs" ) { goto FOBS }
    if ( $keyword eq 'm' || substr($keyword, 0,3) eq "mtz" ) { goto MTZ }
    if ( $keyword eq 'p' || substr($keyword, 0,5) eq "phase" ) { goto PHASE }
    if ( $keyword eq 'r' || substr($keyword, 0,5) eq "resol" ) { goto RESOL }
    if ( $keyword eq 's' || substr($keyword, 0,5) eq "sigma" ) { goto SIGMA }
    if ( $keyword eq 'w' || substr($keyword, 0,6) eq "weight" ) { goto WEIGHT }
    if ( substr($keyword, 0,4) eq "doit" ) { $doit = "TRUE" ; next ; }
    print "unknown option >$keyword<\n" ; $PROC_ERROR = "TRUE" ;

#  -o|--output)  set file name of the created macro [$MACRO_FILE]
HELP:
    print << "END_OF_TEXT" ;
  -h|--help)    prints this message with available options and current status 
  -a|--hlabcd)  HL ABCD coefficients [@{$my_main{"MTZ_HLA"}} @{$my_main{"MTZ_HLB"}} @{$my_main{"MTZ_HLC"}} @{$my_main{"MTZ_HLD"}}]
  -m|--mtz)     mtz file name with HL ABCD coefficients [@{$my_main{"FILE_MTZ"}}]
  -f|--fobs)    fobs LABEL from mtz file [@{$my_main{"MTZ_FOBS"}}]
  -p|--phase)   phase on fobs from mtz file [@{$my_main{"MTZ_PHASE"}}]
  -r|--resol)   set resolution limits for diffraction data [$RESOL_MIN - $RESOL_MAX]
  -s|--sigma)   sigma fobs LABEL from mtz file [@{$my_main{"MTZ_SIGMA"}}]
  -w|--weight)  figure of merrit (weight) on fobs from mtz file [@{$my_main{"MTZ_WEIGHT"}}]
     --doit)    create the macro(s)
END_OF_TEXT
exit ;

OUTPUT: $MACRO_FILE = shift ( @{$commands{$keyword}} ) ; next ;

MTZ: ${$my_main{"FILE_MTZ"}}[0] = shift ( @{$commands{$keyword}} ) ; next ;
FOBS: ${$my_main{"MTZ_FOBS"}}[0] = shift ( @{$commands{$keyword}} ) ; next ;
HLABCD: $my_main{"MTZ_HLA"}[0] = shift ( @{$commands{$keyword}} ) ; 
        $my_main{"MTZ_HLB"}[0] = shift ( @{$commands{$keyword}} ) ; 
        $my_main{"MTZ_HLC"}[0] = shift ( @{$commands{$keyword}} ) ; 
        $my_main{"MTZ_HLD"}[0] = shift ( @{$commands{$keyword}} ) ; next ;
PHASE: ${$my_main{"MTZ_PHASE"}}[0] = shift ( @{$commands{$keyword}} ) ; next ;
RESOL: $RESOL_MIN = shift ( @{$commands{$keyword}} ) ; 
       $RESOL_MAX = shift ( @{$commands{$keyword}} ) ; next ;
SIGMA: ${$my_main{"MTZ_SIGMA"}}[0] = shift ( @{$commands{$keyword}} ) ; next ;
WEIGHT: ${$my_main{"MTZ_WEIGHT"}}[0] = shift ( @{$commands{$keyword}} ) ; next ;
}

if ( $PROC_ERROR eq "TRUE" ) { exit ; }

#######################################

$conf_file = "main_config.data" ; 
main_config::write_main_config_file ( $conf_file, %my_main ) ; 

if ( $doit eq "FALSE" )  { exit ; }

######################################

$doit = "TRUE" ;

if ( !exists( $my_main{"FILE_MTZ"} ) )  { $doit = "FALSE" ; }
if ( !exists( $my_main{"MTZ_FOBS"} ) )  { $doit = "FALSE" ; }
if ( !exists( $my_main{"MTZ_SIGMA"} ) ) { $doit = "FALSE" ; }
if ( !exists( $my_main{"MTZ_HLA"} ) )   { $doit = "FALSE" ; }
if ( !exists( $my_main{"MTZ_HLB"} ) )   { $doit = "FALSE" ; }
if ( !exists( $my_main{"MTZ_HLC"} ) )   { $doit = "FALSE" ; }
if ( !exists( $my_main{"MTZ_HLD"} ) )   { $doit = "FALSE" ; }
if ( !exists( $my_main{"MTZ_WEIGHT"} )) { $doit = "FALSE" ; }
if ( !exists( $my_main{"MTZ_PHASE"} ) ) { $doit = "FALSE" ; }

if ( $doit eq "FALSE" )  
{ 
print "creating \"$MACRO_FILE\" failed - missing column LABELS for phase combination \n" ;
exit ; 
}


print "creating \"$MACRO_FILE\" \n" ;
if ( -f $MACRO_FILE ) 
{ 
    my $back = join ('', $MACRO_FILE, ".old" ) ; 
    system ( "mv  $MACRO_FILE $back" ) ; 
}

my $CELL_DATA ;

if ( -f $my_main{"FILE_CELL"}[0] ) 
{
    open ( CELL,  $my_main{"FILE_CELL"}[0] ) ;
    $CELL_DATA = <CELL> ; chomp( $CELL_DATA ) ;
    close(CELL) ;
}
else
{
    print "ERROR: ", $my_main{"FILE_CELL"}[0], " not found" ;
    $CELL_DATA = "CAN NOT FIND CELL CONSTANTS" ;
}
print "UNIT CELL: [$CELL_DATA] \n" ;

my $SPACE_GROUP = $my_main{"FILE_SYMM"}[0] ; $SPACE_GROUP =~ s/_//g ;
print "SPACE GROUP [$SPACE_GROUP] \n" ;

my $FILE = "main_2_sigmaa_2_main.ccp4" ;
print " creating \"$FILE\" \n" ;

open ( MACRO,  "> $FILE" ) || die "FILE OPEN ERROR: >$FILE< \n" ;

my $FOBS = $my_main{"MTZ_FOBS"}[0] ;
my $SIG_FOBS = $my_main{"MTZ_SIGMA"}[0] ;
my $HLA = $my_main{"MTZ_HLA"}[0] ;
my $HLB = $my_main{"MTZ_HLB"}[0] ;
my $HLC = $my_main{"MTZ_HLC"}[0] ;
my $HLD = $my_main{"MTZ_HLD"}[0] ;
my $PHIB = $my_main{"MTZ_PHASE"}[0] ;
my $FOM = $my_main{"MTZ_WEIGHT"}[0] ;
my $TEST_FREE = $my_main{"MTZ_TEST"}[0] ;

print MACRO << "END_OF_TEXT" ;
# 
## rm sigmaa.phase main_fcalc.mtz main_both.mtz sigmaa.mtz 
# f2mtz.ccp4
#
f2mtz HKLIN main_fcalc.phase  HKLOUT main_fcalc.mtz << EOF
TITLE main to MTZ
CELL  $CELL_DATA
LABOUT   H  K  L  FC PHIC $TEST_FREE
CTYPOUT  H  H  H  F  P  I
FORMAT '(5x, F5.0, 1X, F5.0, 1X, F5.0, 7x, F11.2, F9.2, 5X, F4.0)'
SYMMETRY $SPACE_GROUP
END
EOF
 

#
# cad - Combine assorted data
#
cad HKLIN1  $my_main{"FILE_MTZ"}[0] HKLIN2  main_fcalc.mtz HKLOUT  main_both.mtz <<EOF  

TITLE ......

LABIN   FILE 1  E1=$FOBS E2=$SIG_FOBS  E3=$PHIB E4=$FOM \\ 
        E5=$HLA E6=$HLB E7=$HLC E8=$HLD
CTYP    FILE 1  E1=F E2=Q E3=P E4=W E5=A E6=A E7=A E8=A
LABOUT  FILE 1  E1=$FOBS E2=$SIG_FOBS  E3=PHIB E4=FOM \\  
        E5=HLA E6=HLB E7=HLC E8=HLD

LABIN   FILE 2  E1=FC     E2=PHIC     E3=$TEST_FREE
CTYP    FILE 2  E1=F      E2=P        E3=I
LABOUT  FILE 2  E1=FC     E2=PHIC     E3=$TEST_FREE

END
EOF

#
# phase combination
# -----------------
#
sigmaa HKLIN main_both.mtz HKLOUT sigmaa.mtz << EOF
RESOLUTION $RESOL_MIN $RESOL_MAX
RANGES 10  1000    
ERROR
COMBINE PART   1
LABI FP=$FOBS SIGFP=$SIG_FOBS PHIBP=PHIB WP=FOM \\ 
     HLA=HLA HLB=HLB HLC=HLC HLD=HLD FC=FC PHIC=PHIC
LABO PHCMB=PHI_COMB WCMB=FOM_COMB  
END
EOF

#
# cad - Combine assorted data
#
cad HKLIN1  $my_main{"FILE_MTZ"}[0] HKLIN2  sigmaa.mtz HKLOUT  sigmaa_out.mtz <<EOF  

TITLE ......


LABIN   FILE 1  E1=$FOBS E2=$SIG_FOBS  E3=$TEST_FREE 
CTYP    FILE 1  E1=F E2=Q E3=I
LABOUT  FILE 1  E1=$FOBS E2=$SIG_FOBS  E3=$TEST_FREE

LABIN   FILE 2  E1=PHI_COMB E2=FOM_COMB
CTYP    FILE 2  E1=P E2=W
LABOUT  FILE 2  E1=PHI_COMB E2=FOM_COMB 

END
EOF

#
# mtz_2_main.ccp4
#
mtz2various HKLIN sigmaa_out.mtz HKLOUT sigmaa.phase << EOF
RESOLUTION $RESOL_MIN $RESOL_MAX
EXCLUDE SIGP 1.0
OUTPUT MAIN 
LABIN FP=FP  SIGFP=SIGFP PHIB=PHI_COMB  FOM=FOM_COMB FREE=FreeR_flag
#OUTPUT USER (3HIND, 3I4, 4H FOB, F9.2, 4H SIG, F8.2, 4H PHA,F7.1,3H WE, F7.3,5H TEST,i4)
#LABIN FP=$FOBS SIGFP=$SIG_FOBS PHIB=PHI_COMB W=FOM_COMB I=$TEST_FREE
END
EOF
END_OF_TEXT

close (MACRO) ;

system ( "chmod +x main_2_sigmaa_2_main.ccp4" ) ;


# print " creating \"$MACRO_FILE\" \n" ;
# 
# open ( MACRO,  "> $MACRO_FILE" ) || die "FILE OPEN ERROR: >$MACRO_FILE< \n" ;
# 
# print MACRO << "END_OF_TEXT" ;
# subroutine char SEGMENTS integer THE_MAP
# 
# <?MAIN_CMDS:re_phase.cmds SEGMENTS THE_MAP
# 
# write over file main_fcalc.phase refl fcalc polar select defined end key TEST
# shell ./main_2_sigmaa_2_main.ccp4  > sigmma.out
# read file sigmaa.phase refl init \\ 
#      limit @{$my_main{"DFR_LIMIT_H"}}, @{$my_main{"DFR_LIMIT_K"}}, @{$my_main{"DFR_LIMIT_L"}} \\ 
#      reso $RESOL_MIN $RESOL_MAX re_read 
# ! refl key TEST sele .n all end 
# refl key WORK_REFL sele defined .a .n TEST end 
# 
# <MAIN_CMDS:calc_fo_map.cmds THE_MAP
# return
# 
# END_OF_TEXT
# 
# close (MACRO) ;
# 

