#!/usr/bin/perl -w
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 ) ; 

#####################################################################
my $NCRYST = $my_main{"SG_NCRYST"}[0] ;
my $CRYST ;

if ( !exists( $my_main{"DM_SOLV_TREAT"} ) ) { push ( @{$my_main{"DM_SOLV_TREAT"}}, "ATOMS" ) } ; 
#if ( $my_main{"DM_SOLV_TREAT"} eq "OFF" ) { $my_main{"DM_SOLV_TREAT"} = "ATOMS" } ; 
if ( !exists( $my_main{"DM_SOLV_WAY"} ) ) { push ( @{$my_main{"DM_SOLV_WAY"}}, "FFT" ) } ; 
if ( !exists( $my_main{"DM_SOLV_METHOD"} ) ) { push ( @{$my_main{"DM_SOLV_METHOD"}}, "WANG" ) } ; 
if ( !exists( $my_main{"DM_SOLV_SPHERE"} ) ) { push ( @{$my_main{"DM_SOLV_SPHERE"}}, "0.0" ) } ; 
if ( !exists( $my_main{"DM_SOLV_CONT"} ) ) { push ( @{$my_main{"DM_SOLV_CONT"}}, "0.0" ) } ; 
if ( !exists( $my_main{"DM_SOLV_FUNCT"} ) ) { push ( @{$my_main{"DM_SOLV_FUNCT"}}, "LINEAR" ) } ; 
if ( !exists( $my_main{"DM_SOLV_FUNCT_B"} ) ) { push ( @{$my_main{"DM_SOLV_FUNCT_B"}}, "1.0" ) } ; 
if ( !exists( $my_main{"DM_SOLV_FLIP"} ) ) 
{ 
    for ( $CRYST = 0; $CRYST < $NCRYST ; $CRYST++ )
    { push ( @{$my_main{"DM_SOLV_FLIP"}}, "0.0" ) }
} ; 
if ( !exists( $my_main{"DM_DIR_MASK"} ) ) { push ( @{$my_main{"DM_DIR_MASK"}}, "" ) } ; 
if ( !exists( $my_main{"DM_DIR_RT"} ) ) { push ( @{$my_main{"DM_DIR_RT"}}, "" ) } ; 
if ( !exists( $my_main{"DM_MAP_OUT"} ) ) { push ( @{$my_main{"DM_MAP_OUT"}}, "2FOFC" ) } ; 

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

my $MACRO_FILE = "dm_loop.com" ;
my $PROC_ERROR = "FALSE" ;
my $doit = "FALSE" ;

my $keyword ; 
foreach $keyword ( sort keys (%commands) ) 
{
    if ( $keyword eq 'h' || substr($keyword, 0,4) eq "help" ) { goto HELP }
    if ( $keyword eq 'o' || substr($keyword, 0,6) eq "output" ) { goto OUTPUT }
    if ( $keyword eq 'p' || substr($keyword, 0,9) eq "procedure" ) { goto METHOD }
    if ( $keyword eq 's' || substr($keyword, 0,6) eq "sphere" ) { goto SPHERE }
    if ( $keyword eq 'c' || substr($keyword, 0,7) eq "content" ) { goto CONTENT }
    if ( $keyword eq 'x' || substr($keyword, 0,7) eq "express" ) { goto EXPRESSION }
    if ( $keyword eq 'b' || substr($keyword, 0,4) eq "bval" ) { goto FUN_BVAL }
    if ( $keyword eq 't' || substr($keyword, 0,5) eq "treat" ) { goto TREATMENT }
    if ( $keyword eq 'w' || substr($keyword, 0,3) eq "way" ) { goto WAY }
    if ( $keyword eq 'f' || substr($keyword, 0,4) eq "flip" ) { goto FLIP }
    if ( $keyword eq 'm' || substr($keyword, 0,7) eq "map_out" ) { goto MAP_OUT }
    if ( substr($keyword, 0,4) eq "doit" ) { $doit = "TRUE" ; next ; }
    print "unknown option >$keyword<\n" ; $PROC_ERROR = "TRUE" ;

HELP:
    print << "END_OF_TEXT" ;
  -h|--help)       prints this message with available options and current status 
  -o|--output)     set file name of the created macro [$MACRO_FILE]
  -b|--bval)       mask function spread (B-value) [$my_main{"DM_SOLV_FUNCT_B"}[0]]
  -c|--content)    solvent content [@{$my_main{"DM_SOLV_CONT"}}]
  -x|--expression) expression form of protein mask [$my_main{"DM_SOLV_FUNCT"}[0]]
  -p|--procedure)  solvent flattening procedure [WANG/SOLOMON] [$my_main{"DM_SOLV_METHOD"}[0]]
  -m|--map_out)    output kind of map [FOBS/3FO2FC/2FOFC/COMB] [$my_main{"DM_MAP_OUT"}[0]]
  -f|--flip)       solvent flip (flip = value / (1 - value)) [@{$my_main{"DM_SOLV_FLIP"}}]
  -s|--sphere)     solvent flattening sphere radius [@{$my_main{"DM_SOLV_SPHERE"}}]
  -t|--treat)      mask generation [ATOMS/STATISTICS/BOTH] [$my_main{"DM_SOLV_TREAT"}[0]]
  -w|--way)        solvent flattening way [REAL_SPACE/FFT] [$my_main{"DM_SOLV_WAY"}[0]]
     --doit)       create the macro 
END_OF_TEXT
    exit ;

OUTPUT: $MACRO_FILE = shift ( @{$commands{$keyword}} ) ; next ;
DIRECT: @{$my_main{"DM_DIR"}} = shift ( @{$commands{$keyword}} ) ; next ;
METHOD: @{$my_main{"DM_SOLV_METHOD"}} =  uc( shift ( @{$commands{$keyword}} ) ) ; next ;
SPHERE: @{$my_main{"DM_SOLV_SPHERE"}} = @{$commands{$keyword}} ; next ;
CONTENT: @{$my_main{"DM_SOLV_CONT"}} = @{$commands{$keyword}} ; next ;
EXPRESSION: @{$my_main{"DM_SOLV_FUNCT"}} = shift ( @{$commands{$keyword}} ) ; next ;
FUN_BVAL: @{$my_main{"DM_SOLV_FUNCT_B"}} = shift ( @{$commands{$keyword}} ) ; next ;
TREATMENT: @{$my_main{"DM_SOLV_TREAT"}} = uc( shift ( @{$commands{$keyword}}) ) ; next ;
WAY: @{$my_main{"DM_SOLV_WAY"}} =  uc( shift ( @{$commands{$keyword}} ) ) ; next ;
FLIP: @{$my_main{"DM_SOLV_FLIP"}} = @{$commands{$keyword}} ; next ;
MAP_OUT: @{$my_main{"DM_MAP_OUT"}} = 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 ; }

my $VARI = ${@{$my_main{"DM_SOLV_METHOD"}}}[0] ;
my $CALC_WAY = ${@{$my_main{"DM_SOLV_WAY"}}}[0] ;
my $FL_MASK_SOLVENT=0 ; 
my $FL_MASK_ATOMS=0 ; 
if ( ${@{$my_main{"DM_SOLV_TREAT"}}}[0] =~ "STATIST" || 
     ${@{$my_main{"DM_SOLV_TREAT"}}}[0] =~ "BOTH" ) { $FL_MASK_SOLVENT= 1 ; }
if ( ${@{$my_main{"DM_SOLV_TREAT"}}}[0] =~ "ATOM" || 
     ${@{$my_main{"DM_SOLV_TREAT"}}}[0] =~ "BOTH" ) { $FL_MASK_ATOMS= 1 ; }


if ( $FL_MASK_SOLVENT > 0 ) 
{
my @METHOD = grep ( m/$VARI/, ("WANG", "SOLOMON" ) ) ;
if ( @METHOD <= 0 ) 
{ print "$MACRO_FILE %ERROR: no solvent accounting method specified ?? [@{$my_main{\"DM_SOLV_METHOD\"}}] \n"; exit }
my @TEST = grep ( m/$CALC_WAY/, ("REAL_SPACE", "FFT" ) ) ;
if ( @TEST <= 0 ) 
{ print "$MACRO_FILE %ERROR: no calcuation way specified ?? [@{$my_main{\"DM_SOLV_WAY\"}}] \n"; exit }
}

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

print "creating \"$MACRO_FILE\" \n" ;

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

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

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

my ( $MASK_FILE, $ROT_FILE, $SEGM_FROM, $SEGM_TO, $GROUP, $STRATEGY, 
     $MAP_WORK, $MAP_ADD, $MAP_FROM, $MAP_TO ) ;
my $IGR ;
my %segm_cryst = () ;

print MACRO << "END_OF_TEXT" ;
subroutine int CYCLE int CYCLE_N
set vari CYCLE = CYCLE + 1
show vari CYCLE
if ( CYCLE .gt. CYCLE_N ) return

! initialization: variables and maps 
END_OF_TEXT

if ( $NCRYST == 1 )
{
    $MAP_FROM = 2 ;
    $MAP_TO = 3 ;
    $MAP_WORK = 4 ;
    $MAP_ADD = 5 ;
print MACRO << "END_OF_TEXT" ;
set vari MAP_WORK = $MAP_WORK
set vari MAP_ADD = $MAP_ADD

make map $MAP_TO zero
make map $MAP_TO conv real 
make map $MAP_TO set -1 1 -9999

END_OF_TEXT
}
else
{
    $MAP_WORK = $NCRYST * 2 + 1 ;
    $MAP_ADD = $MAP_WORK + 1 ;

    print MACRO << "END_OF_TEXT" ;
set vari MAP_WORK = $MAP_WORK
set vari MAP_ADD = $MAP_ADD
END_OF_TEXT

    for ( $CRYST = 0; $CRYST < $NCRYST ; $CRYST++ )
    {
	$MAP_TO = $CRYST * 2 + 2 ;

	print MACRO << "END_OF_TEXT" ;
make map $MAP_TO zero
make map $MAP_TO conv real 
make map $MAP_TO set -1 1 -9999

END_OF_TEXT
    }
}

print MACRO "\n! COPYING. ROTATING and ADDING density maps together\n\n" ;

if ( $FL_MASK_ATOMS > 0 ) 
{

    for ( $CRYST = 0; $CRYST < $NCRYST ; $CRYST++ )
	{
	    $GROUP = join ( '', "SG_CRYST" , $CRYST+1 ) ;
	    foreach $SEGM_FROM ( @{$my_main{$GROUP}} )
	    {
		$segm_cryst{$SEGM_FROM} = $CRYST ;
	    }
	}

for ( $IGR = 1; $IGR <= $my_main{"SG_NGROUP"}[0] ; $IGR++ )
{
    $GROUP = join ('', "SG_GROUP", $IGR ) ;
    $STRATEGY = join ('', "SG_GROUP", $IGR, "_STRATEGY" ) ;
#    print " $IGR strategy @{$my_main{$STRATEGY}} group @{$my_main{$GROUP}} \n";
    my $MY_STRATEGY = $my_main{$STRATEGY}[0] ; 
    print " $IGR strategy $MY_STRATEGY ";
    if ( $MY_STRATEGY eq "LINK" ) 
    { 
	$STRATEGY = join ('', "SG_GROUP", $my_main{$STRATEGY}[1], "_STRATEGY" ) ; 
	$MY_STRATEGY = $my_main{$STRATEGY}[0] ; 
    print " MY real: $MY_STRATEGY ";
    }
    print " \n";
# STRATEGY 
    if ( $MY_STRATEGY eq "EACH" )
	{
	    foreach $SEGM_FROM ( @{$my_main{$GROUP}} )
	    {
		$MASK_FILE = join ('', $my_main{"DM_DIR_MASK"}[0], 
				   "mask_", $SEGM_FROM, ".xmap" ) ;

		if ( $NCRYST > 1 ) 
		{ 
		    $MAP_FROM = $segm_cryst{$SEGM_FROM} * 2 + 1 ;
		    $MAP_TO = $MAP_FROM + 1 ;
		    my $FILE_SYMM = 
			join( '', '?MAIN_SYMM:', 
			      ${$my_main{"FILE_SYMM"}}[$segm_cryst{$SEGM_FROM}], '.symm' );
		    print MACRO "read file $FILE_SYMM symm\n" ;
		}

		print MACRO << "END_OF_TEXT" ;

! first copy density into mask of $SEGM_FROM
set vari FILE_MASK = $MASK_FILE
read file FILE_MASK map xpl over MAP_WORK
make map MAP_WORK from $MAP_FROM copy
END_OF_TEXT

                if ( scalar ( @{$my_main{$GROUP}}) > 0 )
		{
		    foreach $SEGM_TO ( @{$my_main{$GROUP}} )
		    {
			if ( $SEGM_FROM ne $SEGM_TO )
			{
			    $ROT_FILE = join ('', $my_main{"DM_DIR_RT"}[0], "mol_", 
					      $SEGM_FROM, "_to_", $SEGM_TO, ".com" ) ;

			    if ( $NCRYST > 1 ) 
			    { 
				$MAP_FROM = $segm_cryst{$SEGM_TO} * 2 + 1 ;
			    }

			    print MACRO << "END_OF_TEXT" ;
< $ROT_FILE
<?MAIN_UTILS:add_a_map FILE_MASK MAP_ADD $MAP_FROM MAP_WORK
END_OF_TEXT
                        }
		     }
		    print MACRO "\n", "make map MAP_WORK rescale\n" ;
		    print MACRO "make map $MAP_TO from MAP_WORK cell\n\n" ;
		}
	    }
	}
################
#
#  from  ONE to ALL other distribution of local masks
#
    elsif ( $MY_STRATEGY eq "ONE" )
    {
	$SEGM_FROM= $my_main{$GROUP}[0] ;
	$MASK_FILE = join ('', $my_main{"DM_DIR_MASK"}[0], "mask_", $SEGM_FROM, ".xmap" ) ;

	print "ELSE <$STRATEGY> ONE <$SEGM_FROM> <$MASK_FILE> \n" ;

	if ( $NCRYST > 1 ) 
	{ 
	    $MAP_FROM = $segm_cryst{$SEGM_FROM} * 2 + 1 ;
	    $MAP_TO = $MAP_FROM + 1 ;
	    my $FILE_SYMM = 
		join( '', '?MAIN_SYMM:', 
		      ${$my_main{"FILE_SYMM"}}[$segm_cryst{$SEGM_FROM}], '.symm' );
	    print MACRO "read file $FILE_SYMM symm\n" ;
	}


	print MACRO << "END_OF_TEXT" ;
! first copy density into mask of ${SEGM_FROM}
set vari FILE_MASK = $MASK_FILE
read file FILE_MASK map xpl over MAP_WORK
make map MAP_WORK from $MAP_FROM copy

END_OF_TEXT

        if ( scalar ( @{$my_main{$GROUP}}) > 0 )
	{
	    foreach $SEGM_TO ( @{$my_main{$GROUP}}) 
	    {
		if ( $SEGM_FROM ne $SEGM_TO )
		{
		    $ROT_FILE = join ('', $my_main{"DM_DIR_RT"}[0], 
				      "mol_", $SEGM_FROM, "_to_", $SEGM_TO, ".com" ) ;
			    if ( $NCRYST > 1 ) 
			    { 
				$MAP_FROM = $segm_cryst{$SEGM_TO} * 2 + 1 ;
			    }
		    print MACRO "< $ROT_FILE \n" ;
		    print MACRO "<?MAIN_UTILS:add_a_map FILE_MASK MAP_ADD $MAP_FROM MAP_WORK \n" ;
		}
	    }
	    print MACRO "\n", "make map MAP_WORK rescale\n" ;
	}
        print MACRO "make map $MAP_TO from MAP_WORK cell\n\n" ;

        foreach $SEGM_TO ( @{$my_main{$GROUP}} )
	{
	    if ( $SEGM_FROM ne $SEGM_TO )
	    {
		$ROT_FILE = join ('', $my_main{"DM_DIR_RT"}[0], 
				  "mol_", $SEGM_TO, "_to_", $SEGM_FROM, ".com" ) ;
		$MASK_FILE = join ('', $my_main{"DM_DIR_MASK"}[0], "mask_", $SEGM_TO, ".xmap" ) ;
		if ( $NCRYST > 1 ) 
		{ 
		    $MAP_TO = $segm_cryst{$SEGM_TO} * 2 + 1 ;
		}


		print MACRO << "END_OF_TEXT" ;
! distribute $SEGM_FROM density into mask of $SEGM_TO 
set vari FILE_MASK = $MASK_FILE
read file FILE_MASK map xpl over MAP_WORK + 1
make map MAP_WORK + 1 set 9000 100000 0.0
< $ROT_FILE
<?MAIN_UTILS:rot_a_map FILE_MASK MAP_WORK + 1 MAP_WORK 
make map MAP_WORK + 1 rescale
make map $MAP_TO from MAP_WORK + 1 cell
END_OF_TEXT
            }
	}
    }
# STRATEGY WHOLE
    elsif ( $MY_STRATEGY eq "WHOLE" )
    {
	my $SEGM_BASE ;
	$MASK_FILE = join ('', $my_main{"DM_DIR_MASK"}[0], 
			   "mask_whole_", $IGR, ".xmap" ) ;
#	print "mask file WHOLE>: $MASK_FILE \n";
	$SEGM_BASE = join ( ' ', ( @{$my_main{$GROUP}} ) ) ;
#	print "segm base: $SEGM_BASE \n";
	if ( $NCRYST > 1 ) 
	{ 
	    $MAP_FROM = $segm_cryst{$my_main{$GROUP}[0]} * 2 + 1 ;
	    $MAP_TO = $MAP_FROM + 1 ;
	    my $FILE_SYMM = 
		join( '', '?MAIN_SYMM:', 
		      ${$my_main{"FILE_SYMM"}}[$segm_cryst{$SEGM_FROM}], '.symm' );
	    print MACRO "read file $FILE_SYMM symm\n" ;
	}
	print MACRO << "END_OF_TEXT" ;

! first copy density into mask of $SEGM_BASE
set vari FILE_MASK = $MASK_FILE
read file FILE_MASK map xpl over MAP_WORK
make map MAP_WORK from $MAP_FROM copy
END_OF_TEXT

        $SEGM_FROM= $my_main{$GROUP}[0] ;
	if ( scalar ( @{$my_main{$GROUP}}) > 0 )
	{
	    foreach $SEGM_TO ( @{$my_main{$GROUP}}) 
	    {
		if ( $SEGM_FROM ne $SEGM_TO )
		{
		    $ROT_FILE = join ('', $my_main{"DM_DIR_RT"}[0], 
				      "mol_", $SEGM_FROM, "_to_", $SEGM_TO, ".com" ) ;
		    print MACRO "< $ROT_FILE \n" ;
		    print MACRO "<?MAIN_UTILS:add_a_map FILE_MASK MAP_ADD $MAP_FROM MAP_WORK \n" ;
		}
	    }
	    print MACRO "\n", "make map MAP_WORK rescale\n" ;
	}
	print MACRO "make map $MAP_TO from MAP_WORK cell\n\n" ;
    }
}
## end of NGROUP loop
}
## end of FL_MASK_ATOMS

## MASK BY STATISTICS
if ( $FL_MASK_SOLVENT > 0 )
    { 
    for ( $CRYST = 0; $CRYST < $NCRYST ; $CRYST++ )
	{
	    my $SOLV_SPHERE = $my_main{"DM_SOLV_SPHERE"}[$CRYST] ;
	    if ( $NCRYST > 1 ) 
	    {
		$MAP_FROM = $CRYST * 2 + 1 ;
		$MAP_TO = $MAP_FROM + 1 ;
		main_config::write_get_diffr_data (*MACRO, \%my_main, $CRYST ) ; 
	    }
	    if ( $VARI eq "WANG" ) 
	    { 
		if ( $CALC_WAY eq "FFT" ) 
		{ 
		    print MACRO "<?MAIN_UTILS:score_map_wang_fft $MAP_FROM MAP_WORK $SOLV_SPHERE \n" ; 
		}
		elsif ( $CALC_WAY eq "REAL_SPACE" ) 
		{print MACRO "<?MAIN_UTILS:score_map_wang_realspace.com $MAP_FROM MAP_WORK $SOLV_SPHERE\n" ; }
	    }
	    elsif ( ${@{$my_main{"DM_SOLV_METHOD"}}}[0] eq "SOLOMON" ) 
	    { 
		if ( $CALC_WAY eq "FFT" ) 
		{ 
		    print MACRO "<?MAIN_UTILS:score_map_solomon_fft $MAP_FROM MAP_WORK $SOLV_SPHERE \n" ; 
		}
		elsif ( $CALC_WAY eq "REAL_SPACE" ) 
		{ print MACRO "<?MAIN_UTILS:score_map_solomon_realspace $MAP_FROM MAP_WORK $SOLV_SPHERE \n" ; }
	    }
	    print MACRO << "END_OF_TEXT" ;
anal ini 
anal map MAP_WORK step 256 
show anal = $my_main{"DM_SOLV_CONT"}[$CRYST] 
make map MAP_WORK set -999999 RESULT_0 -9999
make map MAP_WORK set RESULT_0 999999 9999
make map MAP_WORK from $MAP_FROM copy
make map MAP_WORK rescale
make map $MAP_TO from MAP_WORK cell

END_OF_TEXT
	}
}
## end of mask by statistics

# exit if no cyclic procedure requested
if ( $my_main{"DM_NCYC"}[0] <= 0 )
{
    print MACRO "! NO CYCLIC PROCEDURE \n" ;

    if ( $NCRYST == 1 ) 
	{
	    print MACRO << "END_OF_TEXT" ;
make map $MAP_FROM zero 
make map $MAP_FROM set -1 1 9999 
make map $MAP_FROM from $MAP_TO copy
make map $MAP_FROM set -10000 -9000 0.0
END_OF_TEXT
	}
    else
    {
	for ( $CRYST = 0; $CRYST < $NCRYST ; $CRYST++ )
	{
	    $MAP_FROM = $CRYST * 2 + 1 ;
	    $MAP_TO = $MAP_FROM + 1 ;
	    print MACRO << "END_OF_TEXT" ;
make map $MAP_FROM zero 
make map $MAP_FROM set -1 1 9999 
make map $MAP_FROM from $MAP_TO copy
make map $MAP_FROM set -10000 -9000 0.0

END_OF_TEXT
        }
	print MACRO "return\n" ;
    }
    exit ;
}

# it used to be re_fft.com

for ( $CRYST = 0; $CRYST < $NCRYST ; $CRYST++ )
{
    if ( $NCRYST == 1 ) 
    { 
	$MAP_FROM = 2 ; 
	$MAP_TO = 3 ; 
    } 
    else
    {
	$MAP_FROM = $CRYST * 2 + 1 ;
	$MAP_TO = $CRYST * 2 + 2 ;
	main_config::write_get_diffr_data (*MACRO, \%my_main, $CRYST ) ; 
    }

    if ( $my_main{"DM_SOLV_FLIP"}[$CRYST] == 0.0 ) 
    {
	print MACRO << "END_OF_TEXT" ;

!The zero flat
! make map $MAP_TO set -10000 -9900 9999.0
! make map $MAP_TO from $MAP_FROM invert reshift rescale 0
make map $MAP_TO set -10000 -9900 0.0

END_OF_TEXT
    }
    else
    {
	my $FLIP = $my_main{"DM_SOLV_FLIP"}[0] ;
	my $GAMMA = - $FLIP / ( 1.0 - $FLIP ) ;
    print MACRO << "END_OF_TEXT" ;

! The flipping - inverting the rest - the solomon way
make map $MAP_TO set -10000 -9900 9999.0
make map $MAP_TO from $MAP_FROM invert reshift rescale scale $GAMMA

END_OF_TEXT
    }


    print MACRO "fourier map $MAP_TO \n" ;

    if ( @{$my_main{"DFR_RFREE"}}[0] eq "OFF" )
    { print MACRO "reflect sele WORK_REFL end shell 10  r-values\n" ; }
else
{
    print MACRO
	"reflect sele WORK_REFL end shell 10  r-values rescale\n", 
	"reflect sele TEST end shell 10 r-values fixed\n" ;
}

    print MACRO << "END_OF_TEXT" ;
 
! ! ! At this point you can call the MAIN atomic probability function
! ! if ( HIST_TEMP .gt. 2.0 ) then
! ! refl select WORK_REFL end set ampl phase fwork = fcalc * 1
! ! <>utils/hist_match $MAP_TO HIST_TEMP
! ! end_if

END_OF_TEXT

if ( $my_main{"DM_MAP_OUT"}[0] eq "FOBS" )
{ print MACRO "<?MAIN_CMDS:calc_fo_phase_fc_map.cmds $MAP_FROM \n" ; }

elsif ( $my_main{"DM_MAP_OUT"}[0] eq "3FO2FC" )
{ print MACRO "<?MAIN_CMDS:calc_fofc_diff_map.cmds $MAP_FROM 3.0 2.0\n" ; }

elsif ( $my_main{"DM_MAP_OUT"}[0] eq "2FOFC" )
{ print MACRO "<?MAIN_CMDS:calc_fofc_diff_map.cmds $MAP_FROM 2.0 1.0\n" ; }

#elsif ( $my_main{"DM_MAP_OUT"}[0] eq "SIGMA_A" )
#{ print MACRO "<?MAIN_CMDS:calc_2fofc_sigma_a_map.cmds $MAP_FROM\n" ; }

elsif ( $my_main{"DM_MAP_OUT"}[0] eq "COMB" )
{
my @HKL = ( $my_main{"DFR_LIMIT_H"}[0], $my_main{"DFR_LIMIT_K"}[0], $my_main{"DFR_LIMIT_L"}[0] ) ;

print MACRO << "END_OF_TEXT" ;
! phase combination using sigma_a
write over file main_fcalc.phase refl select WORK_REFL end fcalc polar 
spawn main_2_sigmaa_2_main.ccp4  > sigmma.out
read file sigmaa.phase refl init limit @HKL re_read \\ 
     reso RESOL_MIN RESOL_MAX friedel 
refl key TEST sele .n all end
refl key WORK_REFL sele defined end
< ?MAIN_CMDS:calc_fo_map.cmds $MAP_FROM

END_OF_TEXT

}
else
{ 
    print "  CREATE_DM_LOOP> %ERROR: OUT MAP NOT RECOGNIZED [", @{$my_main{"DM_MAP_OUT"}}, "] \n" ; 
    print MACRO "  CREATE_DM_LOOP> %ERROR: OUT MAP NOT RECOGNIZED [", @{$my_main{"DM_MAP_OUT"}}, "] \n" ; 
}


}
print MACRO << "END_OF_TEXT" ;

rewind file
END_OF_TEXT


close (MACRO) ;
