#!/usr/bin/env perl
use strict;
use lib $ENV{ 'MAIN_CONF'}  ;
use main_config ;
use File::Find ;

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 $n_crystal = $#{$my_main{"FILE_CELL"}} ;
#my $n_crystal = 0 ;
my %new_main = () ;
my %column = () ; 
my @symmetry = () ;
my @words ;
my $record ;
my %column_list ;
my $ICL = 0 ;
my $FILE ;

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

if ( !exists( $my_main{"FILE_MTZ"} ) ) { push ( @{$my_main{"FILE_MTZ"}}, "solve.mtz" )} ;
if ( !exists( $my_main{"FILE_MTZ_NATIVE"} ) ) { push ( @{$my_main{"FILE_MTZ_NATIVE"}}, "USE_FILE" )} ;
if ( !exists( $my_main{"REFL_TEST_FRACT"} ) ) { push ( @{$my_main{"REFL_TEST_FRACT"}}, 20 )} ;
if ( !exists( $my_main{"MTZ_TEST_WAY"} ) ) { push ( @{$my_main{"MTZ_TEST_WAY"}}, "CCP4" )} ;

push ( @{$new_main{"MTZ_FOBS"}}, "" ) ;
push ( @{$new_main{"MTZ_SIGMA"}}, "" ) ;
push ( @{$new_main{"MTZ_PHASE"}}, "" ) ;
push ( @{$new_main{"MTZ_WEIGHT"}}, "" ) ;
push ( @{$new_main{"MTZ_HLA"}}, "" ) ;
push ( @{$new_main{"MTZ_HLB"}}, "" ) ;
push ( @{$new_main{"MTZ_HLC"}}, "" ) ;
push ( @{$new_main{"MTZ_HLD"}}, "" ) ;
push ( @{$new_main{"MTZ_TEST"}}, "" ) ;
push ( @{$new_main{"INIT_MAP"}}, "NONE" ) ;

my $KEY ;
foreach $KEY ( %my_main )
{
#    if ($KEY eq "FILE_MTZ" ) { print "file mtz >$KEY<\n" ; $new_main{$KEY} = $my_main{$KEY} }
    if ($KEY =~  "MTZ" ) { @{$new_main{$KEY}} = @{$my_main{$KEY}} ; }
#    if ($KEY eq "FILE_MTZ_NATIVE" ) { @{$new_main{$KEY}} = (@{$my_main{$KEY}} ) ; }
    if ($KEY eq "FILE_CELL" ) { @{$new_main{$KEY}} = (@{$my_main{$KEY}} ) ; }
    if ($KEY eq "FILE_REFL" ) { @{$new_main{$KEY}} = (@{$my_main{$KEY}} ) ; }
    if ($KEY eq "REFL_TEST_FRACT" ) { @{$new_main{$KEY}} = (@{$my_main{$KEY}} ) ; }
}

#my $MTZ_FILE = ${$new_main{"FILE_MTZ"}}[$n_crystal] ;
#print " top> mtz file >$MTZ_FILE< \n" ;
######################################################

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 'm' || substr($keyword, 0,3) eq "mtz" ) { goto MTZ }
    if ( $keyword eq 'n' || substr($keyword, 0,6) eq "native" ) { goto NATIVE }
    if ( $keyword eq 'f' || substr($keyword, 0,4) eq "fobs" ) { goto FOBS }
    if ( $keyword eq 'p' || substr($keyword, 0,5) eq "phase" ) { goto PHASE }
    if ( $keyword eq 'r' || substr($keyword, 0,8) eq "fraction" ) { goto TEST_FRACT }
    if ( $keyword eq 'w' || substr($keyword, 0,3) eq "way" ) { goto TEST_WAY }
    if ( $keyword eq 's' || substr($keyword, 0,6) eq "sigma" ) { goto SIGMA }
    if ( $keyword eq 'l' || substr($keyword, 0,4) eq "cell" ) { goto CELL }
    if ( $keyword eq 't' || substr($keyword, 0,4) eq "test" ) { goto TEST }
    if ( $keyword eq 'w' || substr($keyword, 0,6) eq "weight" ) { goto WEIGHT }
    if ( $keyword eq 'a' || substr($keyword, 0,3) eq "hla" ) { goto HLA }
    if ( $keyword eq 'b' || substr($keyword, 0,3) eq "hlb" ) { goto HLB }
    if ( $keyword eq 'c' || substr($keyword, 0,3) eq "hlc" ) { goto HLC }
    if ( $keyword eq 'd' || substr($keyword, 0,3) eq "hld" ) { goto HLD }
    if ( substr($keyword, 0,4) eq "doit" ) { $doit = "TRUE" ; next ; }
    print "unknown option >$keyword<\n" ; $PROC_ERROR = "TRUE" ;

HELP:
    my $choice_fobs = join( "/", @{$new_main{"MTZ_FOBS"}} ) ;
    my $choice_sigma = join( "/", @{$new_main{"MTZ_SIGMA"}} ) ;
    my $choice_phase = join( "/", @{$new_main{"MTZ_PHASE"}} ) ;
    my $choice_weight = join( "/", @{$new_main{"MTZ_WEIGHT"}} ) ;
    my $choice_test = join( "/", @{$new_main{"MTZ_TEST"}} ) ;
    my $choice_HLA = join( "/", @{$new_main{"MTZ_HLA"}} ) ;
    my $choice_HLB = join( "/", @{$new_main{"MTZ_HLB"}} ) ;
    my $choice_HLC = join( "/", @{$new_main{"MTZ_HLC"}} ) ;
    my $choice_HLD = join( "/", @{$new_main{"MTZ_HLD"}} ) ;
    print << "END_OF_TEXT" ;
  -h|--help)      prints this message with available options and current status
                  if MTZ labels were not defined before the first found 
                  F, Q, P and W records are taken as FOBS, SIGMA, PHASE and WEIGHT
                  A are matched for HLA, HLB, HLC and HLD keywords 
  -m|--mtz)       mtz input file [$new_main{"FILE_MTZ"}[$n_crystal]]
  -o|--output)    output diffraction file [$new_main{"FILE_REFL"}[$n_crystal]]
  -l|--cell)      cell output file [$new_main{"FILE_CELL"}[$n_crystal]]
  -n|--native)    native mtz file or use mtz2various [USE_FILE/USE_MTZ2VARIOUS] for conversion: [$my_main{"FILE_MTZ_NATIVE"}[$n_crystal]]
  -f|--fobs)      FOBS label [$choice_fobs] in mtz file [$new_main{"MTZ_FOBS"}[0]]
  -s|--sigma)     SIGMA of FOBS label [$choice_sigma] in mtz file [$new_main{"MTZ_SIGMA"}[0]]
  -p|--phase)     PHASE label [$choice_phase] in mtz file [$new_main{"MTZ_PHASE"}[0]]
  -t|--test)      TEST flag label [$choice_test] in mtz file [@{$new_main{"MTZ_TEST"}}]
  -w|--way)       TEST flag way [CPP4/PHENIX/GENERATE] (0-20/0-1) in mtz file [@{$new_main{"MTZ_TEST_WAY"}}]
  -r|--fraction)  fraction for the TEST flag generation [@{$new_main{"REFL_TEST_FRACT"}}]
  -w|--weight)    WEIGHT of PHASE label [$choice_weight] in mtz file [$new_main{"MTZ_WEIGHT"}[0]]
  -a|--hla)       HLA Hendrickson-Latman coeff label [$choice_HLA] in mtz file [$new_main{"MTZ_HLA"}[0]]
  -b|--hlb)       HLB Hendrickson-Latman coeff label [$choice_HLB] in mtz file [$new_main{"MTZ_HLB"}[0]]
  -c|--hlc)       HLC Hendrickson-Latman coeff label [$choice_HLC] in mtz file [$new_main{"MTZ_HLC"}[0]]
  -d|--hld)       HLD Hendrickson-Latman coeff label [$choice_HLD] in mtz file [$new_main{"MTZ_HLD"}[0]]
     --doit)      save extracted data in "main_config.data" 
                  write cell constants and symmetry operators file
                  use "native" create tool or "mtz2various" to extract diffraction data
END_OF_TEXT
    exit ;
# HELP:
#     print << "END_OF_TEXT" ;
#   -h|--help)    prints this message with available options and current status
#                 if MTZ labels were not defined before the first found 
#                 F, Q, P and W records are taken as FOBS, SIGMA, PHASE and WEIGHT
#                 A are matched for HLA, HLB, HLC and HLD keywords 
#   -m|--mtz)     mtz input file [$my_main{"FILE_MTZ"}[0]]
#   -n|--native)  native mtz file [ON] or use mtz2various [OFF] for conversion: [$my_main{"FILE_MTZ_NATIVE"}[0]]
#   -f|--fobs)    FOBS label in mtz file [$new_main{"MTZ_FOBS"}[0]]
#   -s|--sigma)   SIGMA of FOBS label in mtz file [$new_main{"MTZ_SIGMA"}[0]]
#   -p|--phase)   PHASE label in mtz file [$new_main{"MTZ_PHASE"}[0]]
#   -t|--test)    TEST flag label in mtz file [$new_main{"MTZ_TEST"}[0]]
#   -w|--weight)  WEIGHT of PHASE label in mtz file [$new_main{"MTZ_WEIGHT"}[0]]
#   -a|--hla)     HLA Hendrickson-Latman coeff label in mtz file [$new_main{"MTZ_HLA"}[0]]
#   -b|--hlb)     HLB Hendrickson-Latman coeff label in mtz file [$new_main{"MTZ_HLB"}[0]]
#   -c|--hlc)     HLC Hendrickson-Latman coeff label in mtz file [$new_main{"MTZ_HLC"}[0]]
#   -d|--hld)     HLD Hendrickson-Latman coeff label in mtz file [$new_main{"MTZ_HLD"}[0]]
#      --doit)    save extracted data in "main_config.data" 
#                 write cell constants and symmetry operators file
#                 use "native" create tool or "mtz2various" to extract diffraction data
# END_OF_TEXT
#     exit ;
MTZ: $new_main{"FILE_MTZ"}[$n_crystal] = shift ( @{$commands{$keyword}} ) ; next ;
OUTPUT: $new_main{"FILE_REFL"}[$n_crystal] = shift ( @{$commands{$keyword}} ) ; next ;
CELL: ${$new_main{"FILE_CELL"}}[$n_crystal] = shift ( @{$commands{$keyword}} ) ; next ;
NATIVE: ${$new_main{"FILE_MTZ_NATIVE"}}[$n_crystal] = shift ( @{$commands{$keyword}} ) ; next ;
FOBS: @{$new_main{"MTZ_FOBS"}} = shift ( @{$commands{$keyword}} ) ; next ;
SIGMA: @{$new_main{"MTZ_SIGMA"}} = shift ( @{$commands{$keyword}} ) ; next ;
PHASE: @{$new_main{"MTZ_PHASE"}} = shift ( @{$commands{$keyword}} ) ; next ;
TEST: @{$new_main{"MTZ_TEST"}} = shift ( @{$commands{$keyword}} ) ; next ;
TEST_WAY: @{$new_main{"MTZ_TEST_WAY"}} = shift ( @{$commands{$keyword}} ) ; next ;
TEST_FRACT: @{$new_main{"REFL_TEST_FRACT"}} = shift ( @{$commands{$keyword}} ) ; next ;
WEIGHT: @{$new_main{"MTZ_WEIGHT"}} = shift ( @{$commands{$keyword}} ) ; next ;
HLA: @{$new_main{"MTZ_HLA"}} = shift ( @{$commands{$keyword}} ) ; next ;
HLB: @{$new_main{"MTZ_HLB"}} = shift ( @{$commands{$keyword}} ) ; next ;
HLC: @{$new_main{"MTZ_HLC"}} = shift ( @{$commands{$keyword}} ) ; next ;
HLD: @{$new_main{"MTZ_HLD"}} = shift ( @{$commands{$keyword}} ) ; next ;

# MTZ: @{$my_main{"FILE_MTZ"}} = shift ( @{$commands{$keyword}} ) ; next ;
# NATIVE: @{$my_main{"FILE_MTZ_NATIVE"}} = shift ( @{$commands{$keyword}} ) ; next ;
# FOBS: @{$new_main{"MTZ_FOBS"}} = shift ( @{$commands{$keyword}} ) ; next ;
# SIGMA: @{$new_main{"MTZ_SIGMA"}} = shift ( @{$commands{$keyword}} ) ; next ;
# PHASE: @{$new_main{"MTZ_PHASE"}} = shift ( @{$commands{$keyword}} ) ; next ;
# TEST: @{$new_main{"MTZ_TEST"}} = shift ( @{$commands{$keyword}} ) ; next ;
# WEIGHT: @{$new_main{"MTZ_WEIGHT"}} = shift ( @{$commands{$keyword}} ) ; next ;
# HLA: @{$new_main{"MTZ_HLA"}} = shift ( @{$commands{$keyword}} ) ; next ;
# HLB: @{$new_main{"MTZ_HLB"}} = shift ( @{$commands{$keyword}} ) ; next ;
# HLC: @{$new_main{"MTZ_HLC"}} = shift ( @{$commands{$keyword}} ) ; next ;
# HLD: @{$new_main{"MTZ_HLD"}} = shift ( @{$commands{$keyword}} ) ; next ;
}

#my $MTZ_FILE = ${$new_main{"FILE_MTZ"}}[$n_crystal] ;
#print " top> mtz file >$MTZ_FILE< \n" ;

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

#######################################
my $RAND_FRACT = $my_main{"REFL_TEST_FRACT"}[0] ;
my $TEST_WAY = $my_main{"MTZ_TEST_WAY"}[0] ;

my $MTZ_FILE = ${$new_main{"FILE_MTZ"}}[$n_crystal] ;
print "reading mtz file \"$MTZ_FILE\" $n_crystal >>>", join("/", @{$my_main{"FILE_MTZ"}} ) , "<<<\n" ;

open ( MTZ,  "< $MTZ_FILE" ) || die "FILE OPEN ERROR: >$MTZ_FILE< \n" ;
# binmode(MTZ); 

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

# $x = length($record); printf ( "$x \n");

while ( <MTZ>) { $record = $_; } close ( MTZ ) ;


my @base = split ( 'VERS MTZ', $record ) ;

my $text = join ('', 'VERS MTZ', $base[1] ) ;

my @mtz_tail = () ;

my ($I, $N) ; $N = -1 ;
for ( $I = 0 ; $I <= length ($text) ; $I = $I + 80 )
{ 
    $N++; 
    push ( @mtz_tail, substr($text, $I, 79 ) ) ; 
#    printf ( "%5d : %5d : %5d >%s< \n", $I, $I+79, $N, $mtz_tail[$N] );
}

my $line ;
$I = 0 ;
my $out ;
while ( $I < $N )
{
#foreach $line (@mtz_tail ) 
    $line = $mtz_tail[$I] ; $I++ ;
    @words = split (" ", $line ) ;
# CELL
    if ( $line =~ m"^CELL" ) 
    {
	print " $line \n" ;
	my $out = shift ( @words ) ;
	@{$new_main{"CELL_DATA"}} = @words ; 
    }
# RESOLUTION
    if ( $line =~ m"^RESO" ) 
    {
#	print " $line \n" ;
#	print " RESOLUTIONS WORDS: @words \n" ;
	my $out = shift ( @words ) ;
	if ( $words[0] == 0.0 )
	{
	    $new_main{"RESOLUTION"}[0] = 999.0 ;
	}
	else
	{
	    $new_main{"RESOLUTION"}[0] = sprintf ("%7.2f",  sqrt(1. / $words[0]) ) ;
	}
	$new_main{"RESOLUTION"}[1] = sprintf ("%7.2f",  sqrt(1. / $words[1]) ) ;
	$new_main{"RESOLUTION"}[0] =~ s/ //g ;
	$new_main{"RESOLUTION"}[1] =~ s/ //g ;
#	printf " RESOLUTION: @{$new_main{\"RESOLOLUTION\"}} \n",    ;
    }
# symmetry file info
    if ( $line =~ m"^SYMINF" ) 
    {
#	  print " $line \n" ;
	  $out = lc ( $words[5] ) ;
#	  print "MY SPACE GROUP: $out \n" ;
	  my @digits = split ('', $out ) ;
	  if ( $digits[0] eq "\'" ) 
	  {
	  my @dig_breaks = split ('\'', $line ) ;
	  my $dig_out = lc ( $dig_breaks[1] ) ;
	  @digits = split (' ', $dig_out ) ;
	  $out = join ( '_', @digits ) ;
	  $out =~ s/_// ;
#	  $out = join( '.', $out, 'symm' ) ;
#	  print "MY SPACE GROUP: $out - wom work\n" ;

	  }
	  find ( \&find_space_group, $ENV{"MAIN_SYMM"} ) ;
    if ( !exists( $new_main{"SPACE_GROUP"} ) ) 
    { 
	print "%WARNING: the space group $out not found in MAIN_SYMM\n\n" ;
	@{$new_main{"SPACE_GROUP"}} = ( $out ) ; 
    }
#	  my $CELL_DAT = $my_main{"FILE_CELL"}[0] ;
#	  open ( TXT,  "> $CELL_DAT" ) || print "FILE OPEN ERROR: >$CELL_DAT< \n" ;
#	  print TXT "@words \n" ;
#	  close ( TXT ) ;
    }
# column info
    if ( $line =~ m"^COLUMN" ) 
    {
	  $ICL++ ;
#	  push ( @{column_list{$words[1]}}, $ICL );
	  @{$column_list{ $words[1] }} = ( $ICL ) ;
#	  printf ( " %d: %s %d \n", $ICL, $words[1], $column_list{$words[1]} )  ;

	  if ( $words[2] eq "H" ) { next ; } #push ( @{$column{$words[1]}}, $words[2] ) ; }
	  push ( @{$column{$words[1]}}, $words[2] ) ;
	  if ( length( $new_main{"MTZ_FOBS"}[0]) <= 0 && $words[2] eq "F" )
	  { @{$new_main{"MTZ_FOBS"}} = ( $words[1] ) ; }
 	  if ( length( $new_main{"MTZ_SIGMA"}[0]) <= 0 && $words[2] eq "Q" ) 
 	     { @{$new_main{"MTZ_SIGMA"}} = ( $words[1] )} ;
 	  if ( length( $new_main{"MTZ_PHASE"}[0]) <= 0 && $words[2] eq "P" ) 
 	     { 
		 @{$new_main{"MTZ_PHASE"}} = ( $words[1] ) ;
		 @{$new_main{"INIT_MAP"}} = ( "FOBS" ) ;
	     } 
 	  if ( length( $new_main{"MTZ_WEIGHT"}[0]) <= 0 && $words[2] eq "W" ) 
 	     { @{$new_main{"MTZ_WEIGHT"}} = ( $words[1] )} ;
 	  if ( length( $new_main{"MTZ_HLA"}[0]) <= 0 && $words[2] eq "A" && uc($words[1]) =~ m"HLA" ) 
 	     { @{$new_main{"MTZ_HLA"}} = ( $words[1] )} ;
 	  if ( length( $new_main{"MTZ_HLB"}[0]) <= 0 && $words[2] eq "A" && uc($words[1]) =~ m"HLB" ) 
 	     { @{$new_main{"MTZ_HLB"}} = ( $words[1] )} ;
 	  if ( length( $new_main{"MTZ_HLC"}[0]) <= 0 && $words[2] eq "A" && uc($words[1]) =~ m"HLC" ) 
 	     { @{$new_main{"MTZ_HLC"}} = ( $words[1] )} ;
 	  if ( length( $new_main{"MTZ_HLD"}[0]) <= 0 && $words[2] eq "A" && uc($words[1]) =~ m"HLD" ) 
 	     { @{$new_main{"MTZ_HLD"}} = ( $words[1] )} ;
 	  if ( length( $new_main{"MTZ_TEST"}[0]) <= 0 && $words[2] eq "I" && uc($words[1]) =~ m"FRE" ) 
 	     { @{$new_main{"MTZ_TEST"}} = ( $words[1] )} ;
    }
    if ( $line =~ m"^SYMM" ) 
    {
#	  print " $line \n" ;
	  my $out = shift ( @words ) ;
	  my $rest = join ( " ", @words ) ;
	  push ( @symmetry, $rest ) ;
    }

}

foreach $KEY ( sort keys (%new_main) ) 
{
    my $colkey = $new_main{$KEY}[0] ;
    printf " %20s:   @{$new_main{$KEY}} \n", $KEY ;
    if ( exists ($column{$colkey})){ delete $column{$colkey} ; }
}
print "\n\nUNUSED MTZ FILE COLUMNS:\n" ;
foreach $KEY ( sort keys (%column) ) 
{ 
    if (  ${$column{$KEY}}[0] eq "F" ) 
    { 	if ( join ("/",  @{$new_main{"MTZ_FOBS"}}) !~ $KEY )
	{ push ( @{$new_main{"MTZ_FOBS"}},  $KEY ) ; print " f:",  @{$new_main{"MTZ_FOBS"}}, "\n" ;} }

    if (  ${$column{$KEY}}[0] eq "P" ) 
    { 	if (join ("/",  @{$new_main{"MTZ_PHASE"}}) !~ $KEY )
	{ push ( @{$new_main{"MTZ_PHASE"}},  $KEY ) ; print " p:",  @{$new_main{"MTZ_PHASE"}}, "\n" ;} }

    if (  ${$column{$KEY}}[0] eq "Q" ) 
    { 	if ( join ("/",  @{$new_main{"MTZ_SIGMA"}}) !~ $KEY )
	{ push ( @{$new_main{"MTZ_SIGMA"}},  $KEY ) ; print " q:",  @{$new_main{"MTZ_SIGMA"}}, "\n" ;} }

    if (  ${$column{$KEY}}[0] eq "W" ) 
    { 	if ( join ("/",  @{$new_main{"MTZ_WEIGHT"}}) !~ $KEY )
	{ push ( @{$new_main{"MTZ_WEIGHT"}},  $KEY ) ; print " w:",  @{$new_main{"MTZ_WEIGHT"}}, "\n" ;} }

    printf " %20s:   %s \n", @{$column{$KEY}}, $KEY ; 
} print "\n" ;



foreach $KEY ( sort keys (%new_main) ) 
{
    if ( $KEY eq "SPACE_GROUP" )
    {
	@{$my_main{"FILE_SYMM"}} = ( @{$new_main{"SPACE_GROUP"}}) ;
    }
    elsif ( $KEY eq "RESOLUTION" )
    { 
	@{$my_main{"DFR_RES_MIN"}} = ( $new_main{"RESOLUTION"}[0] ) ;
	@{$my_main{"DFR_RES_MAX"}} = ( $new_main{"RESOLUTION"}[1] ) ;
    }
    else
    {
#	print "key> $KEY: @{$new_main{$KEY}} \n" ;
	@{$my_main{$KEY}} = ( @{$new_main{$KEY}}) ; 

# 	if ( $KEY eq "MTZ_FOBS" )
# 	{ @{$my_main{"MTZ_FOBS"}} = ( @{$new_main{"MTZ_FOBS"}} ) ; 	next ; }
# 	if ( $KEY eq "MTZ_SIGMA" )
# 	{ @{$my_main{"MTZ_SIGMA"}} = ( @{$new_main{"MTZ_SIGMA"}} ) ; next ; }
# 	if ( $KEY eq "MTZ_PHASE" )
# 	{ 
# 	    @{$my_main{"MTZ_PHASE"}} = ( @{$new_main{"MTZ_PHASE"}} ) ; 
# 	    @{$my_main{"INIT_MAP"}} = ( @{$new_main{"INIT_MAP"}} ) ; 
# 	    next ; 
# 	}
# 	if ( $KEY eq "MTZ_WEIGHT" )
# 	{ @{$my_main{"MTZ_WEIGHT"}} = ( @{$new_main{"MTZ_WEIGHT"}} ) ; next ; }
# 	if ( $KEY eq "MTZ_HLA" )
# 	{ @{$my_main{"MTZ_HLA"}} = ( @{$new_main{"MTZ_HLA"}} ) ; next ; }
# 	if ( $KEY eq "MTZ_HLB" )
# 	{ @{$my_main{"MTZ_HLB"}} = ( @{$new_main{"MTZ_HLB"}} ) ; next ; }
# 	if ( $KEY eq "MTZ_HLC" )
# 	{ @{$my_main{"MTZ_HLC"}} = ( @{$new_main{"MTZ_HLC"}} ) ; next ; }
# 	if ( $KEY eq "MTZ_HLD" )
# 	{ @{$my_main{"MTZ_HLD"}} = ( @{$new_main{"MTZ_HLD"}} ) ; next ; }
# 	if ( $KEY eq "MTZ_TEST" )
# 	{ @{$my_main{"MTZ_TEST"}} = ( @{$new_main{"MTZ_TEST"}} ) ; next ; }
# 	print "failed transfer $KEY: @{$new_main{$KEY}} \n" ;
    }
}

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

$conf_file = "main_config.data" ; 

# foreach $KEY ( keys %new_main )
# { 
#     print " new main >$KEY< \n" ;
#     if ( defined($new_main{$KEY}) ) { @{$new_main{$KEY}} = @{$my_main{$KEY}} } 
# }
#print "main>", $my_main{"FILE_MTZ"}[$n_crystal], " -$n_crystal- ", $new_main{"FILE_MTZ"}[$n_crystal], "<\n" ;

#print $#{$my_main{"FILE_MTZ"}}, " --- ", $#{$new_main{"FILE_MTZ"}}, ${$new_main{"FILE_MTZ"}}[0], ${$new_main{"FILE_MTZ"}}[0],":\n" ;

main_config::write_main_config_file ( $conf_file, %my_main ) ; 

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

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

# print " DOOOOOOOOOOOOOOOOOOOOOOOOOOO IIIIIIIIITTTT\n" ;

# cell data

$KEY =  "CELL_DATA" ;
$FILE = $my_main{"FILE_CELL"}[0] ;
print "creating \"$FILE\"\n" ;
open ( TXT,  "> $FILE" ) || print "FILE OPEN ERROR: >$FILE< \n" ;
printf TXT "%9.3f %9.3f %9.3f %8.2f %8.2f %8.2f\n", 
    @{$new_main{"CELL_DATA"}} ;
close ( TXT ) ;

if ( scalar (@symmetry) > 0 ) 
{
    $FILE = join ( '', $my_main{"FILE_SYMM"}[0], '.symm' ) ;
    print "creating \"$FILE\"\n" ;
    open ( TXT,  "> $FILE" ) || print "FILE OPEN ERROR: >$FILE< \n" ;
    my $ll ;
    foreach $ll ( @symmetry ) 
    {
	print TXT $ll, "\n" ;
    }
    close ( TXT ) ;
}

if ( $my_main{"FILE_MTZ_NATIVE"}[$n_crystal] eq "USE_FILE" ) 
{ 
    read_mtz_file () ; 
    main_config::write_main_config_file ( $conf_file, %my_main ) ; 
}
else
{ create_mtz2various () ; }

exit ;

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

sub read_mtz_file
{

    my @out_file ;
open ( MTZ,  "< $MTZ_FILE" ) || die "FILE OPEN ERROR: >$MTZ_FILE< \n" ;
my @x ; my $y ; my $y2 ;

binmode(MTZ); 
read(MTZ, $record, 4);  printf ( "$record \n") ; 
read(MTZ, $record, 8); $y = unpack("a8", $record);   printf ( "$y \n") ; 
#read(MTZ, $record, 4); $y = unpack("H8", $record);   printf ( "$y \n") ; 
#read(MTZ, $record, 72); $y2 = unpack("H144", $record);  print ( $y,  " x ", $y2, "\n" ) ; print ("\n") ; 
read(MTZ, $record, 68) ;

my $float_pack = join( '', "f", $ICL ) ;
my $hex_pack = join( '', "h", 16*$ICL ) ;
#print ( "float_pack: $float_pack $ICL \n" ) ;
#    print ( "FLOAT PACK : $float_pack ICAL: $ICL \n" ) ;
#$float_pack = "h40" ;
my $data_val ; my $data_list ; my $data_form ;
my $data_lab ; my $chrs ;
my $ICL2 ; my $sig ;
my $nn=0; my $NO = 0; my $NAN=0 ;

my $free1 = 0 ;
my $free2 = 0 ;
my $rh_min = 99999 ;
my $rk_min = 99999 ;
my $rl_min = 99999 ;
$sig = 0 ;
while ( read( MTZ, $record, 4*$ICL ) && $sig == 0 ) 
{ 
    my @out_list ; my @out_form ;
#    @x = unpack("f25", $record); 
    $chrs = unpack("a8", $record ) ; if ( $chrs eq "VERS MTZ" ) { $sig = 1 ; printf "SIG EXIT\n"; next ; } 
#    print ( "FLOAT PACK : $float_pack \n" ) ;
    @x = unpack( $float_pack, $record); 
#    print( "@x \n") ;
#    printf ( "%5d %5d %5d %8.2f %8.2f %8.2f %8.2f %8.2f %8.2f %8.2f\n", @x);
#    my @iy = unpack( $hex_pack, $record); 
#    print( "Y:", @iy, "\n") ;
#    my $fobs_lab = $my_main{"MTZ_FOBS"}[0] ;

#    if ( $nn > 2 ) { exit; }
    $chrs = sprintf ( "IND %4d %4d %4d", $x[0], $x[1], $x[2] ) ; 
	      push( @out_form, $chrs ) ;
    if ( $x[0] < $rh_min ) { $rh_min = $x[0] } ;
    if ( $x[1] < $rk_min ) { $rk_min = $x[1] } ;
    if ( $x[2] < $rl_min ) { $rl_min = $x[2] } ;

    $nn++ ;
    if ( length( $new_main{"MTZ_FOBS"}[0]) >= 0 ) 
    {
	$data_lab = $my_main{"MTZ_FOBS"}[0] ;
	if ( exists ($column_list{$data_lab}) )
	     {
		 $ICL2 = $column_list{$data_lab}[0] ; 
		 $data_val = $x[ $ICL2 - 1 ] ;
#		 if ( $data_val < 999999 && $data_val >= 0.01 ) 
		 if ( $data_val < 999999 ) 
		 {  
		     $chrs = sprintf ( "FOB %8.2f", $data_val) ;
		     push( @out_form, $chrs ) ; 
		 }
	     }
	else { $NO++; next ; }
    }
    if ( length( $new_main{"MTZ_SIGMA"}[0]) >= 0 ) 
    {
	$data_lab = $my_main{"MTZ_SIGMA"}[0] ;
	if ( exists ($column_list{$data_lab}) )
	     {
		 $ICL2 = $column_list{$data_lab}[0] ; 
		 $data_val = $x[ $ICL2 - 1 ] ;
		 $chrs = sprintf ( "SIG %7.2f", $data_val) ;
		 push( @out_form, $chrs ) ; 
	     }
    }
    if ( length( $new_main{"MTZ_PHASE"}[0]) >= 0 ) 
    {
	$data_lab = $my_main{"MTZ_PHASE"}[0] ;
	if ( exists ($column_list{$data_lab}) )
	     {
		 $ICL2 = $column_list{$data_lab}[0] ; 
		 $data_val = $x[ $ICL2 - 1 ] ;
		 $chrs = sprintf ( "PHA %6.1f", $data_val) ;
		 push( @out_form, $chrs ) ; 
	     }
    }
    if ( length( $new_main{"MTZ_WEIGHT"}[0]) >= 0 ) 
    {
	$data_lab = $my_main{"MTZ_WEIGHT"}[0] ;
	if ( exists ($column_list{$data_lab}) )
	     {
		 $ICL2 = $column_list{$data_lab}[0] ; 
		 $data_val = $x[ $ICL2 - 1 ] ;
		 $chrs = sprintf ( "WE %6.3f", $data_val) ;
		 push( @out_form, $chrs ) ; 
	     }
    }
    if ( length( $new_main{"MTZ_TEST"}[0]) >= 0 ) 
    {
	$data_lab = $my_main{"MTZ_TEST"}[0] ;
	$data_val = -999 ;
	if ( exists ($column_list{$data_lab}) )
	{
	    $ICL2 = $column_list{$data_lab}[0] ; 
	    $data_val = $x[ $ICL2 - 1 ] ;
	    if ( $TEST_WAY eq "CCP4" )
	    { if ( $data_val == 0 ) { $data_val=1; } else {$data_val=0; } }
#	    print " first free $data_lab .. $x[0], $x[1], $x[2] --> $data_val \n" ;
	}
# # 	if ( $data_val <= 0 && defined($my_main{"MTZ_TEST"}[1] ) )
# # 	{
# # 	    $data_lab = $my_main{"MTZ_TEST"}[1] ;
# # #	    print " secnd free $data_lab $x[0], $x[1], $x[2] --> $data_val \n" ;
# # 	    if ( exists ($column_list{$data_lab}) )
# # 	    {
# # 		$ICL2 = $column_list{$data_lab}[0] ; 
# # #		$data_val = $x[ $ICL2 - 1 ] ;
# # #		if ( $data_val == 0 ) { $data_val=1; } else {$data_val=0; }
# # #		if ( $data_val > 0 ) { $free2++ ; }
# # 	    }
# # 	}
	if ( $data_val == -999 ||  "TEST_WAY" eq "GENERATE" )
	{
	    $data_val = int (rand $RAND_FRACT) ;
	    if ( $data_val != 1 ) { $data_val = 0 ; }
#	    print " test generator $data_val \n" ;
	}
	
	if ( $data_val > 0 ) { $free1++ ; }
	$chrs = sprintf ( "TEST %d", $data_val) ;
	push( @out_form, $chrs ) ; 
    }
    else
    {
	$data_val = int (rand $RAND_FRACT) ;
	if ( $data_val != 1 ) { $data_val = 0 ; }
	$chrs = sprintf ( "TEST %d", $data_val) ;
	push( @out_form, $chrs ) ; 
    }
    $data_form = join( " ", @out_form, "\n" ) ;
    if ( lc($data_form) =~ "nan" ) 
    {
	$NAN++ ;
	if ( $NAN < 10 ) { print ( $NAN , "> ", $data_form  ) ; }
    } 
    else
    { push ( @out_file, $data_form ) }
#    printf ( " debig_line: $fobs_lab $ICL $fobs \n" ) ;
#	if ( length( $new_main{"MTZ_PHASE"}[0]) >= 0 ) { } ;
#	printf ("INDEX %5d %5d %5d FOBS %8.2f SIG %8.2f \n", $x[0], $x[1], $x[2], $fobs, $sig ) ; 
}
# check to inverse the TEST and WORK
if ( $nn - $free1 < $free1 )
{
    my @in_text = @out_file ;
    @out_file = () ;
    my $string ;
    foreach $data_form ( @in_text )
    {
	$string = $data_form ;
	if ( $string =~ m/TEST 0/ )
	{ $string =~ s/TEST 1/TEST 0/; }
	else
	{ $string =~ s/TEST 1/TEST 0/; }
	push ( @out_file, $string ) ;
    }
}


close ( MTZ ) ;

$FILE = $my_main{"FILE_REFL"}[0] ;
open ( TXT,  "> $FILE" ) || print "FILE OPEN ERROR: >$FILE< \n" ;
foreach $data_form ( @out_file )
{
    printf ( TXT $data_form  ) ; 
}
close ( TXT ) ;

printf ( " NN processed: $nn  failed: $NO undefined (not extracted) : $NAN TESTs $free1 $free2\n" ) ;
@{$my_main{"DFR_LIMIT_H"}} = ($rh_min) ;
@{$my_main{"DFR_LIMIT_K"}} = ($rk_min) ;
@{$my_main{"DFR_LIMIT_L"}} = ($rl_min) ;
print " use_mtz.pl> done: HKL limits $rh_min, $rk_min, $rk_min :: resolution range ", $my_main{"DFR_RES_MIN"}[0], " - ", $my_main{"DFR_RES_MAX"}[0], "\n" ;

}

sub read_mtz_file_x
{

open ( MTZ,  "< $MTZ_FILE" ) || die "FILE OPEN ERROR: >$MTZ_FILE< \n" ;
my @x ; my $y ; my $y2 ;
binmode(MTZ); 
read(MTZ, $record, 4);  printf ( "$record \n") ; 
read(MTZ, $record, 8); $y = unpack("a8", $record);   printf ( "$y \n") ; 
#read(MTZ, $record, 4); $y = unpack("H8", $record);   printf ( "$y \n") ; 
#read(MTZ, $record, 72); $y2 = unpack("H144", $record);  print ( $y,  " x ", $y2, "\n" ) ; print ("\n") ; 
read(MTZ, $record, 68) ;

my $float_pack = join( '', "f", $ICL ) ;
my $hex_pack = join( '', "h", 16*$ICL ) ;
#print ( "float_pack: $float_pack $ICL \n" ) ;
#    print ( "FLOAT PACK : $float_pack ICAL: $ICL \n" ) ;
#$float_pack = "h40" ;
my $data_val ; my $data_list ; my $data_form ;
my $data_lab ; my $chrs ;
my $ICL2 ; my $sig ;
my $nn=0; my $NO = 0; my $NAN=0 ;

$FILE = $my_main{"FILE_REFL"}[0] ;
open ( TXT,  "> $FILE" ) || print "FILE OPEN ERROR: >$FILE< \n" ;
$sig = 0 ;
while ( read( MTZ, $record, 4*$ICL ) && $sig == 0 ) 
{ 
    my @out_list ; my @out_form ;
#    @x = unpack("f25", $record); 
    $chrs = unpack("a8", $record ) ; if ( $chrs eq "VERS MTZ" ) { $sig = 1 ; printf "SIG EXIT\n"; next ; } 
#    print ( "FLOAT PACK : $float_pack \n" ) ;
    @x = unpack( $float_pack, $record); 
#    print( "@x \n") ;
#    printf ( "%5d %5d %5d %8.2f %8.2f %8.2f %8.2f %8.2f %8.2f %8.2f\n", @x);
#    my @iy = unpack( $hex_pack, $record); 
#    print( "Y:", @iy, "\n") ;
#    my $fobs_lab = $my_main{"MTZ_FOBS"}[0] ;

#    if ( $nn > 2 ) { exit; }
    $chrs = sprintf ( "IND %4d %4d %4d", $x[0], $x[1], $x[2] ) ; 
	      push( @out_form, $chrs ) ;

    $nn++ ;
    if ( length( $new_main{"MTZ_FOBS"}[0]) >= 0 ) 
    {
	$data_lab = $my_main{"MTZ_FOBS"}[0] ;
	if ( exists ($column_list{$data_lab}) )
	     {
		 $ICL2 = $column_list{$data_lab}[0] ; 
		 $data_val = $x[ $ICL2 - 1 ] ;
#		 if ( $data_val < 999999 && $data_val >= 0.01 ) 
		 if ( $data_val < 999999 ) 
		 {  
		     $chrs = sprintf ( "FOB %8.2f", $data_val) ;
		     push( @out_form, $chrs ) ; 
		 }
	     }
	else { $NO++; next ; }
    }
    if ( length( $new_main{"MTZ_SIGMA"}[0]) >= 0 ) 
    {
	$data_lab = $my_main{"MTZ_SIGMA"}[0] ;
	if ( exists ($column_list{$data_lab}) )
	     {
		 $ICL2 = $column_list{$data_lab}[0] ; 
		 $data_val = $x[ $ICL2 - 1 ] ;
		 $chrs = sprintf ( "SIG %7.2f", $data_val) ;
		 push( @out_form, $chrs ) ; 
	     }
    }
    if ( length( $new_main{"MTZ_PHASE"}[0]) >= 0 ) 
    {
	$data_lab = $my_main{"MTZ_PHASE"}[0] ;
	if ( exists ($column_list{$data_lab}) )
	     {
		 $ICL2 = $column_list{$data_lab}[0] ; 
		 $data_val = $x[ $ICL2 - 1 ] ;
		 $chrs = sprintf ( "PHA %6.1f", $data_val) ;
		 push( @out_form, $chrs ) ; 
	     }
    }
    if ( length( $new_main{"MTZ_WEIGHT"}[0]) >= 0 ) 
    {
	$data_lab = $my_main{"MTZ_WEIGHT"}[0] ;
	if ( exists ($column_list{$data_lab}) )
	     {
		 $ICL2 = $column_list{$data_lab}[0] ; 
		 $data_val = $x[ $ICL2 - 1 ] ;
		 $chrs = sprintf ( "WE %6.3f", $data_val) ;
		 push( @out_form, $chrs ) ; 
	     }
    }
    if ( length( $new_main{"MTZ_TEST"}[0]) >= 0 ) 
    {
	$data_lab = $my_main{"MTZ_TEST"}[0] ;
	if ( exists ($column_list{$data_lab}) )
	{
	    $ICL2 = $column_list{$data_lab}[0] ; 
	    $data_val = $x[ $ICL2 - 1 ] ;
	    if ( $data_val == 0 ) { $data_val=1; } else {$data_val=0; }
	}
	else
	{
	    $data_val = int (rand $RAND_FRACT) ;
	    if ( $data_val != 1 ) { $data_val = 0 ; }
	}
	$chrs = sprintf ( " TEST %d", $data_val) ;
	push( @out_form, $chrs ) ; 
    }
    else
    {
	$data_val = int (rand $RAND_FRACT) ;
	if ( $data_val != 1 ) { $data_val = 0 ; }
	$chrs = sprintf ( " TEST %d", $data_val) ;
	push( @out_form, $chrs ) ; 
    }
    $data_form = join( " ", @out_form, "\n" ) ;
    if ( lc($data_form) =~ "nan" ) 
    {
	$NAN++ ;
	if ( $NAN < 10 ) { print ( $NAN , "> ", $data_form  ) ; }
    } 
    else
    { printf ( TXT $data_form  ) ; }
#    printf ( " debig_line: $fobs_lab $ICL $fobs \n" ) ;
#	if ( length( $new_main{"MTZ_PHASE"}[0]) >= 0 ) { } ;
#	printf ("INDEX %5d %5d %5d FOBS %8.2f SIG %8.2f \n", $x[0], $x[1], $x[2], $fobs, $sig ) ; 

} 
printf ( " NN processed: $nn  failed: $NO undefined (not extracted) : $NAN \n" ) ;
close ( MTZ ) ;
close ( TXT ) ;
}

sub create_mtz2various
{
# create the main_2_mtz files
    my @out_form = () ; my @out_lab = () ;
    my $user_form ; my $user_lab ; my $data_lab ;

    push( @out_form, "( 2HIN,3i5" ) ;


    if ( length( $new_main{"MTZ_FOBS"}[0]) >= 0 ) 
    {
	$data_lab = $my_main{"MTZ_FOBS"}[0] ;
	if ( exists ($column_list{$data_lab}) )
	     {
		 push( @out_form, ",3H FO,f11.2" ) ;
		 push( @out_lab, "FP=" ) ;
		 push( @out_lab, $data_lab ) ;
		 push( @out_lab, " " ) ;
	     }
    }
    if ( length( $new_main{"MTZ_SIGMA"}[0]) >= 0 ) 
    {
	$data_lab = $my_main{"MTZ_SIGMA"}[0] ;
	if ( exists ($column_list{$data_lab}) )
	     {
		 push( @out_form, ",3H SI,f7.2" ) ;
		 push( @out_lab, "SIGFP=" ) ;
		 push( @out_lab, $data_lab ) ;
		 push( @out_lab, " " ) ;
	     }
    }
    if ( length( $new_main{"MTZ_PHASE"}[0]) >= 0 ) 
    {
	$data_lab = $my_main{"MTZ_PHASE"}[0] ;
	if ( exists ($column_list{$data_lab}) )
	     {
		 push( @out_form, ",3H PH,f7.1" ) ;
		 push( @out_lab, "PHIB=" ) ;
		 push( @out_lab, $data_lab ) ;
		 push( @out_lab, " " ) ;
	     }
    }
    if ( length( $new_main{"MTZ_WEIGHT"}[0]) >= 0 ) 
    {
	$data_lab = $my_main{"MTZ_WEIGHT"}[0] ;
	if ( exists ($column_list{$data_lab}) )
	     {
		 push( @out_form, ",3H WE,f6.3" ) ;
		 push( @out_lab, "W=" ) ;
		 push( @out_lab, $data_lab ) ;
		 push( @out_lab, " " ) ;
	     }
    }
    if ( length( $new_main{"MTZ_TEST"}[0]) >= 0 ) 
    {
	$data_lab = $my_main{"MTZ_TEST"}[0] ;
	if ( exists ($column_list{$data_lab}) )
	     {
		 push( @out_form, ",5H TEST,f4.0" ) ;
		 push( @out_lab, "FREE=" ) ;
		 push( @out_lab, $data_lab ) ;
		 push( @out_lab, " " ) ;
	     }
    }
		 push( @out_form, ")" ) ;



    $FILE = "mtz_2_main.com" ;
    print "creating \"$FILE\"\n" ;
    $user_form= join("", @out_form) ;
    $user_lab= join("", @out_lab) ;
    open ( TXT,  "> $FILE" ) || print "FILE OPEN ERROR: >$FILE< \n" ;

    print TXT << "END_OF_TEXT" ;
#
# mtz_2_main.ccp4
#
mtz2various HKLIN ${$my_main{"FILE_MTZ"}}[0] HKLOUT ${$my_main{"FILE_REFL"}}[0] << EOF
RESOLUTION $my_main{"DFR_RES_MIN"}[0] $my_main{"DFR_RES_MAX"}[0]
EXCLUDE SIGP 1.0
OUTPUT USER $user_form 
LABIN $user_lab
END
EOF

END_OF_TEXT
close ( TXT ) ;


    print "executing \"$FILE\" > $FILE.log\n" ;
    system ( "chmod +x $FILE ; ./$FILE > $FILE.log" ) ;

my $D_FILE = ${$my_main{"FILE_REFL"}}[0];
open ( TXT,  "< $D_FILE" ) || print "FILE OPEN ERROR: >$D_FILE< \n" ;

my @my_data = () ;
my $line ;
 
while(<TXT>)
{
    chomp ;
    $line = $_ ;
    my @vals = split ('TEST', $line );
    my $ind ;
    my $new_line ;
    if ( $#vals > 1 ) 
    {

	if ( $vals[1] > 0 ) { $ind = 0 ;} else { $ind = 1 ;} 
#    print $vals[1], " = ", $ind, "\n" ;
	my $chrs = sprintf ( " TEST %4d", $ind) ;
	$new_line = join('', $vals[0], $chrs ) ;
    }
    else
    {
	$new_line = $vals[0] ;
    }
    push( @my_data, $new_line ) ;
}
close ( TXT ) ;

#$D_FILE = "x.dat" ;
open ( TXT,  "> $D_FILE" ) || print "FILE OPEN ERROR: >$D_FILE< \n" ;
 
 foreach $line ( @my_data ) 
 {
     print TXT $line, "\n" ;
 }
 close (TXT) ;
} 

sub find_space_group
{
    my $found = "NO" ;

    if ( $File::Find::name =~ m"symm" ) 
    { 
	my $XX = $File::Find::name ; # $XX =~ s/_//g ;
#	    print "file: $XX and $out \n" ; 
	if ( $XX =~ m"$out" ) 
	{
	    $found = "YES" ;
	    my @Y = split ('/', $File::Find::name ) ;
	    my $GROUP = $Y[scalar(@Y)-1] ;
	    substr( $GROUP, -5 ) = '' ;
#	    print "comparison: $out $GROUP $File::Find::name \n" ;
	    if ( $GROUP eq $out )
	    {
#		print "SUCESS: $out $GROUP $File::Find::name \n" ;
		@{$new_main{"SPACE_GROUP"}} = ( $GROUP ) ;
	    }
	}
    }
    if ( $found eq "NO" ) 
    { 
#	print "%WARNING: the space group $out not found in MAIN_SYMM\n\n" ;
	@{$new_main{"SPACE_GROUP"}} = ( $out ) ; 
    }
}
