#!/bin/bash
file=Map.ps
grdfile=../DATA/newbathy.grd
#grdfile=../DATA/Bathymetry_GEBCO_30s_world.grd

# Chile
xmin=-80
xmax=-50
ymax=-10
ymin=-35

midlon=`gmt gmtmath -Q $xmin $xmax ADD 2 DIV =`
midlat=`gmt gmtmath -Q $ymin $ymax ADD 2 DIV =`
R="-R$xmin/$xmax/$ymin/$ymax"
J="-JM$midlon/$midlat/16c"

# Make a 5-degree grid
B="-B5g5"

gmt grdcut $grdfile $R -Gtmpf.grd
gmt grdinfo tmpf.grd
gmt makecpt -Cglobe -T-8500/8500/50 -Z > cptfile2.cpt

#Create map with top title
gmt psbasemap $B -B+t"Chile" $J $R -K -P > $file
gmt grdgradient tmpf.grd -Nt1 -A225 -Gtmpgrd.grd
gmt grdimage $J $R tmpf.grd -Itmpgrd.grd -O -K -Ccptfile2.cpt >> $file
gmt pscoast $J -B $R -Dh -N1/1.00p,- -K -O -W >> $file

# Add text on a longitude/latitude on map (f12: size text)
gmt pstext -R -J -N -K -O -V -F+f12,1,blue+j << EOF >> $file
-55.0 -12.0 BR Add some text
EOF

# Add text on a longitude/latitude as sub-title outside map
gmt pstext -R -J -N -K -O -V -F+f15,1,black+j << EOF >> $file
-69.0 -38.8 BR ----------
EOF

# Draw seismic events (-h3: lines to skip on top)(0.06 size magnitude circles)
gmt psxy -R -J -O chilexy.txt -Wfaint -i3,2,4,5s0.1 -h3 -Scc -Cquakes.cpt -K >> $file

# Create legend input file for NEIS quake plot
# the line: N 1.9i 2.4i 2.0i placement of circles
cat > neis.legend << END
N 1.9i 2.4i 2.0i
#V 0 1p
S 0.1i c 0.1i red 0.25p 0.2i Shallow depth (0-100 km)
S 0.1i c 0.1i green 0.25p 0.2i Intermediate depth (100-300 km)
S 0.1i c 0.1i blue 0.25p 0.2i Very deep (> 300 km)
D 0 1p
V 0 1p
N 7
#V 0 1p
S 0.1i c 0.06i - 0.25p 0.3i M 3
S 0.1i c 0.07i - 0.25p 0.3i M 4
S 0.1i c 0.08i - 0.25p 0.3i M 5
S 0.1i c 0.09i - 0.25p 0.3i M 6
S 0.1i c 0.10i - 0.25p 0.3i M 7
S 0.1i c 0.11i - 0.25p 0.3i M 8
S 0.1i c 0.12i - 0.25p 0.3i M 9
#D 0 1p
#V 0 1p
#N 1
END
# placement and size of legend
gmt pslegend -F+glightyellow+p1p,0 -Dx0c/-2.5c+w16.0c/1.5c+l1.2 -C0.2c/0.2c -V -O neis.legend >> $file
 

outeps=`echo $file | sed 's/ps/eps/g'`
eps2eps $file $outeps
gmt psconvert $outeps -Tj -P
gv $file
