#!/usr/bin/perl -w

# --------------------------------------------------------------------------------
# reformat an sp3-c file (created by IGS ACC) to sp3-a via STDIN/STDOUT 
#
# usage :
# -------
# sp3c_2_sp3a.pl < file_sp3c > file_sp3a
# 
# --------------------------------------------------------------------------------

$l1 = '%c cc cc ccc ccc cccc cccc cccc cccc ccccc ccccc ccccc ccccc';
$l2 = '%f  0.0000000  0.000000000  0.00000000000  0.000000000000000';

# ... modifying header
#     ----------------

while (<>) {
  s/^\#c/\#a/   if ( m/^\#c/ );   # substite #c -> #a (version symbol)
  $_ = "$l1\n"  if ( m/^\%c / );  # substite all %c-lines with $l1
  $_ = "$l2\n"  if ( m/^\%f / );  # substite all %f-lines with $l2
  s/G0/  /g     if ( m/^\+ / );   # substite 'G01G03G04...' -> '  1  3  4...' 
  s/G/ /g       if ( m/^\+ / );   # substite 'G20G21G22...' -> ' 20 21 22...'
  print;

  last if ( /^\*/ );              # end loop on first epoch record
}

# ... modifying data part
#     -------------------

while (<>) {
  chomp;
  s/^PG0/P  /;                    # substite PG03 -> P  3
  s/^PG/P /;                      # substite PG29 -> P 29
  $_ = substr $_, 0, 60;          # cut to 60 characters
  print "$_\n"; 
}

# ... end
