#! /bin/csh -f
# 2D2pipe - converts Bruker DRX 2D ser files to nmrPipe format
# Called by drx2pipe
# Duncan Smith
# Version 1.4	August 5, 1999
#
# Variables are:  1 DATASET  2 TD  3 XMODE  4 SWH  5 BF  6 CARR  7 NUC  8 EXPT  9 YMODE

# Get offset frequencies of indirect carrier in Hz
setenv O2 `awk '/\#\#\$O1/ {printf "%9.3f", $2}' ${1}/acqu2s`
if (($O2 == "") || ($O2 == 0.000)) then
        echo 'WARNING - unable to find indirect carrier - setting to 0.0'
        echo ' '
endif

# Get size of FIDs and number in indirect dimension
setenv TD2 `awk '/\#\#\$TD/ {print $2}' ${1}/acqu2s`
if (($TD2 == "") || ($TD2 <= 1)) then
	echo 'Unable to find size of indirect dimension - setup aborted '
	echo 'First 2 FIDs probably not yet complete \! '
	exit (1)
endif
if ($TD2 % 2 == 1) then
        setenv TD2 `expr $TD2 - 1`
endif
setenv COMP `expr ${2} / 2`
setenv COMP2 `expr $TD2 / 2`

# Get indirect spectral width in Hz
setenv SWH2 `awk '/\#\#\$SW_h/ {printf "%9.3f", $2}' ${1}/acqu2s`
if (($SWH2 == "") || ($SWH2 == 0.000)) then
        echo 'Unable to find indirect spectral width - setup aborted \!'
        exit (1)
endif

# Get nuclei of indirect dimension
setenv NUC2 `awk '/\#\#\$NUC1/ {print $2}' ${1}/acqu2s | sed 's/<//' | sed 's/>//'`
if (($NUC2 == "") || ($NUC2 == "off")) then
        echo 'Unable to find indirect nucleus name - using default'
        echo ' '
        set NUC2 = none
endif
if ($NUC2 == "1H") then
	set NUC2 = 1Hind
endif

# Get basic frequency for indirect dimension
setenv BF2 `awk '/\#\#\$BF1/ {print $2}' ${1}/acqu2s | awk '{printf "%10.6f", $0}'`
if (($BF2 == "") || ($BF2  == 0.000000)) then
	echo 'Unable to find indirect frequency - using 100.0MHz'
	echo ' '
	set BF2 = `echo 100.0 | awk '{printf "%10.6f", $0}'`
endif

# Calculate carrier in ppm for indirect dimension
setenv CARR2 `echo $O2 / $BF2 | bc -l | awk '{printf "%7.3f", $0}'`

# Append convert.com file
echo ${2} $TD2 | awk '{printf "  -xN %13d    -yN %13d   \\\n", $1, $2 }' >> convert.com
echo $COMP $COMP2 | awk '{printf "  -xT %13d    -yT %13d   \\\n", $1, $2 }' >> convert.com
echo ${3} ${9} | awk '{printf "  -xMODE %10s    -yMODE %10s   \\\n", $1, $2 }' >> convert.com
echo ${4} $SWH2 | awk '{printf "  -xSW %12.3f    -ySW %12.3f   \\\n", $1, $2 }' >> convert.com
echo ${4} ${5} $SWH2 $BF2 | awk '{printf "# -xSWppm %9.3f    -ySWppm %9.3f   \\\n", $1/$2, $3/$4 }' >> convert.com
echo ${5} $BF2 | awk '{printf "  -xOBS %11.6f    -yOBS %11.6f   \\\n", $1, $2 }' >> convert.com
echo ${6} $CARR2 | awk '{printf "  -xCAR %11.3f    -yCAR %11.3f   \\\n", $1, $2 }' >> convert.com
echo ${7} $NUC2 | awk '{printf "  -xLAB %11s    -yLAB %11s   \\\n", $1, $2 }' >> convert.com
echo '  -ndim           2    -aq2D      States   \' >> convert.com

