Index: palm/trunk/SOURCE/netcdf_interface_mod.f90
===================================================================
--- palm/trunk/SOURCE/netcdf_interface_mod.f90	(revision 3942)
+++ palm/trunk/SOURCE/netcdf_interface_mod.f90	(revision 3953)
@@ -25,4 +25,8 @@
 ! -----------------
 ! $Id$
+! bugfix: set origin_time and starting point of time coordinate according to
+!         day_of_year_init and time_utc_init
+! 
+! 3942 2019-04-30 13:08:30Z kanani
 ! Add specifier to netcdf_handle_error to simplify identification of attribute
 ! causing the error
@@ -492,4 +496,5 @@
     CHARACTER(LEN=20), DIMENSION(11) ::  netcdf_precision = ' '
     CHARACTER(LEN=40) ::  netcdf_data_format_string
+    CHARACTER(LEN=23) ::  origin_time_string  !< string containing date and time of origin
 
     INTEGER(iwp) ::  id_dim_agtnum, id_dim_time_agt,                           &
@@ -681,4 +686,7 @@
                skip_time_do3d, topography, num_leg, num_var_fl,                &
                urban_surface
+
+    USE date_and_time_mod,                                                     &
+       ONLY:   day_of_year_init, time_utc_init
 
     USE grid_variables,                                                        &
@@ -902,5 +910,7 @@
 
     ENDIF
-
+!
+!-- Compose date-time string for origin_time
+    origin_time_string = date_time_string( day_of_year_init, time_utc_init )
 !
 !-- Convert coord_ref_sys into vector (used for lat/lon calculation)
@@ -962,5 +972,5 @@
                                   (/ id_dim_time_mask(mid,av) /), 'time',      &
                                   NF90_DOUBLE, id_var_time_mask(mid,av),       &
-                                 'seconds since '//TRIM(init_model%origin_time), 'time', 468, 469, 000 )
+                                 'seconds since '//origin_time_string, 'time', 468, 469, 000 )
           CALL netcdf_create_att( id_set_mask(mid,av), id_var_time_mask(mid,av), 'standard_name', 'time', 000)
           CALL netcdf_create_att( id_set_mask(mid,av), id_var_time_mask(mid,av), 'axis', 'T', 000)
@@ -1745,5 +1755,5 @@
           CALL netcdf_create_var( id_set_3d(av), (/ id_dim_time_3d(av) /),     &
                                   'time', NF90_DOUBLE, id_var_time_3d(av),     &
-                                  'seconds since '//TRIM(init_model%origin_time), 'time', 65, 66, 00 )
+                                  'seconds since '//origin_time_string, 'time', 65, 66, 00 )
           CALL netcdf_create_att( id_set_3d(av), id_var_time_3d(av), 'standard_name', 'time', 000)
           CALL netcdf_create_att( id_set_3d(av), id_var_time_3d(av), 'axis', 'T', 000)
@@ -2551,5 +2561,5 @@
 
           CALL netcdf_create_var( id_set_agt, (/ id_dim_time_agt /), 'time',   &
-                                  NF90_REAL4, id_var_time_agt, 'seconds since '//TRIM(init_model%origin_time), 'time',  &
+                                  NF90_REAL4, id_var_time_agt, 'seconds since '//origin_time_string, 'time',  &
                                   332, 333, 000 )
           CALL netcdf_create_att( id_set_agt, id_var_time_agt, 'standard_name', 'time', 000)
@@ -2689,5 +2699,5 @@
           CALL netcdf_create_var( id_set_xy(av), (/ id_dim_time_xy(av) /),     &
                                   'time', NF90_DOUBLE, id_var_time_xy(av),     &
-                                  'seconds since '//TRIM(init_model%origin_time), 'time', 100, 101, 000 )
+                                  'seconds since '//origin_time_string, 'time', 100, 101, 000 )
           CALL netcdf_create_att( id_set_xy(av), id_var_time_xy(av), 'standard_name', 'time', 000)
           CALL netcdf_create_att( id_set_xy(av), id_var_time_xy(av), 'axis', 'T', 000)
@@ -3643,5 +3653,5 @@
           CALL netcdf_create_var( id_set_xz(av), (/ id_dim_time_xz(av) /),     &
                                   'time', NF90_DOUBLE, id_var_time_xz(av),     &
-                                  'seconds since '//TRIM(init_model%origin_time), 'time', 143, 144, 000 )
+                                  'seconds since '//origin_time_string, 'time', 143, 144, 000 )
           CALL netcdf_create_att( id_set_xz(av), id_var_time_xz(av), 'standard_name', 'time', 000)
           CALL netcdf_create_att( id_set_xz(av), id_var_time_xz(av), 'axis', 'T', 000)
@@ -4501,5 +4511,5 @@
           CALL netcdf_create_var( id_set_yz(av), (/ id_dim_time_yz(av) /),     &
                                   'time', NF90_DOUBLE, id_var_time_yz(av),     &
-                                  'seconds since '//TRIM(init_model%origin_time), 'time', 182, 183, 000 )
+                                  'seconds since '//origin_time_string, 'time', 182, 183, 000 )
           CALL netcdf_create_att( id_set_yz(av), id_var_time_yz(av), 'standard_name', 'time', 000)
           CALL netcdf_create_att( id_set_yz(av), id_var_time_yz(av), 'axis', 'T', 000)
@@ -5482,5 +5492,5 @@
                                   id_dim_time_pr, 220 )
           CALL netcdf_create_var( id_set_pr, (/ id_dim_time_pr /), 'time',     &
-                                  NF90_DOUBLE, id_var_time_pr, 'seconds since '//TRIM(init_model%origin_time), 'time',  &
+                                  NF90_DOUBLE, id_var_time_pr, 'seconds since '//origin_time_string, 'time',  &
                                   221, 222, 000 )
           CALL netcdf_create_att( id_set_pr, id_var_time_pr, 'standard_name', 'time', 000)
@@ -5735,5 +5745,5 @@
                                   id_dim_time_ts, 250 )
           CALL netcdf_create_var( id_set_ts, (/ id_dim_time_ts /), 'time',     &
-                                  NF90_DOUBLE, id_var_time_ts, 'seconds since '//TRIM(init_model%origin_time), 'time',  &
+                                  NF90_DOUBLE, id_var_time_ts, 'seconds since '//origin_time_string, 'time',  &
                                   251, 252, 000 )
           CALL netcdf_create_att( id_set_ts, id_var_time_ts, 'standard_name', 'time', 000)
@@ -5932,5 +5942,5 @@
                                   id_dim_time_sp, 270 )
           CALL netcdf_create_var( id_set_sp, (/ id_dim_time_sp /), 'time',     &
-                                  NF90_DOUBLE, id_var_time_sp, 'seconds since '//TRIM(init_model%origin_time), 'time',  &
+                                  NF90_DOUBLE, id_var_time_sp, 'seconds since '//origin_time_string, 'time',  &
                                   271, 272, 000 )
           CALL netcdf_create_att( id_set_sp, id_var_time_sp, 'standard_name', 'time', 000)
@@ -6425,5 +6435,5 @@
                                   id_dim_time_pts, 397 )
           CALL netcdf_create_var( id_set_pts, (/ id_dim_time_pts /), 'time',   &
-                                  NF90_DOUBLE, id_var_time_pts, 'seconds since '//TRIM(init_model%origin_time), 'time', &
+                                  NF90_DOUBLE, id_var_time_pts, 'seconds since '//origin_time_string, 'time', &
                                   398, 399, 000 )
           CALL netcdf_create_att( id_set_pts, id_var_time_pts, 'standard_name', 'time', 000)
@@ -6623,5 +6633,5 @@
                                   id_dim_time_fl, 250 )
           CALL netcdf_create_var( id_set_fl, (/ id_dim_time_fl /), 'time',     &
-                                  NF90_DOUBLE, id_var_time_fl, 'seconds since '//TRIM(init_model%origin_time), 'time',  &
+                                  NF90_DOUBLE, id_var_time_fl, 'seconds since '//origin_time_string, 'time',  &
                                   251, 252, 000 )
           CALL netcdf_create_att( id_set_fl, id_var_time_fl, 'standard_name', 'time', 000)
@@ -7115,5 +7125,5 @@
     CALL netcdf_handle_error( 'netcdf_create_global_atts 5', error_no )
 
-    nc_stat = NF90_PUT_ATT( ncid, NF90_GLOBAL, 'origin_time', init_model%origin_time )
+    nc_stat = NF90_PUT_ATT( ncid, NF90_GLOBAL, 'origin_time', origin_time_string )
     CALL netcdf_handle_error( 'netcdf_create_global_atts 6', error_no )
     nc_stat = NF90_PUT_ATT( ncid, NF90_GLOBAL, 'origin_lat', init_model%latitude )
@@ -7457,3 +7467,92 @@
  END SUBROUTINE convert_utm_to_geographic
 
+ !------------------------------------------------------------------------------!
+ ! Description:
+ ! ------------
+ !> Compose string containing date and time of format 'YYYY-MM-DD hh:mm:ss ZZZ'
+ !> from day_of_year and second_of_day.
+ !------------------------------------------------------------------------------!
+ FUNCTION date_time_string( day_of_year, second_of_day )
+
+    IMPLICIT NONE
+
+    CHARACTER(LEN=1) ::  plus_minus  !< either '+' or '-'
+
+    CHARACTER(LEN=23) ::  date_time_string  !< string containing date and time
+
+    INTEGER(iwp) ::  day          !< day of month
+    INTEGER(iwp) ::  err          !< error code
+    INTEGER(iwp) ::  i            !< loop index
+    INTEGER(iwp) ::  hour         !< hour of the day
+    INTEGER(iwp) ::  minute       !< minute of the hour
+    INTEGER(iwp) ::  month        !< month of year
+    INTEGER(iwp) ::  year = 2019  !< year (no leap year)
+    INTEGER(iwp) ::  zone = 0     !< time zone
+
+    INTEGER(iwp), INTENT(IN) ::  day_of_year  !< day of year to start with
+
+    INTEGER, DIMENSION(12) :: days_per_month = &  !< total number of days for
+    (/31,28,31,30,31,30,31,31,30,31,30,31/)       !< each month (no leap year)
+
+    REAL(wp) ::  second  !< second of the minute
+
+    REAL(wp), INTENT(IN) ::  second_of_day  !< second of the day
+
+
+    err = 0_iwp
+!
+!-- Check day_of_year
+    IF ( day_of_year < 1_iwp .OR. day_of_year > SUM(days_per_month) )  THEN
+       err = err + 1_iwp
+    ENDIF
+!
+!-- Check second_of_day
+    IF ( second_of_day < 0.0_wp  .OR.  second_of_day > 86400.0_wp )  THEN
+       err = err + 2_iwp
+    ENDIF
+!
+!-- Execute only if given values are valid
+    IF ( err == 0_iwp )  THEN
+
+       day = day_of_year
+       month = 0_iwp
+
+       DO  i = 1, 12
+          day = day - days_per_month(i)
+          IF ( day < 0 )  THEN
+             day = day + days_per_month(i)
+             month = i
+             EXIT
+          ENDIF
+       ENDDO
+
+       hour = INT( second_of_day / 3600.0_wp, KIND=iwp )
+
+       second = second_of_day - 3600.0_wp * REAL( hour, KIND=wp )
+
+       minute =  INT( second / 60.0_wp, KIND=iwp )
+       
+       second = second - 60.0_wp * REAL( minute, KIND=wp )
+
+       IF ( zone < 0_iwp )  THEN
+          plus_minus = '-'
+       ELSE
+          plus_minus = '+'
+       ENDIF
+
+       WRITE( date_time_string, 100 )                               &
+          year, month, day, hour, minute, INT( second, KIND=iwp ),  &
+          plus_minus, zone
+
+    ELSE
+!
+!--   Return empty string if input is invalid
+      date_time_string = REPEAT( " ", LEN(date_time_string) )
+
+    ENDIF
+
+ 100 FORMAT (I4,'-',I2.2,'-',I2.2,1X,I2.2,':',I2.2,':',I2.2,X,A1,I2.2)
+
+ END FUNCTION date_time_string
+
  END MODULE netcdf_interface
