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

 

Difference Bearing Calculator in PHP

A long standing application checks the actual weather reports for various UK airfileds and then compares to a forecast. One of the variables is the wind direction.

The application compares a compass bearing for the actual and forecasted wind direction and then states the difference in degrees. All sounds easy. Alas the first release of PHP code for this seemed to be incorrect for the fourth quarter (i.e. Q4 is between 270 and 360 degrees).

Here’s the code that has now been proven to work.

Function FindBearing( $forecast, $actual )
{

    if ($actual == "VRB") { 
        // the forecast has variable so we return zero 
        // as it could be anything given the winds are light, 
        // that's not something we can handle
        return 0;
    }
    
    if  ($forecast <= 180 ) { // in Q1 or Q2
        if ( $actual <= 180 ) { // in Q1 or Q2
            return abs($actual - $forecast);
        } 
        // forecast in Q3 or Q4
        return 180-abs((360-$actual)-abs(180-$forecast));
        
    } else { // forecast in Q3 or Q4
        if ( $actual > 180 ) { // forecast in Q3 and Q4 as well so easy
            return abs($actual - $forecast);
        }
        
        // forecast in Q3 or Q4 and forecast in Q1 and Q2
        $result = abs($actual - $forecast);
        
        if ($result <= 180){
            return $result;
        }
        return 180 - abs(180-$result);
    }

    // else return a bearing that isn't possible     
    return "-1";
}

The function can be tested by using this simple PHP script:


for ($i = 0; $i <= 360; $i=$i + 1) {
        for ($j = 0; $j <= 360; $j=$j + 1) {
            $diff = FindBearing( $i, $j );
            
            echo "
forecast=$i, actual=$j, diff=$diff"; if (($diff < 0) || ($diff>180)){ echo "ERROR"; } } }

Unix Shell CGI Script using find, stat or xarg for File Listing

This little code snippet was required to find out the date and time for files for use in a CGI script.

It uses a conventional shell script to start with, setting some local variables to use in the script. It then outputs the HTML content type to enable CGI working.

After that it it uses find with the output as a printf statement. The nice thing about using printf is that we can just select the name of the file without the full path (you don’t want to tell the world exactly where it lives!).

 #!/bin/sh
 RUNPATH=/home/
 PATTERN=
 NOW=$(date +"%H:%M:%S %d-%h-%Y")
 echo "Content-type: text/html"
 echo ""
 echo ""
 echo "
"
 echo "

Production Times and Sizes at $NOW

" echo "
"
 find $RUNPATH -name "$PATTERN*" -printf "%AD %Ar %10s %f\n"
 echo "

"
And that’s it!

You can change the format specifiers in the printf to suit what you want.

A sample output looks like this:

Production Times and Sizes at 13:31:07 04-Jul-2013

07/04/13 08:04:31 AM      77956 00fcst-movie-FULL-UK2.flv
07/04/13 06:20:23 AM      61540 00fcst-movie-FULL-UK2.wmv
07/04/13 06:20:22 AM     124996 00fcst-movie-FULL-UK2.avi
07/04/13 11:30:06 AM    8061942 00fcst-movie-FULL-UKEA62-WINDOW.wmv
07/04/13 11:30:05 AM    6447314 00fcst-movie-FULL-UKEA62-WINDOW.avi
07/04/13 11:30:06 AM    4371525 00fcst-movie-FULL-UKEA62-WINDOW.flv
07/04/13 11:41:05 AM    4409202 00fcst-movie-FULL-UK4.flv
07/04/13 07:05:02 AM    8527096 00fcst-movie-FULL-UK4.wmv
07/04/13 07:05:02 AM   11068020 00fcst-movie-FULL-UK4.avi

You can of course consider using stat and xarg, by replacing the tail of the find like this if you do want the full path:

find $RUNPATH | grep $PATTERN | xargs stat -c "%y %10s %n"