﻿@echo off

:: ------------------------------------------------------------------------------------------------
:: Copyright (c) 2022 Ernst Lobsiger
::
:: This program is free software: you can redistribute it and/or modify it under the terms of the
:: GNU General Public License as published by the Free Software Foundation, either version 3 of
:: the License, or (at your option) any later version.
::
:: This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
:: without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
:: See the GNU General Public License for more details.
::
:: You should have received a copy of the GNU General Public License
:: along with this program.  If not, see <http://www.gnu.org/licenses/>.
:: ------------------------------------------------------------------------------------------------

:: ******************** THIS SCRIPT ASSUMES YOUR PC TIME SETTING IS PLAIN UTC *********************
::
:: This CMD script prepares and archives Bracknell charts to be processed using QGIS later
:: Input file is *.gif from the INTERNET, output files are w x h = 2148 x 1452, 2bit *.png
::
:: Input file name:
:: ppva_fullsize.gif              This file should not be seen, deleted when charts are processed 
:: These analysis ground pressure maps are issued 30 minutes, up to 4 hours after 00 06 12 18 UTC
:: Typical output file names:
:: Bracknell-202112261200.png     This must be used side by side with a so called world file with
:: Bracknell-202112261200.pngw    identical name but special suffix (.pngw is what QGIS expects!)
::
:: Wikipedia: "In September 2003 the Met Office moved its headquarters from Bracknell in Berkshire
:: to a purpose-built £80m structure at Exeter Business Park, near junction 29 of the M5 motorway."
:: I had to find out why many members of our "Old Farts" club are using the term "Bracknell Chart".
::
:: You need a working CLI wget.exe somewhere. Here I use GNU Wget 1.15 built with compiler mingw32
:: This has been downloaded from https://eternallybored.org/misc/wget
:: You also need IM convert.exe. I added the one distributed with my PyTROLL/Satpy Starter 3.0 Kit 
::
:: Belp, Oase 2022/01/10                                                            Ernst Lobsiger
::
:: ******************** THIS SCRIPT ASSUMES YOUR PC TIME SETTING IS PLAIN UTC *********************
:: **************************** YOU USE IT AT YOUR OWN RISK AND PERIL *****************************
:: ******************* DO NOT TOUCH THE CODE BELOW UNLESS YOU KNOW WHAT YOU DO ********************

:: Default path to ImageMagick convert.exe
set convert=".\exe\convert.exe"
:: Where to find your CLI Windows wget.exe
set cliwget=".\exe\wget.exe"
:: That's the file we use, issue is LATEST
set ppva="ppva_fullsize.gif"
:: Output directory for overlays (must exist)
set out=".\Charts"

:: cd to script.cmd directory
cd /D "%~dp0"
:: Used as working directory
del %ppva%* /f

:: The date command of CMD is outright unusable, using PowerShell Get-Date is "Mission Impossible, 2.0" (but sometimes it seems to work!)
:: This is the latest issue of a Bracknell chart that can possibly exist, PowerShell rounds to next integer(!), have to use [math]::floor
for /f %%G in ('PowerShell ^(Get-Date '1/1/1970'^).AddSeconds^([long][math]::floor^(^(Get-Date -uformat +%%s^)/21600^)*21600^).ToString^(\"yyyMMddHH\"^)') do set issue=%%G00

if exist %out%\Bracknell-%issue%.png (
  echo You already have the latest possible  Bracknell-%issue%.png chart ...
  goto :eof
)

:: Download the LATEST mslp chart. Ask between 13:00 and 17:00 to get the 12:00 UTC issue
%cliwget% --no-check-certificate https://www.weathercharts.net/ukmo_mslp_analysis/%ppva%

if not exist %ppva% (
    echo Sorry, could not download %ppva% !
    goto :eof
)

:: Yes, this *must* stay on one line. It's the longest CMD statement I have ever written. Just some easy short lines in GNU/Linux BASH!
for /f %%G in ('PowerShell ^(Get-Date '1/1/1970'^).AddSeconds^([long][math]::floor^(^(Get-Date -Date ^(Get-ChildItem %ppva%^).lastwritetime -uformat +%%s^)/21600^)*21600^).ToString^(\"yyyMMddHH\"^)') do set issue=%%G00

if exist %out%\Bracknell-%issue%.png (
    echo You already have the latest available Bracknell-%issue%.png chart ...
    goto :eof
)

:: %convert% %ppva% -crop 2142x1446+3+66 -bordercolor black -border 3x3 -threshold 80%% -transparent white PNG32:%out%\Bracknell-%issue%.png
:: I changed from PNG32 to PNG2 which is only 60% in size. This should work the same way in QGIS with slightly different first layer icone.

%convert% %ppva% -crop 2142x1446+3+66 -bordercolor black -border 3x3 -threshold 80%% -transparent white %out%\Bracknell-%issue%.png
copy .\bak\Bracknell.pngw %out%\Bracknell-%issue%.pngw /y

del %ppva%* /f




