#! /bin/sh
#!perl -w # --*- Perl -*--
eval 'exec perl -x $0 ${1+"$@"}'
    if 0;
#------------------------------------------------------------------------------
#$Author: antanas $
#$Date: 2021-04-28 19:35:53 +0300 (Wed, 28 Apr 2021) $
#$Revision: 8738 $
#$URL: svn+ssh://www.crystallography.net/home/coder/svn-repositories/cod-tools/tags/v3.8.1/scripts/cif_mpod_v2_to_v1 $
#------------------------------------------------------------------------------
#*
#* Convert MPOD structures from MPOD dictionary V2 to MPOD dict. V1 based
#* data names.
#*
#* USAGE:
#*    $0 --options input1.cif input*.cif
#**

use strict;
use warnings;
use COD::CIF::Parser qw( parse_cif );
use COD::CIF::Tags::Manage qw( exclude_tag );
use COD::CIF::Tags::Print qw( print_cif );
use COD::SOptions qw( getOptions );
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";

#* OPTIONS:
#*   --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(
    "--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 }
);

my @scalar_property_tags = qw(
    _prop_residual_resistivity_ratio
    _prop_residual_resistivity_ratio_high_temperature
    _prop_residual_resistivity_ratio_low_temperature
    _prop_superconducting_critical_temperature_thermodynamic
    _prop_superconducting_critical_temperature_onset
    _prop_superconducting_critical_temperature_onset_90
    _prop_superconducting_critical_temperature_mid_50
    _prop_superconducting_critical_temperature_offset_10
    _prop_superconducting_zero_resistivity_temperature
    _prop_superconducting_resistivity_transition_width
    _prop_magnetic_paramagnetic_critical_temperature_Neel
    _prop_magnetic_paramagnetic_critical_temperature_Neel_transitionwidth
    _prop_magnetic_antiferromagnetic_ordering_temperature_Neel
);

my @property_tags = qw(
    _prop_heat_capacity_C
    _prop_dielectric_permittivity_relative_epsrij
    _prop_dielectric_permittivity_relative_epsrijS
    _prop_dielectric_permittivity_relative_epsrijT
    _prop_dielectric_stiffness_relative_betrijS
    _prop_dielectric_stiffness_relative_betrijT
    _prop_elastic_stiffness_cij
    _prop_elastic_stiffness_cijD
    _prop_elastic_stiffness_cijE
    _prop_elastic_compliance_sij
    _prop_elastic_compliance_sijD
    _prop_elastic_compliance_sijE
    _prop_electric_resistivity_rhoeij
    _prop_electric_remnant_polarisation_Pri
    _prop_electric_coercive_field_Eci
    _prop_internal_friction_Qij-1
    _prop_electromechanical_coupling_kij
    _prop_optical_index_ordinary_no
    _prop_optical_index_extraordinary_ne
    _prop_piezoelectric_gij
    _prop_piezoelectric_eij
    _prop_piezoelectric_hij
    _prop_piezoelectric_dij
    _prop_piezooptic_piij
    _prop_superconducting_critical_field1_Hc1i
    _prop_superconducting_critical_field2_Hc2i
    _prop_superconducting_irreversibility_field_Hirri
    _prop_superconducting_coherence_length_ksii
    _prop_superconducting_penetration_depth_lambdai
    _prop_electrostriction_Dij
    _prop_electrostriction_Dprimeij
    _prop_photoelastic_pij
    _prop_thermal_conductivity_kappaij
    _prop_thermal_diffusivity_kappadij
    _prop_thermal_expansion_alphaij
    _prop_thermal_expansion_Tij
    _prop_thermoelectric_Seebeck_Seij
);

my @condition_tags = qw(
    _prop_measurement_method
    _prop_conditions_temperature
    _prop_conditions_temperature_range_begin
    _prop_conditions_temperature_range_end
    _prop_conditions_temperature_cycle
    _prop_conditions_pressure
    _prop_conditions_frequency
    _prop_conditions_magnetic_field
    _prop_conditions_wavelength
);

my @measurement_tags = qw(
    _prop_measurement_method
    _prop_measurement_poling
    _prop_frame
);

my @data_tags = qw(
    _prop_data_label
    _prop_data_value
    _prop_data_tensorial_index
);

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

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

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};
        my $tags = $datablock->{tags};

        my @conditions = ();
        for my $condition (@condition_tags) {
            my $condition_key = lc( $condition );
            if( exists $values->{$condition_key} ) {
                push( @conditions, $condition );
            }
        }

        my @property_data = ();
        my %property_names = ();
        my %property_labels = ();
        for my $property (@property_tags) {
            my $property_key = lc( $property );
            if( exists $values->{$property_key} ) {
                $property_names{$property} = 1;
                $property =~ /_([^_]*?)$/;
                my $property_label = $1;
                $property_labels{$property} = $property_label;
                for my $i (0..$#{$values->{$property_key}}) {
                    next if $values->{$property_key}[$i] eq ".";
                    my $tensorial_index =
                        $values->{_prop_tensorial_index}[$i];
                    my @property_data_row =
                        ( $property_label, $tensorial_index,
                          $values->{$property_key}[$i] );
                    for my $condition (@conditions) {
                        push( @property_data_row,
                              $values->{$condition}[$i] );
                    }
                    push( @property_data, \@property_data_row );
                }
            }
        }
        # Delete version 1 value tags:
        for my $tag (@condition_tags, @property_tags, @data_tags) {
            exclude_tag( $datablock, lc($tag) );
        }
        exclude_tag( $datablock, "_prop_tensorial_index" );
        # Print out the rest of the CIF data:
        print_cif( $datablock, {
            exclude_misspelled_tags => 0,
            preserve_loop_order => 1,
            fold_long_fields => 1,
            folding_width => 78,
            dictionary_tags => { map {$_,$_} @{$datablock->{tags}} },
            dictionary_tag_list => $datablock->{tags},
            keep_tag_order => 1,
        });

        # Print out the collected tensor values if there are any:
        for my $property (sort keys %property_names ) {
            print $property, " '$property_labels{$property}'\n";
        }
        if( @property_data ) {
            print "loop_\n";
            print "_prop_data_label\n";
            print "_prop_data_tensorial_index\n";
            print "_prop_data_value\n";
            for my $key (@conditions) {
                print $key, "\n";
            }
            for my $property_value_row (@property_data) {
                print join( " ", @$property_value_row ), "\n"
            }
        }
    }
}
