#! /bin/tcsh -f
# 3D2pipe - converts Bruker DRX 3D ser files to nmrPipe format
# Called by drx2pipe
# Duncan Smith
# Version 1.9	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 carriers in Hz
setenv O2 `awk '/\#\#\$O1/ {printf "%9.3f", $2}' ${1}/acqu2s`
setenv O3 `awk '/\#\#\$O1/ {printf "%9.3f", $2}' ${1}/acqu3s`
if (($O2 == "") || ($O3 == "") || ($O2 == 0.000) || ($O3 == 0.000)) then
        echo 'WARNING - unable to find indirect carriers - setting to 0.0 \!'
        echo ' '
endif

# Get size of FIDs and number in indirect dimensions
setenv TD2 `awk '/\#\#\$TD/ {print $2}' ${1}/acqu2s`
setenv TD3 `awk '/\#\#\$TD/ {print $2}' ${1}/acqu3s`
if (($TD2 == "") || ($TD3 == "") || ($TD2 <= 1) || ($TD3 <= 1)) then
	setenv EXPT_UPPER `echo $8 | awk '{x=toupper($1); print x}'`
        echo 'Unable to find size of indirect dimension(s) - setup aborted \!'
	echo 'First 2 planes of 3D '$EXPT_UPPER' experiment probably not yet completed \!'
        exit (1)
endif
if ($TD2 % 2 == 1) then
        setenv TD2 `expr $TD2 - 1`
endif
if ($TD3 % 2 == 1) then
        setenv TD3 `expr $TD3 - 1`
endif
setenv COMP `expr ${2} / 2`
setenv COMP2 `expr $TD2 / 2`
setenv COMP3 `expr $TD3 / 2`

# Get indirect spectral widths in Hz
setenv SWH3 `awk '/\#\#\$SW_h/ {printf "%9.3f", $2}' ${1}/acqu3s`
if (($SWH3 == "") || ($SWH3 == 0.000)) then
        echo 'Unable to find indirect spectral widths - setup aborted \!'
        exit (1)
endif
setenv LINE1 `awk '/\#\#\$IN\=/ {print NR+1}' ${1}/acqus | xargs -i awk 'NR=={} {print $0}' ${1}/acqus`
setenv LINE2 `awk '/\#\#\$IN\=/ {print NR+2}' ${1}/acqus | xargs -i awk 'NR=={} {print $0}' ${1}/acqus`
setenv IN10 `echo $LINE1 $LINE2 | awk '{print $11 * 1000000}'`
if (($IN10 == 0.0) || ($IN10 == "")) then
	echo 'Unable to find Z dimension increment value - setup aborted \!'
	exit (1)
else
	set SWH2 = `echo 1000000 \/ \(2 \* $IN10 \) | bc -l | awk '{printf "%9.3f", $0}'`
endif

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

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

# Calculate carriers in ppm for indirect dimensions
if (($NUC2 == "13C") && ($NUC3 == "13C")) then
	setenv O2 `awk '/\#\#\$O4/ {printf "%9.3f", $2}' ${1}/acqus`
	set NUC3 = C13
endif
if (($NUC2 == "15N") && ($NUC3 == "15N")) then
	set NUC3 = N15
endif
setenv CARR2 `echo $O2 / $BF2 | bc -l | awk '{printf "%7.3f", $0}'`
setenv CARR3 `echo $O3 / $BF3 | bc -l | awk '{printf "%7.3f", $0}'`

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

