#! /bin/tcsh -f
# drx2pipe - converts Bruker DRX data files to NMRPipe format
# Calls one of 1D2pipe, 2D2pipe, 3D2pipe or 4D2pipe
# Duncan Smith
# Version 1.15	August 5, 1999
#

# Set variables
set ANSWER = y
set DATASET

# Get path for dataset
if ($1 == "") then
        echo -n '\nEnter path for dataset (including EXPNO) ...    '
        set DATASET = $<
else
        set DATASET = $1
endif
echo ' '

if ($DATASET == "") then
	if (-r ./convert.com) then
		set DATASET = `awk '/bruk2pipe/ {print $3}' ./convert.com | sed 's/\/ser//' | sed 's/\/fid//'`
	endif
endif

# Get number of dimensions and check for existence of acqus, acqu2s,
# acqu3s, fid and ser files and local fid directory
if (-r $DATASET/acqus) then
else
	echo 'Unable to read acqus file - aborted \!'
	echo 'Check dataset pathname'
	echo ' '
	exit (0)
endif

setenv NDIM `awk '/PARMODE/ {print $2+1}' $DATASET/acqus`
if ($NDIM <= 0 || $NDIM == "") then
	echo 'Unable to determine number of dimensions - aborted \!'
	echo ' '
	exit (0)
endif

if ($NDIM == 1) then
	if (-r $DATASET/fid) then
	else
		echo 'Unable to read 1D fid file - aborted \!'
		echo ' '
		exit (0)
	endif
else
	if (-r $DATASET/ser) then
	else
		echo 'Unable to read ser file - aborted \!'
		echo 'First FID probably not yet completed'
		echo ' '
		exit (0)
	endif
endif

if ($NDIM > 1) then
	if (-r $DATASET/acqu2s) then
	else
		echo 'Unable to read acqu2s file - conversion aborted \!'
		echo ' '
		exit (0)
	endif
endif

if ($NDIM == 3) then
	if (-r $DATASET/acqu3s) then
	else
		echo 'Unable to read acqu3s file - conversion aborted \!'
		echo 'Are you sure this is a 3D dataset?'
		echo ' '
		exit (0)
	endif
endif

if (-e fid) then
	if (-f fid) then
		echo 'fid exists and is a file, conversion aborted \!'
		echo ' '
		exit (0)
	endif
else
	echo 'fid directory does not exist, creating it ... '
	echo ' '
	mkdir fid
	chmod 775 fid
endif

echo 'Fetching information from acqu files ... '
echo ' '

# Get instrument name
setenv INSTRUMENT `awk '/INSTRUM/ {print $2}' $DATASET/acqus | sed 's/\<//' | sed 's/\>//'`
#### EDIT THE LINE BELOW TO INCLUDE YOUR INSTRUMENT NAMES - USING AN ALIAS SUCH AS "DRX500" IS BETTER THAN "SPECT" ####
if ((`echo $INSTRUMENT | awk '{print index($1, "drx")}'` == 0) && (`echo $INSTRUMENT | awk '{print index($1, "dmx")}'` == 0)) then
	echo 'Unable to find instrument name - aborted'
	echo 'First FID not yet completed or data was not acquired on an Avance \!'
	echo ' '
	exit (0)
endif

# Get pulse program name and guess experiment
setenv PULSEPROG `awk '/PULPROG/ {print $2}' $DATASET/acqus | sed 's/<//' | sed 's/>//'`
if ($PULSEPROG == "") then
	echo 'WARNING - unable to find pulse program name - using default'
	echo ' '
	set PULSEPROG = no_name
endif

#### EDIT THE PAIRS OF LETTERS BELOW ACCORDING TO YOUR PULSE PROGRAM NAMING CONVENTION ####
setenv EXPT `echo $PULSEPROG | sed 's/4d//' | sed 's/3d//' | sed 's/2d//' | sed 's/gs//' | sed 's/gp//' | sed 's/ea//' | sed 's/2H//'`
if (`echo $PULSEPROG | awk '{print index($1, "4d")}'` != 0) then
	set NDIM = 4
endif

# Get temperature
setenv TEMP `awk '/\#\#\$TE/ {printf "%5.1f", $2}' $DATASET/acqus`
if (($TEMP == "") || ($TEMP == 0.0)) then
	echo 'WARNING - unable to find temperature - setting to 0 \!'
        echo ' '
	set TEMP = 0.0
endif

# Get number of scans per FID
setenv NS `awk '/\#\#\$NS/ {print $2}' $DATASET/acqus`
if (($NS == "") || ($NS <= 0)) then
	echo 'WARNING - unable to find number of scans - setting to 0'
	echo ' '
	set NS = 0
endif

# Get offset frequency of carrier in Hz
setenv O1 `awk '/\#\#\$O1/ {printf "%9.3f", $2}' $DATASET/acqus`
if ($O1 == 0.000) then
        echo 'WARNING - acquisition dimension carrier is 0 \!'
	echo 'Check this out'
        echo ' '
endif

# Get size of FID
setenv TD `awk '/\#\#\$TD/ {print $2}' $DATASET/acqus`
if (($TD == "") || ($TD == 0)) then
        echo 'Unable to find size of FID - setup aborted \!'
	echo 'Check value of TD'
	echo ' '
        exit (0)
endif
if ($TD % 2 == 1) then
        setenv TD `expr $TD - 1`
endif

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

# Get nucleus of acquisition dimension
setenv NUC `awk '/\#\#\$NUC1/ {print $2}' $DATASET/acqus | sed 's/<//' | sed 's/>//'`
if (($NUC == "") || ($NUC == "off")) then
	echo 'WARNING - unable to find nucleus name - using default'
        echo ' '
        set NUC = none
endif

# Get basic frequency for acquisition dimension
setenv BF `awk '/\#\#\$BF1/ {print $2}' $DATASET/acqus | awk '{printf "%10.6f", $0}'`
if ($BF  == 0.000000) then
        echo 'WARNING - unable to find observe frequency - using 100.0MHz'
        echo ' '
        set BF = `echo 100.0 | awk '{printf "%10.6f", $0}'`
endif

# Calculate carrier in ppm for acquisition dimension
setenv CARR `echo $O1 / $BF | bc -l | awk '{printf "%7.3f", $0}'`

# Get acquisition mode and set xMODE
setenv AQMOD `awk '/AQ_mod/ {print $2}' $DATASET/acqus`
if ($AQMOD == 0) then
	setenv XMODE "Real"
else if ($AQMOD == 1) then
	setenv XMODE "Complex"
else if ($AQMOD == 2) then
	setenv XMODE "Sequential"
else if ($AQMOD == 3) then
	setenv XMODE "DQD"
	setenv DSPFVS `awk '/DSPFVS/ {print $2}' $DATASET/acqus`
else
	echo 'WARNING - AQ_mod parameter unknown - setting to Complex'
	echo ' '
	set XMODE = Complex
endif

# Get decimation parameter
setenv DECIM `awk '/DECIM/ {printf "%d", $2}' $DATASET/acqus`
if (($DECIM == "") || ($DECIM < 2)) then
	echo 'Illegal decimation parameter: '$DECIM' - setup aborted \!'
	echo ' '
	exit (0)
endif

# Create convert.com file
echo 'Creating convert.com file ...'
echo ' '
echo '#\! /bin/csh -f' > convert.com
echo '#  convert.com' >> convert.com
echo "#  Created by drx2pipe on" `date` >> convert.com
echo "#  Data recorded at "$TEMP"K on "$INSTRUMENT >> convert.com
awk '/\$\$/ { print $0 }' $DATASET/acqus | sed 's/\/acqus//' | sed 's/\$\$/# /' >> convert.com
echo "#  using pulse program "$PULSEPROG" with "$NS" scans per FID" >> convert.com

setenv PIPEINFO `awk '/Pipeinfo/ {print $2}' $DATASET/pulseprogram`

if ($PIPEINFO == "NOESY") then
	setenv MIX_TIME `awk '/\#\#\$D\=/ {print NR+1}' $DATASET/acqus | xargs -i awk 'NR=={} {print $10*1000}' $DATASET/acqus`
	echo "#  NOESY mixing time = "$MIX_TIME"ms" >> convert.com
else if ($PIPEINFO == "ROESY") then
	setenv MIX_TIME `awk '/\#\#\$P\=/ {print NR+1}' $DATASET/acqus | xargs -i awk 'NR=={} {print $16/1000}' $DATASET/acqus`
	echo "#  ROESY mixing time = "$MIX_TIME"ms" >> convert.com
endif

if ($PIPEINFO == "DIPSI-2") then
	setenv LINE1 `awk '/\#\#\$P\=/ {print NR+1}' $DATASET/acqus | xargs -i awk 'NR=={} {print $0}' $DATASET/acqus`
	setenv LINE2 `awk '/\#\#\$P\=/ {print NR+2}' $DATASET/acqus | xargs -i awk 'NR=={} {print $0}' $DATASET/acqus`
	setenv PULSE `echo $LINE1 $LINE2 | awk '{print $22}'`
	setenv NUM_LOOPS `awk '/\#\#\$L\=/ {print NR+1}' $DATASET/acqus | xargs -i awk 'NR=={} {print $2}' $DATASET/acqus`
	set MIX = `echo $NUM_LOOPS \* \( \($PULSE \* 172.661 \) + 96.0 \) / 1000.0 | bc -l | awk '{printf("%3.1f", $0)}'`
	echo "#  TOCSY mixing time = "$MIX"ms" >> convert.com
else if ($PIPEINFO == "DIPSI-3") then
	setenv PULSE `awk '/\#\#\$P\=/ {print NR+1}' $DATASET/acqus | xargs -i awk 'NR=={} {print $14}' $DATASET/acqus`
	setenv NUM_LOOPS `awk '/\#\#\$L\=/ {print NR+1}' $DATASET/acqus | xargs -i awk 'NR=={} {print $2}' $DATASET/acqus`
	set MIX = `echo $NUM_LOOPS \* \( \($PULSE \* 217.3 \) + 216.0 \) / 1000.0 | bc -l | awk '{printf("%3.1f", $0)}'`
	echo "#  TOCSY mixing time = "$MIX"ms" >> convert.com
endif

if ($NDIM > 1) then
	awk '/NMRPipe/ {print $0}' $DATASET/pulseprogram | sed 's/;/#/' | sed 's/NMRPipe//' >> convert.com
endif
echo '#' >> convert.com

#### EDIT THE STRING "ea" TO THE PAIR OF LETTERS YOU USE TO DEFINE ECHO/ANTIECHO ####
if (`echo $PULSEPROG | awk '{print index($1, "ea")}'` != 0) then
	setenv YMODE "Rance-Kay"
else
	setenv YMODE "Complex"
endif

if ($XMODE == "DQD") then
	if ($NDIM == 1) then
		echo 'bruk2pipe -in '$DATASET'/fid -DMX -decim '$DECIM' -noswap -dspfvs '$DSPFVS' \\' >> convert.com
	else
		echo 'bruk2pipe -in '$DATASET'/ser -DMX -decim '$DECIM' -noswap -dspfvs '$DSPFVS' \\' >> convert.com
	endif
else
	if ($NDIM == 1) then
		echo 'bruk2pipe -in '$DATASET'/fid -DMX -decim '$DECIM' -noswap \\' >> convert.com
	else
		echo 'bruk2pipe -in '$DATASET'/ser -DMX -decim '$DECIM' -noswap \\' >> convert.com
	endif
endif

# Call nD2pipe script for indirect dimension information
#### EDIT THE STRING "directory_name" TO THE LOCATION OF THE drx2pipe AND nD2pipe SCRIPTS ####
if (-r /directory_name/$NDIM'D2pipe') then
	eval `echo $NDIM'D2pipe' $DATASET $TD $XMODE $SWH $BF $CARR $NUC $EXPT $YMODE`
	set STATUS = $status
	if ( $STATUS != 0 ) then
		echo ' '
		exit (0)
	endif
else
	echo 'Unable to read '$NDIM'D2pipe script - conversion aborted \!'
	echo ' '
	exit (0)
endif

if ($NDIM == 3) then
	echo '  | pipe2xyz -out fid/'$EXPT'%03d.fid -verb -ov -nofs ' >> convert.com
else if ($NDIM >= 4) then
	echo '  | pipe2xyz -out fid/'$EXPT'%05d.fid -verb -ov -nofs ' >> convert.com
else
	echo '  -out fid/'$EXPT'.fid -verb -ov -nofs ' >> convert.com
endif

chmod 774 convert.com
cat convert.com

# Check for available disk space
echo ' '
echo -n 'Checking available disk space ... '
if ($NDIM == 1) then
	setenv DATASIZE `ls -l $DATASET/fid | awk '{print $5 / 512}'`
else
	setenv DATASIZE `ls -l $DATASET/ser | awk '{print $5 / 512}'`
endif
setenv AVAILABLE `df -b . | awk '/fs/ {print $5}'`
if ($DATASIZE >= $AVAILABLE) then
	echo ' '
	echo ' '
	echo 'Insufficient disk space available - data not converted'
	echo ' '
	exit (0)
else
	echo 'OK'
endif

echo ' '
echo -n 'Start '$NDIM'D data conversion or edit convert.com?   y/n/e [y]    '
set ANSWER = $<
if ($ANSWER == 'e') then
	if ($EDITOR == "") then
		setenv EDITOR vi
	endif
	$EDITOR ./convert.com
	echo ' '
	echo -n 'Start '$NDIM'D data conversion?   y/n [y]     '
	set ANSWER = $<
endif
if (($ANSWER == 'y') || ($ANSWER == "")) then
	echo ' '
	echo 'Converting data to nmrPipe ... '
	echo ' '
	./convert.com
	set STATUS = $status
	if ($STATUS != 0) then
		echo ' '
		echo 'Unable to execute convert.com file'
		echo ' '
		exit (0)
	endif
	echo ' '
	echo 'Conversion complete \!'
else
	echo ' '
	echo '... data not converted'
endif
echo ' '

