#!/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 $loop_count = 0 ;
my $fl_fobs = "NOT" ;
my $fl_sig = "NOT" ;

my $fl_iobs = "NOT" ;
my $fl_intsig = "NOT" ;
my $FOB_check ;
my $IOB_check ;
my $Hind_check ;
my $Kind_check ;
my $Lind_check ;



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

if ( !exists( $my_main{"FILE_SFCIF"} ) ) { push ( @{$my_main{"FILE_SFCIF"}}, "sf.cif" )} ;
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{"CIF_FOBS"}}, "" ) ;
push ( @{$new_main{"CIF_SIGMA"}}, "" ) ;
push ( @{$new_main{"CIF_PHASE"}}, "" ) ;
push ( @{$new_main{"CIF_WEIGHT"}}, "" ) ;
push ( @{$new_main{"CIF_TEST"}}, "" ) ;
push ( @{$new_main{"CIF_IND_H"}}, "" ) ;
push ( @{$new_main{"CIF_IND_K"}}, "" ) ;
push ( @{$new_main{"CIF_IND_L"}}, "" ) ;
push ( @{$new_main{"CIF_FCALC"}}, "" ) ;
push ( @{$new_main{"CIF_FCALC_PHASE"}}, "" ) ;
push ( @{$new_main{"INIT_MAP"}}, "NONE" ) ;
push ( @{$new_main{"CIF_IOBS"}}, "" ) ;
push ( @{$new_main{"CIF_ISIG"}}, "" ) ;


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 'c' || substr($keyword, 0,8) eq "cif_file" ) { goto CIF_FILE }
    if ( $keyword eq 'f' || substr($keyword, 0,4) eq "fobs" ) { goto FOBS }
    if ( $keyword eq 'l' || substr($keyword, 0,5) eq "fcalc" ) { goto FCALC }
    if ( $keyword eq 'm' || substr($keyword, 0,8) eq "fc_phase" ) { goto FC_PHASE }
    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 ( 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 SF 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 
  -c|--cif_file)  SF CIF input file [$my_main{"FILE_SFCIF"}[0]]
  -f|--fobs)      FOBS label in cif file [$new_main{"CIF_FOBS"}[0]]
  -s|--sigma)     SIGMA of FOBS label in mtz file [$new_main{"CIF_SIGMA"}[0]]
  -p|--phase)     PHASE label in cif file [$new_main{"CIF_PHASE"}[0]]
  -l|--fcalc)     FCALC label in cif file [$new_main{"CIF_FCALC"}[0]]
  -m|--fc_phase)  FCALC PHASE label in cif file [$new_main{"CIF_FCALC_PHASE"}[0]]
  -t|--test)      TEST flag label in mtz file [$new_main{"CIF_TEST"}[0]]
  -w|--weight)    WEIGHT of PHASE label in mtz file [$new_main{"CIF_WEIGHT"}[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 ;
CIF_FILE: @{$my_main{"FILE_SFCIF"}} = shift ( @{$commands{$keyword}} ) ; next ;
FOBS: @{$new_main{"CIF_FOBS"}} = shift ( @{$commands{$keyword}} ) ; next ;
FCALC: @{$new_main{"CIF_FCALC"}} = shift ( @{$commands{$keyword}} ) ; next ;
FC_PHASE: @{$new_main{"CIF_FCALC_PHASE"}} = shift ( @{$commands{$keyword}} ) ; next ;
SIGMA: @{$new_main{"CIF_SIGMA"}} = shift ( @{$commands{$keyword}} ) ; next ;
PHASE: @{$new_main{"CIF_PHASE"}} = shift ( @{$commands{$keyword}} ) ; next ;
TEST: @{$new_main{"CIF_TEST"}} = shift ( @{$commands{$keyword}} ) ; next ;
WEIGHT: @{$new_main{"CIF_WEIGHT"}} = shift ( @{$commands{$keyword}} ) ; next ;
}

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

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

my $CIF_FILE = $my_main{"FILE_SFCIF"}[0] ;
print "reading sf cif file \"$CIF_FILE\" \n" ;

my @sf_lines = () ;

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

while ( <SFC>) 
{ $record = $_; 
  chomp $record ; 
  push( @sf_lines, 
	$record ) ; 
} close ( SFC ) ;

#my $I ;
#for ($I=0; $I<40; $I++) { print $I, "> ", $sf_lines[$I], "\n" ; }
my @sf_head = () ;
my $nn = 0; 
my $line_beg = -1 ;
my $line_end = -1 ;
my $IOK = 1 ;
$ICL = 0 ;
%column = () ;
%column_list = () ;
foreach $record ( @sf_lines )
{
#    my @chrs = () ; 
#    @chrs = split ( '', $record ) ;
#    @words = split ( ' ', $record ) ;
    $nn++;

    if( ( substr($record, 0, 6 ) eq "_refln" ) || ( substr($record, 0, 6 ) eq " _refl" ) )
    {
	
	push ( @sf_head, $record ) ; 
	
#	print $nn, " ============== record:", $record, "\n" ;
	@words = split ( '\.', $record ) ;
#	print scalar(@words), " words:", @words, "\n" ;
#	print $words[0];
	if ( $words[0] eq "_refln" ||  $words[0] eq "_reflns" ||  $words[0] eq " _refln"  || $words[0] eq " _reflns") 
	{
	    $line_beg = $nn ; $IOK = 0 ;
	    $ICL++ ;
	    $column{$words[1]} = $ICL ;
	    $column_list{$words[1]} = $ICL ;
	    print $nn, " column: $ICL name: $words[1] \n" ;
	    
#	    if ( length( $new_main{"CIF_IND_H"}[0]) <= 0 && lc($words[1]) eq "index_h" ) 
	    if ( length( $new_main{"CIF_IND_H"}[0]) <= 0 && $words[1] =~ "index_h" ) 
	    { @{$new_main{"CIF_IND_H"}} = ( $words[1] ) } ;

#           preveri ali je tam white space!!!??? ZZZ
	    if ( length( $new_main{"CIF_IND_K"}[0]) <= 0 && $words[1] =~ "index_k" ) 
	    { @{$new_main{"CIF_IND_K"}} = ( $words[1] )} ;
	    if ( length( $new_main{"CIF_IND_L"}[0]) <= 0 && $words[1] =~ "index_l" ) 
	    { @{$new_main{"CIF_IND_L"}} = ( $words[1] )} ;
	    if ( length( $new_main{"CIF_FOBS"}[0]) <= 0 
		 && substr($words[1], 0,6) eq "F_meas" && $words[1] !~ m"sigma")
	    { @{$new_main{"CIF_FOBS"}} = ( $words[1] ) ; $fl_fobs = $loop_count ; }
	    if ( length( $new_main{"CIF_SIGMA"}[0]) <= 0 
		 && substr($words[1], 0,6) eq "F_meas" && $words[1] =~ m"sigma")
	    { @{$new_main{"CIF_SIGMA"}} = ( $words[1] ) ; $fl_sig = $loop_count ;}
	    
# preference for structure factors
	    if ( length( $new_main{"CIF_FOBS"}[0]) <= 0 
		 && substr($words[1], 0,6) eq "intens" && $words[1] !~ m"sigma")
	    { @{$new_main{"CIF_IOBS"}} = ( $words[1] ) ; $fl_iobs = $loop_count ; }
	    if ( length( $new_main{"CIF_SIGMA"}[0]) <= 0 
		 && substr($words[1], 0,6) eq "intens" && $words[1] =~ m"sigma")
	    { @{$new_main{"CIF_ISIG"}} = ( $words[1] ) ; $fl_intsig = $loop_count ;}


	    if ( length( $new_main{"CIF_FCALC"}[0]) <= 0 
		 && substr($words[1], 0,1) eq "F" && $words[1] =~ m"calc")
	    { @{$new_main{"CIF_FCALC"}} = ( $words[1] ) ; }

	    if ( length( $new_main{"CIF_FCALC_PHASE"}[0]) <= 0 
		 && substr($words[1], 0,5) eq "phase" && $words[1] =~ m"calc")
	    { @{$new_main{"CIF_FCALC_PHASE"}} = ( $words[1] ) ; }
	    
	    if ( length( $new_main{"CIF_TEST"}[0]) <= 0 && lc($words[1]) =~ m"test" ) 
	    { @{$new_main{"CIF_TEST"}} = ( $words[1] )} ;
	}
    }
    elsif ( substr($record, 0, 4 ) eq "#END" ) 
    {
	print " END: last $nn \n" ;
	$line_end = $nn ;
	last ;
    }
    elsif ( substr($record, 0, 1 ) eq "#" ) 
    {
	$IOK = 1 ;
#	$ICL = 0 ;
#	%column_list = () ;
    }
    elsif ( substr($record, 0, 5 ) eq "loop_" ) 
    {
	print $nn, "   ---- loop --- \n" ;
	$IOK = 1 ;
	$loop_count++ ;
#uncomment!!
	$ICL = 0 ;
#	%column_list = () ;
    }
    else
    {
	if ( $IOK == 0 ) { $line_end = $nn } ;
    }
}

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

foreach $KEY ( sort keys (%new_main) ) 
{
   # print "$KEY: @{$new_main{$KEY}} \n" ;
    # key-i so ze vneseni, ce obstajajo
    if ( $KEY eq "SPACE_GROUP" )
    {
	@{$my_main{"FILE_SYMM"}} = @{$new_main{"SPACE_GROUP"}} ;
	next ;
    }
    if ( $KEY eq "CIF_IND_H" )
    { @{$my_main{"CIF_IND_H"}} = ( @{$new_main{"CIF_IND_H"}} ) ; 	next ; }
    if ( $KEY eq "CIF_IND_K" )
    { @{$my_main{"CIF_IND_K"}} = ( @{$new_main{"CIF_IND_K"}} ) ; 	next ; }
    if ( $KEY eq "CIF_IND_L" )
    { @{$my_main{"CIF_IND_L"}} = ( @{$new_main{"CIF_IND_L"}} ) ; 	next ; }

    if ( $KEY eq "CIF_FOBS" )
    { @{$my_main{"CIF_FOBS"}} = ( @{$new_main{"CIF_FOBS"}} ) ; 	next ; }
    if ( $KEY eq "CIF_SIGMA" )
    { @{$my_main{"CIF_SIGMA"}} = ( @{$new_main{"CIF_SIGMA"}} ) ; next ; }

    if ( $KEY eq "CIF_IOBS" )
    { @{$my_main{"CIF_IOBS"}} = ( @{$new_main{"CIF_IOBS"}} ) ; 	next ; }
    if ( $KEY eq "CIF_ISIG" )
    { @{$my_main{"CIF_ISIG"}} = ( @{$new_main{"CIF_ISIG"}} ) ; next ; }



    if ( $KEY eq "CIF_PHASE" )
    { 
	@{$my_main{"CIF_PHASE"}} = ( @{$new_main{"CIF_PHASE"}} ) ; 
	@{$my_main{"INIT_MAP"}} = ( @{$new_main{"INIT_MAP"}} ) ; 
	next ; 
    }

    if ( $KEY eq "CIF_FCALC" )
    { @{$my_main{"CIF_FCALC"}} = ( @{$new_main{"CIF_FCALC"}} ) ; 	next ; }

    if ( $KEY eq "CIF_FCALC_PHASE" )
    { @{$my_main{"CIF_FCALC_PHASE"}} = ( @{$new_main{"CIF_FCALC_PHASE"}} ) ; 	next ; }

    if ( $KEY eq "CIF_WEIGHT" )
    { @{$my_main{"CIF_WEIGHT"}} = ( @{$new_main{"CIF_WEIGHT"}} ) ; next ; }
    if ( $KEY eq "CIF_HLA" )
    { @{$my_main{"CIF_HLA"}} = ( @{$new_main{"CIF_HLA"}} ) ; next ; }
    if ( $KEY eq "CIF_HLB" )
    { @{$my_main{"CIF_HLB"}} = ( @{$new_main{"CIF_HLB"}} ) ; next ; }
    if ( $KEY eq "CIF_HLC" )
    { @{$my_main{"CIF_HLC"}} = ( @{$new_main{"CIF_HLC"}} ) ; next ; }
    if ( $KEY eq "CIF_HLD" )
    { @{$my_main{"CIF_HLD"}} = ( @{$new_main{"CIF_HLD"}} ) ; next ; }
    if ( $KEY eq "CIF_TEST" )
    { @{$my_main{"CIF_TEST"}} = ( @{$new_main{"CIF_TEST"}} ) ; next ; }
    if ( $KEY eq "RESOLUTION" )
    { 
	@{$my_main{"DFR_RES_MIN"}} = ( $new_main{"RESOLUTION"}[0] ) ;
	@{$my_main{"DFR_RES_MAX"}} = ( $new_main{"RESOLUTION"}[1] ) ;
	next ; 
    }
}

foreach $KEY ( sort keys (%column) )
{
    print "UNUSED COLUMNS: $KEY ---> ", $column{$KEY}, "\n" ;
}

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

$conf_file = "main_config.data" ; 
main_config::write_main_config_file ( $conf_file, %my_main ) ; 

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

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


$FILE = $my_main{"FILE_CELL"}[0] ;
open ( TXT,  "< $FILE" ) || print "FILE OPEN ERROR: >$FILE< \n" ;
while ( <TXT> ) 
{
    $record = $_ ; 
    print $record ;
}
chomp ( $record) ;
close ( TXT ) ;
@words = split(" ", $record) ;
#print " cell record: $record \n" ;
my $A = $words[0] ;
my $B = $words[1] ;
my $C = $words[2] ;
my $AL = $words[3] ;
my $BE = $words[4] ;
my $GA = $words[5] ;

my $fobs ;
$AL = $AL / 180. * 3.1415 ;
$BE = $BE / 180. * 3.1415 ;
$GA = $GA / 180. * 3.1415 ;
#print " cell: $AL $BE $GA \n" ;
my $COAL = cos ( $AL ) ;
my $COBE = cos ( $BE ) ;
my $COGA = cos ( $GA ) ;
my $SIAL=sqrt(1. - $COAL*$COAL ) ;
my $SIBE=sqrt(1. - $COBE*$COBE ) ;
my $SIGA=sqrt(1. - $COGA*$COGA ) ;
my $VOL=$A*$B*$C*sqrt(1.-$COAL**2-$COBE**2-$COGA**2+2.*$COAL*$COBE*$COGA) ;

my $COALST=($COBE*$COGA-$COAL) / ($SIBE*$SIGA) ;
my $SIALST=sqrt(1. - $COALST*$COALST ) ;
my @tr ;

$tr[0] = 1 ;
$tr[1] = $COGA ;
$tr[2] = $COBE ;
$tr[3] = 0. ;
$tr[4] = $SIGA ;
$tr[5] = - $SIBE * $COALST ;
$tr[6] = 0. ;
$tr[7] = 0. ;
$tr[8] = $SIBE * $SIALST ;

my @aort ;
my @bort ;
my @cort ;
my @arep ;
my @brep ;
my @crep ;
# make the real space cell vectors
$aort[0] = $A*$tr[0] ; $aort[1] = $A*$tr[3] ; $aort[2] = $A*$tr[6] ; 
$bort[0] = $B*$tr[1] ; $bort[1] = $B*$tr[4] ; $bort[2] = $B*$tr[7] ;
$cort[0] = $C*$tr[2] ; $cort[1] = $C*$tr[5] ; $cort[2] = $C*$tr[8] ;

# print "AORT: ", @aort, "\n" ;
# print "BORT: ", @bort, "\n" ;
# print "CORT: ", @cort, "\n" ;
$VOL = 1.0 / $VOL ;
# make the reciprocal cell vectors
@crep = main_config::vec_prod( @aort, @bort ) ; @crep = main_config::sc_prod ( @crep,  $VOL ) ;
@arep = main_config::vec_prod( @bort, @cort ) ; @arep = main_config::sc_prod ( @arep,  $VOL ) ;
@brep = main_config::vec_prod( @cort, @aort ) ; @brep = main_config::sc_prod ( @brep,  $VOL ) ;

# $line= join ( " x ", @arep ) ; 
# print "AREP: ",  $line, "\n" ;
# $line= join ( " x ", @brep ) ;
# print "BREP: ",  $line, "\n" ;
# $line= join ( " x ", @crep ) ;
# print "CREP: ", $line, "\n" ;
my $rmin = -999999.0 ;
my $rmax = 999999.0 ;

my $ii = $line_beg ;
my $data_val ; my $data_list ; my $data_form ;
my $data_lab ; my $chrs ;
my $ICL2 ; my $sig ;
my $NO = 0; my $NAN=0 ;
my ( $rh, $rk, $rl, $dh2 ) ;



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



while ( $ii < $line_end )
{
    $ii++ ;
#    print $ii, " ", $nn , "\n" ;
    my @out_form = () ;
    my @x ;
    push( @out_form, "IND" ) ;
    if ( length($sf_lines[$ii-1]) <= 0 ) { next } ;
    @x = split ( ' ', $sf_lines[$ii-1] ) ;
    if (  substr($x[0], 0, 1 ) eq "#" ) { next } ;
#    if ( $x[0] != m"[0-9]" ) { next } ;

#    $chrs = sprintf ( "%4d %4d %4d", $x[$column_list{"index_h"}], 
#		      $x[$column_list{"index_k"}], $x[$column_list{"index_l"}] ) ; 
#    push( @out_form, $chrs ) ;

    if ( length( $new_main{"CIF_IND_H"}[0]) >= 0 ) 
    {
	$data_lab = $my_main{"CIF_IND_H"}[0] ;
#	print ' ind h:', $data_lab, "\n" ;
	if ( exists ($column_list{$data_lab}) )
	     {
		 $ICL2 = $column_list{$data_lab} ;
#		 print $ICL2, " column list\n" ;
		 $data_val = $x[ $ICL2 - 1 ] ;
#		 print $data_val, " data val \n" ;
		 $chrs = sprintf ( "%4d", $data_val) ;
		 push( @out_form, $chrs ) ; 
		 $rh = $data_val ;
	     }
	else { $NO++; $Hind_check = 13 ; next ; }
    }

    if ( length( $new_main{"CIF_IND_K"}[0]) >= 0 ) 
    {
	$data_lab = $my_main{"CIF_IND_K"}[0] ;
#	print ' ind h:', $data_lab, "\n" ;
	if ( exists ($column_list{$data_lab}) )
	     {
		 $ICL2 = $column_list{$data_lab} ; 
		 $data_val = $x[ $ICL2 - 1 ] ;
		 $chrs = sprintf ( "%4d", $data_val) ;
		 push( @out_form, $chrs ) ; 
		 $rk = $data_val ;
		 
	     }
	else { $NO++; $Kind_check = 14 ; next ; }
    }
    if ( length( $new_main{"CIF_IND_L"}[0]) >= 0 ) 
    {
	$data_lab = $my_main{"CIF_IND_L"}[0] ;
#	print ' ind h:', $data_lab, "\n" ;
	if ( exists ($column_list{$data_lab}) )
	     {
		 $ICL2 = $column_list{$data_lab} ; 
		 $data_val = $x[ $ICL2 - 1 ] ;
		 $chrs = sprintf ( "%4d", $data_val) ;
		 push( @out_form, $chrs ) ; 
		 $rl = $data_val ;

	     }
	else { $NO++; $Lind_check = 15 ; next ; }
    }
    my @t = () ;

#    print 'a=', @arep, '\n b=', @brep, '\n c=', @crep, "\n" ;
#    print $rh, $rk, $rl,  " indices\n" ;
    $t[0] = $arep[0] * $rh + $brep[0] * $rk + $crep[0] * $rl ;
    $t[1] = $arep[1] * $rh + $brep[1] * $rk + $crep[1] * $rl ;
    $t[2] = $arep[2] * $rh + $brep[2] * $rk + $crep[2] * $rl ;
    $dh2 = sqrt ( $t[0]*$t[0] + $t[1]*$t[1] + $t[2]*$t[2]  ) ;
    $dh2 = 1.0 / $dh2 ;

    if ( $dh2 < $rmax ) { $rmax = $dh2 } ;
    if ( $dh2 > $rmin ) { $rmin = $dh2 } ;
    
    if ( length( $new_main{"CIF_FOBS"}[0]) > 0 ) 
    {
	$data_lab = $my_main{"CIF_FOBS"}[0] ;
#	print ' fob:', $data_lab, "\n" ;
	if ( exists ($column_list{$data_lab}) )
	     {
		 $ICL2 = $column_list{$data_lab} ; 
		 $data_val = $x[ $ICL2 - 1 ] ;
		 if ( $data_val =~ '\?' ) 
		 { 
#		     print " FOBS DATA VAL::  $data_val \n" ; 
		     next ; 
		 }
		 if ( $data_val < 999999 && $data_val >= 0.01 ) 
		 {  
		     
		     $chrs = sprintf ( "FOB %9.2f", $data_val) ;
		     push( @out_form, $chrs ) ; 
		 }
	     }
	else { $NO++; $FOB_check=12  ; next ; }
    }
    
    if ( length( $new_main{"CIF_SIGMA"}[0]) > 0 ) 
    {
	$data_lab = $my_main{"CIF_SIGMA"}[0] ;
#	print ' sigm:', $data_lab, "\n" ;
	if ( exists ($column_list{$data_lab}) )
	     {
		 $ICL2 = $column_list{$data_lab} ; 
		 $data_val = $x[ $ICL2 - 1 ] ;
		 if ( $data_val =~ '\?' ) { next ; }
		 $chrs = sprintf ( "SIG %7.2f", $data_val) ;
		 push( @out_form, $chrs ) ; 
	     }
#	else { $NO++; print "NO sig \n" ; next ; }
    } 

#intensity introduced
  
    if ( length( $new_main{"CIF_IOBS"}[0]) > 0 ) 
    {
	$data_lab = $my_main{"CIF_IOBS"}[0] ;
#	print ' fob:', $data_lab, "\n" ;
	if ( exists ($column_list{$data_lab}) )
	     {
		 $ICL2 = $column_list{$data_lab} ; 
		 $data_val = $x[ $ICL2 - 1 ] ;
		 if ( $data_val =~ '\?' ) 
		 { 
#		     print " FOBS DATA VAL::  $data_val \n" ; 
		     next ; 
		 }
		 if ( $data_val < 999999 && $data_val >= 0.01 ) 
		 {  
		     
		     $chrs = sprintf ( "IOB %9.2f", $data_val) ;
		     push( @out_form, $chrs ) ; 
		 }
	     }
	else { $NO++; $IOB_check=12  ; next ; }
    }
    

    if ( length( $new_main{"CIF_ISIG"}[0]) > 0 ) 
    {
	$data_lab = $my_main{"CIF_ISIG"}[0] ;
#	print ' sigm:', $data_lab, "\n" ;
	if ( exists ($column_list{$data_lab}) )
	     {
		 $ICL2 = $column_list{$data_lab} ; 
		 $data_val = $x[ $ICL2 - 1 ] ;
		 if ( $data_val =~ '\?' ) { next ; }

		 $chrs = sprintf ( "SIG %7.2f", $data_val) ;
		 push( @out_form, $chrs ) ; 
	     }
#	else { $NO++; print "NO sig \n" ; next ; }
    } 


    if ( length( $new_main{"CIF_FCALC"}[0]) >= 0 &&  length( $new_main{"CIF_FCALC_PHASE"}[0]) >= 0) 
    {
	$data_lab = $my_main{"CIF_FCALC"}[0] ;
#	print ' sigm:', $data_lab, "\n" ;
	if ( exists ($column_list{$data_lab}) )
	     {
		 $ICL2 = $column_list{$data_lab} ; 
		 $data_val = $x[ $ICL2 - 1 ] ;
		 if ( $data_val =~ '\?' ) { next ; }
		 $chrs = sprintf ( "POLAR %9.2f", $data_val) ;
		 push( @out_form, $chrs ) ; 
	     }

	$data_lab = $my_main{"CIF_FCALC_PHASE"}[0] ;
#	print ' sigm:', $data_lab, "\n" ;
	if ( exists ($column_list{$data_lab}) )
	     {
		 $ICL2 = $column_list{$data_lab} ; 
		 $data_val = $x[ $ICL2 - 1 ] ;
		 if ( $data_val =~ '\?' ) { next ; }
		 $chrs = sprintf ( "%8.1f", $data_val) ;
		 push( @out_form, $chrs ) ; 
	     }


#	else { $NO++; print "NO sig \n" ; next ; }
    } 

    if ( length( $new_main{"CIF_TEST"}[0]) >= 0 ) 
    {
	$data_lab = $my_main{"CIF_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" ) ;
#    print "MY DATA FORM> ", $data_form ;

    print TXT $data_form ; 

}

$my_main{"DFR_RES_MIN"}[0] = int(100*$rmin) / 100. ;
$my_main{"DFR_RES_MAX"}[0] = int(100*$rmax) / 100. ;


print " use_cif.pl> done: resolution range ", $my_main{"DFR_RES_MIN"}[0], " - ", $my_main{"DFR_RES_MAX"}[0], "\n" ;

if ( $FOB_check ) { print "\n WARNING: no FOB included; diffract.dat might be empty \n\n" ; }
if ( $Hind_check ) { print "\n WARNING: no H values included; diffract.dat might be empty \n\n" ; }
if ( $Kind_check ) { print "\n WARNING: no K values included; diffract.dat might be empty \n\n" ; }
if ( $Lind_check ) { print "\n WARNING: no L values included; diffract.dat might be empty \n\n" ; }

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

$conf_file = "main_config.data" ; 
main_config::write_main_config_file ( $conf_file, %my_main ) ; 

exit ;


