#!/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 %new_main = () ;
my %column = () ; 
my @symmetry = () ;
my @words ;
my $record ;
my %column_list ;
my $ICL = 0 ;
my $FILE ;
my $n_crystals = scalar( @{$my_main{"FILE_SYMM"}} ) ;
if ( $n_crystals < 0 ) { $n_crystals = 1 } ;
#####################################################################

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"}}, "ON" )} ;
if ( !exists( $my_main{"REFL_TEST_FRACT"} ) ) { push ( @{$my_main{"REFL_TEST_FRACT"}}, 20 )} ;
my $RAND_FRACT = $my_main{"REFL_TEST_FRACT"}[0] ;

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 =~ m"MTZ_" ) { @{$new_main{$KEY}} = @{$my_main{$KEY}} }
}

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

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 'c' || substr($keyword, 0,7) eq "crystal" ) { goto CRYSTAL }
    if ( $keyword eq 'f' || substr($keyword, 0,4) eq "fobs" ) { goto FOBS }
    if ( $keyword eq 'p' || substr($keyword, 0,6) eq "phase" ) { goto PHASE }
    if ( $keyword eq 's' || substr($keyword, 0,6) eq "sigma" ) { goto SIGMA }
    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:
    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]]
  -c|--crystal) crystal target [$n_crystals]
  -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: @{$my_main{"FILE_MTZ"}} = shift ( @{$commands{$keyword}} ) ; next ;
CRYSTAL: $n_crystals = 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 ;
}

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

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

my $MTZ_FILE = $my_main{"FILE_MTZ"}[0] ;
print "reading mtz file \"$MTZ_FILE\" \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) ) 
{ printf " %20s:   %s \n", @{$column{$KEY}}, $KEY ; } print "\n" ;



foreach $KEY ( sort keys (%new_main) ) 
{
#    print "$KEY: @{$new_main{$KEY}} \n" ;
    if ( $KEY eq "SPACE_GROUP" )
    {
	${$my_main{"FILE_SYMM"}}[$n_crystals-1] = ${$new_main{"SPACE_GROUP"}}[0] ;
#	print "-------------- $KEY: space group @{$new_main{$KEY}} === ", 
#	${$my_main{"FILE_SYMM"}}[$n_crystals-1], " \n" ;
	next ;
    }
    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 ; }
    if ( $KEY eq "RESOLUTION" )
    { 
	${$my_main{"DFR_RES_MIN"}}[$n_crystals-1] = ( $new_main{"RESOLUTION"}[0] ) ;
	${$my_main{"DFR_RES_MAX"}}[$n_crystals-1] = ( $new_main{"RESOLUTION"}[1] ) ;
	next ; 
    }
}

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

$conf_file = "main_config.data" ; 
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"}[$n_crystals-1] ;
if ( $FILE eq "" )
{
$FILE = $my_main{"FILE_CELL"}[0] ;
}
print " $FILE - for cells\n" ;
if ( $n_crystals > 1 && $FILE eq $my_main{"FILE_CELL"}[0] )
{ 
    my @xx = split (".", $FILE ) ;
    my $ext = pop (@xx) ;
    $FILE = join ("", @xx, "_", $n_crystals, ".",  $ext )  ; 
print " $FILE - inside cells\n" ;
    $my_main{"FILE_CELL"}[$n_crystals-1] = $FILE ;
}
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"}[$n_crystals-1], '.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"}[0] eq "ON" ) 
{ read_mtz_file () ; }
else
{ create_mtz2various () ; }

exit ;

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

sub read_mtz_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 ;

$FILE = $my_main{"FILE_REFL"}[0] ;
if ( $n_crystals > 1 && $FILE eq $my_main{"FILE_REFL"}[0] )
{ 
    $FILE = $FILE . "_" . $n_crystals ; 
    $my_main{"FILE_REFL"}[$n_crystals-1] = $FILE ;
}
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 ) 
		 {  
		     $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 == 19 ) { $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, ",4H PHA,f6.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" ;

$FILE = $my_main{"FILE_REFL"}[0] ;
if ( $n_crystals > 1 && $FILE eq $my_main{"FILE_REFL"}[0] )
{ 
    $FILE = $FILE . "_" . $n_crystals ; 
    $my_main{"FILE_REFL"}[$n_crystals-1] = $FILE ;
}
    print TXT << "END_OF_TEXT" ;
#
# mtz_2_main.ccp4
#
mtz2various HKLIN ${$my_main{"FILE_MTZ"}}[0] HKLOUT $FILE << 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 ;
    if ( $vals[1] > 0 ) { $ind = 0 ;} else { $ind = 1 ;} 
#    print $vals[1], " = ", $ind, "\n" ;
    my $chrs = sprintf ( "TEST %4d", $ind) ;
    my $new_line = join('', $vals[0], $chrs ) ;
    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 ) ; 
    }
}
