From 858a469d110c0d4da900f64dd619bdede811a139 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Oriol=20Tint=C3=B3?= <oriol.tinto@lmu.de>
Date: Wed, 21 Jun 2023 16:03:50 +0200
Subject: [PATCH] Update prepare_experiment.sh to download additional grid info
 files.

---
 .../event-generator/prepare_experiment.sh     | 33 +++++++++++++++++++
 1 file changed, 33 insertions(+)

diff --git a/templates/event-generator/prepare_experiment.sh b/templates/event-generator/prepare_experiment.sh
index 61e98b4..39f68d9 100644
--- a/templates/event-generator/prepare_experiment.sh
+++ b/templates/event-generator/prepare_experiment.sh
@@ -29,12 +29,45 @@ function download_file() {
   fi
 }
 
+function check_url() {
+  # Check if the url exists
+  local url="$1"
+  status=$(
+    (curl -s --head $url | head -n 1 | grep "HTTP/2 200 " >/dev/null)
+    echo $?
+  )
+  if [ $status -eq 0 ]; then
+    echo "true"
+  else
+    echo "false"
+  fi
+}
+
+function get_grfinfo_file(){
+  local filename="$1"
+  new_suffix="-grfinfo.nc"
+  new_filename="$(basename "$filename" .nc)$new_suffix"
+  echo "$new_filename"
+}
+
 # Download grid files and external parameters
 BASEURL=http://icon-downloads.mpimet.mpg.de/grids/public/edzw
 download_file $BASEURL/${DYNAMICS_GRID_FILENAME}
 download_file $BASEURL/${RADIATION_GRID_FILE}
 download_file $BASEURL/${EXTERNAL_PARAMETERS_FILE}
 
+# In case the grfinfo files exist, we download them as well
+RADIATION_GRID_INFO_FILE=$( get_grfinfo_file ${RADIATION_GRID_FILE} )
+DYNAMICS_GRID_INFO_FILE=$( get_grfinfo_file ${DYNAMICS_GRID_FILENAME} )
+
+if [ $( check_url "$BASEURL/${RADIATION_GRID_INFO_FILE}" ) == "true" ]; then
+  download_file "$BASEURL/${RADIATION_GRID_INFO_FILE}"
+fi
+
+if [ $( check_url "$BASEURL/${DYNAMICS_GRID_INFO_FILE}" ) == "true" ]; then
+  download_file "$BASEURL/${DYNAMICS_GRID_INFO_FILE}"
+fi
+
 
 # Define the function
 function grid_filename_from_extpar() {
-- 
GitLab