#! /bin/sh
#!perl -w # --*- Perl -*--
eval 'exec perl -x $0 ${1+"$@"}'
    if 0;
#------------------------------------------------------------------------------
#$Author: antanas $
#$Date: 2022-09-25 19:15:11 +0300 (Sun, 25 Sep 2022) $
#$Revision: 9431 $
#$URL: svn+ssh://www.crystallography.net/home/coder/svn-repositories/cod-tools/tags/v3.8.0/scripts/cif_symop_apply $
#------------------------------------------------------------------------------
#*
#* Apply symmetry operator(s) to all atoms in a CIF.
#*
#* USAGE:
#*    $0 --options input1.cif input*.cif
#**

use strict;
use warnings;
use COD::CIF::Parser qw( parse_cif );
use COD::Spacegroups::Symop::Parse qw( symop_from_string );
use COD::Spacegroups::Symop::Algebra qw( symop_mul symop_vector_mul );
use COD::CIF::Tags::Print qw( print_cif );
use COD::SOptions qw( getOptions get_value );
use COD::SUsage qw( usage options );
use COD::ErrorHandler qw( process_parser_messages report_message );
use COD::ToolsVersion qw( get_version_string );

my $die_on_error_level = {
    ERROR   => 1,
    WARNING => 0,
    NOTE    => 0
};

my $use_parser = 'c';

my @symops = ();

#* OPTIONS:
#*   -s, --symop, --symmetry-operator -x,-y,z
#*                     Apply the specified symmetry operator.
#*
#*   -a, --add-symop, --add-symmetry-operator -x,-y,z
#*                     Add the specified symmetry operator to the applied
#*                     operator list.
#*
#*   --use-perl-parser
#*                     Use development CIF parser written in Perl.
#*   --use-c-parser
#*                     Use faster C/Yacc CIF parser. Default.
#*
#*   --help, --usage
#*                     Output a short usage message (this message) and exit.
#*   --version
#*                     Output version information and exit.
#**
@ARGV = getOptions(
    '-s,--symop,--symmetry-operator' => sub { @symops = (get_value()) },
    '-a,--add-symop,--add-symmetry-operator' => \@symops,
    '--use-perl-parser'  => sub { $use_parser = 'perl' },
    '--use-c-parser'     => sub { $use_parser = 'c' },

    '--options'      => sub { options; exit },
    '--help,--usage' => sub { usage; exit },
    '--version'      => sub { print get_version_string(), "\n"; exit }
);

@ARGV = ( '-' ) unless @ARGV;

binmode STDOUT, ':encoding(UTF-8)';
binmode STDERR, ':encoding(UTF-8)';

my $symop_matrix = symop_from_string('x,y,z');

for my $symop (@symops) {
    $symop_matrix = symop_mul
        ( $symop_matrix, symop_from_string( $symop ));
}

for my $filename (@ARGV) {

    my $options = { 'parser' => $use_parser, 'no_print' => 1 };
    my ( $data, $err_count, $messages ) = parse_cif( $filename, $options );
    process_parser_messages( $messages, $die_on_error_level );

    if( !@{$data} || !defined $data->[0] || !defined $data->[0]{'name'} ) {
        report_message( {
           'program'   => $0,
           'filename'  => $filename,
           'err_level' => 'WARNING',
           'message'   => 'file seems to be empty'
        }, $die_on_error_level->{'WARNING'} );
        next;
    }

    for my $datablock (@{$data}) {
        my $values = $datablock->{values};
        for( my $i = 0; $i <= $#{$values->{'_atom_site_fract_x'}}; $i ++ ) {
            my @xyz;
            for my $data_name ( '_atom_site_fract_x',
                                '_atom_site_fract_y',
                                '_atom_site_fract_z' ) {
                my $coord = $values->{$data_name}[$i];
                $coord =~ s/[(][0-9]+[)]$//;
                push @xyz, $coord;
            };

            my $new_xyz = symop_vector_mul( $symop_matrix, \@xyz );

            $values->{'_atom_site_fract_x'}[$i] = $new_xyz->[0];
            $values->{'_atom_site_fract_y'}[$i] = $new_xyz->[1];
            $values->{'_atom_site_fract_z'}[$i] = $new_xyz->[2];
        }
        do {
            local $, = ' ';
            local $\ = "\n";
            print @{$datablock->{'tags'}};
        } if 0;
        print_cif( $datablock, {
            'dictionary_tags' => $datablock->{'values'},
            'dictionary_tag_list' => $datablock->{'tags'},
        });
    }
}
