1

Let me preface this by saying my last computer class was in high school 1968-69. I'm sure I'm not using best practices and I always appreciate help there. Everything I do is self taught and this is the first truly original piece of code I've written.

In this case I'm trying to produce weather forecast pages. Here are samples for Honolulu. The data comes from NWS NDFD (national digital forecast database) via api.weather.gov in json. I pluck those variables and plug them into ImageMagick.

Two problems. The only way I could accommodate the forecast, which is a different length every time, was to use the caption command. But SUNNY, next to a three or four line forecast is jarring. Is there a better way or at least a way to limit the upper font size?

Also, this takes a lot longer than I expected. Is there a way for me to speed the process?

Thanks in advance for your help. I learn a lot here.

#!/bin/bash
#process forecast json 
#8Oct2019
#@geofffox

cd /tmp/json
#curl -o kofk https://api.weather.gov/gridpoints/OAX/31,93/forecast
#curl -o kofk https://api.weather.gov/gridpoints/AFG/381,359/forecast
#curl -o kofk https://api.weather.gov/gridpoints/APX/36,23/forecast
#curl -o kofk https://api.weather.gov/gridpoints/HFO/153,144/forecast
curl -o kofk https://api.weather.gov/gridpoints/OKX/66,65/forecast

counter=0
while [ $counter -le 13 ]
do


number["$counter"]=$(cat kofk | jq -r  '.properties.periods['$counter'].number')
name["$counter"]=$(cat kofk | jq -r  '.properties.periods['$counter'].name')
start["$counter"]=$(cat kofk | jq -r  '.properties.periods['$counter'].startTime')
end["$counter"]=$(cat kofk | jq -r  '.properties.periods['$counter'].endTime')
swch["$counter"]=$(cat kofk | jq -r  '.properties.periods['$counter'].isDaytime')
temp["$counter"]=$(cat kofk | jq -r  '.properties.periods['$counter'].temperature')
wind["$counter"]=$(cat kofk | jq -r  '.properties.periods['$counter'].windSpeed')
wdir["$counter"]=$(cat kofk | jq -r  '.properties.periods['$counter'].windDirection')
shrt["$counter"]=$(cat kofk | jq -r  '.properties.periods['$counter'].shortForecast')
long["$counter"]=$(cat kofk | jq -r  '.properties.periods['$counter'].detailedForecast')


echo $counter
((counter++))
done


innerLoop=0
rm /var/www/html/output/json/kofk/*.png
while [ $innerLoop -le 13 ]
do 
echo $innerLoop

convert -size 1920x1080 xc:blue PNG32:/var/www/html/output/json/kofk/kofk.png

    convert  -background rgba\(0,0,0,0.001\) -fill white -stroke black -strokewidth 3 -gravity west -font Open-Sans-Extrabold -size 700x400 caption:"${shrt["$innerLoop"]^^}" \( +clone -shadow 70x12+5+5 \) +swap \
    -flatten  -trim +repage  /var/www/html/output/json/kofk/shrt["$innerLoop"].png

 convert /var/www/html/output/json/kofk/kofk.png -gravity northwest -pointsize 50 -fill white -font Open-Sans-Bold -stroke black -strokewidth 2 -draw "text 950,115 '${name["$innerLoop"]^^}'" /var/www/html/output/json/kofk/kofk["$innerLoop"].png 

        if ${swch["$innerLoop"]} == false; then

 convert /var/www/html/output/json/kofk/kofk["$innerLoop"].png -pointsize 50 -fill white -font Open-Sans-Bold -stroke black -strokewidth 2  -draw "text 950 700 'DAYTIME HIGH:'" /var/www/html/output/json/kofk/kofk["$innerLoop"].png

        else
 convert /var/www/html/output/json/kofk/kofk["$innerLoop"].png -pointsize 50 -fill white -font Open-Sans-Bold -stroke black -strokewidth 2  -draw "text 950 700 'OVERNIGHT LOW:'" /var/www/html/output/json/kofk/kofk["$innerLoop"].png

       fi

 convert /var/www/html/output/json/kofk/kofk["$innerLoop"].png -pointsize 200 -fill black -font Open-Sans-Extrabold -draw "text 1405 705 '${temp["$innerLoop"]^^}°'" -fill white -stroke black -strokewidth 5 -draw "text 1400 700 '${temp["$innerLoop"]^^}°'" /var/www/html/output/json/kofk/kofk["$innerLoop"].png
 convert /var/www/html/output/json/kofk/kofk["$innerLoop"].png -pointsize 50 -fill white -font Open-Sans-Bold -stroke black -strokewidth 2  -draw "text 950 750 'WIND: ${wdir["$innerLoop"]}"" ${wind["$innerLoop"]^^}'" /var/www/html/output/json/kofk/kofk["$innerLoop"].png

 convert -composite -gravity west -geometry +950-175 /var/www/html/output/json/kofk/kofk["$innerLoop"].png /var/www/html/output/json/kofk/shrt["$innerLoop"].png /var/www/html/output/json/kofk/kofk["$innerLoop"].png 

rm /var/www/html/output/json/kofk/shrt["$innerLoop"].png

 ((innerLoop++))
done


exit
Eidolon108
  • 403
  • 4
  • 15
Geoff Fox
  • 131
  • 4
  • With caption: you can specify limit the height without specifying the width such as -size xH. You can also add "\n" to force to a new line. Or you can use two captions with different sizes, one for SUNNY and the other for the rest. Also if you know the text is "SUNNY", you can use a conditional in your script to make its text smaller. But perhaps you might want to use PANGO: markup so that you can make the various words different sizes if you need. See https://imagemagick.org/Usage/text/#caption and https://imagemagick.org/Usage/text/#pango – fmw42 Oct 29 '19 at 00:21
  • The whole idea is this runs as a cron with no human intervention. What you're saying is customizing each iteration? – Geoff Fox Oct 29 '19 at 00:24
  • No, perhaps script the changes based upon the text. Try using -size xH rather than -size WxH. But the former may have issues with width, then. Unfortunately, I do not have a good grasp of your text flexibility to know what exactly to suggest. Can you show one that has the issues for which you are concerned? – fmw42 Oct 29 '19 at 00:27
  • Another possibility is to check the font size that caption wants to create in your script and then repeat to force a smaller size if it is too big. You can get that from adding -debug annotate. But that presents a lot of text that you would have to parse or search for the pointsize information. – fmw42 Oct 29 '19 at 00:30
  • [Here are two extremes.](https://1drv.ms/u/s!AmH2yScXoO_4yTQajfPZ509CnYhL?e=Dbkavo). One is a single word, the other three lines. There might be forecasts slightly longer, but this is getting close. – Geoff Fox Oct 29 '19 at 00:38
  • i placed a suggested edit that includes addition of the [bash] tag, which has a bunch of watchers, but i want to note that there's a 5-tag limit, so I suggested removal of the [nws] tag. Hope it helps your question get attention – Eidolon108 Oct 29 '19 at 00:38
  • `@Geoff Fox`. Those look fine to me. What is wrong with the large text for `SUNNY`? If you need to make it smaller, then as I suggested earlier test the text for the word "SUNNY" in your script, since it seems to be the only word in that section. If found, then use a different convert command with a smaller height in your -size WxH. – fmw42 Oct 29 '19 at 01:04
  • I should have made this more clear. The idea is to produce this en mass. I already make around 40,000 maps a day. So, it has to run without intervention. It might be beyond ImageMagick's capability. There's always a hack. Just have to find it. And thanks for fixing my tags. I didn't know. – Geoff Fox Oct 29 '19 at 01:19

0 Answers0