#!/bin/bash
###############################################################################
# Script escrito para descargar imágenes de EUMETSAT y establecerlas con una
# resolución de 1920x1080 píxeles.
#
# Marcos Molina Cano - 23/03/2018
###############################################################################
#set -x

# Nombre del script.
scriptName=eumetsat_downloader.sh

function showUse {
  echo
  echo "Uso: ${scriptName} <A|B|C|D|E>"
  echo
  echo "     A - 0 degrees natural colours full disk."
  echo "     B - 0 degrees western Europe natural colours."
  echo "     C - 41.5 degrees natural colours full disk."
  echo "     D - GOES 16 natural colour."
  echo "     E - Himawari 8 natural colour."
  echo
}

# URL de las imágenes.
rootMeteosat=http://oiswww.eumetsat.org/IPPS/html/latestImages
rootGoes=https://cdn.star.nesdis.noaa.gov/GOES16/ABI/FD/GEOCOLOR
rootHimawari=http://himawari8-dl.nict.go.jp/himawari8/img/D531106

# Control de parámetros de entrada.
if [ ${#} -eq 1 ] ; then
  if [ "${1}" = "A" ] ; then
    image=${rootMeteosat}/EUMETSAT_MSG_RGBNatColour_FullResolution.jpg
  elif [ "${1}" = "B" ] ; then
    image=${rootMeteosat}/EUMETSAT_MSG_RGBNatColour_WesternEurope.jpg
  elif [ "${1}" = "C" ] ; then
    image=${rootMeteosat}/EUMETSAT_MSGIODC_RGBNatColour_FullResolution.jpg
  elif [ "${1}" = "D" ] ; then
    image=${rootGoes}/1808x1808.jpg
  elif [ "${1}" = "E" ] ; then
    # Se obtiene la hora de la última imagen y se definen las 4 teselas.
    latestJson=${rootHimawari}/latest.json
    latestDate=$(curl --silent ${latestJson} | jq '.date' | cut -c 2- | cut -c -19)
  else
    showUse
    exit
  fi
else
  showUse
  exit
fi

# Nombre de la imagen tras ser descargada.
imageNameJpg=/home/marcos/Imágenes/fondo.jpg
imageNamePng=/home/marcos/Imágenes/fondo.png

# Descarga de la imagen y transformación.
if [ "${1}" != "E" ] ; then
  curl --silent ${image} > ${imageNameJpg}.tmp
else
  tileRoot=${rootHimawari}/2d/550/$(date -u +%Y/%m/%d/%H%M%S --date="${latestDate} UTC")
  image_0_0=${tileRoot}_0_0.png
  image_1_0=${tileRoot}_1_0.png
  image_0_1=${tileRoot}_0_1.png
  image_1_1=${tileRoot}_1_1.png
  imageNameTilePng_0_0=/home/marcos/Imágenes/fondo_0_0.png
  imageNameTilePng_1_0=/home/marcos/Imágenes/fondo_1_0.png
  imageNameTilePng_0_1=/home/marcos/Imágenes/fondo_0_1.png
  imageNameTilePng_1_1=/home/marcos/Imágenes/fondo_1_1.png
  curl --silent ${image_0_0} > ${imageNameTilePng_0_0}
  curl --silent ${image_1_0} > ${imageNameTilePng_1_0}
  curl --silent ${image_0_1} > ${imageNameTilePng_0_1}
  curl --silent ${image_1_1} > ${imageNameTilePng_1_1}

  # Pegamos las 4 imágenes y las pasamos a jpg.
  convert +append ${imageNameTilePng_0_0} ${imageNameTilePng_1_0} ${imageNamePng}.tmpA
  convert +append ${imageNameTilePng_0_1} ${imageNameTilePng_1_1} ${imageNamePng}.tmpB
  convert -append ${imageNamePng}.tmpA ${imageNamePng}.tmpB ${imageNamePng}
  convert ${imageNamePng} ${imageNameJpg}.tmp
fi

# Transformación de la imagen para adecuar a la pantalla.
convert ${imageNameJpg}.tmp -resize 1920x1080 -gravity center -background black -extent 1920x1080 ${imageNameJpg}

# Si es la imagen del Himawari le escribimos la fecha y la hora.
if [ "${1}" = "E" ] ; then
  convert ${imageNameJpg} -font helvetica -fill white -pointsize 20 -draw "text 500,1070 '${latestDate} UTC'" ${imageNameJpg}
fi
