#! /bin/csh

if ($#argv != 7) then
	echo "usage: $0 MOA_public MOA_private arcticGrid minArctic maxPred arcticSid oDir"
	echo "  example: $0 ../../seasat2/data/public ../../seasat2/data/private ../arctic/ver1_netcdf_geo.grd 79 80 00099 .."
	exit
endif

set MOA_public	= $1;	shift
set MOA_private = $1;	shift
set arcticGrd 	= $1;	shift
set minArctic 	= $1;	shift
set maxPred 	= $1;	shift
set arcticSid 	= $1;	shift
set oDir	 	= $1;	shift

# need lots of sig figs to keep surface from moaning

gmtset D_FORMAT = %.16lg


# make it all from scratch, or at least from cm files.

/bin/rm -rf   $oDir/cmTile  >& /dev/null
/bin/mkdir -p $oDir/cmTile
/bin/mkdir -p $oDir/cmTile/huge
/bin/mkdir -p $oDir/cmTile/cmFiles


set badlist = ../cmData/badlist

# get all cm data not flagged with "9999", and not in bad list.
#
# use all processors. 
#
# In this case, several jobs are very quick, 
# so launch all 12 and let them thrash just a little bit...
#
# use unique extension cmxyzi to ease debug and partial runs

#FIXME: for some reason these can not be run in parallel; which is makes this unreasonably slow.
echo "-CREATE- .cmxyzi files from private data"
csh jjCrunchCM $badlist $MOA_private/NGA $oDir/cmTile/cmFiles/DNC_prop.tmp ;
csh jjCrunchCM $badlist $MOA_private/IFREMER $oDir/cmTile/cmFiles/IFREMER.cmxyzi ;
csh jjCrunchCM $badlist $MOA_private/GEBCO $oDir/cmTile/cmFiles/GEBCO.cmxyzi ;
csh jjCrunchCM $badlist $MOA_private/3DGBR $oDir/cmTile/cmFiles/3DGBR.cmxyzi ;
wait
csh jjCrunchCM $badlist $MOA_public/NOAA_geodas $oDir/cmTile/cmFiles/NOAA_geodas.cmxyzi ;
csh jjCrunchCM $badlist $MOA_public/NGA $oDir/cmTile/cmFiles/NGA.cmxyzi ;
csh jjCrunchCM $badlist $MOA_public/US_multi $oDir/cmTile/cmFiles/US_multi.cmxyzi ;
csh jjCrunchCM $badlist $MOA_public/CCOM $oDir/cmTile/cmFiles/CCOM.cmxyzi ;
wait
csh jjCrunchCM $badlist $MOA_public/JAMSTEC $oDir/cmTile/cmFiles/JAMSTEC.cmxyzi ;
csh jjCrunchCM $badlist $MOA_public/NAVO $oDir/cmTile/cmFiles/NAVO.cmxyzi ;
csh jjCrunchCM $badlist $MOA_public/NOAA $oDir/cmTile/cmFiles/NOAA.cmxyzi ;
csh jjCrunchCM $badlist $MOA_public/SIO $oDir/cmTile/cmFiles/SIO.cmxyzi ;
wait
csh jjCrunchCM $badlist $MOA_public/SIO_multi $oDir/cmTile/cmFiles/SIO_multi.cmxyzi ;
csh jjCrunchCM $badlist $MOA_public/IBCAO $oDir/cmTile/cmFiles/IBCAO.cmxyzi ;
csh jjCrunchCM $badlist $MOA_public/US_multi $oDir/cmTile/cmFiles/US_multi.cmxyzi ;
csh jjCrunchCM $badlist $MOA_public/lakes $oDir/cmTile/cmFiles/lakes.cmxyzi ;
wait

# use data from DNC, but set the sid = 0...
# this is a relatively small file, no need to background it.

#FIXME: need some smart way to select DNC or not...
if (6 == 6) then
	# FIXME: zero DNC sid
	echo "zeroing DNC sid..."
	cat $oDir/cmTile/cmFiles/DNC_prop.tmp | awk '{ print $1, $2, $3, 00000 }'	>! $oDir/cmTile/cmFiles/DNC_prop.cmxyzi
else
	# FIXME: keep DNC sid
	echo "keeping DNC sid..."
	cat  $oDir/cmTile/cmFiles/DNC_prop.tmp | awk '{ print }' 	                >! $oDir/cmTile/cmFiles/DNC_prop.cmxyzi
endif
# /bin/rm -rf $oDir/cmTile/cmFiles/DNC_prop.tmp



# gather all this data into one huge file.
# this is convenient and efficient if DRAM > 10GB.

cat  $oDir/cmTile/cmFiles/*.cmxyzi >! $oDir/cmTile/huge/huge.xyzi



# Dave wants to hack in some arctic grid.
# Later, some where else, we replace predicted with arctic above $maxPred

set GRD2XYZ_AWKSCRIPT = '{ printf "%s %05d\n", $0, '$arcticSid' }'

grd2xyz $arcticGrd -R/-180/180/$minArctic/$maxPred -S | \
		awk "$GRD2XYZ_AWKSCRIPT" \
		>! $oDir/cmTile/cmFiles/arcticHackForDave

cat $oDir/cmTile/cmFiles/arcticHackForDave >> $oDir/cmTile/huge/huge.xyzi


# Now break huge file into SRTM tiles.

makeAllCmHugeStrips.sh -180 180 -90 90 15 15 $oDir/cmTile/huge $oDir/cmTile/huge/huge.xyzi
makeAllCmSmallTiles.sh -180 180 -90 90 15 15 $oDir/cmTile/ $oDir/cmTile/huge/


# last, but certainly not least, deal with 1 degree pad at +/-180 
#	blockmedian doesn't work with lon > 180, in the -R spec
#		but it does work if data itself crosses +/-180
#
# append thin strip of tiles with lon (+179 to +180) 
#	unto tiles with lon (-180 to -140) 
#
# DO --NOT-- screw around with +/-180; 
#
# do NOT freak out, it is just 1 degree pad

cat $oDir/cmTile/e165n90.xyzi | awk '{ if ($1 >= +179) { print } }' >> $oDir/cmTile/w180n90.xyzi &
cat $oDir/cmTile/e165n75.xyzi | awk '{ if ($1 >= +179) { print } }' >> $oDir/cmTile/w180n75.xyzi &
cat $oDir/cmTile/e165n60.xyzi | awk '{ if ($1 >= +179) { print } }' >> $oDir/cmTile/w180n60.xyzi &
cat $oDir/cmTile/e165n45.xyzi | awk '{ if ($1 >= +179) { print } }' >> $oDir/cmTile/w180n45.xyzi &
cat $oDir/cmTile/e165n30.xyzi | awk '{ if ($1 >= +179) { print } }' >> $oDir/cmTile/w180n30.xyzi &
cat $oDir/cmTile/e165n15.xyzi | awk '{ if ($1 >= +179) { print } }' >> $oDir/cmTile/w180n15.xyzi &
cat $oDir/cmTile/e165n00.xyzi | awk '{ if ($1 >= +179) { print } }' >> $oDir/cmTile/w180n00.xyzi &
cat $oDir/cmTile/e165s15.xyzi | awk '{ if ($1 >= +179) { print } }' >> $oDir/cmTile/w180s15.xyzi &
cat $oDir/cmTile/e165s30.xyzi | awk '{ if ($1 >= +179) { print } }' >> $oDir/cmTile/w180s30.xyzi &
cat $oDir/cmTile/e165s45.xyzi | awk '{ if ($1 >= +179) { print } }' >> $oDir/cmTile/w180s45.xyzi &
cat $oDir/cmTile/e165s60.xyzi | awk '{ if ($1 >= +179) { print } }' >> $oDir/cmTile/w180s60.xyzi &
cat $oDir/cmTile/e165s75.xyzi | awk '{ if ($1 >= +179) { print } }' >> $oDir/cmTile/w180s75.xyzi &
wait
cat $oDir/cmTile/w180n90.xyzi | awk '{ if ($1 >= +179) { print } }' >> $oDir/cmTile/e165n90.xyzi &
cat $oDir/cmTile/w180n75.xyzi | awk '{ if ($1 >= +179) { print } }' >> $oDir/cmTile/e165n75.xyzi &
cat $oDir/cmTile/w180n60.xyzi | awk '{ if ($1 >= +179) { print } }' >> $oDir/cmTile/e165n60.xyzi &
cat $oDir/cmTile/w180n45.xyzi | awk '{ if ($1 >= +179) { print } }' >> $oDir/cmTile/e165n45.xyzi &
cat $oDir/cmTile/w180n30.xyzi | awk '{ if ($1 >= +179) { print } }' >> $oDir/cmTile/e165n30.xyzi &
cat $oDir/cmTile/w180n15.xyzi | awk '{ if ($1 >= +179) { print } }' >> $oDir/cmTile/e165n15.xyzi &
cat $oDir/cmTile/w180n00.xyzi | awk '{ if ($1 >= +179) { print } }' >> $oDir/cmTile/e165n00.xyzi &
cat $oDir/cmTile/w180s15.xyzi | awk '{ if ($1 >= +179) { print } }' >> $oDir/cmTile/e165s15.xyzi &
cat $oDir/cmTile/w180s30.xyzi | awk '{ if ($1 >= +179) { print } }' >> $oDir/cmTile/e165s30.xyzi &
cat $oDir/cmTile/w180s45.xyzi | awk '{ if ($1 >= +179) { print } }' >> $oDir/cmTile/e165s45.xyzi &
cat $oDir/cmTile/w180s60.xyzi | awk '{ if ($1 >= +179) { print } }' >> $oDir/cmTile/e165s60.xyzi &
cat $oDir/cmTile/w180s75.xyzi | awk '{ if ($1 >= +179) { print } }' >> $oDir/cmTile/e165s75.xyzi &
wait

/bin/rm $oDir/tmp/`basename $0`*
date
