#!/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 ) ; 

#print " map kicked random: {", @{$my_main{"MAP_KICK_SERIES"}}, "}\n" ;
#####################################################################

if ( !exists($my_main{"MAP_KIND"} ) )  { push ( @{$my_main{"MAP_KIND"}} , "2FOFC+FOFC" ) ; }
if ( !exists($my_main{"MAP_KIND_REAL"} ) )  { push ( @{$my_main{"MAP_KIND_REAL"}} , "2FOFC+FOFC" ) ; }
if ( !exists($my_main{"MAP_OMIT"}  ) )  { push ( @{$my_main{"MAP_OMIT"}} , "OFF" ) ; }
if ( !exists($my_main{"MAP_BULK"}  ) )  { push ( @{$my_main{"MAP_BULK"}} , "ON" ) ; }
if ( !exists($my_main{"MAP_ANISO"}  ) )  { push ( @{$my_main{"MAP_ANISO"}} , "ON" ) ; }
if ( !exists($my_main{"MAP_TARGET"} ) )  { push ( @{$my_main{"MAP_TARGET"}} , "FREE_KICK" ) ; }
if ( !exists($my_main{"MAP_TARGET_REAL"} ) )  { push ( @{$my_main{"MAP_TARGET_REAL"}} , "FREE_KICK" ) ; }
if ( !exists($my_main{"MAP_GENERATION"} ) )  { push ( @{$my_main{"MAP_GENERATION"}} , "FIRST" ) ; }
if ( !exists($my_main{"MAP_WAY"} ) )  { push ( @{$my_main{"MAP_WAY"}} , "SINGLE" ) ; }
if ( !exists($my_main{"MAP_KICK_SERIES"} ) )  { push ( @{$my_main{"MAP_KICK_SERIES"}} , "FIXED" ) ; }
if ( !exists($my_main{"MAP_KICK_SIZE"} ) )  { push ( @{$my_main{"MAP_KICK_SIZE"}} , 0.8 ) ; }
if ( !exists($my_main{"MAP_KICK_NN"}  ) )  { push ( @{$my_main{"MAP_KICK_NN"}}  , 30 ) ; }
if ( !exists($my_main{"MAP_PRECISION"}  ) )  { push ( @{$my_main{"MAP_PRECISION"}}  , 7 ) ; }
if ( !exists($my_main{"MAP_NUMBER"}  ) )  { @{$my_main{"MAP_NUMBER"}}  = ( 1 ) ; }
if ( !exists($my_main{"MAP_NUMBER_REAL"}  ) )  { @{$my_main{"MAP_NUMBER_REAL"}} = ( 1 ) ; }
if ( !exists($my_main{"MAP_GRID_DENSITY"}  ) )  { push ( @{$my_main{"MAP_GRID_DENSITY"}}  , 3 ) ; }
if ( !exists($my_main{"MAP_BSHARP"}  ) )  { push ( @{$my_main{"MAP_BSHARP"}}  , "OFF" ) ; }
if ( !exists($my_main{"MAP_BSHARP_FACTOR"}  ) )  { push ( @{$my_main{"MAP_BSHARP_FACTOR"}}  , 30. ) ; }
if ( !exists($my_main{"MAP_WRITE"}  ) )  { push ( @{$my_main{"MAP_WRITE"}}  , "OFF" ) ; }
if ( !exists($my_main{"MAP_ZONES"}  ) )  { push ( @{$my_main{"MAP_ZONES"}}  , 0 ) ; }
if ( !exists($my_main{"MAP_BADD"}  ) )  { push ( @{$my_main{"MAP_BADD"}}  , 0 ) ; }
if ( !exists($my_main{"MAP_MISSING_FC"}  ) )  { push ( @{$my_main{"MAP_MISSING_FC"}}  , "YES" ) ; }
if ( !exists($my_main{"MAP_RESOLUTION"}  ) )  { push ( @{$my_main{"MAP_RESOLUTION"}}  , @{$my_main{"DFR_RES_MAX"}} ) } ;
if ( !exists( $my_main{"DFR_RFREE"} ) ) { push ( @{$my_main{"DFR_RFREE"}}, "OFF" ) } ; 


######################################################
#print " map after setup kicked random: {", @{$my_main{"MAP_KICK_SERIES"}}, "}\n" ;

my $fl_real = "OFF" ;
my $PROC_ERROR = "FALSE" ;
my $doit = "FALSE" ;
my $keyword ; 
my $MACRO_FILE = "map_calculate.cmds" ;

foreach $keyword ( sort keys (%commands) ) 
{
#    print "option >$keyword<\n" ; 
    if ( $keyword eq 'h' || substr($keyword, 0,4) eq "help" ) { goto HELP }
    if (                    substr($keyword, 0,8) eq "map_real"  ) { goto MAP_REAL }
    if ( $keyword eq 'm' || substr($keyword, 0,3) eq "map" ) { goto MAP }
    if ( $keyword eq 'o' || substr($keyword, 0,4) eq "omit" ) { goto OMIT }
    if ( $keyword eq 'b' || substr($keyword, 0,4) eq "bulk" ) { goto BULK }
    if ( $keyword eq 'a' || substr($keyword, 0,5) eq "aniso" ) { goto ANISO }

    if (                    substr($keyword, 0,11) eq "target_real"  ) { goto TARGET_REAL }
    if ( $keyword eq 't' || substr($keyword, 0,6) eq "target" ) { goto TARGET }
    if ( $keyword eq 'y' || substr($keyword, 0,3) eq "way" ) { goto WAY }
    if ( $keyword eq 'g' || substr($keyword, 0,10) eq "generation" ) { goto GENERATION }
    if ( $keyword eq 'k' || substr($keyword, 0,4) eq "kick" ) { goto KICK }
    if ( $keyword eq 'r' || substr($keyword, 0,6) eq "series" ) { goto SERIES }
    if ( $keyword eq 'v' || substr($keyword, 0,7) eq "average" ) { goto AVERAGE }
    if (                    substr($keyword, 0,11) eq "number_real"  ) { goto NUMBER_REAL }
    if ( $keyword eq 'n' || substr($keyword, 0,6) eq "number"  ) { goto NUMBER }
    if ( $keyword eq 'f' || substr($keyword, 0,10) eq "sharp_fact" ) { goto SHARP_FACT }
    if ( $keyword eq 'c' || substr($keyword, 0,10) eq "missing_fc" ) { goto MISSING_FC }
    if ( $keyword eq 's' || substr($keyword, 0,5) eq "sharp" ) { goto SHARP }
    if ( $keyword eq 'd' || substr($keyword, 0,7) eq "density" ) { goto DENSITY }
    if ( $keyword eq 'p' || substr($keyword, 0,9) eq "precision" ) { goto PRECISION }
    if ( $keyword eq 'w' || substr($keyword, 0,5) eq "write" ) { goto WRITE }
    if ( $keyword eq 'z' || substr($keyword, 0,5) eq "zones" ) { goto ZONES }
    if ( $keyword eq 'e' || substr($keyword, 0,4) eq "free" ) { goto FREE }
    if ( $keyword eq '+' || substr($keyword, 0,4) eq "badd" ) { goto BADD }
    if ( substr($keyword, 0,5) eq "macro" ) { goto MACRO }

    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 
-m|--map)         map of [2FOFC+FOFC/2FOFC/FOFC/PHASE_COMB/FOBS/FCALC/3FO2FC/REFINE] [@{$my_main{"MAP_KIND"}}]
-n|--number)      MAP number [1/2/3/4/5/6/7/8/9/10] [@{$my_main{"MAP_NUMBER"}}]
-t|--target)      target function [FREE_KICK/CROS_CORR/UN/SIGMA] current = [@{$my_main{"MAP_TARGET"}}]
-b|--bulk)        bulk solvent correction [ON/OFF/USE] [@{$my_main{"MAP_BULK"}}]
-a|--aniso)       anisotropic overall correction [ON/OFF/USE] [@{$my_main{"MAP_ANISO"}}]

-o|--omit)        omit map [ON/OFF] [@{$my_main{"MAP_OMIT"}}]
-g|--generation)  map generation [FIRST/SECOND/THIRD] current = [@{$my_main{"MAP_GENERATION"}}]

-y|--way)         map ways [SINGLE/KICK] current = [@{$my_main{"MAP_WAY"}}]
-k|--kick)        maximal xyz kick size [0.0 - 1.33]  current = [@{$my_main{"MAP_KICK_SIZE"}}]
-r|--series)      kick size can be [FIXED/RANDOM/SERIES/X_ERROR] = [@{$my_main{"MAP_KICK_SERIES"}}]
-v|--average)     kick steps for averaging current = [@{$my_main{"MAP_KICK_NN"}}]
-c|--missing_fc)  include missing reflections [YES/NO] [@{$my_main{"MAP_MISSING_FC"}}]

-s|--sharp)       B-factor sharpening for maps [ON/OFF] [@{$my_main{"MAP_BSHARP"}}]
-f|--sharp_fact)  B-factor sharpening factor: - desharp + sharp [@{$my_main{"MAP_BSHARP_FACTOR"}}]

-d|--density)     grid density per resolution [@{$my_main{"MAP_GRID_DENSITY"}}]
-p|--precision)   precision of density generation [1/2/3/4/5/6/7/8] [@{$my_main{"MAP_PRECISION"}}]
-w|--write)       write map files [ON/OFF] [@{$my_main{"MAP_WRITE"}}]
-z|--zones)       number of zones - 0 for auto  [@{$my_main{"MAP_ZONES"}}]
-e|--free)        r-free flag [ON/OFF] current [@{$my_main{"DFR_RFREE"}}]

-+|--badd)        b-factor add value for FFT - 0 for auto [@{$my_main{"MAP_BADD"}}]
   --doit)        create the macro

END_OF_TEXT
    exit ;

  MAP:   @{$my_main{"MAP_KIND"}} = shift ( @{$commands{$keyword}} ) ; next ;
  MAP_REAL: $fl_real = "ON" ;   @{$my_main{"MAP_KIND_REAL"}} = shift ( @{$commands{$keyword}} ) ; next ;
  OMIT:  @{$my_main{"MAP_OMIT"}} = shift ( @{$commands{$keyword}} ) ; next ;
  BULK:  @{$my_main{"MAP_BULK"}} = shift ( @{$commands{$keyword}} ) ; next ;
  ANISO:  @{$my_main{"MAP_ANISO"}} = shift ( @{$commands{$keyword}} ) ; next ;
  TARGET: @{$my_main{"MAP_TARGET"}} = uc(shift ( @{$commands{$keyword}} )) ; next ;
  TARGET_REAL: $fl_real = "ON" ; @{$my_main{"MAP_TARGET_REAL"}} = uc(shift ( @{$commands{$keyword}} )) ; next ;
  GENERATION:   @{$my_main{"MAP_GENERATION"}} = shift ( @{$commands{$keyword}} ) ; next ;
  WAY:   @{$my_main{"MAP_WAY"}} = shift ( @{$commands{$keyword}} ) ; next ;
  KICK:  @{$my_main{"MAP_KICK_SIZE"}} = shift ( @{$commands{$keyword}} ) ;  next ;
  SERIES:  @{$my_main{"MAP_KICK_SERIES"}} = shift ( @{$commands{$keyword}} ) ;  next ;
  AVERAGE: @{$my_main{"MAP_KICK_NN"}} = shift ( @{$commands{$keyword}} ) ; next ;
  NUMBER:  @{$my_main{"MAP_NUMBER"}} = shift ( @{$commands{$keyword}} ) ; next ;
  NUMBER_REAL:  $fl_real = "ON" ; @{$my_main{"MAP_NUMBER_REAL"}} = shift ( @{$commands{$keyword}} ) ; next ;
  MISSING_FC: @{$my_main{"MAP_MISSING_FC"}} = shift ( @{$commands{$keyword}} ) ; next ;

  SHARP:   
@{$my_main{"MAP_BSHARP"}} = shift ( @{$commands{$keyword}} ) ;
#print " map sharp:{", @{$my_main{"MAP_BSHARP"}}, "}\n" ;
 next ;
  SHARP_FACT:   
    @{$my_main{"MAP_BSHARP_FACTOR"}} = shift ( @{$commands{$keyword}} ) ; 
#print " fact sharp:{", @{$my_main{"MAP_BSHARP_FACTOR"}}, "}\n" ;
next ;
  DENSITY:  @{$my_main{"MAP_GRID_DENSITY"}} = shift ( @{$commands{$keyword}} ) ; next ;
  PRECISION:  @{$my_main{"MAP_PRECISION"}} = shift ( @{$commands{$keyword}} ) ; next ;
  WRITE:   @{$my_main{"MAP_WRITE"}} = shift ( @{$commands{$keyword}} ) ; next ;
  ZONES:   @{$my_main{"MAP_ZONES"}} = shift ( @{$commands{$keyword}} ) ; next ;
  FREE:  @{$my_main{"DFR_RFREE"}} = uc(shift ( @{$commands{$keyword}} )) ; next ;
  BADD:   @{$my_main{"MAP_BADD"}} = shift ( @{$commands{$keyword}} ) ; next ;
  MACRO:   $MACRO_FILE = 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 ) ; 

###  . ${MAIN_CONF}main_config.data.save.sh

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

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


my $THE_MAP = ${$my_main{"MAP_NUMBER"}}[0] ;
if ( $fl_real eq "ON" ) { $THE_MAP = ${$my_main{"MAP_NUMBER_REAL"}}[0] ; }
my $MAP_TARGET = ${$my_main{"MAP_TARGET"}}[0] ;
if ( $fl_real eq "ON" ) { $MAP_TARGET = $my_main{"MAP_TARGET_REAL"}[0] ; }

if ( $my_main{"MAP_KIND"}[0] eq "REFINE" ) { &create_kick_map_refine ; exit; }

if ( $fl_real eq "ON" ) { $MACRO_FILE = "map_calculate_4refine.cmds" ; }
my $missing_fc =  ${$my_main{"MAP_MISSING_FC"}}[0] ;

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" ;


#    print "SHARP: top flag{",  $my_main{"MAP_BSHARP"}[0], "}\n" ;

my %FILE_WORDS =
    (
     "ROOT" => "./",
     "KICK" => "",
     "KICK_NN" => "",
     "KIND" =>  "_" . $my_main{"MAP_KIND"}[0],
     "TARGET" => "_" . $my_main{"MAP_TARGET"}[0],
     "OMIT" => "",
     "SECOND" => "",
     "EXT" => ".xmap"
    ) ;
    

my $REFL_SELECTION = "defined" ;
#if ( $missing_fc eq "YES" ) { $REFL_SELECTION = $REFL_SELECTION . " .o asymm " ; }

if ( $my_main{"MAP_OMIT"}[0] eq "ON" ) { $FILE_WORDS{"OMIT"} = "_omit" ; }
					  
my $THE_PRECISION = ${$my_main{"MAP_PRECISION"}}[0] ;
my $THE_GRID_DENSITY = ${$my_main{"MAP_GRID_DENSITY"}}[0] ;
print MACRO << "END_OF_TEXT" ;
set vari THE_MAP = $THE_MAP
make map THE_MAP from 0 empty init auto $THE_GRID_DENSITY real cell
make map THE_MAP atom ( precision $THE_PRECISION )
END_OF_TEXT

my $MAP_LABEL = "" ;
my $MAP_LABEL2 = "" ;
my $MAP_KIND = ${$my_main{"MAP_KIND"}}[0] ;

if ( $MAP_TARGET eq "FREE_KICK" ) { $MAP_LABEL = "FK" ; }
if ( $MAP_TARGET eq "CROS_CORR" ) { $MAP_LABEL = "CR" ; }
if ( $MAP_TARGET eq "UN" ) { $MAP_LABEL = "UN" ; }
if ( $MAP_TARGET eq "SIGMA" ) { $MAP_LABEL = "SG" ; }


if ( $MAP_KIND eq "2FOFC" ) { $MAP_LABEL = $MAP_LABEL . " 2FOFC" ; }
if ( $MAP_KIND eq "FOFC" ) { $MAP_LABEL = $MAP_LABEL . " FO-FC" ; }
if ( $MAP_KIND eq "2FOFC+FOFC" ) 
{ 
    $MAP_LABEL2 = $MAP_LABEL . " FO-FC" ; 
    $MAP_LABEL = $MAP_LABEL . " 2FOFC" ; 
}
if ( $MAP_KIND eq "FOBS" ) { $MAP_LABEL = $MAP_LABEL . " FOBS" ; }
if ( $MAP_KIND eq "PHASE_COMB" ) { $MAP_LABEL = $MAP_LABEL . " PH_CMB" ; }
if ( $MAP_KIND eq "3FO2FC" ) { $MAP_LABEL = $MAP_LABEL . " 3FO2FC" ; }
if ( $MAP_KIND eq "FCALC" ) { $MAP_LABEL = $MAP_LABEL . " FCALC" ; }

if ( ${$my_main{"MAP_WAY"}}[0] eq "KICK" ) { $MAP_LABEL = $MAP_LABEL . " kick"; }

$MAP_LABEL = '"'. $MAP_LABEL . '"' ;
$MAP_LABEL2 = '"'. $MAP_LABEL2 . '"' ;

if ( ${$my_main{"MAP_WAY"}}[0] eq "SINGLE" ) { &do_single_map () ; }
if ( ${$my_main{"MAP_WAY"}}[0] eq "KICK" ) { &do_kick_map () ; }
exit ;
 
    sub do_single_map 
{

    my $MAP_OMIT = ${$my_main{"MAP_OMIT"}}[0] ;
    my $MAP_KIND = ${$my_main{"MAP_KIND"}}[0] ;
    if ( $fl_real eq "ON" ) { $MAP_KIND = $my_main{"MAP_KIND_REAL"}[0] } ;

    if ($MAP_TARGET eq "FREE_KICK" ) 
    {

	&create_re_phase () ;

	print MACRO << "END_OF_TEXT" ;
refl sele defined end ab_init 
refl sele defined end ab_use 1
refl sele defined end ab_init 
refl sele all end set fstore = fcalc * 1.0
set vari MY_KICK = ML_ERROR_X

make map $THE_MAP conv real
make map $THE_MAP zero 
END_OF_TEXT

if ( $MAP_OMIT eq "ON" ) 
{
    print MACRO << "END_OF_TEXT" ;
   make map $THE_MAP atom ( symm dens kick MY_KICK ) -
           sele segm name WORK_SEGM .a .n active end
END_OF_TEXT
}
else
{
    print MACRO << "END_OF_TEXT" ;
   make map $THE_MAP atom ( symm dens kick MY_KICK ) -
           sele segm name WORK_SEGM end
END_OF_TEXT
}
    
print MACRO << "END_OF_TEXT" ;

 fourier map $THE_MAP 
 reflection select defined end r-values fix ! rescale
 refl sele defined end ab_init 
 refl sele all end set fcalc = fstore * 1.0
END_OF_TEXT

#     foreach (1..3) {
# 
# print MACRO << "END_OF_TEXT" ;
# make map $THE_MAP conv real
# make map $THE_MAP zero 
# END_OF_TEXT
# 
# 
# if ( $MAP_OMIT eq "ON" ) 
# {
#     print MACRO << "END_OF_TEXT" ;
#    make map $THE_MAP atom ( symm dens kick MY_KICK ) -
#            sele segm name WORK_SEGM .a .n active end
# END_OF_TEXT
# }
# else
# {
#     print MACRO << "END_OF_TEXT" ;
#    make map $THE_MAP atom ( symm dens kick MY_KICK ) -
#            sele segm name WORK_SEGM end
# END_OF_TEXT
# }
#     
# print MACRO << "END_OF_TEXT" ;
# 
#  fourier map $THE_MAP 
#  reflection select defined end r-values fix ! rescale
#  refl sele defined end ab_add
#  refl sele all end set fcalc = fstore * 1.0
# END_OF_TEXT
# 
# }

if ( $MAP_KIND eq "2FOFC" ) 
{
#! echo "SIGMA A 2FO-FC" SIGMAA_MAP RFOBS
    print MACRO << "END_OF_TEXT" ;
make map THE_MAP zero
make map THE_MAP conv complex
END_OF_TEXT
if ( $missing_fc eq "YES" ) { print MACRO "refl sele asymm .a .n defined end a_use \n" ; }
    print MACRO << "END_OF_TEXT" ;
refl sele defined end ab_use 2
END_OF_TEXT

if ( $my_main{"MAP_BSHARP"}[0] eq "ON" )
{
    my $BSHARP = ${$my_main{"MAP_BSHARP_FACTOR"}}[0] ;
#    print "SHARP: {", $BSHARP, "}\n" ;
    print MACRO "refl set ampl scale fwork const 1. temp $BSHARP fwork = fwork * 1.0\n" ;
}

if ( $missing_fc eq "YES" ) { print MACRO "refl fill-map THE_MAP sele asymm .a .n defined end\n" ; }
    print MACRO << "END_OF_TEXT" ;
refl fill-map THE_MAP select $REFL_SELECTION end
four map THE_MAP back 
make map THE_MAP rescale 
image map THE_MAP label $MAP_LABEL
image map THE_MAP over
END_OF_TEXT
}

if ( $MAP_KIND eq "FOFC" ) 
{
#! echo "SIGMA A 2FO-FC" SIGMAA_MAP RFOBS
    print MACRO << "END_OF_TEXT" ;
make map THE_MAP zero
make map THE_MAP conv complex
refl sele defined end ab_use 1
refl fill-map THE_MAP select defined end
four map THE_MAP back 
make map THE_MAP rescale 
image map THE_MAP label $MAP_LABEL
image map THE_MAP over
END_OF_TEXT
}

if ( $MAP_KIND eq "2FOFC+FOFC" ) 
{
#! echo "SIGMA A 2FO-FC + FOFC " SIGMAA_MAP RFOBS
    print MACRO << "END_OF_TEXT" ;
make map THE_MAP zero
make map THE_MAP conv complex
END_OF_TEXT
if ( $missing_fc eq "YES" ) { print MACRO "refl sele asymm .a .n defined end a_use \n" ; }
    print MACRO << "END_OF_TEXT" ;
refl sele defined end ab_use 2
END_OF_TEXT

if ( $my_main{"MAP_BSHARP"}[0] eq "ON" )
{
    my $BSHARP = ${$my_main{"MAP_BSHARP_FACTOR"}}[0] ;
#    print "SHARP: {", $BSHARP, "}\n" ;
    print MACRO "refl set ampl scale fwork const 1. temp $BSHARP fwork = fwork * 1.0\n" ;
}

if ( $missing_fc eq "YES" ) { print MACRO "refl fill-map THE_MAP sele asymm .a .n defined end \n" ; }
    print MACRO << "END_OF_TEXT" ;
refl fill-map THE_MAP select $REFL_SELECTION end
four map THE_MAP back 
make map THE_MAP rescale 
image map THE_MAP label $MAP_LABEL
image map THE_MAP over


make map THE_MAP + 1 from THE_MAP init real zero
set vari THE_MAP = THE_MAP + 1
show map THE_MAP
if ( nmaps .lt. THE_MAP ) return

make map THE_MAP zero
make map THE_MAP conv complex
refl sele defined end ab_use 1
refl fill-map THE_MAP select defined end
four map THE_MAP back 
make map THE_MAP rescale 
image map THE_MAP label $MAP_LABEL2
image map THE_MAP over

END_OF_TEXT


}



#    print "create_map_calc.pl> ERROR: FREE_KICK maps require the KICK map specifications\n" ;

return ;
}

if (${$my_main{"MAP_KIND"}}[0] eq "FOBS" ) 
{

    &create_single_map () ; 

print MACRO << "END_OF_TEXT" ;
refl 
set ampl phase fwork = 0
sele defined end set ampl phase fwork = fobs * 1.
exit

END_OF_TEXT


if ( $my_main{"MAP_BSHARP"}[0] eq "ON" )
{
    my $BSHARP = ${$my_main{"MAP_BSHARP_FACTOR"}}[0] ;
#    print "SHARP: {", $BSHARP, "}\n" ;
    print MACRO "refl set ampl scale fwork const 1. temp $BSHARP fwork = fwork * 1.0\n" ;
}

print MACRO << "END_OF_TEXT" ;
refl fill-map THE_MAP select defined end
four map THE_MAP back 
make map THE_MAP rescale 
image map THE_MAP label $MAP_LABEL
image map THE_MAP over

END_OF_TEXT

}
elsif (${$my_main{"MAP_KIND"}}[0] eq "PHASE_COMB" ) 
{
my $RESOL_MIN = $my_main{"DFR_RES_MIN"}[0] ;
my $RESOL_MAX = $my_main{"DFR_RES_MAX"}[0] ;
system ( "tk_config.pl -m create_phase_comb.pl& " ) ;
# print MACRO << "END_OF_TEXT" ;
# subroutine char SEGMENTS integer THE_MAP
# 
# END_OF_TEXT

    &create_re_phase () ;

print MACRO << "END_OF_TEXT" ;
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

}
else
{
    &create_re_phase () ;
    &create_single_map () ; 
}

#    print " WRITE FIRST MAP {", ${$my_main{"MAP_WRITE"}}[0], "}\n" ; 
    if ( ${$my_main{"MAP_WRITE"}}[0] eq "ON" ) { &write_map () ; }
    if ( ${$my_main{"MAP_GENERATION"}}[0] eq "SECOND" || ${$my_main{"MAP_GENERATION"}}[0] eq "THIRD")
    { 

	print MACRO
#	    "set weight sele segm name WORK_SEGM .a map THE_MAP -1000. 0.8 end = 0\n" ;
	    "set weight sele segm name WORK_SEGM end auto ( dens MAP_CONT map THE_MAP root CA )\n" ;
#	&create_re_phase_next () ;
	&create_re_phase () ;
	&create_single_map () ; 
	$FILE_WORDS{"SECOND"} = "_second" ;

	if ( ${$my_main{"MAP_WRITE"}}[0] eq "ON" ) { &write_map () ; }
    }
    if ( ${$my_main{"MAP_GENERATION"}}[0] eq "THIRD" )
    { 
	print MACRO
	    "set weight sele segm name WORK_SEGM .a map THE_MAP -1000. 0.8 end = 0\n" ;
#	    "set weight sele segm name WORK_SEGM end auto ( dens MAP_CONT map THE_MAP root CA )\n" ;
#	&create_re_phase_next () ;
	&create_re_phase () ;
	&create_single_map () ; 
	$FILE_WORDS{"SECOND"} = "_third" ;
	if ( ${$my_main{"MAP_WRITE"}}[0] eq "ON" ) { &write_map () ; }
    }

    print MACRO "\n return\n" ;

}



sub do_kick_map
{
#print " map way:  ${$my_main{\"MAP_WAY\"}}[0] \n" ;
if ( $MAP_TARGET eq "FREE_KICK" ) 
{
    &create_re_phase () ;
    &create_kick_free_kick_map () ; 
    $FILE_WORDS{"KICK"} = "kick_" . $my_main{"MAP_KICK_SIZE"}[0] ;
    if ( $my_main{"MAP_KICK_SERIES"}[0] eq "RANDOM" ) 
    { 
	$FILE_WORDS{"KICK"} = "kick_rnd_" . $my_main{"MAP_KICK_SIZE"}[0] ; 
    }
    $FILE_WORDS{"KICK_NN"} = "_" . $my_main{"MAP_KICK_NN"}[0] . "x" ;


    if ( ${$my_main{"MAP_WRITE"}}[0] eq "ON" ) { &write_map () ; }
    if ( ${$my_main{"MAP_GENERATION"}}[0] eq "SECOND" || ${$my_main{"MAP_GENERATION"}}[0] eq "THIRD")
    { 
	print MACRO
	    "set weight sele segm name WORK_SEGM .a map THE_MAP -1000. 0.8 end = 0\n" ;
#	    "set weight sele segm name WORK_SEGM end auto ( dens MAP_CONT map THE_MAP root CA )\n" ;
	&create_re_phase () ;
	&create_kick_free_kick_map () ; 
	$FILE_WORDS{"SECOND"} = "_second" ;
	if ( ${$my_main{"MAP_WRITE"}}[0] eq "ON" ) { &write_map () ; }
    }
    if ( ${$my_main{"MAP_GENERATION"}}[0] eq "THIRD" )
    { 
	print MACRO
	    "set weight sele segm name WORK_SEGM .a map THE_MAP -1000. 0.8 end = 0\n" ;
#	    "set weight sele segm name WORK_SEGM end auto ( dens MAP_CONT map THE_MAP root CA )\n" ;
	&create_re_phase () ;
	&create_kick_free_kick_map () ; 
	$FILE_WORDS{"SECOND"} = "_third" ;
	if ( ${$my_main{"MAP_WRITE"}}[0] eq "ON" ) { &write_map () ; }
    }
    print MACRO "\n return\n" ;
}
else
{
    &create_re_phase () ;
    &create_kick_map () ; 
    $FILE_WORDS{"KICK"} = "kick_" . $my_main{"MAP_KICK_SIZE"}[0] ;
    if ( $my_main{"MAP_KICK_SERIES"}[0] eq "RANDOM" ) 
    { 
	$FILE_WORDS{"KICK"} = "kick_rnd_" . $my_main{"MAP_KICK_SIZE"}[0] ; 
    }
    $FILE_WORDS{"KICK_NN"} = "_" . $my_main{"MAP_KICK_NN"}[0] . "x" ;


    if ( ${$my_main{"MAP_WRITE"}}[0] eq "ON" ) { &write_map () ; }
    if ( ${$my_main{"MAP_GENERATION"}}[0] eq "SECOND" || ${$my_main{"MAP_GENERATION"}}[0] eq "THIRD")
    { 
	print MACRO
	    "set weight sele segm name WORK_SEGM end auto ( dens MAP_CONT map THE_MAP root CA )\n" ;
#	    "set weight sele segm name WORK_SEGM .a map THE_MAP -1000. 0.8 end = 0\n" ;
	&create_re_phase () ;
	&create_kick_map () ; 
	$FILE_WORDS{"SECOND"} = "_second" ;
	if ( ${$my_main{"MAP_WRITE"}}[0] eq "ON" ) { &write_map () ; }
    }
    if ( ${$my_main{"MAP_GENERATION"}}[0] eq "THIRD" )
    { 
	print MACRO
	    "set weight sele segm name WORK_SEGM end auto ( dens MAP_CONT map THE_MAP root CA )\n" ;
#	    "set weight sele segm name WORK_SEGM .a map THE_MAP -1000. 0.8 end = 0\n" ;
	&create_re_phase () ;
	&create_kick_map () ; 
	$FILE_WORDS{"SECOND"} = "_third" ;
	if ( ${$my_main{"MAP_WRITE"}}[0] eq "ON" ) { &write_map () ; }
    }
    print MACRO "\n return\n" ;
}
}

close ( MACRO ) ;

sub create_single_map
{

print MACRO << "END_OF_TEXT" ;

make map THE_MAP zero
make map THE_MAP conv complex

END_OF_TEXT

if ( ${$my_main{"MAP_KIND"}}[0] eq "3FO2FC" ) 
{
    print " IN MAP 3FO2FC\n" ;
if ( $MAP_TARGET eq "CROS_CORR" )
{
print MACRO << "END_OF_TEXT" ;

! echo "SIGMA A 2FO-FC" SIGMAA_MAP RFOBS
refl
sigma_a 2
sele asym .a .not defined end sigma_fc
exit
END_OF_TEXT
}

if ( $MAP_TARGET eq "UN" )
{
print MACRO << "END_OF_TEXT" ;
refl 
weigh const
set ampl phase fwork = fcalc * 1.
set ampl scale fcalc     fwork = fobs * 3.0 - fcalc * 2.0
END_OF_TEXT

if ( $missing_fc eq "YES" ) 
{ print MACRO "sele asym .a .not defined end set ampl phase scale fcalc fwork = fcalc * 1. \n" ; }

print MACRO << "END_OF_TEXT" ;
exit
END_OF_TEXT
}

if ( $MAP_TARGET eq "SIGMA" )
{
print MACRO << "END_OF_TEXT" ;
refl 
weigh const
weigh sigma
set ampl phase fwork = fcalc * 1.
set ampl scale fcalc     fwork = fobs * 3.0 - fcalc * 2.0
if ( $missing_fc eq "YES" ) 
{ print MACRO "sele asym .a .not defined end set ampl phase scale fcalc fwork = fcalc * 1. \n" ; }

print MACRO << "END_OF_TEXT" ;
exit
END_OF_TEXT
}

if ( $my_main{"MAP_BSHARP"}[0] eq "ON" )
{
    my $BSHARP = ${$my_main{"MAP_BSHARP_FACTOR"}}[0] ;
    print MACRO "refl set ampl scale fwork const 1. temp $BSHARP fwork = fwork * 1.0\n" ;
}

if ( $missing_fc eq "YES" ) { print MACRO "refl fill-map THE_MAP sele asymm .a .n defined end \n" ; }
print MACRO << "END_OF_TEXT" ;
refl fill-map THE_MAP select $REFL_SELECTION end
four map THE_MAP back 
make map THE_MAP rescale 
image map THE_MAP label $MAP_LABEL
image map THE_MAP over
END_OF_TEXT

}


if (${$my_main{"MAP_KIND"}}[0] eq "2FOFC" ) 
{

    print " IN MAP 2FO - FC\n" ;

if ( $MAP_TARGET eq "CROS_CORR" )
{
print MACRO << "END_OF_TEXT" ;

! echo "SIGMA A 2FO-FC" SIGMAA_MAP RFOBS
refl
sigma_a 2
END_OF_TEXT

if ( $missing_fc eq "YES" ) 
{ print MACRO "sele asym .a .not defined end sigma_fc \n" ; }

print MACRO << "END_OF_TEXT" ;
exit
END_OF_TEXT
}

if ( $MAP_TARGET eq "SIGMA" )
{
print MACRO << "END_OF_TEXT" ;
refl 
weigh sigma
set ampl phase fwork = fcalc * 1.
set ampl scale fcalc     fwork = fobs * 2.0 - fcalc * 1.0
END_OF_TEXT
if ( $missing_fc eq "YES" ) 
{ print MACRO "sele asym .a .not defined end set ampl phase scale fcalc fwork = fcalc * 1. \n" ; }

print MACRO << "END_OF_TEXT" ;
exit
END_OF_TEXT
}

if ( $MAP_TARGET eq "UN" )
{
print MACRO << "END_OF_TEXT" ;
refl 
weigh const
set ampl phase fwork = fcalc * 1.
set ampl scale fcalc     fwork = fobs * 2.0 - fcalc * 1.0
END_OF_TEXT
if ( $missing_fc eq "YES" ) 
{ print MACRO "sele asym .a .not defined end set ampl phase scale fcalc fwork = fcalc * 1. \n" ; }

print MACRO << "END_OF_TEXT" ;
exit
END_OF_TEXT
}

if ( $my_main{"MAP_BSHARP"}[0] eq "ON" )
{
    my $BSHARP = ${$my_main{"MAP_BSHARP_FACTOR"}}[0] ;
    print MACRO "refl set ampl scale fwork const 1. temp $BSHARP fwork = fwork * 1.0\n" ;
}

if ( $missing_fc eq "YES" ) { print MACRO "refl fill-map THE_MAP sele asymm .a .n defined end\n" ; }
print MACRO << "END_OF_TEXT" ;
refl fill-map THE_MAP select $REFL_SELECTION end
four map THE_MAP back 
make map THE_MAP rescale 
image map THE_MAP label $MAP_LABEL
image map THE_MAP over
END_OF_TEXT

}

if (${$my_main{"MAP_KIND"}}[0] eq "FOFC" ) 
{
if ( $MAP_TARGET eq "CROS_CORR" )
{
print MACRO << "END_OF_TEXT" ;

! echo "SIGMA A 2FO-FC" SIGMAA_MAP RFOBS
refl
sigma_a 1
exit
END_OF_TEXT
}

if ( $MAP_TARGET eq "SIGMA" )
{
print MACRO << "END_OF_TEXT" ;
refl 
weigh const
set ampl phase fwork = fcalc * 1.
set ampl scale fcalc     fwork = fobs * 1.0 - fcalc * 1.0
exit
END_OF_TEXT
}

if ( $MAP_TARGET eq "UN" )
{
print MACRO << "END_OF_TEXT" ;
refl 
weigh const
set ampl phase fwork = fcalc * 1.
set ampl scale fcalc     fwork = fobs * 1.0 - fcalc * 1.0
exit
END_OF_TEXT
}

if ( $my_main{"MAP_BSHARP"}[0] eq "ON" )
{
    my $BSHARP = ${$my_main{"MAP_BSHARP_FACTOR"}}[0] ;
    print MACRO "refl set ampl scale fwork const 1. temp $BSHARP fwork = fwork * 1.0\n" ;
}

print MACRO << "END_OF_TEXT" ;

refl fill-map THE_MAP select defined end
four map THE_MAP back 
make map THE_MAP rescale 
image map THE_MAP label $MAP_LABEL
image map THE_MAP dens 3.0 map_boxx map_boxx map_boxx

make map THE_MAP + 1 from THE_MAP init 0 real
set vari THE_MAP = THE_MAP + 1
make map THE_MAP conv complex
refl fill-map THE_MAP select defined end
four map THE_MAP back 
make map THE_MAP rescale 
image map THE_MAP dens -3.0 map_boxx map_boxx map_boxx
image map THE_MAP label $MAP_LABEL

image map THE_MAP over
END_OF_TEXT

}

if (${$my_main{"MAP_KIND"}}[0] eq "2FOFC+FOFC" ) 
{

    print " IN MAP 2FO - FC\n" ;

if ( $MAP_TARGET eq "CROS_CORR" )
{
print MACRO << "END_OF_TEXT" ;

! echo "SIGMA A 2FO-FC" SIGMAA_MAP RFOBS
refl
sigma_a 2
END_OF_TEXT
if ( $missing_fc eq "YES" ) 
{ print MACRO "sele asym .a .not defined end sigma_fc \n" ; }

print MACRO << "END_OF_TEXT" ;
exit
END_OF_TEXT
}

if ( $MAP_TARGET eq "UN" )
{
print MACRO << "END_OF_TEXT" ;
refl 
weigh const
set ampl phase fwork = fcalc * 1.
set ampl scale fcalc  fwork = fobs * 2.0 - fcalc * 1.0
END_OF_TEXT

if ( $missing_fc eq "YES" ) 
{ print MACRO "sele asym .a .not defined end set ampl phase scale fcalc fwork = fcalc * 1. \n" ; }

print MACRO << "END_OF_TEXT" ;
exit
END_OF_TEXT
}

if ( $my_main{"MAP_BSHARP"}[0] eq "ON" )
{
    my $BSHARP = ${$my_main{"MAP_BSHARP_FACTOR"}}[0] ;
    print MACRO "refl set ampl scale fwork const 1. temp $BSHARP fwork = fwork * 1.0\n" ;
}

if ( $missing_fc eq "YES" ) { print MACRO "refl fill-map THE_MAP sele asymm .a .n defined end \n" ; }
print MACRO << "END_OF_TEXT" ;
refl fill-map THE_MAP select $REFL_SELECTION end
four map THE_MAP back 
make map THE_MAP rescale 
image map THE_MAP label $MAP_LABEL
image map THE_MAP over
END_OF_TEXT

# FO-FC

    print " IN MAP FO - FC\n" ;

print MACRO << "END_OF_TEXT" ;
make map THE_MAP + 1 from THE_MAP init 0 real cell
set vari THE_MAP = THE_MAP + 1
make map THE_MAP conv complex

END_OF_TEXT

if ( $MAP_TARGET eq "CROS_CORR" )
{
print MACRO << "END_OF_TEXT" ;

! echo "SIGMA A FO-FC" SIGMAA_MAP RFOBS
refl
sigma_a 1
exit
END_OF_TEXT
}

if ( $MAP_TARGET eq "UN" )
{
print MACRO << "END_OF_TEXT" ;
refl 
weigh const
set ampl phase fwork = fcalc * 1.
set ampl scale fcalc fwork = fobs * 1.0 - fcalc * 1.0
exit
END_OF_TEXT
}

if ( $my_main{"MAP_BSHARP"}[0] eq "ON" )
{
    my $BSHARP = ${$my_main{"MAP_BSHARP_FACTOR"}}[0] ;
    print MACRO "refl set ampl scale fwork const 1. temp $BSHARP fwork = fwork * 1.0\n" ;
}

print MACRO << "END_OF_TEXT" ;
refl fill-map THE_MAP select defined end
four map THE_MAP back 
make map THE_MAP rescale 
image map THE_MAP label $MAP_LABEL2
image map THE_MAP over
make map THE_MAP + 1 from THE_MAP full init real copy
image map THE_MAP + 1 label $MAP_LABEL2
image map THE_MAP + 1  over

END_OF_TEXT

}



if (${$my_main{"MAP_KIND"}}[0] eq "FCALC" ) 
{
if ( $MAP_TARGET eq "CROS_CORR" )
{
print MACRO << "END_OF_TEXT" ;
refl 
weigh const
sele asym end sigma_fc
fill-map THE_MAP select asym end
exit
END_OF_TEXT
}
else
{
print MACRO << "END_OF_TEXT" ;
refl 
weigh const
set ampl phase fwork = fcalc * 1.
fill-map THE_MAP select asym end
exit
END_OF_TEXT
}

print MACRO << "END_OF_TEXT" ;
four map THE_MAP back 
make map THE_MAP rescale 
image map THE_MAP label $MAP_LABEL
image map THE_MAP over

END_OF_TEXT

}

# #if (${$my_main{"MAP_KIND"}}[0] eq "FOBS" ) 
# #{
# #print MACRO << "END_OF_TEXT" ;
# #refl 
# #set ampl phase fwork = fobs * 1.
# #fill-map THE_MAP select defined end
# #exit
# #
# #four map THE_MAP back 
# #make map THE_MAP rescale 
# #image map THE_MAP over
# #
# #END_OF_TEXT
# #
# #}
}


sub create_re_phase
{

if ( ${$my_main{"MAP_OMIT"}}[0] eq "ON" )
{
print MACRO << "END_OF_TEXT" ;

key map_dens select segm name WORK_SEGM .a .n active end
set vari II integer atom sele map_dens end
if ( II .le. 0 ) then

echo " ------------> %ERROR: YOU CAN NOT OMIT all WORK SEGMENTS - MODIFY key active"
return
end_if


END_OF_TEXT
}
else
{
print MACRO << "END_OF_TEXT" ;
key map_dens select segm name WORK_SEGM end
END_OF_TEXT
}

my $NZONES = ${$my_main{"MAP_ZONES"}}[0] ;
my $ZONE_TEXT ;
if ( $NZONES > 0 ) { $ZONE_TEXT = join ( " ", "shell", $NZONES ) ; } else { $ZONE_TEXT = "" ; } ;

my $BADD = ${$my_main{"MAP_BADD"}}[0] ;
if ( $BADD > 0 ) 
{ 
    my $TEXT = join ( " ", "four b_add", $BADD ) ;
print MACRO $TEXT, "\n" ; 
}



print MACRO << "END_OF_TEXT" ;

set vari THE_MAP = $THE_MAP

if ( RESOL_MAX .gt. 1.5 ) then
set weight sele segm name WORK_SEGM .a atom name H* end = 0.0
set temp sele segm name WORK_SEGM .a temp -100. 2.0 end = 2.0
end_if

END_OF_TEXT

print MACRO "refl bulk ( least_square )\n" ; 

if ( ${$my_main{"MAP_BULK"}}[0] eq "ON" || ${$my_main{"MAP_BULK"}}[0] eq "USE" )
{
print MACRO << "END_OF_TEXT" ;
refl aniso off
make map THE_MAP empty init from 0 auto $THE_GRID_DENSITY real cell

set rad sele segm name WORK_SEGM .a .n segm name XWAT* end  vdw
scale 1.06 rad sele  segm name WORK_SEGM end 

make map THE_MAP bulk \\
     ( select segm name WORK_SEGM end \\
       solvent 0.0 fill 1.8 shrink 0.4 ) 


make map THE_MAP set 999990 1000000 1.0
make map THE_MAP set -1000000 -999990 0.0

fourier map THE_MAP 
refl bulk off
refl sele defined .a resol RESOL_MIN 3.0 end set ampl phase fpart = fcalc * 1.0
refl bulk ( on )

END_OF_TEXT
}
if (  ${$my_main{"MAP_BULK"}}[0] eq "USE" )
{
print MACRO << "END_OF_TEXT" ;
refl bulk on

END_OF_TEXT
}
elsif (  ${$my_main{"MAP_BULK"}}[0] eq "OFF" )
{
print MACRO << "END_OF_TEXT" ;
refl bulk off

END_OF_TEXT
}




#refl sele defined .a resol RESOL_MIN 3.0 end aniso ( on init )
if ( ${$my_main{"MAP_ANISO"}}[0] eq "ON" )
{
print MACRO << "END_OF_TEXT" ;
refl sele defined end aniso ( on init )
END_OF_TEXT
}
elsif (  ${$my_main{"MAP_ANISO"}}[0] eq "USE" )
{
print MACRO << "END_OF_TEXT" ;
refl aniso on
END_OF_TEXT
}
elsif (  ${$my_main{"MAP_ANISO"}}[0] eq "OFF" )
{
print MACRO << "END_OF_TEXT" ;
refl aniso off

END_OF_TEXT
}

my $FFT = "FFT" ;
if ( "FFT" eq $FFT ) {
print MACRO << "END_OF_TEXT" ;

make map THE_MAP conv real
 make map THE_MAP zero

   make map THE_MAP atom symm -
           atom dens -
           sele map_dens end

! make map THE_MAP set -1000000 -100 0.0


fourier map THE_MAP 
END_OF_TEXT
}
else {
print MACRO << "END_OF_TEXT" ;
fourier refl sele defined end atom sele map_dens end direct

END_OF_TEXT
} 
#	print MACRO "refl aniso ( init )\n" ; 
#{

if ( ${$my_main{"MAP_OMIT"}}[0] ne "ON" )
{
    if ( ${$my_main{"MAP_ANISO"}}[0] eq "ON" )
    {
#	if ( $MAP_TARGET eq "CROS_CORR" ) 
#	{ print MACRO "refl aniso ( max_like )\n" ; }
	print MACRO "reflection sele WORK_REFL end $ZONE_TEXT r-values ( aniso test TEST )\n" ;
    }
    
    if ( ${$my_main{"MAP_BULK"}}[0] eq "ON" )
    {    
	print MACRO "reflection sele WORK_REFL end $ZONE_TEXT r-values ( bulk  test TEST ) \n" ;
    }
    
    if ( ${$my_main{"MAP_BULK"}}[0] ne "ON" && ${$my_main{"MAP_ANISO"}}[0] ne "ON" )
    { 
	print MACRO "reflection sele WORK_REFL end $ZONE_TEXT r-values test TEST\n" ;
    }
}
else
{
    print MACRO "reflection sele WORK_REFL end $ZONE_TEXT r-values\n" ;
}


print MACRO << "END_OF_TEXT" ;

 set vari IRESULT_0 global = -1
 refl show key TEST
 show vari IRESULT_0
 if ( IRESULT_0 .gt. 0 ) then
    reflection sele TEST end r-values fix
 end_if
END_OF_TEXT
}

sub create_re_phase_next
{

if ( ${$my_main{"MAP_OMIT"}}[0] eq "ON" )
{
print MACRO << "END_OF_TEXT" ;

key map_dens select segm name WORK_SEGM .a .n active end
set vari II integer atom sele map_dens end
if ( II .le. 0 ) then

echo " ------------> %ERROR: YOU CAN NOT OMIT all WORK SEGMENTS - MODIFY key active"
return
end_if


END_OF_TEXT
}
else
{
print MACRO << "END_OF_TEXT" ;
key map_dens select segm name WORK_SEGM end
END_OF_TEXT
}

print MACRO << "END_OF_TEXT" ;

set vari THE_MAP = $THE_MAP

if ( RESOL_MAX .gt. 1.5 ) then
set weight sele segm name WORK_SEGM .a atom name H* end = 0.0
set temp sele segm name WORK_SEGM .a temp -100. 1.0 end = 1.0
end_if

make map THE_MAP conv real
make map THE_MAP zero

   make map THE_MAP atom symm -
           atom dens -
           sele map_dens end

fourier map THE_MAP 

reflection sele WORK_REFL end r-values

 set vari IRESULT_0 global = -1
 refl show key TEST
 show vari IRESULT_0
 if ( IRESULT_0 .gt. 0 ) then
    reflection sele TEST end r-values fix
 end_if
END_OF_TEXT
}

sub create_kick_map
{

    my $NN_KICK = $my_main{"MAP_KICK_NN"}[0];
    my $KICK = $my_main{"MAP_KICK_SIZE"}[0] ;
    my $KICK_SERIES = $my_main{"MAP_KICK_SERIES"}[0] ;
    my $MAP_KIND = $my_main{"MAP_KIND"}[0] ;
    if ( $fl_real eq "ON" ) { $MAP_KIND = $my_main{"MAP_KIND_REAL"}[0] ; }
    my $MAP_OMIT = $my_main{"MAP_OMIT"}[0] ;


    my $RFOBS = 999.0 ; my $RFCALC = -999.0 ;
    if ( $MAP_KIND eq "FOFC" ) { $RFOBS = 1.0 ; $RFCALC = 1.0 ; }
    if ( $MAP_KIND eq "2FOFC" ) { $RFOBS = 2.0 ; $RFCALC = 1.0 ; }
    if ( $MAP_KIND eq "3FO2FC" ) { $RFOBS = 3.0 ; $RFCALC = 2.0 ; }

if ( $RFOBS == 999.0 )
{
    print " create_map_calc.pl> EROOR: map kind $MAP_KIND can not be kicked\n" ;
    exit ;
}

#    print " kicks: $KICK, $NN_KICK\n" ;

if ( $MAP_TARGET eq "CROS_CORR" && $MAP_KIND eq "2FOFC" ) 
{
#! echo "SIGMA A 2FO-FC" SIGMAA_MAP RFOBS
    print MACRO << "END_OF_TEXT" ;
refl
sigma_a 2
exit
END_OF_TEXT
}

if ( $MAP_TARGET eq "CROS_CORR" && $MAP_KIND eq "FOFC" ) 
{
#! echo "SIGMA A 2FO-FC" SIGMAA_MAP RFOBS
    print MACRO << "END_OF_TEXT" ;
refl
sigma_1 1
exit
END_OF_TEXT
}

print MACRO "refl set ampl fstore = 0.\n" ;
#print MACRO "refl aniso off \n" ;
#print MACRO "refl bulk off \n" ;

#make map 1 zero

my $MY_SERIES = 1 ;
my $MY_KICK = $KICK ;
my $NN_SERIES ;
if ( $KICK_SERIES eq "SERIES" ) { $MY_KICK = 0.0 ; $MY_SERIES = 10}


for ( $NN_SERIES = 1; $NN_SERIES <= $MY_SERIES ; $NN_SERIES++ )
{
    if ( $KICK_SERIES eq "SERIES" ) { $MY_KICK =  0.1 * $NN_SERIES * $KICK; }
#    print " $NN_SERIES, $MY_SERIES = series: $MY_KICK\n" ;

    my $NN ;
    for ( $NN = 0; $NN < $NN_KICK ; $NN++ )
    {
	if ( $KICK_SERIES eq "RANDOM" ) { $MY_KICK = rand ( $KICK ) ; }
#	print "! cycle $NN $NN_KICK $MY_KICK\n" ;
	print MACRO "echo cycle $NN of $NN_KICK kick: $MY_KICK\n" ;
	print MACRO << "END_OF_TEXT" ;

make map $THE_MAP conv real
make map $THE_MAP zero 
END_OF_TEXT
# make map $THE_MAP set -10 10 -9999.
# set coor sele segm name WORK_SEGM end kick $MY_KICK 

if ( $MAP_OMIT eq "ON" ) 
{
    print MACRO << "END_OF_TEXT" ;
   make map $THE_MAP atom ( symm dens kick $MY_KICK ) -
           sele segm name WORK_SEGM .a .n active end
END_OF_TEXT
}
else
{
    print MACRO << "END_OF_TEXT" ;
   make map $THE_MAP atom ( symm dens kick $MY_KICK ) -
           sele segm name WORK_SEGM end
END_OF_TEXT
}

# <>cmds/un_do.cmds

# make map $THE_MAP set -100000 -100 0.

print MACRO << "END_OF_TEXT" ;

 fourier map $THE_MAP 
 reflection select WORK_REFL end r-values 
END_OF_TEXT

#!set vari BSHARP = -6 * 3.14 * 3.14 * KICK * KICK

if ( $MAP_TARGET eq "CROS_CORR" && $MAP_KIND eq "2FOFC" ) 
{
#! echo "SIGMA A 2FO-FC" SIGMAA_MAP RFOBS
    print MACRO << "END_OF_TEXT" ;
refl
sigma_b 2
exit
END_OF_TEXT
}

if ( $MAP_TARGET eq "CROS_CORR" && $MAP_KIND eq "FOFC" ) 
{
#! echo "SIGMA A 2FO-FC" SIGMAA_MAP RFOBS
    print MACRO << "END_OF_TEXT" ;
refl
sigma_b 1
exit
END_OF_TEXT
}

if ( $MAP_TARGET eq "UN" ) 
{
    print MACRO << "END_OF_TEXT" ;
refl 
weigh const
set ampl phase fwork = fcalc * 1.
sele defined end set ampl scale fcalc   fwork = fobs * $RFOBS - fcalc * $RFCALC

exit
END_OF_TEXT
}

print MACRO "refl set ampl phase fstore = fstore * 1.0 + fwork * 0.1\n" ;

# print MACRO << "END_OF_TEXT" ;
# make map $THE_MAP zero
# make map $THE_MAP conv complex
# refl fill-map $THE_MAP select  WORK_REFL .o TEST end
# four map $THE_MAP back 
# make map $THE_MAP prescale 
# make map 1 from $THE_MAP add
# 
# END_OF_TEXT


    }
}

print MACRO << "END_OF_TEXT" ;
make map $THE_MAP conv complex
make map $THE_MAP zero
refl 
sele defined end set ampl phase fwork = fstore * 1.0
exit
END_OF_TEXT


    print "SHARP: in flag{",  $my_main{"MAP_BSHARP"}[0], "}\n" ;
if ( $my_main{"MAP_BSHARP"}[0] eq "ON" )
{
    my $BSHARP = ${$my_main{"MAP_BSHARP_FACTOR"}}[0] ;
    print "SHARP: in long{", $BSHARP, "}\n" ;
    print MACRO "refl sele defined end set ampl scale fwork const 1. temp $BSHARP fwork = fwork * 1.0\n" ;
}

print MACRO << "END_OF_TEXT" ;
refl fill-map THE_MAP select $REFL_SELECTION end
four map $THE_MAP back 
make map $THE_MAP prescale 
image map $THE_MAP label $MAP_LABEL

image map $THE_MAP over

END_OF_TEXT

}

sub create_kick_free_kick_map
{

    my $NN_KICK = $my_main{"MAP_KICK_NN"}[0];
    my $KICK = $my_main{"MAP_KICK_SIZE"}[0] ;
    my $KICK_SERIES = $my_main{"MAP_KICK_SERIES"}[0] ;
    my $MAP_KIND = $my_main{"MAP_KIND"}[0] ;
    if ( $fl_real eq "ON" ) { $MAP_KIND = $my_main{"MAP_KIND_REAL"}[0] ; }
    my $MAP_OMIT = $my_main{"MAP_OMIT"}[0] ;


    my $RFOBS = 999.0 ; my $RFCALC = -999.0 ;
    if ( $MAP_KIND eq "FOFC" ) { $RFOBS = 1.0 ; $RFCALC = 1.0 ; }
    if ( $MAP_KIND eq "2FOFC" ) { $RFOBS = 2.0 ; $RFCALC = 1.0 ; }
    if ( $MAP_KIND eq "3FO2FC" ) { $RFOBS = 3.0 ; $RFCALC = 2.0 ; }

#    print " kicks: $KICK, $NN_KICK\n" ;
if ( $RFOBS == 999.0 )
{
    print " create_map_calc.pl> EROOR: map kind $MAP_KIND can not be kicked\n" ;
    exit ;
}

#! echo "SIGMA A 2FO-FC" SIGMAA_MAP RFOBS
print MACRO << "END_OF_TEXT" ;
refl sele defined end ab_init 
refl sele defined end ab_use 1
refl sele defined end ab_init 

END_OF_TEXT

#make map 1 zero

my $MY_SERIES = 1 ;
my $MY_KICK = $KICK ;
my $NN_SERIES ;
if ( $KICK_SERIES eq "SERIES" ) { $MY_KICK = 0.0 ; $MY_SERIES = 10}
if ( $KICK_SERIES eq "X_ERROR" ) 
{ 
    print MACRO << "END_OF_TEXT" ;

set vari MY_KICK = $MY_KICK * ML_ERROR_X

END_OF_TEXT
}


for ( $NN_SERIES = 1; $NN_SERIES <= $MY_SERIES ; $NN_SERIES++ )
{
    if ( $KICK_SERIES eq "SERIES" ) { $MY_KICK =  $NN_SERIES * $KICK / $MY_SERIES; }
#    print " $NN_SERIES, $MY_SERIES = series: $MY_KICK\n" ;

    my $NN ;
    for ( $NN = 0; $NN < $NN_KICK ; $NN++ )
    {
	if ( $KICK_SERIES eq "RANDOM" ) { $MY_KICK = rand ( $KICK ) ; }
#	print "! cycle $NN $NN_KICK $MY_KICK\n" ;
	print MACRO "! cycle $NN $NN_KICK \n" ;
	print MACRO << "END_OF_TEXT" ;

make map $THE_MAP conv real
make map $THE_MAP zero 
END_OF_TEXT
# make map $THE_MAP set -10 10 -9999.
# set coor sele segm name WORK_SEGM end kick $MY_KICK 

if ( $KICK_SERIES eq "X_ERROR" ) 
{ 
if ( $MAP_OMIT eq "ON" ) 
{
    print MACRO << "END_OF_TEXT" ;
   make map $THE_MAP atom ( symm dens kick MY_KICK ) -
           sele segm name WORK_SEGM .a .n active end
END_OF_TEXT
}
else
{
    print MACRO << "END_OF_TEXT" ;
   make map $THE_MAP atom ( symm dens kick MY_KICK ) -
           sele segm name WORK_SEGM end
END_OF_TEXT
}

} 
else
{
if ( $MAP_OMIT eq "ON" ) 
{
    print MACRO << "END_OF_TEXT" ;
   make map $THE_MAP atom ( symm dens kick $MY_KICK ) -
           sele segm name WORK_SEGM .a .n active end
END_OF_TEXT
}
else
{
    print MACRO << "END_OF_TEXT" ;
   make map $THE_MAP atom ( symm dens kick $MY_KICK ) -
           sele segm name WORK_SEGM end
END_OF_TEXT
}
}
# <>cmds/un_do.cmds

# make map $THE_MAP set -100000 -100 0.

print MACRO << "END_OF_TEXT" ;

 fourier map $THE_MAP 
 reflection select defined end r-values fix ! rescale
 refl sele defined end ab_init 
END_OF_TEXT

if ( $MAP_KIND eq "2FOFC" ) 
{
#! echo "SIGMA A 2FO-FC" SIGMAA_MAP RFOBS
if ( $missing_fc eq "YES" ) { print MACRO "refl sele asymm .a .n defined end a_use \n" ; }
    print MACRO << "END_OF_TEXT" ;
refl sele defined end ab_use 2
END_OF_TEXT
}

if ( $MAP_KIND eq "FOFC" ) 
{
#! echo "SIGMA A 2FO-FC" SIGMAA_MAP RFOBS
    print MACRO << "END_OF_TEXT" ;
refl sele defined end ab_use 1
END_OF_TEXT
}


    print MACRO << "END_OF_TEXT" ;
 refl set ampl phase fstore = fstore * 1.0 + fwork * 0.1
END_OF_TEXT

#!set vari BSHARP = -6 * 3.14 * 3.14 * KICK * KICK

    }
}

print MACRO << "END_OF_TEXT" ;
make map $THE_MAP conv complex
make map $THE_MAP zero
refl sele all end set fcalc = fstore * 1.0
 reflection select defined end r-values ! fix
END_OF_TEXT


print MACRO << "END_OF_TEXT" ;
!refl sele asym .a .not defined end set ampl phase scale fcalc fwork = fcalc * 1. 
END_OF_TEXT


#    print "SHARP: in flag{",  $my_main{"MAP_BSHARP"}[0], "}\n" ;
if ( $my_main{"MAP_BSHARP"}[0] eq "ON" )
{
    my $BSHARP = ${$my_main{"MAP_BSHARP_FACTOR"}}[0] ;
#    print "SHARP: in long{", $BSHARP, "}\n" ;
    print MACRO "refl sele defined end set ampl scale fwork const 1. temp $BSHARP fwork = fwork * 1.0\n" ;
}

print MACRO << "END_OF_TEXT" ;
refl fill-map sele $REFL_SELECTION end
four map $THE_MAP back 
make map $THE_MAP prescale 
image map $THE_MAP label $MAP_LABEL
image map $THE_MAP over

END_OF_TEXT

}

sub create_series_map
{

}

sub create_kick_map_refine
{

print " KICK MAP REFINE\n" ;

    my $NN_KICK = $my_main{"MAP_KICK_NN"}[0];
    my $KICK = $my_main{"MAP_KICK_SIZE"}[0] ;
    my $KICK_SERIES = $my_main{"MAP_KICK_SERIES"}[0] ;
    my $MAP_KIND = $my_main{"MAP_KIND"}[0] ;
    my $MAP_OMIT = $my_main{"MAP_OMIT"}[0] ;


    my $RFOBS = 1.0 ; 
    my $RFCALC = 1.0 ; 

#    print " kicks: $KICK, $NN_KICK\n" ;

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 $REFL_SELECTION = "defined" ;
if ( $missing_fc eq "YES" ) { $REFL_SELECTION = $REFL_SELECTION . " .o asymm \n" ; }
$RFOBS = 1.0 ; $RFCALC = 1.0 ; 

print MACRO "subroutine \n" ;
print MACRO "refl set ampl fstore = 0.\n" ;
print MACRO "set vari THE_MAP = MAP_TARG \n" ;
#print MACRO "refl aniso off \n" ;
#print MACRO "refl bulk off \n" ;

#make map 1 zero

my $MY_SERIES = 1 ;
my $MY_KICK = $KICK ;
my $NN_SERIES ;
if ( $KICK_SERIES eq "SERIES" ) { $MY_KICK = 0.0 ; $MY_SERIES = 10}


for ( $NN_SERIES = 1; $NN_SERIES <= $MY_SERIES ; $NN_SERIES++ )
{
    if ( $KICK_SERIES eq "SERIES" ) { $MY_KICK =  0.1 * $NN_SERIES * $KICK; }
#    print " $NN_SERIES, $MY_SERIES = series: $MY_KICK\n" ;

    my $NN ;
    for ( $NN = 0; $NN < $NN_KICK ; $NN++ )
    {
	if ( $KICK_SERIES eq "RANDOM" ) { $MY_KICK = rand ( $KICK ) ; }
#	print "! cycle $NN $NN_KICK $MY_KICK\n" ;
	print MACRO "! cycle $NN $NN_KICK \n" ;
	print MACRO << "END_OF_TEXT" ;

make map THE_MAP conv real
make map THE_MAP zero 
make map THE_MAP atom ( symm dens kick $MY_KICK ) -
           sele segm name WORK_SEGM end
END_OF_TEXT

print MACRO << "END_OF_TEXT" ;

 fourier map THE_MAP 
 reflection select WORK_REFL end r-values 
END_OF_TEXT

#!set vari BSHARP = -6 * 3.14 * 3.14 * KICK * KICK

if ( $MAP_TARGET eq "CROS_CORR" ) 
{
#! echo "SIGMA A 2FO-FC" SIGMAA_MAP RFOBS
    print MACRO << "END_OF_TEXT" ;
refl
sigma_b 1
exit
END_OF_TEXT
}

#!sele defined end set ampl scale fcalc   fwork = fobs * $RFOBS - fcalc * $RFCALC

if ( $MAP_TARGET eq "UN" ) 
{
    print MACRO << "END_OF_TEXT" ;
refl 
weigh const
set ampl phase fwork = fcalc * 1.
sele defined end set ampl scale fcalc  fwork = fcalc * $RFCALC - fobs *  $RFOBS
exit
END_OF_TEXT
}

print MACRO "refl set ampl phase fstore = fstore * 1.0 + fwork * 0.1\n" ;

    }
}



print MACRO << "END_OF_TEXT" ;
make map THE_MAP conv complex
make map THE_MAP zero
refl 
sele defined end set ampl phase fwork = fstore * 1.0
exit
END_OF_TEXT


#    print "SHARP: in flag{",  $my_main{"MAP_BSHARP"}[0], "}\n" ;
if ( $my_main{"MAP_BSHARP"}[0] eq "ON" )
{
    my $BSHARP = ${$my_main{"MAP_BSHARP_FACTOR"}}[0] ;
#    print "SHARP: in long{", $BSHARP, "}\n" ;
    print MACRO "refl set ampl scale fwork const 1. temp $BSHARP fwork = fwork * 1.0\n" ;
}

print MACRO << "END_OF_TEXT" ;
refl fill-map THE_MAP select defined end
four map THE_MAP back 
! make map THE_MAP prescale 

END_OF_TEXT

}


sub write_map
{
#    my ( $ref_words, $ref_the_map ) = @_ ;
#    my %FILE_WORDS = %{$ref_words} ;

    my $FILE_NAME = join( "", $FILE_WORDS{"ROOT"}, $FILE_WORDS{"KICK"}, $FILE_WORDS{"KICK_NN"}, 
			  $FILE_WORDS{"KIND"}, $FILE_WORDS{"TARGET"}, $FILE_WORDS{"OMIT"}, 
			  $FILE_WORDS{"SECOND"}, $FILE_WORDS{"EXT"}  ) ;
    
    print MACRO "write over file $FILE_NAME map $THE_MAP xpl\n" ; 
}




