Index: palm/trunk/SOURCE/date_and_time_mod.f90
===================================================================
--- palm/trunk/SOURCE/date_and_time_mod.f90	(revision 3298)
+++ palm/trunk/SOURCE/date_and_time_mod.f90	(revision 3458)
@@ -19,5 +19,5 @@
 !
 ! Current revisions:
-! -----------------
+! ------------------
 ! 
 ! 
@@ -25,4 +25,9 @@
 ! -----------------
 ! $Id$
+! from chemistry branch r3443, banzhafs:
+! Added initial hour_of_day, hour_of_year, day_of_year and month_of_year to
+! init_date_and_time
+!
+! 3298 2018-10-02 12:21:11Z kanani
 ! - Minor formatting (kanani)
 ! - Added Routines for DEFAULT mode of chemistry emissions (Russo)
@@ -61,4 +66,5 @@
 !>       already implemented changes for calculating it from date_init in 
 !>       calc_date_and_time
+!> @todo time_utc during spin-up  
 !------------------------------------------------------------------------------!
  MODULE date_and_time_mod
@@ -91,5 +97,6 @@
     INTEGER(iwp)        ::  index_mm                                !< index months of the default emission mode
     INTEGER(iwp)        ::  index_dd                                !< index days of the default emission mode
-    INTEGER(iwp)        ::  index_hh                                !< index hours of the default emission mode
+    INTEGER(iwp)        ::  index_hh                                !< index hours of the emission mode
+    INTEGER(iwp)        ::  hours_since_reference_point             !< hours of current simulation
 
     REAL(wp)            ::  time_utc                     !< current model time in UTC
@@ -102,5 +109,5 @@
     REAL(wp), PARAMETER ::  d_seconds_year = 1.0_wp / 31536000.0_wp !< inverse of the seconds per year (1/(365*86400))
     
-    CHARACTER(len=8)    ::  date_init = "31122018"                  !< Starting date of simulation: We selected this because it was a monday
+    CHARACTER(len=8)    ::  date_init = "21062017"                  !< Starting date of simulation: We selected this because it was a monday
  
     !> --- Parameters
@@ -121,4 +128,10 @@
     END INTERFACE time_default_indices
 
+    !-- Get hour index in the PRE-PROCESSED case of chemistry emissions :
+    INTERFACE time_preprocessed_indices
+       MODULE PROCEDURE time_preprocessed_indices
+    END INTERFACE time_preprocessed_indices
+
+
     !-- Calculate current date and time
     INTERFACE calc_date_and_time
@@ -128,5 +141,5 @@
 
     !-- Public Interfaces
-    PUBLIC calc_date_and_time, time_default_indices, init_date_and_time
+    PUBLIC calc_date_and_time, time_default_indices, init_date_and_time, time_preprocessed_indices
 
     !-- Public Variables
@@ -147,4 +160,7 @@
        IMPLICIT NONE
 
+       !--    Variables Definition
+       INTEGER ::  i_mon       !< Index for going through the different months
+
        IF  (day_of_year_init == 0) THEN
           ! Day of the month at starting time
@@ -159,34 +175,10 @@
        ENDIF
 
-    END SUBROUTINE init_date_and_time
-
-!------------------------------------------------------------------------------!
-! Description:
-! ------------
-!> Calculate current date and time of the simulation 
-!------------------------------------------------------------------------------!
- 
-    SUBROUTINE calc_date_and_time
-
-       IMPLICIT NONE
-
-!--    Variables Definition
-       INTEGER		   		:: i_mon       !< Index for going through the different months
-
-       !> Update simulation time in seconds
-       time_update = simulated_time-coupling_start_time
-
-!--    Calculate current day of the simulated time
-       days_since_reference_point=INT(FLOOR( (time_utc_init + time_update) &
-                               / 86400.0_wp ) )
-
-!--    Calculate actual UTC time                        
-       time_utc = MOD((time_utc_init + time_since_reference_point), 86400.0_wp)
-       
-!sB    PRILIMINARY workaround for time_utc bug concerning time_since_reference_point:
-       time_utc_emis = MOD((time_utc_init + time_update), 86400.0_wp)     
-
-!--    Calculate initial day of the year: it is calculated only once. In fact, day_of_year_init is initialized to 0 and then a positive value is passed. This condition is also called only when day_of_year_init is not given in the namelist. 
-
+
+       !-- Calculate initial hour of the day: the first hour of the day is from 00:00:00 to 00:59:59. 
+
+       hour_of_day = INT( FLOOR( time_utc_init/3600.0_wp ) ) + 1
+
+       !-- Calculate initial day day_of_year_init in case date_init is given or day_of_year_init is given
        IF ( day_of_year_init == 0 ) THEN
 
@@ -214,4 +206,132 @@
        ENDIF
 
+       
+       !-- Initial day of the year
+       day_of_year = day_of_year_init
+
+       !-- Initial hour of the year
+       hour_of_year = ( (day_of_year-1) * 24 ) + hour_of_day
+
+       !--Initial day of the month and month of the year
+       !> --------------------------------------------------------------------------------
+       !> The first case is when date_init is not provided: we only know day_of_year_init      
+       IF ( month_of_year == 0 .AND. day_of_month == 0) THEN
+
+         
+          IF ( day_of_year .LE. 31 ) THEN
+
+             month_of_year=1
+             day_of_month=day_of_year
+
+          ELSE
+
+             DO i_mon=2,12   !january is considered in the first case
+                IF ( day_of_year .LE. SUM(days(1:i_mon)) .AND. day_of_year .GT. SUM(days(1:(i_mon-1))) ) THEN
+           
+                   month_of_year=i_mon
+
+                   day_of_month=INT(MOD(day_of_year, SUM(days(1:(i_mon-1))))) 
+
+                   GOTO 38
+
+                ENDIF
+
+             38 ENDDO
+          ENDIF
+       !> --------------------------------------------------------------------------------
+       !> in the second condition both day of month and month_of_year are either given in input (passed to date_init) or we are in some day successive to the initial one, so that day_of_month has already be computed in previous step
+       !>TBD: something to calculate the current year is missing
+       ELSEIF ( day_of_month .GT. 0 .AND. day_of_month .LE. 31 .AND. month_of_year .GT. 0 .AND. month_of_year .LE. 12) THEN
+ 
+          !> calculate month_of_year. TBD: test the condition when day_of_year==31
+ 
+          IF (day_of_year==1) THEN  !> this allows to turn from december to January when passing from a year to another
+ 
+             month_of_year = 1
+        
+          ELSE IF (day_of_year .GT. 1 .AND. day_of_year .GT. SUM(days(1:month_of_year))) THEN
+
+             month_of_year = month_of_year + 1
+
+          ENDIF
+
+          !> calculate day_of_month
+          IF ( month_of_year == 1 ) THEN
+           
+            day_of_month=day_of_year
+
+          ELSE
+
+            day_of_month=INT(MOD(day_of_year, SUM(days(1:(month_of_year-1)))))
+
+          ENDIF
+
+
+       ELSE
+
+          !> Condition when date_init is provided but it is given in the wrong format
+          message_string = 'date_init not provided in the namelist or'            //          &
+                              ' given in the wrong format: MUST BE DDMMYYYY'                  
+          CALL message( 'init_date_and_time', 'DT0102', 2, 2, 0, 6, 0 ) 
+
+       ENDIF
+
+
+    END SUBROUTINE init_date_and_time
+
+!------------------------------------------------------------------------------!
+! Description:
+! ------------
+!> Calculate current date and time of the simulation 
+!------------------------------------------------------------------------------!
+ 
+    SUBROUTINE calc_date_and_time
+
+       IMPLICIT NONE
+
+!--    Variables Definition
+       INTEGER		   		:: i_mon       !< Index for going through the different months
+
+       !> Update simulation time in seconds
+       time_update = simulated_time-coupling_start_time
+
+!--    Calculate current day of the simulated time
+       days_since_reference_point=INT(FLOOR( (time_utc_init + time_update) &
+                               / 86400.0_wp ) )
+
+!--    Calculate actual UTC time                        
+       time_utc = MOD((time_utc_init + time_since_reference_point), 86400.0_wp)
+       
+!sB    PRILIMINARY workaround for time_utc changes due to changes in time_since_reference_point in
+!sB    radiation_model_mod during runtime:
+       time_utc_emis = MOD((time_utc_init + time_update), 86400.0_wp)     
+
+!--    Calculate initial day of the year: it is calculated only once. In fact, day_of_year_init is initialized to 0 and then a positive value is passed. This condition is also called only when day_of_year_init is not given in the namelist. 
+
+       IF ( day_of_year_init == 0 ) THEN
+
+          !> Condition for printing an error when date_init is not provided when day_of_year_init is not given in the namelist or when the format of the date is not the one required by PALM. 
+          IF ( day_of_month .GT. 0 .AND. day_of_month .LE. 31 .AND. month_of_year .GT. 0 .AND. month_of_year .LE. 12) THEN
+        
+             IF ( month_of_year == 1 ) THEN  !!month of year is read in input
+
+                day_of_year_init = day_of_month
+
+             ELSE
+
+                day_of_year_init= SUM(days( 1:(month_of_year-1) )) + day_of_month  !day_of_month is read in input in this case
+
+             ENDIF
+!kanani: Revise, we cannot force users to provide date_init, maybe set a default value?
+!           ELSE
+! 
+!              message_string = 'date_init not provided in the namelist or'             //          &
+!                               ' given in the wrong format: MUST BE DDMMYYYY'                        
+!              CALL message( 'calc_date_and_time', 'DT0100', 2, 2, 0, 6, 0 ) 
+     
+          ENDIF
+
+       ENDIF
+
       !-- Calculate actual hour of the day: the first hour of the day is from 00:00:00 to 00:59:59. 
 
@@ -302,8 +422,42 @@
 
       ENDIF       
-      
+
     END SUBROUTINE calc_date_and_time
 
-!TBD: check the routines used for update of emission indices in the DEFAULT MODE 
+
+!------------------------------------------------------------------------------!
+! Description:
+! ------------
+!> This routine determines the time factor index in the PRE-PROCESSED emissions mode.
+!------------------------------------------------------------------------------!
+
+ SUBROUTINE time_preprocessed_indices(index_hh)
+
+    USE indices
+
+    IMPLICIT NONE
+
+!
+!-- In/output
+    INTEGER, INTENT(INOUT) ::  index_hh    !> Index Hour
+!
+!-- Additional Variables for calculateing indices
+!-- Constants
+    INTEGER, PARAMETER ::  nhour = 24
+
+    IF (days_since_reference_point == 0) THEN
+
+       index_hh=hour_of_day
+
+    ELSE
+
+       index_hh=(days_since_reference_point*nhour)+(hour_of_day)
+
+    ENDIF
+
+
+ END SUBROUTINE time_preprocessed_indices
+
+
 !------------------------------------------------------------------------------!
 ! Description:
