#!/bin/csh
set bsn = $1
set infile = $2
set var = $3
set units = $4
set title = $5
set outfile = $6
set min = $7
set max = $8
set dz = $9
set clrbar = $10
echo "bsn = $bsn ; outfl = $outfile"

#set bsn = pnw_2d

set tmppfx = jnk$$

echo $title >! ${tmppfx}_ttl
set title = `sed 's/\./ /g' ${tmppfx}_ttl`
rm -f ${tmppfx}_ttl

# rm missing data if present as -9999 ... regardless, cpy infile to temporary file:
awk '($3!=-9999){print $0;}' $infile >! ${tmppfx}.xyz
awk '($3==-9999){print $0;}' $infile >! ${tmppfx}.baddat.xyz

set proj = JM7.5i
set symb = Ss0.04c
set brdr = Bf0.5/f0.5
set ttlsz = 18
set csc = -3.5/-1.0/16/0.25h
if ( "$bsn" == world ) then
  set coord = -180/180/-90/90
  set proj = JQ7.5i
  set brdr = Bf30/f15
  set symb = Ss0.07c
  set ttlsz = 14
  set xoffset = 1
  set yoffset = 15.5
  set txtxshift = 9.5
  set txtyshift = 10
  set xcsc = 3.5
  set ycsc = -9.6
else if ( "$bsn" == pnw || "$bsn" == pnw_16d || "$bsn" == pnw_30s || "$bsn" == pnw_2d || "$bsn" == pnw_24d ) then
  set coord = -125.5/-109/41.05/53
  set proj = JM7.5i
  set brdr = Bf1/f1
  set symb = Ss0.35c
  set ttlsz = 14
  set xoffset = 1.5
  set yoffset = 7.5
  set txtxshift = 9.5
  set txtyshift = 20
  set xcsc = 3.5
  set ycsc = -19.6
  if ( "$bsn" == pnw_30s ) then
    set symb = Ss0.01c
  else if ( "$bsn" == pnw_16d ) then
    set symb = Ss0.15c
  else if ( "$bsn" == pnw_2d ) then
    set symb = Ss1.2c
  else if ( "$bsn" == pnw_24d ) then
    set symb = Ss0.1c
  endif
else if ( "$bsn" == pnw49 || "$bsn" == pnw49_30s || "$bsn" == pnw49_16d || "$bsn" == pnw49_24d ) then
  set coord = -124.9/-109.65/41.05/49.25
  set proj = JM6.75i
  set brdr = Bf1/f1
  set symb = Ss0.01c
  set ttlsz = 14
  set xoffset = 0.35
  set yoffset = 15
  set txtxshift = 8.5
  set txtyshift = 12.5
  set xcsc = 0.5
  set ycsc = -10
  set csc = 9/3.8/13/0.25
  if ( "$bsn" == pnw49_16d ) then
    set symb = Ss0.35c
  else if ( "$bsn" == pnw49_24d ) then
    set symb = Ss0.20c
  endif
else if ( "$bsn" == caor || "$bsn" == caor_16d ) then
  set coord = -125.5/-114/32.1/43.9
  set proj = JM5.5i
  set brdr = Bf2/f2
  set symb = Ss0.25c
  set ttlsz = 18
  set xoffset = 3
  set yoffset = 9.5
  set txtxshift = 7
  set txtyshift = 18
  set xcsc = 3.5
  set ycsc = -18
else if ( "$bsn" == wus || "$bsn" == WUS || "$bsn" == wus_2d ) then
  set coord = -125.5/-103/32.1/50.25
  set proj = JM5.5i
  set brdr = Bf2/f2
  set symb = Ss0.25c
  if ( "$bsn" == wus_2d ) then
    set symb = Ss1c
  endif
  set ttlsz = 14
  set xoffset = 4
  set yoffset = 12.5
  set txtxshift = 7
  set txtyshift = 14.9
  set xcsc = 3.5
  set ycsc = -14.5
else if ( "$bsn" == wna || "$bsn" == WNA  || "$bsn" == wna_2d || "$bsn" == wna_16d || "$bsn" == wna_30s ) then
  set coord = -140.5/-109.5/41/60.15
  set proj = JM7.5i
  set brdr = Bf2/f2
  set symb = Ss0.01c
  if ( "$bsn" == wna_16d ) then
    set symb = Ss0.1c
  else if  ( "$bsn" == wna_2d ) then
    set symb = Ss0.8c
  endif
  set ttlsz = 20
  set xoffset = 1
  set yoffset = 8.75
  set txtxshift = 9.5
  set txtyshift = 18.65
  set xcsc = 3.5
  set ycsc = -18.25
###
#  set xoffset = -3
#  set symb = Ss0.1c
#  set xcsc = 11
#  set ycsc = -10
#  set csc = 0/0/15/0.25
else if ( "$bsn" == ak || "$bsn" == AK || "$bsn" == ak_2km || "$bsn" == ak_2d ) then
  awk '{if($2>0){print $0;}else{print $1,$2+360,$3;}}' $infile >! ${tmppfx}.xyz
  set coord = 172/230.5/51/72
  set proj = JM7.8i
  set brdr = Bf2/f2
  set symb = Ss0.15c
  if ( "$bsn" == ak_2km ) then
    set symb = Ss0.02c
  else if  ( "$bsn" == ak_2d ) then
    set symb = Ss0.4c
  endif
  set ttlsz = 14
  set xoffset = 0.5
  set yoffset = 11.5
  set txtxshift = 10
  set txtyshift = 15.5
  set xcsc = 3.5
  set ycsc = -15.1
else if ( "$bsn" == aleutians ) then
  set coord = 172/210/51/60
  set proj = JM7.8i
  set brdr = Bf2/f2
  set symb = Ss0.15c
  set ttlsz = 14
  set xoffset = 0.5
  set yoffset = 11.5
  set txtxshift = 10
  set txtyshift = 15.5
  set xcsc = 3.5
  set ycsc = -15.1
else if ( "$bsn" == bc || "$bsn" == BC || "$bsn" == bc_30s || "$bsn" == bc_16d || "$bsn" == bc_2d ) then
  set coord = -140/-113.5/48.0/60.5
  set proj = JM7.7i
  set brdr = Bf1/f1
  set symb = Ss0.25c
  if ( "$bsn" == bc_30s ) then
    set symb = Ss0.04c
  else if ( "$bsn" == bc_16d ) then
    set symb = Ss0.1c
  else if ( "$bsn" == bc_2d ) then
    set symb = Ss0.75c
  endif
  set ttlsz = 14
  set xoffset = 1.0
  set yoffset = 12.0
  set txtxshift = 9.75
  set txtyshift = 15.9
  set xcsc = 3.5
  set ycsc = -15.5
else if ( "$bsn" == npr || "$bsn" == npr_2d || "$bsn" == NPR || "$bsn" == npr_8d || "$bsn" == npr_4d ) then
  mv ${tmppfx}.xyz ${tmppfx}_2.xyz
  awk '{if($2>0){print $0;}else{print $1,$2+360,$3;}}' ${tmppfx}_2.xyz >! ${tmppfx}.xyz
  rm -f ${tmppfx}_2.xyz
  mv ${tmppfx}.baddat.xyz ${tmppfx}_2.xyz
  awk '{if($2>0){print $0;}else{print $1,$2+360,$3;}}' ${tmppfx}_2.xyz >! ${tmppfx}.baddat.xyz
  rm -f ${tmppfx}_2.xyz
  set coord = 100/260/28/77
  set proj = JM7.5i
  set brdr = Bf5/f5
  if ( "$bsn" == npr_2d ) then
    set symb = Ss0.225c
  else if ( "$bsn" == npr_4d ) then
    set symb = Ss0.07c
  else if ( "$bsn" == npr_8d ) then
    set symb = Ss0.05c
  else
    set symb = Ss0.05c
  endif
  set ttlsz = 14
  set xoffset = 1.0
  set yoffset = 16.0
  set txtxshift = 10
  set txtyshift = 11.1
  set xcsc = 3.5
  set ycsc = -10.6
else if ( "$bsn" == or || "$bsn" == OR || "$bsn" == or_30s ) then 
  set coord = -124.75/-116.25/41.75/46.5
  set proj = JM7.5i
  set brdr = Bf1/f1
  set symb = Ss0.25c
  set ttlsz = 26
  set xoffset = 1.5
  set yoffset = 12.5
  set txtxshift = 9.5
  set txtyshift = 15
  set xcsc = 3.5
  set ycsc = -15
  if ( "$bsn" == or_30s ) then
    set symb = Ss0.05c
  endif
else if ( "$bsn" == hja_30s ) then
  set coord = -122.275/-122.09/44.195/44.285
  set proj = JM7i
  set brdr = Bf0.01/f0.01
  set symb = Ss1.55c
  set ttlsz = 26
  set xoffset = 1
  set yoffset = 14
  set txtxshift = 9
  set txtyshift = 13
  set xcsc = 3.5
  set ycsc = -13
else if ( "$bsn" == us || "$bsn" == US || "$bsn" == us_30s ) then
  set coord = -125.25/-66.5/24/50
  set proj = JM8i
  set brdr = Bf5/f5
  set symb = Ss0.025c  # default: assume 1/24th deg (PRISM climo)
  set ttlsz = 26
  set xoffset = 0.3
  set yoffset = 15.5
  set txtxshift = 10
  set txtyshift = 12
  set xcsc = 3.5
  set ycsc = -12
  if ( "$bsn" == us_30s ) then
    set symb = Ss0.01c
  endif
else if ( "$bsn" == oly ) then
  set coord = -125/-122.2/46.7/48.7
  set proj = JM7.5i
  set brdr = Bf1/1
  set symb = Ss0.075i
  set ttlsz = 36
  set xoffset = 2
  set yoffset = 4
  set txtxshift = 9.5
  set txtyshift = 20
  set xcsc = 3.5
  set ycsc = -19.2
else if ( "$bsn" == ncasc || "$bsn" == ncasc_30s ) then
  set coord = -123/-117.5/46.25/49.25
  set proj = JM7.5i
  set brdr = Bf1/1
  set symb = Ss0.025i
  set ttlsz = 36
  set xoffset = 1
  set yoffset = 11
  set txtxshift = 9.5
  set txtyshift = 16
  set xcsc = 3.5
  set ycsc = -15.8
else if ( "$bsn" == snoq || "$bsn" == snoq_30s || "$bsn" == snoq_16d ) then
  set coord = -122/-120.2/46.85/47.95
  set proj = JM4.5i
  set brdr = Bf0.25/0.25
  set symb = Ss0.05i
  if ( "$bsn" == snoq_16d ) then
    set symb = Ss0.35i
  endif
  set ttlsz = 36
  set xoffset = 2
  set yoffset = 15
  set txtxshift = 5.5
  set txtyshift = 11
  set xcsc = -1
  set ycsc = -16
  set csc = 9/10/12/0.25
else if ( "$bsn" == orwa || "$bsn" == orwa_30s || "$bsn" == orwa_16d  || "$bsn" == wa_30s ) then
  set coord = -126/-116/41.5/49.5
  set proj = JM7.5i
  set brdr = Bf1/1
  set symb = Ss0.01i
  if ( "$bsn" == orwa_16d ) then
    set symb = Ss0.1i
  endif  
  set ttlsz = 36
  set xoffset = 1
  set yoffset = 4
  set txtxshift = 9.5
  set txtyshift = 22
  set xcsc = 3.5
  set ycsc = -21.8
#  set xoffset = -5
#  set csc = 9/10/12/0.25
else if ( "$bsn" == wus ) then
  set coord = -126/-102/29/53
  set proj = JM7.5i
  set brdr = Bf5/5
  set symb = Ss0.075i  
  set ttlsz = 36
  set xoffset = 1
  set yoffset = 2.9
  set txtxshift = 9
  set txtyshift = 25.7
  set xcsc = 3.8
  set ycsc = -25.2
else if ( "$bsn" == gb ) then
  set coord = -123/-108/33/45
  set proj = JM7.5i
  set brdr = Bf1/1
  set symb = Ss0.075i  
  set ttlsz = 36
  set xoffset = 2
  set yoffset = 8
  set txtxshift = 9.5
  set txtyshift = 20
  set xcsc = 3.5
  set ycsc = -19.5
else if ( "$bsn" == mb ) then
  set coord = -116/-102/37/50
  set proj = JM7.5i
  set brdr = Bf1/1
  set symb = Ss0.075i  
  set ttlsz = 36
  set xoffset = 2
  set yoffset = 3
  set txtxshift = 9.5
  set txtyshift = 25
  set xcsc = 3.5
  set ycsc = -25
else if ( "$bsn" == colo ) then
  set coord = -118/-103/29/44
  set proj = JM7.5i
  set brdr = Bf1/1
  set symb = Ss0.075i  
  set ttlsz = 36
  set xoffset = 2
  set yoffset = 4
  set txtxshift = 9.5
  set txtyshift = 24
  set xcsc = 3.5
  set ycsc = -24
else if ( "$bsn" == wlm ) then
  set coord = -123.1/-121.45/43.1/45.1
  set proj = JM5.5i
  set brdr = Bf0.25/f0.25
  set symb = Ss0.175c
  set xoffset = 1.5
  set yoffset = 4
  set txtxshift = 7.2
  set txtyshift = 24
  set xcsc = 3.35
  set ycsc = -23.5
  set csc = -3.5/-1.0/15/0.25h
else if ( "$bsn" == dsc ) then
  set coord = -122.5/-120.4/42.9/44.9
  set proj = JM7i
  set brdr = Bf0.25/f0.25
  set symb = Ss0.175c
  set xoffset = 1.5
  set yoffset = 4
  set txtxshift = 9.05
  set txtyshift = 24
  set xcsc = 3.35
  set ycsc = -23.5
  set csc = -3.5/-1.0/18/0.25h
else if ( "$bsn" == wok ) then
  set coord = -122.5/-119.25/46.75/49.05
  set proj = JM7.5i
  set brdr = Bf0.25/f0.25
  set ttlsz = 36
  set xoffset = 1
  set yoffset = 7
  set txtxshift = 9.5
  set txtyshift = 20.5
  set xcsc = 3.5
  set ycsc = -20.2
  set symb = Ss0.15c
  set csc = -3.5/-1.0/18/0.25h
else if ( "$bsn" == oly ) then
  set coord = -124.75/-122.65/47.3/48.4
  set proj = JM8i
  set brdr = Bf0.25/f0.25
  set xoffset = 0.35
  set yoffset = 11.5
  set txtxshift = 10.5
  set txtyshift = 16.5
  set xcsc = 3.15
  set ycsc = -16.2
  set symb = Ss0.2c
  set csc = -3.5/-1.0/20/0.25h
else if ( "$bsn" == mal ) then
  set coord = -119.9/-118.5/43.0/44.2
  set proj = JM7i
  set brdr = Bf0.25/f0.25
  set xoffset = 1
  set yoffset = 6.5
  set txtxshift = 9.2
  set txtyshift = 21.5
  set xcsc = 3.2
  set ycsc = -21.2
  set symb = Ss0.25c
  set csc = -3.5/-1.0/17/0.25h
else if ( "$bsn" == entiat ) then
  set coord = -121.25/-119.3/46.75/48.45
  set proj = JM7.5i
  set brdr = Bf0.25/f0.25
  set xoffset = 2
  set yoffset = 5
  set txtxshift = 9.5
  set txtyshift = 23
  set xcsc = 3.5
  set ycsc = -22.5
  set symb = Ss0.2c
else
  echo "ERROR: bsn '$bsn' not recognized"
  exit 1
endif

set outdir = `dirname $outfile`
set outps = `basename $outfile .ps`
set outps = `basename $outfile .png`
set outpng = ${outdir}/${outps}.png
set outps = ${outps}.ps

set coastcolor = 100/100/100
set landcolor = 175/175/175
set boundcolor = 255/255/255
GMT gmtset ANNOT_FONT_SIZE_PRIMARY 12 
GMT gmtset PAPER_MEDIA=A4
GMT gmtset LABEL_FONT_SIZE 14
GMT gmtset PLOT_DEGREE_FORMAT -D

set sfx = `echo $min | awk -F. '{print $NF;}'`
if ( "${sfx}" == "cpt" ) then
  echo "using input cpt: $min"
  cp $min ${tmppfx}.cpt
else
  set MIN = `awk 'BEGIN{min=1e10;} {if($3<min){min=$3;}} END{print min;}' ${tmppfx}.xyz`
  set MAX = `awk 'BEGIN{max=-1e9;} {if($3>max){max=$3;}} END{print max;}' ${tmppfx}.xyz`
  echo -n "min/max = $MIN $MAX ; "

  if ( "$min" == "" ) then
    set YLIMS = `echo $MIN $MAX | awk -f /home/disk/margaret/mauger/met_data/getylims.awk`
#    set YLIMS = `echo $MIN $MAX | awk -f /home/disk/margaret/mauger/met_data/getylims_anoms.awk`
  else
    set YLIMS = ( $min $max $dz )
  endif
  echo "ylims = ${YLIMS[1]}/${YLIMS[2]}/${YLIMS[3]}"

  if ( "$clrbar" == "" ) then
    set clrbar = jet
#    set clrbar = gray
  endif
  echo "clrbar = $clrbar"

  GMT gmtset COLOR_BACKGROUND 0/0/63
  GMT gmtset COLOR_FOREGROUND 63/0/0
  GMT makecpt -Z -C$clrbar -T${YLIMS[1]}/${YLIMS[2]}/${YLIMS[3]} >! ${tmppfx}.cpt
#cp ${tmppfx}.cpt tst.cpt
#cat tst.cpt
#  cp ${tmppfx}.cpt ~/met30s/precip_ann.cpt
#  cat ~/met30s/precip_ann.cpt
#  cp ~/met30s/precip_ann.cpt ${tmppfx}.cpt

#  GMT makecpt -Z -C$clrbar -T0/1300/100 >! ${tmppfx}.cpt
endif

# plot scenario maps
#-----------------------

set psc_cmd = "-K -V -P -$proj -R$coord -Di -S$coastcolor -C$coastcolor -G$landcolor -$brdr"

### PLOTS:
GMT pscoast $psc_cmd -Y$yoffset[1] -X$xoffset[1] -N2/2/$boundcolor -N1/2/$boundcolor >! $outps
awk '{print $1, $2, $3;}' ${tmppfx}.xyz | GMT psxy -C${tmppfx}.cpt -P -$proj -R$coord -V -O -K -$symb -: >> $outps
#GMT psxy ${tmppfx}.baddat.xyz -C${tmppfx}.cpt -P -$proj -R$coord -V -O -K -S+0.1c -: >> $outps
#GMT pscoast -V -O -K -$proj -R$coord -N1/2/$boundcolor -N2/2/$boundcolor -N3/2/$boundcolor >> $outps
GMT pstext -N -K -O -JX6.5/9.5 -R0/1/0/1 -X$txtxshift[1] -Y$txtyshift[1] << END >> $outps
0 0.15 $ttlsz 0 21 10 $bsn $title 
END

# scalebar
GMT psscale -C${tmppfx}.cpt -X$xcsc -Y$ycsc -L -E -D$csc -B:"$units": -O >> $outps

/bin/rm -f .gmt* *${tmppfx}*

convert $outps $outpng
#/bin/rm -f $outps

