Adding a Watermark to an Image

This is a common need – how do I add a watermark to an image I have created?

As it became obvious that other sites were not indicating where content was being leeched from, it was clear a another look at doing this was required.

This shell script is designed to loop through a number of RASP images and overlay a watermark to make aware where it has come from.

#!/bin/sh
# Script to add watermark on images
CONVERT_BIN=/usr/bin/convert
COMPOSITE_BIN=/usr/bin/composite

if [ "$1" != "" ]; then

        HOMEDIR=/home/rasp
        TARGETREGION=$1
        LOCATION=$HOMEDIR/$TARGETREGION/HTML/$TARGETREGION/FCST
        TMP_LOCATION=/tmp
        TARGET_LOCATION=$HOMEDIR/$TARGETREGION/HTML/$TARGETREGION/FCST
        SITENAME=
        WM_LOGO=$HOMEDIR/water-mark-logo.png
        WM_STAMP=/tmp/stamp_mask.png
        echo $HOMEDIR
        echo $TARGETREGION
        echo $LOCATION
        echo $TARGET_LOCATION
        echo $SITENAME
else
        echo "Provide a region to sync"
fi

        rm -f $WM_STAMP

  $CONVERT_BIN -size 553x213 xc:black -font Helvetica -pointsize 30 -gravity center \
          -draw "fill white  text  1,1  '$SITENAME'  \
                             text  0,0  '$SITENAME'  \
                 fill black  text -1,-1 '$SITENAME'" \
          +matte $WM_STAMP

# zsfclclmask wstar_bsratio bltopvariab stars experimental1


        for i in sfcwind blwind zsfclclmask wstar_bsratio hglider stars press850 rain1
        do
        # create the combined images
        for j in 0800lst 0900lst 1000lst 1100lst 1200lst 1300lst 1400lst 1500lst 1600lst 1700lst 1800lst 1900lst
        do
            FILENAME_HEAD=$LOCATION/$i.curr.$j.d2.head.png
            FILENAME_BODY=$LOCATION/$i.curr.$j.d2.body.png
            FILENAME_FOOT=$LOCATION/$i.curr.$j.d2.foot.png

            FILENAME_TEMP_ALL=$TMP_LOCATION/$i.curr.$j.temp.d2.png
            FILENAME_TEMP_WATER=$TMP_LOCATION/$i.curr.$j.water.d2.png
            FILENAME_FINAL_ALL=$TMP_LOCATION/$i.curr.$j.combined.d2.png
            FILENAME_RESIZE=$TMP_LOCATION/$i.curr.$j.50pct.d2.png

            echo "Building parameter: $i $j for: $FILENAME_HEAD $FILENAME_BODY $FILENAME_FOOT to: $FILENAME_FINAL_ALL"
            #Join up
            $CONVERT_BIN $FILENAME_HEAD $FILENAME_BODY $FILENAME_FOOT -background White -gravity Center -append $FILENAME_TEMP_ALL
            # Full logo
            $COMPOSITE_BIN -geometry +260+1650 $WM_LOGO $FILENAME_TEMP_ALL $FILENAME_TEMP_WATER
            # Watermark
            $COMPOSITE_BIN -dissolve 18 -tile  /tmp/stamp_mask.png $FILENAME_TEMP_WATER $FILENAME_RESIZE

            $CONVERT_BIN $FILENAME_RESIZE -resize 50%  $FILENAME_FINAL_ALL

            # clear up the images
            rm $FILENAME_TEMP_ALL $FILENAME_RESIZE $FILENAME_TEMP_WATER
        done

        # create the loop images
        echo "Rebuilding loops for parameter: $i"
       $CONVERT_BIN   -delay 90   -loop 0   $TMP_LOCATION/$i.curr.*.combined.d2.png $TARGET_LOCATION/$i.curr.loop.d2.gif

        # clear up the images
        FILENAME_FINAL_ALL=$TMP_LOCATION/$i.curr.*.combined.d2.png
        rm $FILENAME_FINAL_ALL

        done
        # remove WM stamp
        rm -f $WM_STAMP

        echo "Finished normally."

 

Compute Sunrise and Sunset Taking Into Account Daylight Savings in PHP

A requirement came up to plot some graphs with surface sun indicated, and it is useful to show the sun rise and sun set.

The following code snippet shows how to do this when providing a latitude, longitude and day name (Monday through Sunday).

    $gDay = "Friday";
    $gLat = "51.1";
    $gLongs = "0.5"; // "-99.99" would be west of the Greenwich Meridian

    // GMT or BST?
    if (date('I', time())) {
    	$iOffset = 1; // Daylight saving set == BST
    	$sSavings = "BST";
    } else {
    	$iOffset = 0; // GMT
    	$sSavings = "GMT";
    }
    // compute sunset and rise using $gDay ... as this changes the further out in days you go
    if (($iTime = strtotime("next ".$gDay)) === false) {
	$sSunset = date_sunset(time(), SUNFUNCS_RET_STRING, $gLat,$gLongs, 90, $iOffset);
	$sSunrise = date_sunrise(time(), SUNFUNCS_RET_STRING, $gLat,$gLongs, 90, $iOffset);
    } else {
	$sSunset = date_sunset($iTime, SUNFUNCS_RET_STRING, $gLat,$gLongs, 90, $iOffset);
	$sSunrise = date_sunrise($iTime, SUNFUNCS_RET_STRING, $gLat,$gLongs, 90, $iOffset);
    }
	
    $sTailName .= "\nSunrise: $sSunrise -> Sunset: $sSunset ($sSavings)";

The variable $sTailName contains the result as a printable string.

Soundings Replot Utility for RASP

Having been asked to add some RASP sounding plots in Northern Ireland, it became apparent that a script to just test just the ones added would make life easier (than replotting sounding by hour from the command line).Then it became an idea to just have this to replot all soundings (as potentially to do them later on a differnet host).

The script below is the result, fixed for one model with fixed start end points. These can of course be extended/changed as approriate.

#!/bin/bash
# Script expects a model as the first parameter,
# e.g. "sound_plot.sh EI12"
# Also expects "replot" to be available in $HOMEDIR/GM and it works

# Set where the RASP setup lives
HOMEDIR=/home/rasp

# Set where are commands are ... useful to put full path if run
# from a cron job. Ensure they exist, or add a check for it.
REPLOT_CMD=$HOMEDIR/GM/replot
SEQ_CMD=/usr/bin/seq

# Check we got a model and date to plot
if [ $# -eq 2 ]
then
echo "Using input as '$1'"
echo "Using '$REPLOT_CMD' to replot"
echo "Using date as '$2'"
else
echo "Provide a model to try. E.g. UK12 2015-04-03"
exit
fi

case $1 in
EI12) # We'll do just the one here
# Set the start and end sounding number (as in soundingXX)
val1=1
val2=19
SEQ1=$(SEQ_CMD -s " " $val1 $val2)
# Set the start end of hours to use
val3=7
val4=19
SEQ2=$(SEQ_CMD -f "%02g" -s " " $val3 $val4)
# Set the model to use and clear out the temp folder where
# we will put the results
MODEL=$1
;;
*)
echo "Don't know this model to work out start and finish: $1"
exit
;;
esac

# Now sort out the results folder ...
MODEL_FOLDER=$HOMEDIR/$MODEL
TMP_FOLDER=$HOMEDIR/$MODEL/tmp
# Clear it out
rm -rf $TMP_FOLDER
mkdir $TMP_FOLDER

# Output what we set
START_SND=$(printf '%d' "$val3")
END_SND=$(printf '%d' "$val4")
START_HR=$(printf '%02d' "$val1")
END_HR=$(printf '%02d' "$val2")

echo "Sounding sequence: sounding$SEQ1"
echo "Hour sequence: $SEQ2"
echo "Sending results to: $TMP_FOLDER"
echo "Using date of: $2"

#Now do plots, assuming no gaps though
for SND_NAME in $SEQ1
do
SOUNDING_NAME=sounding$SND_NAME
for PROC_HR in $SEQ2
do
echo "$REPLOT_CMD -r $MODEL -d $TMP_FOLDER -p $SOUNDING_NAME -l $TMP_FOLDER -w $MODEL_FOLDER/wrfout_d02_$2_$PROC_HR\:00\:00"
$HOMEDIR/GM/replot -r $MODEL -d $TMP_FOLDER -p $SOUNDING_NAME -l $TMP_FOLDER -w $MODEL_FOLDER/wrfout_d02_$2_$PROC_HR\:00\:00
done
done