#!/usr/bin/env perl
use strict; use warnings;
use feature 'say';

use App::AFNI::SemainsPhysio;
use Pod::Usage;
use Getopt::Long;
use feature 'say';

=head1 NAME

semphysdat - chop physio files to MR timing for AFNI's retroTS


=head1 SYNOPSIS


semphysdat [options] phsyiofile1 physiofile2 MRdir/

Options:

  -s [alt+z],alt-z,seq+z,seq-z,filename # slice order

  -o prefix                             # where to save files,
                                        # trailing slash are important

  -r [matlab]|McRetroTs|                # retoTS method
     show    | none 

-r none is useful if you only want the *dat files

=head1 OUTPUT

=over

=item C<*dat> 

chopped volrage meassurements with triggers removed, 

=item C<*slibase.1D> 

RVT from AFNI's RetroTS ready for C<afni_proc.py> or C<3dretroicor>

=back



=head1 DESCRIPTION

=begin html

<img src="https://raw.githubusercontent.com/LabNeuroCogDevel/semphysdat/master/icon.png"></img>

=end html

B<semphysdat> will chop two physio files given timing of DICOMS in MRdir and save in a format RetroTS.m likes.


=head2 SEE ALSO

App::AFNI::SemainsPhysio

=cut




# read in arguments
my $type      ='matlab';
my $oprefix   ='';
my $sliceOrder='alt+z';

GetOptions('retrotype:s'=>\$type, 
           'oprefix:s'=>\$oprefix,
           'sliceOrder:s'=>\$sliceOrder,
           ) or pod2usage(1);

# now that we've removed the options
# check for inputs
pod2usage(1) if $#ARGV<2;
for (@ARGV) { die "cannot read  '$_', should be physio file or MR dir:" if ! -r $_ }
my ($pfile1,$pfile2,@MRs) = @ARGV;


# intialize object
my $p = App::AFNI::SemainsPhysio->new({VERB=>1,sliceOrder=>$sliceOrder, prefix=>$oprefix});

# read in MR
if ( -d $MRs[0] ){
   $p->readMRdir($MRs[0]);
   warn "only using first MR directory ($MRs[0])"  if $#MRs>0;
} else {
   die "using MRfiles is not implemented!\nln -s the files into a directory and use that";
}

# read in the physio files
for(@ARGV[0..1]){
   $p->readPhysio($_);
   $p->writeMRPhys;
}

# get RVT
$p->retroTS($type)

