#!/usr/bin/perl
# c13_unfold
#
# This script corrects the 13C shift values for crosspeaks for an aliased
# spectrum based on crosspeak sign and combinations of 1H and 13C shifts
#
# syntax: c13_unfold input_file > output_file

$c13_width = 41.41;

# loop through input_file line by line
while (<>){

# split line into fields, store in array of $fld variables
    @fld = split;        #delimits by whitespace
    
# identify and print header lines without change
		if(/#/){
			print "$_";
			next;
		}

# select negative peaks for unfolding of 13C shift
		if($fld[6] < 0){

# select peaks based on combination of 13C and 1H shift
			if($fld[3] > 43 && $fld[1] < 3.0){
				$c13_shift = $fld[3] - $c13_width;
			}
			elsif($fld[3] < 35 && $fld[1] > 2.0){
				$c13_shift = $fld[3] + $c13_width;
			}	
		}
		
# or don't change 13C shift if peak is positive	
		else{
			$c13_shift = $fld[3];
		}
		
# write out each peak with correct 13C shift
		$~ = "PEAKS";
		write;
	}

#define format for output
format PEAKS =
@>>> @##.### @##.### @##.### @ @         @>>>>>>>>>  @>>>>>>> @   @ @>>> @>>> @>>> 0
$fld[0], $fld[1], $fld[2], $c13_shift, $fld[4], $fld[5], $fld[6], $fld[7], $fld[8], $fld[9], $fld[10], $fld[11], $fld[12]
.