From a59faf8a60ad611471b0ed8d8e23a05b01f7444c Mon Sep 17 00:00:00 2001
From: Luis Kornblueh <luis.kornblueh@mpimet.mpg.de>
Date: Thu, 26 Sep 2019 23:59:39 +0200
Subject: [PATCH] Correct comp_weights for the bug found by Helmut Frank

---
 examples/comp_weights.f90 | 26 +++++++++++++++++++-------
 1 file changed, 19 insertions(+), 7 deletions(-)

diff --git a/examples/comp_weights.f90 b/examples/comp_weights.f90
index db72cc52..f1d59bb0 100644
--- a/examples/comp_weights.f90
+++ b/examples/comp_weights.f90
@@ -39,6 +39,13 @@ program comp_weights
   call calculate_time_interpolation_weights(test_date, m1, m2, pw1, pw2)
   write(0,*) 'mtime      1: ', m1, m2, pw1, pw2
   call deallocateDatetime(test_date)
+
+  !-> new tests
+
+  test_date => newDatetime('2017-08-31T00:00:00')
+  call calculate_time_interpolation_weights(test_date, m1, m2, pw1, pw2)
+  write(0,*) 'mtime      2: ', m1, m2, pw1, pw2
+  call deallocateDatetime(test_date)
   
 contains
   
@@ -184,15 +191,19 @@ contains
     integer :: errno
     
     days_in_month = getNoOfDaysInMonthDateTime(current_date) 
+    print *, 'days in month: ', days_in_month
     seconds_in_middle_of_month = 43200 * days_in_month ! 86400 * my_month_len / 2
-    
+    print *, 'seconds in middle of month: ', seconds_in_middle_of_month
     seconds_in_month = getNoOfSecondsElapsedInMonthDateTime(current_date)
-
+    print *, 'seconds in month: ', seconds_in_month
+    
     if (seconds_in_month <= seconds_in_middle_of_month) then
       ! first half of month 
+      print *, "first half of month"
       one_month => newTimedelta('-P1M')
-      previous_month => newDatetime(current_date)
-      previous_month = current_date + one_month
+      previous_month => newDatetime(current_date%date%year, current_date%date%month, 15, 0, 0, 0, 0)      
+      ! use patched current_date stored in previous_month to get a proper previous month
+      previous_month = previous_month + one_month
       days_in_previous_month = getNoOfDaysInMonthDateTime(previous_month)
       seconds_in_middle_of_previous_month = 43200 * days_in_previous_month ! 86400 * my_month_len / 2
       ! simple linear interpolation
@@ -203,10 +214,11 @@ contains
       month2 = current_date%date%month
     else
       ! second half of month
-
+      print *, "second half of month"
       one_month => newTimedelta('P1M')
-      next_month => newDatetime(current_date)
-      next_month = current_date + one_month      
+      next_month => newDatetime(current_date%date%year, current_date%date%month, 15, 0, 0, 0, 0)
+      ! use patched current_date stored in next_month to get a proper next month
+      next_month = next_month + one_month
       days_in_next_month = getNoOfDaysInMonthDateTime(next_month)
       seconds_in_middle_of_next_month = 43200 * days_in_next_month ! 86400 * my_month_len / 2
       ! simple linear interpolation
-- 
GitLab