From f98fad56eca8218d3d4f40c8c7547ecec56ae551 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Oriol=20Tint=C3=B3?= <oriol.tinto@lmu.de>
Date: Tue, 27 Jun 2023 16:43:12 +0200
Subject: [PATCH] Interpolate from the proper grid.

---
 templates/event-generator/adapt_member.sh     | 16 ++------
 .../event-generator/adapt_member_utils.sh     | 40 ++++++-------------
 2 files changed, 15 insertions(+), 41 deletions(-)

diff --git a/templates/event-generator/adapt_member.sh b/templates/event-generator/adapt_member.sh
index 0ad103c..45d6961 100644
--- a/templates/event-generator/adapt_member.sh
+++ b/templates/event-generator/adapt_member.sh
@@ -55,18 +55,8 @@ interpolate_SST "${DESTINATION_GRID}" "${SST_FORCING}" "${INTERPOLATED_SST}"
 integrate_sst_to_extpar "${INTERPOLATED_SST}" "${EXTERNAL_PARAMETERS_FILE}"
 
 
-old_integrate_sst_to_analysis "${INTERPOLATED_SST}" "${ANALYSIS_FILE}"
-# Integrate sst to analysis
-#integrate_sst_to_analysis "${DESTINATION_GRID}" "${ANALYSIS_FILE}"
+# Interpolate analysis
+interpolate_analysis "icon_grid_0024_R02B06_G.nc" "${ANALYSIS_FILE}" "${DESTINATION_GRID}"
 
-ncks -4 --fl_fmt=64bit analysis.nc analysis.nc2
 
-mv analysis.nc2 analysis.nc
-
-
-#python ${PROJ_FOLDER}/templates/event-generator/icon-remap-helper.py --src-grid ${DESTINATION_GRID} \
-#                                                                --dst-grid ${DESTINATION_GRID} \
-#                                                                --source ${ANALYSIS_FILE} \
-#                                                                --dest . \
-#                                                                --output-format nc \
-#                                                                --rename "analysis.nc"
\ No newline at end of file
+integrate_sst_to_analysis "${ANALYSIS_FILE}"
diff --git a/templates/event-generator/adapt_member_utils.sh b/templates/event-generator/adapt_member_utils.sh
index 7811377..36d0e05 100644
--- a/templates/event-generator/adapt_member_utils.sh
+++ b/templates/event-generator/adapt_member_utils.sh
@@ -110,20 +110,21 @@ END
   fi
 }
 
-function integrate_sst_to_analysis() {
+function interpolate_analysis() {
 
-  local DYNAMICS_GRID_FILE="$1"
+  local INPUT_GRID="$1"
   local ANALYSIS_FILE="$2"
+  local OUTPUT_GRID="$3"
 
   echo "Converting analysis to netcdf"
 
   cat >remap.nml <<END
 &remap_nml
 in_filename = "${ANALYSIS_FILE}"
-in_grid_filename = "${DYNAMICS_GRID_FILE}"
+in_grid_filename = "${INPUT_GRID}"
 in_type = 2
-out_grid_filename = "${DYNAMICS_GRID_FILE}"
-out_filename = "analysis.nc"
+out_grid_filename = "${OUTPUT_GRID}"
+out_filename = "interpolated_analysis.nc"
 out_type = 2
 out_filetype = 4
 /
@@ -151,28 +152,11 @@ END
   ulimit -s unlimited
   OMP_NUM_THREADS=1 iconremap --remap_nml remap.nml #-vvv
   rm remap.nml
-
-#  # Somehow, when we interpolate we lose some attributes.
-#  # Here we solve this issue adding the attributes.
-#  cat <<END >add_attributes.py
-## Add sst field to analysis file.
-#from enstools.io import read
-#
-## Open files
-#with read("tmp_extpar.nc") as ds, read("${EXTERNAL_PARAMETERS_FILE}") as original_extpar_ds:
-#    if "rawdata" in original_extpar_ds.attrs:
-#        ds.attrs["rawdata"] = original_extpar_ds.attrs["rawdata"]
-#        ds.to_netcdf("extpar.nc")
-#END
-#  python3 add_attributes.py
-#
-#  rm "tmp_extpar.nc"
-
 }
 
-function old_integrate_sst_to_analysis() {
+function integrate_sst_to_analysis() {
   local INTERPOLATED_SST="$1"
-  local ANALYSIS_FILE="$2"
+  local ANALYSIS_FILE="interpolated_analysis.nc"
 
   cat <<END >integrate_sst_to_analysis.py
 # Add sst field to analysis file.
@@ -183,10 +167,10 @@ import numpy as np
 os.environ['ECCODES_GRIB_NO_INDEX'] = '1'
 # Open files
 with read("${ANALYSIS_FILE}") as ds, read("${INTERPOLATED_SST}") as sst_ds:
-#  date=ds.time.values
-#  day_of_year = int((date - date.astype('datetime64[Y]')) / np.timedelta64(1, 'D'))
-#   Replace analysis T_SEA with info from the sst_clim file.
-#  ds["T_SEA"]= sst_ds["T_SEA"].isel(time=day_of_year+1)
+  date=ds.time.values
+  day_of_year = int((date - date.astype('datetime64[Y]')) / np.timedelta64(1, 'D'))
+   Replace analysis T_SEA with info from the sst_clim file.
+  ds["T_SEA"]= sst_ds["T_SEA"].isel(time=day_of_year+1)
   ds.to_netcdf("analysis.nc")
 END
 
-- 
GitLab