﻿@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, it's always the latest available 
:: These analysis ground pressure maps are issued 30 minutes, up to 4 hours after 00 06 12 18 UTC
:: ----------------------------------------------------------------------------------------------
:: Up to 4 more previous Bracknell charts will be downloaded, processed and named as shown below!
:: ----------------------------------------------------------------------------------------------
:: 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*.gif* /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 ...
) else (
:: 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 ...
  ) else (
    %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
  )
)

: Now that we know the valid date of the LATEST chart we can download up to 4 previous charts going back 6, 12, 18, 24 hours
set lastday=%issue:~0,4%-%issue:~4,2%-%issue:~6,2%
set valhour=%issue:~8,2%
for %%f in (6, 12, 18, 24) do call :get_previous_chart %%f

del ppva*.gif* /f

goto :eof


:get_previous_chart
::
:: Variables lastday and valhour are global, count %1 hours back to (hopefully) get the file naming correct. Valid Date check recommended!
::
for /f %%G in ('PowerShell Get-Date -Date ^(Get-Date -Date %lastday%^).AddHours^(%valhour%-%1^) -uformat +%%Y%%m%%d%%H') do set issue=%%G00
set ppva=ppva_fullsize_%issue:~8,2%z_last.gif

if exist %out%\Bracknell-%issue%.png (
  echo You already have this previous Bracknell-%issue%.png chart ...
) else (
  %cliwget% --no-check-certificate https://www.weathercharts.net/ukmo_mslp_analysis/previous/%ppva%
  if not exist %ppva% (
    echo Sorry, could not download %ppva% !
    goto :eof
  )
  %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
)
goto :eof





