Index: palm/trunk/SOURCE/spectra_mod.f90
===================================================================
--- palm/trunk/SOURCE/spectrum.f90	(revision 1818)
+++ palm/trunk/SOURCE/spectra_mod.f90	(revision 1833)
@@ -1,3 +1,3 @@
-!> @file spectrum.f90
+!> @file spectra_mod.f90
 !--------------------------------------------------------------------------------!
 ! This file is part of PALM.
@@ -19,5 +19,6 @@
 ! Current revisions:
 ! -----------------
-!
+! file renamed, reading the spectra_par NAMELIST moved from package_parin to
+! here
 !
 ! Former revisions:
@@ -91,5 +92,5 @@
 !>            transpose_zyd needs modification).
 !------------------------------------------------------------------------------!
- MODULE spectrum
+ MODULE spectra_mod
 
     USE kinds
@@ -97,41 +98,23 @@
     PRIVATE
 
-    CHARACTER (LEN=6),  DIMENSION(1:5) ::  header_char = (/ 'PS(u) ', 'PS(v) ',&
-                                           'PS(w) ', 'PS(pt)', 'PS(q) ' /)
-    CHARACTER (LEN=2),  DIMENSION(10)  ::  spectra_direction = 'x'
-    CHARACTER (LEN=10), DIMENSION(10)  ::  data_output_sp  = ' '
-    CHARACTER (LEN=25), DIMENSION(1:5) ::  utext_char =                    &
-                                           (/ '-power spectrum of u     ', &
-                                              '-power spectrum of v     ', &
-                                              '-power spectrum of w     ', &
-                                              '-power spectrum of ^1185 ', &
-                                              '-power spectrum of q     ' /)
-    CHARACTER (LEN=39), DIMENSION(1:5) ::  ytext_char =                        &
-                                 (/ 'k ^2236 ^2566^2569<u(k) in m>2s>->2    ', &
-                                    'k ^2236 ^2566^2569<v(k) in m>2s>->2    ', &
-                                    'k ^2236 ^2566^2569<w(k) in m>2s>->2    ', &
-                                    'k ^2236 ^2566^2569<^1185(k) in m>2s>->2', &
-                                    'k ^2236 ^2566^2569<q(k) in m>2s>->2    ' /)
-
-    INTEGER(iwp) ::  klist_x = 0, klist_y = 0, n_sp_x = 0, n_sp_y = 0
-
-    INTEGER(iwp) ::  comp_spectra_level(100) = 999999,                   &
-                     lstyles(100) = (/ 0, 7, 3, 10, 1, 4, 9, 2, 6, 8,    &
-                                       0, 7, 3, 10, 1, 4, 9, 2, 6, 8,    &
-                                       0, 7, 3, 10, 1, 4, 9, 2, 6, 8,    &
-                                       0, 7, 3, 10, 1, 4, 9, 2, 6, 8,    &
-                                       0, 7, 3, 10, 1, 4, 9, 2, 6, 8,    &
-                                       0, 7, 3, 10, 1, 4, 9, 2, 6, 8,    &
-                                       0, 7, 3, 10, 1, 4, 9, 2, 6, 8,    &
-                                       0, 7, 3, 10, 1, 4, 9, 2, 6, 8,    &
-                                       0, 7, 3, 10, 1, 4, 9, 2, 6, 8,    &
-                                       0, 7, 3, 10, 1, 4, 9, 2, 6, 8 /), &
-                     plot_spectra_level(100) = 999999
-
-    REAL(wp)    ::  time_to_start_sp = 0.0_wp
-
-    PUBLIC  comp_spectra_level, data_output_sp, header_char, klist_x, klist_y, &
-            lstyles, n_sp_x, n_sp_y, plot_spectra_level, spectra_direction,    &
-            utext_char, ytext_char
+    CHARACTER (LEN=2),  DIMENSION(10) ::  spectra_direction = 'x'
+    CHARACTER (LEN=10), DIMENSION(10) ::  data_output_sp  = ' '
+
+    INTEGER(iwp) ::  average_count_sp = 0
+    INTEGER(iwp) ::  dosp_time_count = 0
+    INTEGER(iwp) ::  n_sp_x = 0, n_sp_y = 0
+
+    INTEGER(iwp) ::  comp_spectra_level(100) = 999999
+
+    LOGICAL ::  calculate_spectra   = .FALSE.  !< internal switch that spectra are calculated
+    LOGICAL ::  spectra_initialized = .FALSE.  !< internal switch that spectra related quantities are initialized
+
+    REAL(wp) ::  averaging_interval_sp = 9999999.9_wp  !< averaging interval for spectra output
+    REAL(wp) ::  dt_dosp = 9999999.9_wp                !< time interval for spectra output
+    REAL(wp) ::  skip_time_dosp = 9999999.9_wp         !< no output of spectra data before this interval has passed
+
+    REAL(wp), DIMENSION(:), ALLOCATABLE ::  var_d
+
+    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  spectrum_x, spectrum_y
 
     SAVE
@@ -153,8 +136,208 @@
     END INTERFACE calc_spectra_y
 
-    PUBLIC calc_spectra
+    INTERFACE spectra_check_parameters
+       MODULE PROCEDURE spectra_check_parameters
+    END INTERFACE spectra_check_parameters
+
+    INTERFACE spectra_header
+       MODULE PROCEDURE spectra_header
+    END INTERFACE spectra_header
+
+    INTERFACE spectra_init
+       MODULE PROCEDURE spectra_init
+    END INTERFACE spectra_init
+
+    INTERFACE spectra_parin
+       MODULE PROCEDURE spectra_parin
+    END INTERFACE spectra_parin
+
+    PUBLIC average_count_sp, averaging_interval_sp, calc_spectra,              &
+           calculate_spectra, comp_spectra_level, data_output_sp,              &
+           dosp_time_count, dt_dosp, n_sp_x, n_sp_y, plot_spectra_level,       &
+           skip_time_dosp, spectra_check_parameters, spectra_direction,        &
+           spectra_header, spectra_init, spectra_parin, spectrum_x,            &
+           spectrum_y, var_d
 
 
  CONTAINS
+
+!------------------------------------------------------------------------------!
+! Description:
+! ------------
+!> Parin for &spectra_par for calculating spectra
+!------------------------------------------------------------------------------!
+    SUBROUTINE spectra_parin
+
+       USE control_parameters,                                                 &
+           ONLY:  dt_data_output
+
+       IMPLICIT NONE
+
+       CHARACTER (LEN=80) ::  line  !< dummy string that contains the current  &
+                                    !< line of the parameter file
+
+       NAMELIST /spectra_par/  averaging_interval_sp, comp_spectra_level,      &
+                               data_output_sp, dt_dosp, skip_time_dosp,        &
+                               spectra_direction
+
+
+!
+!--    Position the namelist-file at the beginning (it was already opened in
+!--    parin), search for the namelist-group of the package and position the
+!--    file at this line.
+       line = ' '
+
+!
+!--    Try to find the spectra package
+       REWIND ( 11 )
+       line = ' '
+       DO   WHILE ( INDEX( line, '&spectra_par' ) == 0 )
+          READ ( 11, '(A)', END=10 )  line
+       ENDDO
+       BACKSPACE ( 11 )
+
+!
+!--    Read namelist
+       READ ( 11, spectra_par )
+
+!
+!--    Default setting of dt_dosp here (instead of check_parameters), because
+!--    its current value is needed in init_pegrid
+       IF ( dt_dosp == 9999999.9_wp )  dt_dosp = dt_data_output
+
+!
+!--    Set general switch that spectra shall be calculated
+       calculate_spectra = .TRUE.
+
+ 10    CONTINUE
+
+    END SUBROUTINE spectra_parin
+
+
+
+!------------------------------------------------------------------------------!
+! Description:
+! ------------
+!> Initialization of spectra related variables
+!------------------------------------------------------------------------------!
+    SUBROUTINE spectra_init
+
+       USE indices,                                                            &
+           ONLY:  nx, ny, nzb, nzt
+
+       IMPLICIT NONE
+
+       IF ( spectra_initialized )  RETURN
+
+       IF ( dt_dosp /= 9999999.9_wp )  THEN
+          ALLOCATE( spectrum_x( 1:nx/2, 1:10, 1:10 ),                          &
+                    spectrum_y( 1:ny/2, 1:10, 1:10 ) )
+          spectrum_x = 0.0_wp
+          spectrum_y = 0.0_wp
+
+          ALLOCATE( var_d(nzb:nzt+1) )
+          var_d = 0.0_wp
+       ENDIF
+
+       spectra_initialized = .TRUE.
+
+    END SUBROUTINE spectra_init
+
+
+
+!------------------------------------------------------------------------------!
+! Description:
+! ------------
+!> Check spectra related quantities
+!------------------------------------------------------------------------------!
+    SUBROUTINE spectra_check_parameters
+
+       USE control_parameters,                                                 &
+           ONLY:  averaging_interval, message_string, skip_time_data_output
+
+       IMPLICIT NONE
+
+!
+!--    Check the average interval
+       IF ( averaging_interval_sp == 9999999.9_wp )  THEN
+          averaging_interval_sp = averaging_interval
+       ENDIF
+
+       IF ( averaging_interval_sp > dt_dosp )  THEN
+          WRITE( message_string, * )  'averaging_interval_sp = ',              &
+                averaging_interval_sp, ' must be <= dt_dosp = ', dt_dosp
+          CALL message( 'spectra_check_parameters', 'PA0087', 1, 2, 0, 6, 0 )
+       ENDIF
+
+!
+!--    Set the default skip time interval for data output, if necessary
+       IF ( skip_time_dosp == 9999999.9_wp )                                   &
+                                          skip_time_dosp = skip_time_data_output
+
+    END SUBROUTINE spectra_check_parameters
+
+
+
+!------------------------------------------------------------------------------!
+! Description:
+! ------------
+!> Header output for spectra
+!>
+!> @todo Output of netcdf data format and compression level
+!------------------------------------------------------------------------------!
+    SUBROUTINE spectra_header ( io )
+
+       USE control_parameters,                                                 &
+           ONLY:  dt_averaging_input_pr
+
+!       USE netcdf_interface,                                                  &
+!           ONLY:  netcdf_data_format_string, netcdf_deflate
+
+       IMPLICIT NONE
+
+       CHARACTER (LEN=40) ::  output_format       !< internal string
+
+       INTEGER(iwp) ::  i                         !< internal counter
+       INTEGER(iwp), INTENT(IN) ::  io            !< Unit of the output file
+
+!
+!--    Spectra output
+       IF ( dt_dosp /= 9999999.9_wp )  THEN
+          WRITE ( io, 1 )
+
+!          output_format = netcdf_data_format_string
+!          IF ( netcdf_deflate == 0 )  THEN
+!             WRITE ( io, 2 )  output_format
+!          ELSE
+!             WRITE ( io, 3 )  TRIM( output_format ), netcdf_deflate
+!          ENDIF
+          WRITE ( io, 2 )  'see profiles or other quantities'
+          WRITE ( io, 4 )  dt_dosp
+          IF ( skip_time_dosp /= 0.0_wp )  WRITE ( io, 5 )  skip_time_dosp
+          WRITE ( io, 6 )  ( data_output_sp(i), i = 1,10 ),     &
+                           ( spectra_direction(i), i = 1,10 ),  &
+                           ( comp_spectra_level(i), i = 1,100 ), &
+                           averaging_interval_sp, dt_averaging_input_pr
+       ENDIF
+
+     1 FORMAT ('    Spectra:')
+     2 FORMAT ('       Output format: ',A/)
+     3 FORMAT ('       Output format: ',A, '   compressed with level: ',I1/)
+     4 FORMAT ('       Output every ',F7.1,' s'/)
+     5 FORMAT ('       No output during initial ',F8.2,' s')
+     6 FORMAT ('       Arrays:     ', 10(A5,',')/                         &
+               '       Directions: ', 10(A5,',')/                         &
+               '       height levels  k = ', 20(I3,',')/                  &
+               '                          ', 20(I3,',')/                  &
+               '                          ', 20(I3,',')/                  &
+               '                          ', 20(I3,',')/                  &
+               '                          ', 19(I3,','),I3,'.'/           &
+               '       Time averaged over ', F7.1, ' s,' /                &
+               '       Profiles for the time averaging are taken every ', &
+                    F6.1,' s')
+
+    END SUBROUTINE spectra_header
+
+
 
     SUBROUTINE calc_spectra
@@ -164,5 +347,5 @@
 
        USE control_parameters,                                                 &
-           ONLY:  average_count_sp, bc_lr_cyc, bc_ns_cyc, message_string, psolver
+           ONLY:  bc_lr_cyc, bc_ns_cyc, message_string, psolver
 
        USE cpulog,                                                             &
@@ -191,4 +374,8 @@
 
        CALL cpu_log( log_point(30), 'calc_spectra', 'start' )
+
+!
+!--    Initialize spectra related quantities
+       CALL spectra_init
 
 !
@@ -308,5 +495,5 @@
 
        USE statistics,                                                         &
-           ONLY:  hom, var_d
+           ONLY:  hom
 
 
@@ -411,7 +598,4 @@
            ONLY:  comm2d, ierr, myid
 
-       USE statistics,                                                         &
-           ONLY:  spectrum_x, var_d
-
        USE transpose_indices,                                                  &
            ONLY:  nyn_x, nys_x, nzb_x, nzt_x
@@ -554,7 +738,4 @@
            ONLY:  comm2d, ierr, myid
 
-       USE statistics,                                                         &
-           ONLY:  spectrum_y, var_d
-
        USE transpose_indices,                                                  &
            ONLY:  nxl_yd, nxr_yd, nzb_yd, nzt_yd
@@ -669,3 +850,3 @@
     END SUBROUTINE calc_spectra_y
 
- END MODULE spectrum
+ END MODULE spectra_mod
