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

my %color_list = ("WHITE"=> 32, "RED"=>128, "ORANGE"=>144,"YELLOW"=>160,  "GREEN"=>196, "CYAN"=>220, "BLUE"=>244, "AS_IS"=>-1 ) ;


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

if ( !exists($my_main{"MAP_IO_TASK"}  ) )  { push ( @{$my_main{"MAP_IO_TASK"}}  , "COPY" ) ; }
if ( !exists($my_main{"MAP_IO_SOURCE"}  ) )  { push ( @{$my_main{"MAP_IO_SOURCE"}}  , 2 ) ; }
if ( !exists($my_main{"MAP_IO_TARGET"}  ) )  { push ( @{$my_main{"MAP_IO_TARGET"}}  , 3 ) ; }
if ( !exists($my_main{"MAP_IO_CONTOUR"}  ) )  { push ( @{$my_main{"MAP_IO_CONTOUR"}}  , 1 ) ; }
if ( !exists($my_main{"MAP_IO_BOX"}  ) )  { push ( @{$my_main{"MAP_IO_BOX"}}  , 25 ) ; }
if ( !exists($my_main{"MAP_IO_GRID"}  ) )  { push ( @{$my_main{"MAP_IO_GRID"}}  , 0 ) ; }
if ( !exists($my_main{"MAP_IO_COLOR"}  ) )  { push ( @{$my_main{"MAP_IO_COLOR"}}  , "CYAN" ) ; }
if ( !exists($my_main{"MAP_IO_FILE"}  ) )  { push ( @{$my_main{"MAP_IO_FILE"}}  , "my_map.xmap" ) ; }
if ( !exists($my_main{"MAP_IO_SELECTION"}  ) )  { push ( @{$my_main{"MAP_IO_SELECTION"}}  , "active" ) ; }
if ( !exists($my_main{"MAP_IO_MASK_RADIUS"}  ) )  { push ( @{$my_main{"MAP_IO_MASK_RADIUS"}}  , 3.0 ) ; }
if ( !exists($my_main{"MAP_IO_BSHARP"}  ) )  { push ( @{$my_main{"MAP_IO_BSHARP"}}, "ON" ) ; }
if ( !exists($my_main{"MAP_IO_BSHARP_FACTOR"}  ) )  { push ( @{$my_main{"MAP_IO_BSHARP_FACTOR"}}, 0.0 ) ; }
if ( !exists($my_main{"MAP_IO_LABEL"}  ) )  { @{$my_main{"MAP_IO_LABEL"}}  = ("IO_MAP") ; }

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

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

my $keyword ; 
foreach $keyword ( sort keys (%commands) ) 
{
#    print "option >$keyword<\n" ; 
    if ( $keyword eq 'h' || substr($keyword, 0,4) eq "help" ) { goto HELP }
    if ( $keyword eq 't' || substr($keyword, 0,6) eq "target" ) { goto TARGET }
    if ( $keyword eq 's' || substr($keyword, 0,6) eq "source" ) { goto SOURCE }
    if ( $keyword eq 'f' || substr($keyword, 0,4) eq "file" ) { goto FILE }
    if ( $keyword eq 'g' || substr($keyword, 0,4) eq "grid" ) { goto GRID }
    if ( $keyword eq 'c' || substr($keyword, 0,7) eq "contour" ) { goto CONTOUR }
    if ( $keyword eq 's' || substr($keyword, 0,9) eq "selection" ) { goto SELECTION }
    if ( $keyword eq 'x' || substr($keyword, 0,4) eq "task" ) { goto TASK }
    if ( $keyword eq 'r' || substr($keyword, 0,6) eq "radius" ) { goto RADIUS }
    if ( $keyword eq 'b' || substr($keyword, 0,3) eq "box" ) { goto BOX }
    if ( $keyword eq 'a' || substr($keyword, 0,5) eq "color" ) { goto COLOR }
    if ( $keyword eq 'l' || substr($keyword, 0,5) eq "label" ) { goto LABEL }
    if ( $keyword eq 't' || substr($keyword, 0,10) eq "sharp_fact" ) { goto SHARP_FACT }
    if ( $keyword eq 'p' || substr($keyword, 0,5) eq "sharp" ) { goto SHARP }

    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 their current status 
-x|--task)        task to do  [COPY/READ/SAVE_AS/MASK_IN/MASK_OUT] [@{$my_main{"MAP_IO_TASK"}}]
-s|--source)      source MAP number [1/2/3/4/5/6/7/8/9/10] [@{$my_main{"MAP_IO_SOURCE"}}]
-t|--target)      target MAP number [1/2/3/4/5/6/7/8/9/10] [@{$my_main{"MAP_IO_TARGET"}}]
-g|--grid)        grid of the target map [@{$my_main{"MAP_IO_GRID"}}]
-r|--radius)      mask atom radious [@{$my_main{"MAP_IO_MASK_RADIUS"}}]
-c|--contour)     contour the target map at [@{$my_main{"MAP_IO_CONTOUR"}}]
-b|--box)         box of the target map [@{$my_main{"MAP_IO_BOX"}}]
-a|--color)       color of the target map [RED/ORANGE/YELLOW/GREEN/CYAN/BLUE/WHITE] [@{$my_main{"MAP_IO_COLOR"}}]
-l|--label)       map label [@{$my_main{"MAP_IO_LABEL"}}]
-p|--sharp)       B-factor sharpening for maps [ON/OFF] [@{$my_main{"MAP_IO_BSHARP"}}]
-t|--sharp_fact)  B-factor sharpening factor: - desharp + sharp [@{$my_main{"MAP_IO_BSHARP_FACTOR"}}]
-s|--selection)   mask in selection [@{$my_main{"MAP_IO_SELECTION"}}]
-f|--file)        file name [@{$my_main{"MAP_IO_FILE"}}]
   --doit)        create the macro
END_OF_TEXT
    exit ;

  TASK:   @{$my_main{"MAP_IO_TASK"}} = shift ( @{$commands{$keyword}} ) ; next ;
  TARGET:  @{$my_main{"MAP_IO_TARGET"}} = shift ( @{$commands{$keyword}} ) ; next ;
  SOURCE:  @{$my_main{"MAP_IO_SOURCE"}} = shift ( @{$commands{$keyword}} ) ; next ;
  FILE:  @{$my_main{"MAP_IO_FILE"}} = shift ( @{$commands{$keyword}} ) ; next ;
  CONTOUR:  @{$my_main{"MAP_IO_CONTOUR"}} = shift ( @{$commands{$keyword}} ) ; next ;
  BOX:  @{$my_main{"MAP_IO_BOX"}} = shift ( @{$commands{$keyword}} ) ; next ;
  GRID:  @{$my_main{"MAP_IO_GRID"}} = shift ( @{$commands{$keyword}} ) ; next ;
  COLOR:  @{$my_main{"MAP_IO_COLOR"}} = shift ( @{$commands{$keyword}} ) ; next ;
  SHARP:   
@{$my_main{"MAP_IO_BSHARP"}} = shift ( @{$commands{$keyword}} ) ;
#print " map sharp:{", @{$my_main{"MAP_BSHARP"}}, "}\n" ;
 next ;
  SHARP_FACT:   
    @{$my_main{"MAP_IO_BSHARP_FACTOR"}} = shift ( @{$commands{$keyword}} ) ; 
#print " fact sharp:{", @{$my_main{"MAP_BSHARP_FACTOR"}}, "}\n" ;
next ;
  SELECTION:  @{$my_main{"MAP_IO_SELECTION"}} = join (" ", @{$commands{$keyword}} ) ; next ;
  RADIUS:  @{$my_main{"MAP_IO_MASK_RADIUS"}} = join (" ", @{$commands{$keyword}} ) ; next ;
  LABEL:  @{$my_main{"MAP_IO_LABEL"}} = join (" ", @{$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 ; }

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

print "creating \"$MACRO_FILE\" \n" ;
if ( -f $MACRO_FILE ) 
{ 
    my $back = join ('', $MACRO_FILE, ".old" ) ; 
    system ( "mv  $MACRO_FILE $back" ) ; 
}
my $TO_MAP ;
my $FROM_MAP ;
my $MAP_IO_FILE ;
my $CONTOUR ;
my $MY_SELECTION ; 
my $MASK_RADIUS;
my $BOX;
my $NEW_GRID;
my $MAP_LABEL ;
my $COLOR;
open ( MACRO,  "> $MACRO_FILE" ) || die "FILE OPEN ERROR: >$MACRO_FILE< \n" ;

$TO_MAP = ${$my_main{"MAP_IO_TARGET"}}[0] ;
$MAP_LABEL = ${$my_main{"MAP_IO_LABEL"}}[0] ;
$MAP_IO_FILE = ${$my_main{"MAP_IO_FILE"}}[0] ;
$FROM_MAP = ${$my_main{"MAP_IO_SOURCE"}}[0] ;
$CONTOUR =  ${$my_main{"MAP_IO_CONTOUR"}}[0] ;

$COLOR =  $color_list{${$my_main{"MAP_IO_COLOR"}}[0]} ;
my $color_text = "" ;
if ( $COLOR > 0 ) { $color_text = "color ".$COLOR ; } 

$BOX =  ${$my_main{"MAP_IO_BOX"}}[0] ;
$NEW_GRID =  ${$my_main{"MAP_IO_GRID"}}[0] ;
$MASK_RADIUS =  ${$my_main{"MAP_IO_MASK_RADIUS"}}[0] ;
$MY_SELECTION =  join( " ", @{$my_main{"MAP_IO_SELECTION"}} ) ;

# tasks

if ( ${$my_main{"MAP_IO_TASK"}}[0] eq "COPY" )
{
print MACRO << "END_OF_TEXT" ;
make map $TO_MAP from $FROM_MAP full init copy
image $color_text map $TO_MAP label MAP_COPY dens $CONTOUR $BOX $BOX $BOX
return
END_OF_TEXT
}

if ( ${$my_main{"MAP_IO_TASK"}}[0] eq "MASK_IN" )
{
print MACRO << "END_OF_TEXT" ;
make map  $TO_MAP from $FROM_MAP empty init 
make map $TO_MAP atom ( mask $MASK_RADIUS ) sele $MY_SELECTION end
make map  $TO_MAP from $FROM_MAP copy
image $color_text map $TO_MAP label MASK_IN dens $CONTOUR $BOX $BOX $BOX
return
END_OF_TEXT
}

if ( ${$my_main{"MAP_IO_TASK"}}[0] eq "MASK_OUT" )
{
print MACRO << "END_OF_TEXT" ;
make map  $TO_MAP from $FROM_MAP empty init 
make map $TO_MAP atom ( mask  $MASK_RADIUS ) sele $MY_SELECTION end
make map  $TO_MAP set 999990 999999 999
make map $TO_MAP scale -1.0
make map  $TO_MAP set 900 1111 999999
make map  $TO_MAP from $FROM_MAP copy
image $color_text map $TO_MAP label MAP_OUT dens $CONTOUR $BOX $BOX $BOX
return
END_OF_TEXT
}

if ( ${$my_main{"MAP_IO_TASK"}}[0] eq "READ" )
{
    my @xx = split ('\.', $MAP_IO_FILE ) ; 
    my $file_ext = pop (@xx) ;
    if ( lc($file_ext) eq "mrc" ) 
    {	    
#	print MACRO "open unit 10 file $MAP_IO_FILE unform rec 1024\n" ;
	print MACRO "read file $MAP_IO_FILE map mrc init over $TO_MAP \n" ;
	print MACRO "make map $TO_MAP rescale\n" ;
if ( $NEW_GRID > 0 )
{
    print MACRO << "END_OF_TEXT" ;

read refl init MAIN limit -333 -333 0 reso RESOL_MIN RESOL_MAX cryst 1

refl key WORK_REFL sele all end
refl show KEY WORK_REFL

make map init from $TO_MAP real full cell
show map
set vari THE_MAP = MAP_LAST
if ( THE_MAP .le. 0 ) return
make map THE_MAP from $TO_MAP copy
show map THE_MAP 
four map THE_MAP 
dele map THE_MAP 
refl move fcalc fobs
make map $TO_MAP from 0 auto_grid $NEW_GRID init zero real cell 
make map $TO_MAP conv complex
refl move fobs fwork
END_OF_TEXT

if ( $my_main{"MAP_IO_BSHARP"}[0] eq "ON" )
{
    my $BSHARP = ${$my_main{"MAP_IO_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 $TO_MAP sele defined end
four map $TO_MAP back 
make map $TO_MAP rescale 

END_OF_TEXT
}
    }
    else
    {
	print MACRO "read file $MAP_IO_FILE map xplor over $TO_MAP \n"  ;
    }
print MACRO << "END_OF_TEXT" ;
 image $color_text map $TO_MAP label $MAP_LABEL dens $CONTOUR $BOX $BOX $BOX
 return
END_OF_TEXT

}
if ( ${$my_main{"MAP_IO_TASK"}}[0] eq "SAVE_AS" )
{

    my $MAP_FORMAT = "mrc" ;
    if (index($MAP_IO_FILE, ".mrc") != -1) 
    {
	$MAP_FORMAT = "mrc" ;
#	print ">$MAP_IO_FILE< contains mrc\n";
    } 
    if (index($MAP_IO_FILE, ".xmap") != -1) 
    {
	$MAP_FORMAT = "xmap" ;
#	print ">$MAP_IO_FILE< contains xmap\n";
    } 

print MACRO << "END_OF_TEXT" ;
write over file $MAP_IO_FILE map $FROM_MAP $MAP_FORMAT
return
END_OF_TEXT
}

close (MACRO ) ;
exit ;
