#!/bin/csh
# Converts an 8-bit greyscale TIFF file to a segy file
# Assumes that there is one pixel horizontally for each trace
# and 1 pixel vertically for each time sample.
# Assumes the sample interval in the resulting segy file 
# is 4ms (i.e. 250 samples or pixels per second)
#
# Author: Andrew MacRae, (Andrew.MacRae at SMU.CA)

# usage:
# tif2segy filename.tif

# output will be put in filename.segy

set tiffile = $1

if ( !( -e "$tiffile" ) ) then
 echo "tif2segy -- convert a TIFF image file to a SEGY file"
 echo "By Andrew MacRae, and the authors of NetPBM and Seismic Unix"
 echo
 echo "Usage: tif2segy filename.tif"
 echo
 echo "Input file should be a TIFF file with the content of the seismic plot"
 echo "(i.e. no labels or annotation -- only the data) with 1 pixel per "
 echo "trace horizontally, and one pixel per sample vertically."
 echo "The number of traces and number of samples are calculated from the image size."
 echo "Output is placed in filename.segy."
 echo
 echo "The program assumes that the "netpbm" image tools and "Seismic Unix""
 echo "are already in the command path."
 exit(1)
else
 echo "tif2segy -- convert a TIFF image file to a SEGY file"
 echo "By Andrew MacRae, and the authors of NetPBM and Seismic Unix"
 echo
endif


if( `which tifftopnm | cut -f 1 -d ' '` == 'no' ) then
 echo "Could not find the NetPBM tools (e.g., tifftopnm)."
 echo "You need to install them, or put them into your command path."
 exit(1)
endif

if( `which suaddhead | cut -f 1 -d ' '` == 'no' ) then
 echo "Could not find Seismic Unix."
 echo "You need to install it, or put the programs into your command path."
 exit(1)
endif

# sample interval -- 4000 = 4 milliseconds
set interval = 4000 

# get size of image:
# horizontal pixels -> number of traces
# vertical pixels -> number of samples

set traces = `tifftopnm < $tiffile | pnmfile | cut -f 3 -d ' ' -s`
set samples = `tifftopnm < $tiffile | pnmfile | cut -f 5 -d ' ' -s`

echo image file $tiffile has $traces horizontal pixels which will become "traces"

# convert image to byte values and invert values (tifftopnm and pnminvert)
# rotate and flip to Seismic Unix standard trace-sample orientation
# (pnmflip), chop off PGM image header (tail), reformat data from
# 8-bit character values to floating point (recast), and then
# add trace headers and insert sample interval (suaddhead and
# sushw).  Output to Seismic Unix data file (.su file)

tifftopnm < $tiffile | pnminvert | pnmflip -r90 -tb | tail +4 | recast in=uchar out=float | suaddhead ns=$samples ftn=0 | sushw key=dt a=$interval > $tiffile:r.su

# create binary and EBCDIC headers
segyhdrs < $tiffile:r.su

# default header is not needed
rm header

# write out a 40-line, 80 character/line header to be converted to EBCDIC

# get the name of the file, without a .tif ending
set linename = $tiffile:r

echo "C                                                                              " > tif2segy_header

# This will output a line with the linename (derived from the filename)
# and then truncate the line to 80 characters
# Please modify this header to describe your data.
echo "C Line from file: " $linename "                                                              " | cut -c1-79 >> tif2segy_header
echo "C                                                                              " >> tif2segy_header
echo "C      This SEGY file was created by _________________                         " >> tif2segy_header
echo "C                                                                              " >> tif2segy_header
echo "C                                                                              " >> tif2segy_header
echo "C      File was converted using tif2segy, netpbm, and Seismic Unix             " >> tif2segy_header
echo "C      Author of tif2segy script is Andrew MacRae (andrew.macrae at smu.ca)    " >> tif2segy_header

set i = 0
while ($i < 32 )
  echo "C                                                                              " >> tif2segy_header
  set i = ($i + 1)
end
 
# write a SEGY file using headers and Seismic Unix file.  Include 'endian=0' if using a little endian machine.
segywrite tape=$tiffile:r.segy bfile=binary hfile=tif2segy_header endian=0 < $tiffile:r.su

# leave cleanup to the user, in case they want to review the 
# Seismic Unix files
echo "Cleaning up temporary files: tif2segy_header, binary, and" $tiffile:r.su

rm tif2segy_header
rm binary
rm $tiffile:r.su

exit(0)

