[GRASS-SVN] r44742 - grass-promo/tutorials/batch_processing/earthquakes

svn_grass at osgeo.org svn_grass at osgeo.org
Tue Dec 28 03:58:01 EST 2010


Author: hamish
Date: 2010-12-28 00:58:01 -0800 (Tue, 28 Dec 2010)
New Revision: 44742

Added:
   grass-promo/tutorials/batch_processing/earthquakes/README
   grass-promo/tutorials/batch_processing/earthquakes/do_quakes_latlon.sh
   grass-promo/tutorials/batch_processing/earthquakes/do_quakes_wintri.sh
   grass-promo/tutorials/batch_processing/earthquakes/earthquake_plot_job.sh
   grass-promo/tutorials/batch_processing/earthquakes/php_demo/
Log:
earthquake cronjob example

Added: grass-promo/tutorials/batch_processing/earthquakes/README
===================================================================
--- grass-promo/tutorials/batch_processing/earthquakes/README	                        (rev 0)
+++ grass-promo/tutorials/batch_processing/earthquakes/README	2010-12-28 08:58:01 UTC (rev 44742)
@@ -0,0 +1,11 @@
+Example of a cron job running on a server to fetch earthquake data from the USGS
+for the last week and plot it up.
+
+Based on Markus's PHP earthquake demo (copied here for completeness)
+
+
+This is just a (working) example. You'll have to adjust the paths, etc. to match your needs.
+
+
+HB Dec2010
+

Added: grass-promo/tutorials/batch_processing/earthquakes/do_quakes_latlon.sh
===================================================================
--- grass-promo/tutorials/batch_processing/earthquakes/do_quakes_latlon.sh	                        (rev 0)
+++ grass-promo/tutorials/batch_processing/earthquakes/do_quakes_latlon.sh	2010-12-28 08:58:01 UTC (rev 44742)
@@ -0,0 +1,185 @@
+#!/bin/sh
+#
+# script to download, import, and plot earthquakes from the last week.
+#
+# (c) 2010 Hamish Bowman, and the GRASS Development Team
+# Based on earlier version by Markus Neteler, which in turn was based on
+#  comments from Sharyn Namnath and Glynn Clements.
+#
+# This script is licensed under the GPL >=2. See GPL.TXT which comes with
+#  the GRASS source code for details.
+#
+# World DTM picture is from the ETOPO1 dataset,
+#   http://www.ngdc.noaa.gov/mgg/global/global.html
+# Nice colors were done in GMT by J. Varner and E. Lim, CIRES, University
+#  of Colorado at Boulder.
+#
+# Earthquake data is downloaded from the USGS
+#  http://neic.usgs.gov/neis/gis/bulletin.asc
+#
+
+if  [ -z "$GISBASE" ] ; then
+    echo "You must be in GRASS GIS to run this program." >&2
+    exit 1
+fi
+
+#GRASS_PNGFILE=earthquakes.png
+GRASS_PNGFILE=earthquakes.ppm
+GRASS_WIDTH=900
+GRASS_HEIGHT=450
+GRASS_TRUECOLOR=TRUE
+GRASS_PNG_COMPRESSION=9
+GRASS_VERBOSE=0
+export GRASS_WIDTH GRASS_HEIGHT GRASS_TRUECOLOR GRASS_PNG_COMPRESSION \
+   GRASS_VERBOSE GRASS_PNGFILE
+
+TMPDIR="/var/local/grass/tmp"
+if [ ! -d "$TMPDIR" ] ; then
+   mkdir -p "$TMPDIR"
+fi
+cd "$TMPDIR"
+
+
+### download and import
+
+wget -nv -O "$TMPDIR/bulletin.tmp" http://neic.usgs.gov/neis/gis/bulletin.asc 2>&1
+if [ $? -ne 0 ] ; then
+   echo "Failed to download data from the USGS." >&2
+   exit 1
+fi
+
+# bulletin.asc format:
+#Date,TimeUTC,Latitude,Longitude,Magnitude,Depth
+# date is yy/mm/dd so not SQL Date type!
+
+COL_DEF="e_date varchar(8), e_time varchar(10), latitude double precision, \
+  longitude double precision, magnitude double precision, depth double precision"
+
+INPUT="$TMPDIR/bulletin.tmp"
+
+db.connect -c
+DBDRIVER=`db.connect -p | grep '^driver:' | cut -f2 -d:`
+
+if [ "$DBDRIVER" != 'sqlite' ] ; then
+   # a bit slower, but we can use longer column names
+   db.connect driver=sqlite database='$GISDBASE/$LOCATION_NAME/$MAPSET/sqlite.db'
+fi
+
+v.in.ascii in="$INPUT" out=recent_earthquakes skip=1 \
+    fs=',' y=3 x=4 z=6 column="$COL_DEF" --overwrite
+
+
+### unlog & scale magnitude
+
+# http://earthquake.usgs.gov/learn/topics/measure.php
+#    1 J = 1e7 erg
+#    1 PJ = 1e15 J
+#   Energy_petajoules = ( 10^(11.8 + 1.5*Ms) ) * 1e-07 * 1e-15
+#
+# rationale: keep within pixel equiv of 90deg lon from source pt so a
+#            quake in the middle makes it onto the plot.
+#   (8.8^10) * 1e-7 = 278.5     : very rough unlog scale
+#
+
+v.db.addcol recent_earthquakes column="magn_energy DOUBLE PRECISION"
+
+# how to do POW(x,n) in SQLite?
+#v.db.update recent_earthquakes column=magn_energy value="POW(magnitude,10) * 1e-7" --verbose
+
+v.db.select recent_earthquakes column=cat,magnitude | awk -F'|' \
+  '{printf("UPDATE recent_earthquakes SET magn_energy=%f WHERE cat=%d;\n", ($2^10)*1e-7, $1)}' | db.execute
+
+
+
+# get the timestamp
+YMD=20`tail -n 1 "$INPUT"  | cut -f1 -d,`
+if [ `echo "$YMD" | wc -c` -ne 11 ] ; then
+    echo "Bad timestamp ($YMD). Using system date instead." >&2
+    YMD=`date +%Y/%m/%d`
+fi
+
+
+### draw
+
+g.region n=90N s=90S w=25W e=25W res=0:24  # -p
+
+d.mon start=PNG
+
+d.rgb r=color_etopo1_ice_900.red at etopo1 g=color_etopo1_ice_900.green at etopo1 \
+    b=color_etopo1_ice_900.blue at etopo1
+
+# d.rast -o usgs_logo
+# d.rast -o grass_logo
+
+d.font Vera
+echo "Earthquakes for the week ending $YMD" | \
+   d.text at=1,2 color=60:60:60 size=3
+
+#echo "
+#.G 250:250:250
+#.C red
+#Shallow
+#.C yellow
+#Intermediate
+#.C green
+#Deep" | d.text at=99,99 size=3 align=ur
+
+
+#quake3.png
+#d.vect recent_earthquakes icon=extra/ring fcolor=none size_col=magn_energy \
+#   size=5 wcol=magn_energy wscale=0.01 -z zcolor=ryg  
+#
+# size=5 * 0.1
+# magn^3
+# wscale=0.01
+
+#quake5.png:
+#d.vect recent_earthquakes icon=extra/ring fcolor=none size_col=magn_energy \
+#   size=1 wcol=magn_energy wscale=0.0005 -z zcolor=ryg
+#
+# size=1 * 0.01
+# magn^5
+# wscale=0.0005
+
+
+#quake10.png:
+d.vect recent_earthquakes icon=extra/ring fcolor=none size_col=magn_energy \
+   size=3 wcol=magn_energy wscale=0.2 -z zcolor=ryg 
+#
+# size=3
+# (magn^10) * 1e-7
+# wscale=0.1
+
+#quake2.72.png:
+# size=1
+# magn^2.72
+# wscale=0.01
+
+sync
+d.mon stop=PNG
+sync
+
+IMG=/usr/local/grass/earthquakes/graphics/earthquakes_logo_overlay
+#pngtopnm $IMG.png > $IMG.pnm
+#pngtopnm -alpha $IMG.png > $IMG.pgm
+pnmcomp -alpha $IMG.pgm $IMG.pnm earthquakes.ppm | \
+   pnmtopng > earthquakes.png
+
+if [ $? -ne 0 ] ; then
+   echo "Failed to compose image." 1>&2
+   exit 1
+fi
+
+pngtopnm earthquakes.png | pnmtojpeg -quality=85 > earthquakes.jpg
+convert -geometry 75% -quality 85 earthquakes.png earthquakes_small.jpg
+convert -geometry 40% -quality 85 earthquakes.png earthquakes_tiny.jpg
+
+
+### cleanup and closeup
+g.remove vect=recent_earthquakes --quiet
+rm -f "$TMPDIR/bulletin.tmp"
+
+cp -f earthquakes.png earthquakes*.jpg /var/www/grass/
+
+# all done.
+exit 0


Property changes on: grass-promo/tutorials/batch_processing/earthquakes/do_quakes_latlon.sh
___________________________________________________________________
Added: svn:executable
   + *

Added: grass-promo/tutorials/batch_processing/earthquakes/do_quakes_wintri.sh
===================================================================
--- grass-promo/tutorials/batch_processing/earthquakes/do_quakes_wintri.sh	                        (rev 0)
+++ grass-promo/tutorials/batch_processing/earthquakes/do_quakes_wintri.sh	2010-12-28 08:58:01 UTC (rev 44742)
@@ -0,0 +1,205 @@
+#!/bin/sh
+#
+# script to download, import, and plot earthquakes from the last week.
+#  modified version to plot in the Winkel Tripel projection
+#
+# (c) 2010 Hamish Bowman, and the GRASS Development Team
+# Based on earlier version by Markus Neteler, which in turn was based on
+#  comments from Sharyn Namnath and Glynn Clements.
+#
+# This script is licensed under the GPL >=2. See GPL.TXT which comes with
+#  the GRASS source code for details.
+#
+# World DTM picture is from the ETOPO1 dataset,
+#   http://www.ngdc.noaa.gov/mgg/global/global.html
+# Nice colors were done in GMT by J. Varner and E. Lim, CIRES, University
+#  of Colorado at Boulder.
+#
+# Earthquake data is downloaded from the USGS
+#  http://neic.usgs.gov/neis/gis/bulletin.asc
+#
+
+if  [ -z "$GISBASE" ] ; then
+    echo "You must be in GRASS GIS to run this program." >&2
+    exit 1
+fi
+
+#GRASS_PNGFILE=earthquakes.png
+GRASS_PNGFILE=earthquakes.ppm
+GRASS_WIDTH=900
+GRASS_HEIGHT=450
+GRASS_TRUECOLOR=TRUE
+GRASS_PNG_COMPRESSION=9
+GRASS_VERBOSE=0
+export GRASS_WIDTH GRASS_HEIGHT GRASS_TRUECOLOR GRASS_PNG_COMPRESSION \
+   GRASS_VERBOSE GRASS_PNGFILE
+
+TMPDIR="/var/local/grass/tmp"
+if [ ! -d "$TMPDIR" ] ; then
+   mkdir -p "$TMPDIR"
+fi
+cd "$TMPDIR"
+
+# remove old stuff
+g.remove vect=recent_earthquakes --quiet
+
+
+### download and import
+
+wget -nv -O "$TMPDIR/bulletin.tmp" http://neic.usgs.gov/neis/gis/bulletin.asc 2>&1
+if [ $? -ne 0 ] ; then
+   echo "Failed to download data from the USGS." >&2
+   exit 1
+fi
+
+# bulletin.asc format:
+#Date,TimeUTC,Latitude,Longitude,Magnitude,Depth
+# date is yy/mm/dd so not SQL Date type!
+
+INPUT="$TMPDIR/bulletin.tmp"
+
+sed -i -e 's/^Date/#Date/' "$INPUT"
+
+cut -f3,4 -d, "$INPUT" | awk -F, '{print $2 "\t" $1}' | \
+   m.proj -ig fs=, | cut -f1,2 -d, | \
+   awk '{if (NR!=1) {print} else {print "easting,northing"}}' \
+  > "$INPUT.wintri.coord"
+
+paste -d, "$INPUT" "$INPUT.wintri.coord" > "$INPUT.wintri"
+
+
+COL_DEF="e_date varchar(8), e_time varchar(10), latitude double precision, \
+  longitude double precision, magnitude double precision, depth double precision, \
+  easting double precision, northing double precision"
+
+db.connect -c
+DBDRIVER=`db.connect -p | grep '^driver:' | cut -f2 -d:`
+
+if [ "$DBDRIVER" != 'sqlite' ] ; then
+   # a bit slower, but we can use longer column names
+   db.connect driver=sqlite database='$GISDBASE/$LOCATION_NAME/$MAPSET/sqlite.db'
+fi
+
+v.in.ascii in="$INPUT.wintri" out=recent_earthquakes skip=1 \
+    fs=',' x=7 y=8 z=6 column="$COL_DEF" --overwrite
+
+
+### unlog & scale magnitude
+
+# http://earthquake.usgs.gov/learn/topics/measure.php
+#    1 J = 1e7 erg
+#    1 PJ = 1e15 J
+#   Energy_petajoules = ( 10^(11.8 + 1.5*Ms) ) * 1e-07 * 1e-15
+#
+# rationale: keep within pixel equiv of 90deg lon from source pt so a
+#            quake in the middle makes it onto the plot.
+#   (8.8^10) * 1e-7 = 278.5     : very rough unlog scale
+#
+
+v.db.addcol recent_earthquakes column="magn_energy DOUBLE PRECISION"
+
+# how to do POW(x,n) in SQLite?
+#v.db.update recent_earthquakes column=magn_energy value="POW(magnitude,10) * 1e-7" --verbose
+
+v.db.select recent_earthquakes column=cat,magnitude | awk -F'|' \
+  '{printf("UPDATE recent_earthquakes SET magn_energy=%f WHERE cat=%d;\n", ($2^10)*1e-7, $1)}' | db.execute
+
+
+
+# get the timestamp
+YMD=20`tail -n 1 "$INPUT"  | cut -f1 -d,`
+if [ `echo "$YMD" | wc -c` -ne 11 ] ; then
+    echo "Bad timestamp ($YMD). Using system date instead." >&2
+    YMD=`date +%Y/%m/%d`
+fi
+
+
+### draw
+# LatLon:
+#g.region n=90N s=90S w=25W e=25W res=0:24  # -p
+# Winkel Tripel:
+g.region n=10035000 s=-10035000 w=-20070000 e=20070000 res=44600  # rows=450 cols=900
+
+
+d.mon start=PNG
+
+MAP=color_etopo1_ice_900
+d.rgb r=$MAP.red at etopo1 g=$MAP.green at etopo1 b=$MAP.blue at etopo1
+
+# d.rast -o usgs_logo
+# d.rast -o grass_logo
+
+d.font Vera
+echo "Earthquakes for the week ending $YMD" | \
+   d.text at=1,2 color=60:60:60 size=3
+
+
+echo "
+.C red
+Shallow
+.C yellow
+Intermediate
+.C green
+Deep
+" | d.text at=99,99 size=3 align=ur
+
+
+#quake3.png
+#d.vect recent_earthquakes icon=extra/ring fcolor=none size_col=magn_energy \
+#   size=5 wcol=magn_energy wscale=0.01 -z zcolor=ryg  
+#
+# size=5 * 0.1
+# magn^3
+# wscale=0.01
+
+#quake5.png:
+#d.vect recent_earthquakes icon=extra/ring fcolor=none size_col=magn_energy \
+#   size=1 wcol=magn_energy wscale=0.0005 -z zcolor=ryg
+#
+# size=1 * 0.01
+# magn^5
+# wscale=0.0005
+
+
+#quake10.png:
+d.vect recent_earthquakes icon=extra/ring fcolor=none size_col=magn_energy \
+   size=3 wcol=magn_energy wscale=0.2 -z zcolor=ryg 
+#
+# size=3
+# (magn^10) * 1e-7
+# wscale=0.1
+
+#quake2.72.png:
+# size=1
+# magn^2.72
+# wscale=0.01
+
+sync
+d.mon stop=PNG
+sync
+
+IMG=/usr/local/grass/earthquakes/graphics/earthquakes_logo_overlay
+#pngtopnm $IMG.png > $IMG.pnm
+#pngtopnm -alpha $IMG.png > $IMG.pgm
+pnmcomp -alpha $IMG.pgm $IMG.pnm earthquakes.ppm | \
+   pnmtopng > earthquakes.png
+
+if [ $? -ne 0 ] ; then
+   echo "Failed to compose image." 1>&2
+   exit 1
+fi
+
+#pngtopnm earthquakes.png | pnmtojpeg -quality=85 > earthquakes.jpg
+#convert -geometry 75% -quality 85 earthquakes.png earthquakes_small.jpg
+#convert -geometry 40% -quality 85 earthquakes.png earthquakes_tiny.jpg
+
+
+### cleanup and closeup
+#g.remove vect=recent_earthquakes --quiet
+rm -f "$INPUT"*
+
+#cp -f earthquakes.png earthquakes*.jpg /var/www/grass/
+cp earthquakes.png /var/www/grass/alternate_projections/earthquakes_wintri.png
+
+# all done.
+exit 0


Property changes on: grass-promo/tutorials/batch_processing/earthquakes/do_quakes_wintri.sh
___________________________________________________________________
Added: svn:executable
   + *

Added: grass-promo/tutorials/batch_processing/earthquakes/earthquake_plot_job.sh
===================================================================
--- grass-promo/tutorials/batch_processing/earthquakes/earthquake_plot_job.sh	                        (rev 0)
+++ grass-promo/tutorials/batch_processing/earthquakes/earthquake_plot_job.sh	2010-12-28 08:58:01 UTC (rev 44742)
@@ -0,0 +1,50 @@
+#!/bin/sh
+############################################################################
+#
+# MODULE:       earthquake_plot_job.sh
+# AUTHOR:       M. Hamish Bowman, Depts.of Marine Science and Geology,
+#                 Otago Univeristy, New Zealand
+# PURPOSE:      Wrapper script to launch GRASS batch job
+#                 (perhaps called by cron)
+#
+# COPYRIGHT:    (c) 2010 Hamish Bowman, and the GRASS Development Team
+#               This program is free software under the GNU General Public
+#               License (>=v2). Read the file COPYING that comes with GRASS
+#               for details.
+#
+#############################################################################
+# Inspired by Markus Neteler's PHP demo, which in turn was seeded from the ideas
+#    of [...] and Glynn Clements
+# Adjust paths etc. to suit.
+
+## sample crontab:
+#  # m h         dom mon dow   command
+#    4 2,8,14,20 *   *   *     /usr/local/grass/earthquakes/earthquake_plot_job.sh > /usr/local/grass/earthquakes/earthquake_plot_job.log  2> /usr/local/grass/earthquakes/earthquake_plot_job.err
+#
+
+
+GRASS_BASEDIR="/usr/local/grass"
+
+
+# avoid a needless search
+GRASS_HTML_BROWSER=false
+export GRASS_HTML_BROWSER
+
+#### Run the job to make and install the latest earthquake plots
+
+# unprojected (Plate Carree) version
+date
+GRASS_BATCH_JOB="$GRASS_BASEDIR/earthquakes/do_quakes_latlon.sh"
+export GRASS_BATCH_JOB
+nice grass64 "$GRASS_BASEDIR/grassdata/ll_wgs84/earthquakes"
+unset GRASS_BATCH_JOB
+
+# projected version
+date
+GRASS_BATCH_JOB="$GRASS_BASEDIR/earthquakes/do_quakes_wintri.sh"
+export GRASS_BATCH_JOB
+nice grass64 "$GRASS_BASEDIR/grassdata/winkel_III/earthquakes"
+unset GRASS_BATCH_JOB
+
+
+exit


Property changes on: grass-promo/tutorials/batch_processing/earthquakes/earthquake_plot_job.sh
___________________________________________________________________
Added: svn:executable
   + *



More information about the grass-commit mailing list