real_em.F

References to this file elsewhere.
1 !  Create an initial data set for the WRF model based on real data.  This
2 !  program is specifically set up for the Eulerian, mass-based coordinate.
3 PROGRAM real_data
4 
5    USE module_machine
6    USE module_domain
7    USE module_initialize_real
8    USE module_io_domain
9    USE module_driver_constants
10    USE module_configure
11    USE module_timing
12 #ifdef WRF_CHEM
13    USE module_input_chem_data
14    USE module_input_chem_bioemiss
15    USE module_input_chem_emissopt3
16 #endif
17    USE module_utility
18 #ifdef DM_PARALLEL
19    USE module_dm
20 #endif
21 
22    IMPLICIT NONE
23 
24 #ifdef WRF_CHEM
25   ! interface
26    INTERFACE
27      ! mediation-supplied 
28      SUBROUTINE med_read_wrf_chem_bioemiss ( grid , config_flags)
29        USE module_domain
30        TYPE (domain) grid
31        TYPE (grid_config_rec_type) config_flags
32      END SUBROUTINE med_read_wrf_chem_bioemiss
33    END INTERFACE
34 #endif
35 
36    REAL    :: time , bdyfrq
37 
38    INTEGER :: loop , levels_to_process , debug_level
39 
40 
41    TYPE(domain) , POINTER :: null_domain
42    TYPE(domain) , POINTER :: grid , another_grid
43    TYPE(domain) , POINTER :: grid_ptr , grid_ptr2
44    TYPE (grid_config_rec_type)              :: config_flags
45    INTEGER                :: number_at_same_level
46 
47    INTEGER :: max_dom, domain_id , grid_id , parent_id , parent_id1 , id
48    INTEGER :: e_we , e_sn , i_parent_start , j_parent_start
49    INTEGER :: idum1, idum2 
50 #ifdef DM_PARALLEL
51    INTEGER                 :: nbytes
52    INTEGER, PARAMETER      :: configbuflen = 4* CONFIG_BUF_LEN
53    INTEGER                 :: configbuf( configbuflen )
54    LOGICAL , EXTERNAL      :: wrf_dm_on_monitor
55 #endif
56    LOGICAL found_the_id
57 
58    INTEGER :: ids , ide , jds , jde , kds , kde
59    INTEGER :: ims , ime , jms , jme , kms , kme
60    INTEGER :: ips , ipe , jps , jpe , kps , kpe
61    INTEGER :: ijds , ijde , spec_bdy_width
62    INTEGER :: i , j , k , idts, rc
63    INTEGER :: sibling_count , parent_id_hold , dom_loop
64 
65    CHARACTER (LEN=80)     :: message
66 
67    INTEGER :: start_year , start_month , start_day , start_hour , start_minute , start_second
68    INTEGER ::   end_year ,   end_month ,   end_day ,   end_hour ,   end_minute ,   end_second
69    INTEGER :: interval_seconds , real_data_init_type
70    INTEGER :: time_loop_max , time_loop
71 real::t1,t2
72    INTERFACE
73      SUBROUTINE Setup_Timekeeping( grid )
74       USE module_domain
75       TYPE(domain), POINTER :: grid
76      END SUBROUTINE Setup_Timekeeping
77    END INTERFACE
78 
79 #include "version_decl"
80 
81    !  Define the name of this program (program_name defined in module_domain)
82 
83    ! NOTE: share/input_wrf.F tests first 7 chars of this name to decide 
84    ! whether to read P_TOP as metadata from the SI (yes, if .eq. REAL_EM)
85 
86    program_name = "REAL_EM " // TRIM(release_version) // " PREPROCESSOR"
87 
88 #ifdef DM_PARALLEL
89    CALL disable_quilting
90 #endif
91 
92    !  Initialize the modules used by the WRF system.  Many of the CALLs made from the
93    !  init_modules routine are NO-OPs.  Typical initializations are: the size of a
94    !  REAL, setting the file handles to a pre-use value, defining moisture and
95    !  chemistry indices, etc.
96 
97    CALL wrf_debug ( 100 , 'real_em: calling init_modules ' )
98    CALL init_modules(1)   ! Phase 1 returns after MPI_INIT() (if it is called)
99    CALL WRFU_Initialize( defaultCalendar=WRFU_CAL_GREGORIAN, rc=rc )
100    CALL init_modules(2)   ! Phase 2 resumes after MPI_INIT() (if it is called)
101 
102    !  The configuration switches mostly come from the NAMELIST input.
103 
104 #ifdef DM_PARALLEL
105    IF ( wrf_dm_on_monitor() ) THEN
106       CALL initial_config
107    ENDIF
108    CALL get_config_as_buffer( configbuf, configbuflen, nbytes )
109    CALL wrf_dm_bcast_bytes( configbuf, nbytes )
110    CALL set_config_as_buffer( configbuf, configbuflen )
111    CALL wrf_dm_initialize
112 #else
113    CALL initial_config
114 #endif
115 
116    CALL nl_get_debug_level ( 1, debug_level )
117    CALL set_wrf_debug_level ( debug_level )
118 
119    CALL wrf_message ( program_name )
120 
121    !  Allocate the space for the mother of all domains.
122 
123    NULLIFY( null_domain )
124    CALL wrf_debug ( 100 , 'real_em: calling alloc_and_configure_domain ' )
125    CALL alloc_and_configure_domain ( domain_id  = 1           , &
126                                      grid       = head_grid   , &
127                                      parent     = null_domain , &
128                                      kid        = -1            )
129 
130    grid => head_grid
131    CALL nl_get_max_dom ( 1 , max_dom )
132 
133    IF ( model_config_rec%interval_seconds .LE. 0 ) THEN
134      CALL wrf_error_fatal( 'namelist value for interval_seconds must be > 0')
135    ENDIF
136 
137    all_domains : DO domain_id = 1 , max_dom
138 
139       IF ( ( model_config_rec%input_from_file(domain_id) ) .OR. &
140            ( domain_id .EQ. 1 ) ) THEN
141 
142          IF ( domain_id .GT. 1 ) THEN
143 
144             CALL nl_get_grid_id        ( domain_id, grid_id        )
145             CALL nl_get_parent_id      ( domain_id, parent_id      )
146             CALL nl_get_e_we           ( domain_id, e_we           )
147             CALL nl_get_e_sn           ( domain_id, e_sn           )
148             CALL nl_get_i_parent_start ( domain_id, i_parent_start )
149             CALL nl_get_j_parent_start ( domain_id, j_parent_start )
150             WRITE (message,FMT='(A,2I3,2I4,2I3)') &
151             'new allocated  domain: id, par id, dims i/j, start i/j =', &
152             grid_id, parent_id, e_we, e_sn, i_parent_start, j_parent_start
153 
154             CALL wrf_debug ( 100 , message )
155             CALL nl_get_grid_id        ( parent_id, grid_id        )
156             CALL nl_get_parent_id      ( parent_id, parent_id1     )
157             CALL nl_get_e_we           ( parent_id, e_we           )
158             CALL nl_get_e_sn           ( parent_id, e_sn           )
159             CALL nl_get_i_parent_start ( parent_id, i_parent_start )
160             CALL nl_get_j_parent_start ( parent_id, j_parent_start )
161             WRITE (message,FMT='(A,2I3,2I4,2I3)') &
162             'parent domain: id, par id, dims i/j, start i/j =', &
163             grid_id, parent_id1, e_we, e_sn, i_parent_start, j_parent_start
164             CALL wrf_debug ( 100 , message )
165 
166             CALL nl_get_grid_id        ( domain_id, grid_id        )
167             CALL nl_get_parent_id      ( domain_id, parent_id      )
168             CALL nl_get_e_we           ( domain_id, e_we           )
169             CALL nl_get_e_sn           ( domain_id, e_sn           )
170             CALL nl_get_i_parent_start ( domain_id, i_parent_start )
171             CALL nl_get_j_parent_start ( domain_id, j_parent_start )
172             grid_ptr2 => head_grid
173             found_the_id = .FALSE.
174             CALL find_my_parent ( grid_ptr2 , grid_ptr , domain_id , parent_id , found_the_id )
175             IF ( found_the_id ) THEN
176 
177                sibling_count = 0
178                DO dom_loop = 2 , domain_id
179                  CALL nl_get_parent_id ( dom_loop, parent_id_hold )
180                  IF ( parent_id_hold .EQ. parent_id ) THEN
181                     sibling_count = sibling_count + 1
182                  END IF
183                END DO
184                CALL alloc_and_configure_domain ( domain_id  = domain_id    , &
185                                                  grid       = another_grid , &
186                                                  parent     = grid_ptr     , &
187                                                  kid        = sibling_count )
188                grid => another_grid
189             ELSE
190               CALL wrf_error_fatal( 'real_em.F: Could not find the parent domain')
191             END IF
192          END IF
193 
194          CALL Setup_Timekeeping ( grid )
195          CALL set_current_grid_ptr( grid )
196          CALL domain_clockprint ( 150, grid, &
197                 'DEBUG real:  clock after Setup_Timekeeping,' )
198          CALL domain_clock_set( grid, &
199                                 time_step_seconds=model_config_rec%interval_seconds )
200          CALL domain_clockprint ( 150, grid, &
201                 'DEBUG real:  clock after timeStep set,' )
202 
203 
204          CALL wrf_debug ( 100 , 'real_em: calling set_scalar_indices_from_config ' )
205          CALL set_scalar_indices_from_config ( grid%id , idum1, idum2 )
206 
207          CALL wrf_debug ( 100 , 'real_em: calling model_to_grid_config_rec ' )
208          CALL model_to_grid_config_rec ( grid%id , model_config_rec , config_flags )
209 
210          !  Initialize the WRF IO: open files, init file handles, etc.
211 
212          CALL wrf_debug ( 100 , 'real_em: calling init_wrfio' )
213          CALL init_wrfio
214 
215          !  Some of the configuration values may have been modified from the initial READ
216          !  of the NAMELIST, so we re-broadcast the configuration records.
217 
218 #ifdef DM_PARALLEL
219          CALL wrf_debug ( 100 , 'real_em: re-broadcast the configuration records' )
220          CALL get_config_as_buffer( configbuf, configbuflen, nbytes )
221          CALL wrf_dm_bcast_bytes( configbuf, nbytes )
222          CALL set_config_as_buffer( configbuf, configbuflen )
223 #endif
224 
225          !   No looping in this layer.  
226 
227          CALL wrf_debug ( 100 , 'calling med_sidata_input' )
228          CALL med_sidata_input ( grid , config_flags )
229          CALL wrf_debug ( 100 , 'backfrom med_sidata_input' )
230 
231       ELSE 
232          CYCLE all_domains
233       END IF
234 
235    END DO all_domains
236 
237    CALL set_current_grid_ptr( head_grid )
238 
239    !  We are done.
240 
241    CALL wrf_debug (   0 , 'real_em: SUCCESS COMPLETE REAL_EM INIT' )
242 
243    CALL wrf_shutdown
244 
245    CALL WRFU_Finalize( rc=rc )
246 
247 END PROGRAM real_data
248 
249 SUBROUTINE med_sidata_input ( grid , config_flags )
250   ! Driver layer
251    USE module_domain
252    USE module_io_domain
253   ! Model layer
254    USE module_configure
255    USE module_bc_time_utilities
256    USE module_initialize_real
257    USE module_optional_si_input
258 #ifdef WRF_CHEM
259    USE module_input_chem_data
260    USE module_input_chem_bioemiss
261    USE module_input_chem_emissopt3
262 #endif
263 
264    USE module_date_time
265    USE module_utility
266 
267    IMPLICIT NONE
268 
269 
270   ! Interface 
271    INTERFACE
272      SUBROUTINE start_domain ( grid , allowed_to_read )  ! comes from module_start in appropriate dyn_ directory
273        USE module_domain
274        TYPE (domain) grid
275        LOGICAL, INTENT(IN) :: allowed_to_read
276      END SUBROUTINE start_domain
277    END INTERFACE
278 
279   ! Arguments
280    TYPE(domain)                :: grid
281    TYPE (grid_config_rec_type) :: config_flags
282   ! Local
283    INTEGER                :: time_step_begin_restart
284    INTEGER                :: idsi , ierr , myproc
285    CHARACTER (LEN=80)      :: si_inpname
286    CHARACTER (LEN=80)      :: message
287 
288    CHARACTER(LEN=19) :: start_date_char , end_date_char , current_date_char , next_date_char
289 
290    INTEGER :: time_loop_max , loop, rc
291    INTEGER :: julyr , julday 
292    REAL :: gmt
293 real::t1,t2,t3,t4
294 
295    grid%input_from_file = .true.
296    grid%input_from_file = .false.
297 
298    CALL compute_si_start_and_end ( model_config_rec%start_year  (grid%id) , &
299                                    model_config_rec%start_month (grid%id) , &
300                                    model_config_rec%start_day   (grid%id) , &
301                                    model_config_rec%start_hour  (grid%id) , &
302                                    model_config_rec%start_minute(grid%id) , &
303                                    model_config_rec%start_second(grid%id) , &
304                                    model_config_rec%  end_year  (grid%id) , & 
305                                    model_config_rec%  end_month (grid%id) , &
306                                    model_config_rec%  end_day   (grid%id) , &
307                                    model_config_rec%  end_hour  (grid%id) , &
308                                    model_config_rec%  end_minute(grid%id) , &
309                                    model_config_rec%  end_second(grid%id) , &
310                                    model_config_rec%interval_seconds      , &
311                                    model_config_rec%real_data_init_type   , &
312                                    start_date_char , end_date_char , time_loop_max )
313 
314    !  Override stop time with value computed above.  
315    CALL domain_clock_set( grid, stop_timestr=end_date_char )
316 
317    ! TBH:  for now, turn off stop time and let it run data-driven
318    CALL WRFU_ClockStopTimeDisable( grid%domain_clock, rc=rc ) 
319    CALL wrf_check_error( WRFU_SUCCESS, rc, &
320                          'WRFU_ClockStopTimeDisable(grid%domain_clock) FAILED', &
321                          __FILE__ , &
322                          __LINE__  )
323    CALL domain_clockprint ( 150, grid, &
324           'DEBUG med_sidata_input:  clock after stopTime set,' )
325 
326    !  Here we define the initial time to process, for later use by the code.
327    
328    current_date_char = start_date_char
329    start_date = start_date_char // '.0000'
330    current_date = start_date
331 
332    CALL nl_set_bdyfrq ( grid%id , REAL(model_config_rec%interval_seconds) )
333 
334    !!!!!!!  Loop over each time period to process.
335 
336    CALL cpu_time ( t1 )
337    DO loop = 1 , time_loop_max
338 
339       internal_time_loop = loop
340       IF ( ( grid%id .GT. 1 ) .AND. ( loop .GT. 1 ) .AND. &
341            ( model_config_rec%grid_fdda(grid%id) .EQ. 0 ) .AND. &
342            ( model_config_rec%sst_update .EQ. 0 ) ) EXIT
343 
344       print *,' '
345       print *,'-----------------------------------------------------------------------------'
346       print *,' '
347       print '(A,I2,A,A,A,I4,A,I4)' , &
348       ' Domain ',grid%id,': Current date being processed: ',current_date, ', which is loop #',loop,' out of ',time_loop_max
349 
350       !  After current_date has been set, fill in the julgmt stuff.
351 
352       CALL geth_julgmt ( config_flags%julyr , config_flags%julday , config_flags%gmt )
353 
354         print *,'configflags%julyr, %julday, %gmt:',config_flags%julyr, config_flags%julday, config_flags%gmt
355       !  Now that the specific Julian info is available, save these in the model config record.
356 
357       CALL nl_set_gmt (grid%id, config_flags%gmt)
358       CALL nl_set_julyr (grid%id, config_flags%julyr)
359       CALL nl_set_julday (grid%id, config_flags%julday)
360 
361       !  Open the input file for real.  This is either the "new" one or the "old" one.  The "new" one could have
362       !  a suffix for the type of the data format.  Check to see if either is around.
363 
364       CALL cpu_time ( t3 )
365       IF ( grid%dyn_opt .EQ. dyn_em ) THEN
366          WRITE ( wrf_err_message , FMT='(A,A)' )'med_sidata_input: calling open_r_dataset for ', &
367                                                 TRIM(config_flags%auxinput1_inname)
368          CALL wrf_debug ( 100 , wrf_err_message )
369          IF ( config_flags%auxinput1_inname(1:8) .NE. 'wrf_real' ) THEN
370             CALL construct_filename4a( si_inpname , config_flags%auxinput1_inname , grid%id , 2 , &
371                                        current_date_char , config_flags%io_form_auxinput1 )
372          ELSE
373             CALL construct_filename2a( si_inpname , config_flags%auxinput1_inname , grid%id , 2 , &
374                                        current_date_char )
375          END IF
376          CALL open_r_dataset ( idsi, TRIM(si_inpname) , grid , config_flags , "DATASET=AUXINPUT1", ierr )
377          IF ( ierr .NE. 0 ) THEN
378             CALL wrf_error_fatal( 'error opening ' // TRIM(si_inpname) // &
379                                   ' for input; bad date in namelist or file not in directory' )
380          ENDIF
381       END IF
382 
383       !  Input data.
384 
385       CALL wrf_debug ( 100 , 'med_sidata_input: calling input_aux_model_input1' )
386       CALL input_aux_model_input1 ( idsi ,   grid , config_flags , ierr )
387       CALL cpu_time ( t4 )
388       WRITE ( wrf_err_message , FMT='(A,I10,A)' ) 'Timing for input ',NINT(t4-t3) ,' s.'
389       CALL wrf_debug( 0, wrf_err_message )
390 
391       !  Possible optional SI input.  This sets flags used by init_domain.
392 
393       CALL cpu_time ( t3 )
394       IF ( loop .EQ. 1 ) THEN
395          already_been_here = .FALSE.
396          CALL wrf_debug ( 100 , 'med_sidata_input: calling init_module_optional_si_input' )
397          CALL init_module_optional_si_input ( grid , config_flags )
398       END IF
399       CALL wrf_debug ( 100 , 'med_sidata_input: calling optional_si_input' )
400       CALL optional_si_input ( grid , idsi )
401 
402       !  Initialize the mother domain for this time period with input data.
403 
404       CALL wrf_debug ( 100 , 'med_sidata_input: calling init_domain' )
405       grid%input_from_file = .true.
406       CALL init_domain ( grid )
407       CALL cpu_time ( t4 )
408       WRITE ( wrf_err_message , FMT='(A,I10,A)' ) 'Timing for processing ',NINT(t4-t3) ,' s.'
409       CALL wrf_debug( 0, wrf_err_message )
410       CALL model_to_grid_config_rec ( grid%id , model_config_rec , config_flags )
411 
412       !  Close this file that is output from the SI and input to this pre-proc.
413 
414       CALL wrf_debug ( 100 , 'med_sidata_input: back from init_domain' )
415       CALL close_dataset ( idsi , config_flags , "DATASET=AUXINPUT1" )
416 
417 !     CALL start_domain ( grid , .TRUE. )
418 
419 #ifdef WRF_CHEM
420       IF ( loop == 1 ) THEN
421          IF( grid%chem_opt > 0 ) then
422            ! Read the chemistry data from a previous wrf forecast (wrfout file)
423            IF(grid%chem_in_opt == 1 ) THEN
424               message = 'INITIALIZING CHEMISTRY WITH OLD SIMULATION'
425               CALL wrf_message ( message )
426 
427               CALL input_ext_chem_file( grid )
428 !             IF(grid%emiss_inpt_opt == 3 ) THEN
429 !                message = 'READING EMISSIONS DATA OPT 3'
430 !                CALL  wrf_message ( message )
431 !                CALL med_read_bin_chem_emissopt3 ( grid , config_flags)
432 !             END IF
433 
434               IF(grid%bio_emiss_opt == 2 ) THEN
435                  message = 'READING BEIS3.11 EMISSIONS DATA'
436                  CALL wrf_message ( message )
437                  CALL med_read_wrf_chem_bioemiss ( grid , config_flags)
438               END IF
439  
440            ELSEIF(grid%chem_in_opt == 0)then
441               ! Generate chemistry data from a idealized vertical profile
442               message = 'STARTING WITH BACKGROUND CHEMISTRY '
443               CALL wrf_message ( message )
444 
445               CALL input_chem_profile ( grid )
446 
447               IF(grid%bio_emiss_opt == 2 ) THEN
448                  message = 'READING BEIS3.11 EMISSIONS DATA'
449                  CALL wrf_message ( message )
450                  CALL med_read_wrf_chem_bioemiss ( grid , config_flags)
451               END IF
452 !             IF(grid%emiss_inpt_opt == 3 ) THEN
453 !                message = 'READING EMISSIONS DATA OPT 3'
454 !                CALL  wrf_message ( message )
455 !                CALL med_read_bin_chem_emissopt3 ( grid , config_flags)
456 !             END IF
457 
458            ELSE
459              message = 'RUNNING WITHOUT CHEMISTRY INITIALIZATION'
460              CALL wrf_message ( message )
461            ENDIF
462          ENDIF
463       ENDIF
464 #endif
465 
466       CALL cpu_time ( t3 )
467       CALL assemble_output ( grid , config_flags , loop , time_loop_max )
468       CALL cpu_time ( t4 )
469       WRITE ( wrf_err_message , FMT='(A,I10,A)' ) 'Timing for output ',NINT(t4-t3) ,' s.'
470       CALL wrf_debug( 0, wrf_err_message )
471       CALL cpu_time ( t2 )
472       WRITE ( wrf_err_message , FMT='(A,I4,A,I10,A)' ) 'Timing for loop # ',loop,' = ',NINT(t2-t1) ,' s.'
473       CALL wrf_debug( 0, wrf_err_message )
474 
475       !  If this is not the last time, we define the next time that we are going to process.
476 
477       IF ( loop .NE. time_loop_max ) THEN
478          CALL geth_newdate ( current_date_char , start_date_char , loop * model_config_rec%interval_seconds )
479          current_date =  current_date_char // '.0000'
480          CALL domain_clockprint ( 150, grid, &
481                 'DEBUG med_sidata_input:  clock before current_date set,' )
482          WRITE (wrf_err_message,*) &
483            'DEBUG med_sidata_input:  before currTime set, current_date = ',TRIM(current_date)
484          CALL wrf_debug ( 150 , wrf_err_message )
485          CALL domain_clock_set( grid, current_date(1:19) )
486          CALL domain_clockprint ( 150, grid, &
487                 'DEBUG med_sidata_input:  clock after current_date set,' )
488       END IF
489       CALL cpu_time ( t1 )
490    END DO
491 
492 END SUBROUTINE med_sidata_input
493 
494 SUBROUTINE compute_si_start_and_end (  &
495    start_year , start_month , start_day , start_hour , start_minute , start_second , &
496      end_year ,   end_month ,   end_day ,   end_hour ,   end_minute ,   end_second , &
497    interval_seconds , real_data_init_type , &
498    start_date_char , end_date_char , time_loop_max )
499 
500    USE module_date_time
501 
502    IMPLICIT NONE
503 
504    INTEGER :: start_year , start_month , start_day , start_hour , start_minute , start_second
505    INTEGER ::   end_year ,   end_month ,   end_day ,   end_hour ,   end_minute ,   end_second
506    INTEGER :: interval_seconds , real_data_init_type
507    INTEGER :: time_loop_max , time_loop
508 
509    CHARACTER(LEN=19) :: current_date_char , start_date_char , end_date_char , next_date_char
510 
511    WRITE ( start_date_char , FMT = '(I4.4,"-",I2.2,"-",I2.2,"_",I2.2,":",I2.2,":",I2.2)' ) &
512            start_year,start_month,start_day,start_hour,start_minute,start_second
513    WRITE (   end_date_char , FMT = '(I4.4,"-",I2.2,"-",I2.2,"_",I2.2,":",I2.2,":",I2.2)' ) &
514              end_year,  end_month,  end_day,  end_hour,  end_minute,  end_second
515 
516    IF ( end_date_char .LT. start_date_char ) THEN
517       CALL wrf_error_fatal( 'Ending date in namelist ' // end_date_char // ' prior to beginning date ' // start_date_char )
518    END IF
519 
520 !  start_date = start_date_char // '.0000'
521 
522    !  Figure out our loop count for the processing times.
523 
524    time_loop = 1
525    PRINT '(A,I4,A,A,A)','Time period #',time_loop,' to process = ',start_date_char,'.'
526    current_date_char = start_date_char
527    loop_count : DO
528       CALL geth_newdate ( next_date_char , current_date_char , interval_seconds )
529       IF      ( next_date_char .LT. end_date_char ) THEN
530          time_loop = time_loop + 1
531          PRINT '(A,I4,A,A,A)','Time period #',time_loop,' to process = ',next_date_char,'.'
532          current_date_char = next_date_char
533       ELSE IF ( next_date_char .EQ. end_date_char ) THEN
534          time_loop = time_loop + 1
535          PRINT '(A,I4,A,A,A)','Time period #',time_loop,' to process = ',next_date_char,'.'
536          PRINT '(A,I4,A)','Total analysis times to input = ',time_loop,'.'
537          time_loop_max = time_loop
538          EXIT loop_count
539       ELSE IF ( next_date_char .GT. end_date_char ) THEN
540          PRINT '(A,I4,A)','Total analysis times to input = ',time_loop,'.'
541          time_loop_max = time_loop
542          EXIT loop_count
543       END IF
544    END DO loop_count
545 END SUBROUTINE compute_si_start_and_end
546 
547 SUBROUTINE assemble_output ( grid , config_flags , loop , time_loop_max )
548 
549    USE module_big_step_utilities_em
550    USE module_domain
551    USE module_io_domain
552    USE module_configure
553    USE module_date_time
554    USE module_bc
555    IMPLICIT NONE
556 
557    TYPE(domain)                 :: grid
558    TYPE (grid_config_rec_type)  :: config_flags
559    INTEGER , INTENT(IN)         :: loop , time_loop_max
560 
561    INTEGER :: ids , ide , jds , jde , kds , kde
562    INTEGER :: ims , ime , jms , jme , kms , kme
563    INTEGER :: ips , ipe , jps , jpe , kps , kpe
564    INTEGER :: ijds , ijde , spec_bdy_width
565    INTEGER :: i , j , k , idts
566 
567    INTEGER :: id1 , interval_seconds , ierr, rc, sst_update, grid_fdda
568    INTEGER , SAVE :: id, id2,  id5 
569    CHARACTER (LEN=80) :: inpname , bdyname
570    CHARACTER(LEN= 4) :: loop_char
571 character *19 :: temp19
572 character *24 :: temp24 , temp24b
573 
574    REAL , DIMENSION(:,:,:) , ALLOCATABLE , SAVE :: ubdy3dtemp1 , vbdy3dtemp1 , tbdy3dtemp1 , pbdy3dtemp1 , qbdy3dtemp1
575    REAL , DIMENSION(:,:,:) , ALLOCATABLE , SAVE :: mbdy2dtemp1
576    REAL , DIMENSION(:,:,:) , ALLOCATABLE , SAVE :: ubdy3dtemp2 , vbdy3dtemp2 , tbdy3dtemp2 , pbdy3dtemp2 , qbdy3dtemp2
577    REAL , DIMENSION(:,:,:) , ALLOCATABLE , SAVE :: mbdy2dtemp2
578 real::t1,t2
579 
580    !  Various sizes that we need to be concerned about.
581 
582    ids = grid%sd31
583    ide = grid%ed31
584    kds = grid%sd32
585    kde = grid%ed32
586    jds = grid%sd33
587    jde = grid%ed33
588 
589    ims = grid%sm31
590    ime = grid%em31
591    kms = grid%sm32
592    kme = grid%em32
593    jms = grid%sm33
594    jme = grid%em33
595 
596    ips = grid%sp31
597    ipe = grid%ep31
598    kps = grid%sp32
599    kpe = grid%ep32
600    jps = grid%sp33
601    jpe = grid%ep33
602 
603    ijds = MIN ( ids , jds )
604    ijde = MAX ( ide , jde )
605 
606    !  Boundary width, scalar value.
607 
608    spec_bdy_width = model_config_rec%spec_bdy_width
609    interval_seconds = model_config_rec%interval_seconds
610    sst_update = model_config_rec%sst_update
611    grid_fdda = model_config_rec%grid_fdda(grid%id)
612 
613 
614    IF ( loop .EQ. 1 ) THEN
615 
616       !  This is the space needed to save the current 3d data for use in computing
617       !  the lateral boundary tendencies.
618 
619       IF ( ALLOCATED ( ubdy3dtemp1 ) ) DEALLOCATE ( ubdy3dtemp1 )
620       IF ( ALLOCATED ( vbdy3dtemp1 ) ) DEALLOCATE ( vbdy3dtemp1 )
621       IF ( ALLOCATED ( tbdy3dtemp1 ) ) DEALLOCATE ( tbdy3dtemp1 )
622       IF ( ALLOCATED ( pbdy3dtemp1 ) ) DEALLOCATE ( pbdy3dtemp1 )
623       IF ( ALLOCATED ( qbdy3dtemp1 ) ) DEALLOCATE ( qbdy3dtemp1 )
624       IF ( ALLOCATED ( mbdy2dtemp1 ) ) DEALLOCATE ( mbdy2dtemp1 )
625       IF ( ALLOCATED ( ubdy3dtemp2 ) ) DEALLOCATE ( ubdy3dtemp2 )
626       IF ( ALLOCATED ( vbdy3dtemp2 ) ) DEALLOCATE ( vbdy3dtemp2 )
627       IF ( ALLOCATED ( tbdy3dtemp2 ) ) DEALLOCATE ( tbdy3dtemp2 )
628       IF ( ALLOCATED ( pbdy3dtemp2 ) ) DEALLOCATE ( pbdy3dtemp2 )
629       IF ( ALLOCATED ( qbdy3dtemp2 ) ) DEALLOCATE ( qbdy3dtemp2 )
630       IF ( ALLOCATED ( mbdy2dtemp2 ) ) DEALLOCATE ( mbdy2dtemp2 )
631 
632       ALLOCATE ( ubdy3dtemp1(ims:ime,kms:kme,jms:jme) )
633       ALLOCATE ( vbdy3dtemp1(ims:ime,kms:kme,jms:jme) )
634       ALLOCATE ( tbdy3dtemp1(ims:ime,kms:kme,jms:jme) )
635       ALLOCATE ( pbdy3dtemp1(ims:ime,kms:kme,jms:jme) )
636       ALLOCATE ( qbdy3dtemp1(ims:ime,kms:kme,jms:jme) )
637       ALLOCATE ( mbdy2dtemp1(ims:ime,1:1,    jms:jme) )
638       ALLOCATE ( ubdy3dtemp2(ims:ime,kms:kme,jms:jme) )
639       ALLOCATE ( vbdy3dtemp2(ims:ime,kms:kme,jms:jme) )
640       ALLOCATE ( tbdy3dtemp2(ims:ime,kms:kme,jms:jme) )
641       ALLOCATE ( pbdy3dtemp2(ims:ime,kms:kme,jms:jme) )
642       ALLOCATE ( qbdy3dtemp2(ims:ime,kms:kme,jms:jme) )
643       ALLOCATE ( mbdy2dtemp2(ims:ime,1:1,    jms:jme) )
644 
645       !  Open the wrfinput file.  From this program, this is an *output* file.
646 
647       CALL construct_filename1( inpname , 'wrfinput' , grid%id , 2 )
648       CALL open_w_dataset ( id1, TRIM(inpname) , grid , config_flags , output_model_input , "DATASET=INPUT", ierr )
649       IF ( ierr .NE. 0 ) THEN
650          CALL wrf_error_fatal( 'real: error opening wrfinput for writing' )
651       ENDIF
652       IF(sst_update .EQ. 1)THEN
653         CALL construct_filename1( inpname , 'wrflowinp' , grid%id , 2 )
654         CALL open_w_dataset ( id5, TRIM(inpname) , grid , config_flags , output_aux_model_input5 , "DATASET=AUXINPUT5", ierr )
655         IF ( ierr .NE. 0 ) THEN
656            CALL wrf_error_fatal( 'real: error opening wrflowinp for writing' )
657         ENDIF
658       ENDIF
659 !     CALL calc_current_date ( grid%id , 0. )
660       CALL output_model_input ( id1, grid , config_flags , ierr )
661       CALL close_dataset ( id1 , config_flags , "DATASET=INPUT" )
662       IF(sst_update .EQ. 1)THEN
663         CALL output_aux_model_input5 ( id5, grid , config_flags , ierr )
664       ENDIF
665 
666       !  We need to save the 3d data to compute a difference during the next loop.  Couple the
667       !  3d fields with total mu (mub + mu_2) and the stagger-specific map scale factor.
668 
669       CALL couple ( grid%em_mu_2 , grid%em_mub , ubdy3dtemp1 , grid%em_u_2                 , 'u' , grid%msfu , &
670                     ids, ide, jds, jde, kds, kde, ims, ime, jms, jme, kms, kme, ips, ipe, jps, jpe, kps, kpe )
671       CALL couple ( grid%em_mu_2 , grid%em_mub , vbdy3dtemp1 , grid%em_v_2                 , 'v' , grid%msfv , &
672                     ids, ide, jds, jde, kds, kde, ims, ime, jms, jme, kms, kme, ips, ipe, jps, jpe, kps, kpe )
673       CALL couple ( grid%em_mu_2 , grid%em_mub , tbdy3dtemp1 , grid%em_t_2                 , 't' , grid%msft , &
674                     ids, ide, jds, jde, kds, kde, ims, ime, jms, jme, kms, kme, ips, ipe, jps, jpe, kps, kpe )
675       CALL couple ( grid%em_mu_2 , grid%em_mub , pbdy3dtemp1 , grid%em_ph_2                , 'h' , grid%msft , &
676                     ids, ide, jds, jde, kds, kde, ims, ime, jms, jme, kms, kme, ips, ipe, jps, jpe, kps, kpe )
677       CALL couple ( grid%em_mu_2 , grid%em_mub , qbdy3dtemp1 , grid%moist(:,:,:,P_QV) , 't' , grid%msft , &
678                     ids, ide, jds, jde, kds, kde, ims, ime, jms, jme, kms, kme, ips, ipe, jps, jpe, kps, kpe )
679 
680       DO j = jps , MIN(jde-1,jpe)
681          DO i = ips , MIN(ide-1,ipe)
682             mbdy2dtemp1(i,1,j) = grid%em_mu_2(i,j)
683          END DO
684       END DO
685 
686       IF(grid_fdda .EQ. 1)THEN
687 ! for fdda
688          DO j = jps , jpe
689             DO k = kps , kpe
690                DO i = ips , ipe
691                   grid%fdda3d(i,k,j,p_u_ndg_old) = grid%em_u_2(i,k,j)
692                   grid%fdda3d(i,k,j,p_v_ndg_old) = grid%em_v_2(i,k,j)
693                   grid%fdda3d(i,k,j,p_t_ndg_old) = grid%em_t_2(i,k,j)
694                   grid%fdda3d(i,k,j,p_q_ndg_old) = grid%moist(i,k,j,P_QV)
695                   grid%fdda3d(i,k,j,p_ph_ndg_old) = grid%em_ph_2(i,k,j)
696                END DO
697             END DO
698          END DO
699 
700          DO j = jps , jpe
701             DO i = ips , ipe
702                grid%fdda2d(i,1,j,p_mu_ndg_old) = grid%em_mu_2(i,j)
703             END DO
704          END DO
705       ENDIF
706 
707 
708       !  There are 2 components to the lateral boundaries.  First, there is the starting
709       !  point of this time period - just the outer few rows and columns.
710 
711       CALL stuff_bdy     ( ubdy3dtemp1 , grid%em_u_bxs, grid%em_u_bxe, grid%em_u_bys, grid%em_u_bye, &
712                                                            'U' , spec_bdy_width      , &
713                                                                  ids , ide , jds , jde , kds , kde , &
714                                                                  ims , ime , jms , jme , kms , kme , &
715                                                                  ips , ipe , jps , jpe , kps , kpe )
716       CALL stuff_bdy     ( vbdy3dtemp1 , grid%em_v_bxs, grid%em_v_bxe, grid%em_v_bys, grid%em_v_bye, &
717                                                            'V' , spec_bdy_width      , &
718                                                                  ids , ide , jds , jde , kds , kde , &
719                                                                  ims , ime , jms , jme , kms , kme , &
720                                                                  ips , ipe , jps , jpe , kps , kpe )
721       CALL stuff_bdy     ( tbdy3dtemp1 , grid%em_t_bxs, grid%em_t_bxe, grid%em_t_bys, grid%em_t_bye, &
722                                                            'T' , spec_bdy_width      , &
723                                                                  ids , ide , jds , jde , kds , kde , &
724                                                                  ims , ime , jms , jme , kms , kme , &
725                                                                  ips , ipe , jps , jpe , kps , kpe )
726       CALL stuff_bdy     ( pbdy3dtemp1 , grid%em_ph_bxs, grid%em_ph_bxe, grid%em_ph_bys, grid%em_ph_bye, &
727                                                            'W' , spec_bdy_width      , &
728                                                                  ids , ide , jds , jde , kds , kde , &
729                                                                  ims , ime , jms , jme , kms , kme , &
730                                                                  ips , ipe , jps , jpe , kps , kpe )
731       CALL stuff_bdy     ( qbdy3dtemp1 , grid%moist_bxs(:,:,:,P_QV), grid%moist_bxe(:,:,:,P_QV),     &
732                                          grid%moist_bys(:,:,:,P_QV), grid%moist_bye(:,:,:,P_QV),     &
733                                                            'T' , spec_bdy_width      ,               &
734                                                                  ids , ide , jds , jde , kds , kde , &
735                                                                  ims , ime , jms , jme , kms , kme , &
736                                                                  ips , ipe , jps , jpe , kps , kpe )
737       CALL stuff_bdy     ( mbdy2dtemp1 , grid%em_mu_bxs, grid%em_mu_bxe, grid%em_mu_bys, grid%em_mu_bye, &
738                                                            'M' , spec_bdy_width      , &
739                                                                  ids , ide , jds , jde , 1 , 1 , &
740                                                                  ims , ime , jms , jme , 1 , 1 , &
741                                                                  ips , ipe , jps , jpe , 1 , 1 )
742 
743 
744    ELSE IF ( loop .GT. 1 ) THEN
745 
746       IF(sst_update .EQ. 1)THEN
747         CALL output_aux_model_input5 ( id5, grid , config_flags , ierr )
748       ENDIF
749 
750       !  Open the boundary file.
751 
752 
753       IF ( loop .eq. 2 ) THEN
754        IF(grid%id .eq. 1)THEN
755          CALL construct_filename1( bdyname , 'wrfbdy' , grid%id , 2 )
756          CALL open_w_dataset ( id, TRIM(bdyname) , grid , config_flags , output_boundary , "DATASET=BOUNDARY", ierr )
757          IF ( ierr .NE. 0 ) THEN
758                CALL wrf_error_fatal( 'real: error opening wrfbdy for writing' )
759          ENDIF
760        ENDIF
761        IF(grid_fdda .EQ. 1)THEN
762 ! for fdda
763          CALL construct_filename1( inpname , 'wrffdda' , grid%id , 2 )
764          CALL open_w_dataset ( id2, TRIM(inpname) , grid , config_flags , output_aux_model_input10 , "DATASET=AUXINPUT10", ierr )
765          IF ( ierr .NE. 0 ) THEN
766                CALL wrf_error_fatal( 'real: error opening wrffdda for writing' )
767          ENDIF
768        ENDIF
769       ELSE
770          IF ( .NOT. domain_clockisstoptime(grid) ) THEN
771             CALL domain_clockadvance( grid )
772             CALL domain_clockprint ( 150, grid, &
773                    'DEBUG assemble_output:  clock after ClockAdvance,' )
774          ENDIF
775       END IF
776 
777 
778       !  Couple this time period's data with total mu, and save it in the *bdy3dtemp2 arrays.
779 
780       CALL couple ( grid%em_mu_2 , grid%em_mub , ubdy3dtemp2 , grid%em_u_2                 , 'u' , grid%msfu , &
781                     ids, ide, jds, jde, kds, kde, ims, ime, jms, jme, kms, kme, ips, ipe, jps, jpe, kps, kpe )
782       CALL couple ( grid%em_mu_2 , grid%em_mub , vbdy3dtemp2 , grid%em_v_2                 , 'v' , grid%msfv , &
783                     ids, ide, jds, jde, kds, kde, ims, ime, jms, jme, kms, kme, ips, ipe, jps, jpe, kps, kpe )
784       CALL couple ( grid%em_mu_2 , grid%em_mub , tbdy3dtemp2 , grid%em_t_2                 , 't' , grid%msft , &
785                     ids, ide, jds, jde, kds, kde, ims, ime, jms, jme, kms, kme, ips, ipe, jps, jpe, kps, kpe )
786       CALL couple ( grid%em_mu_2 , grid%em_mub , pbdy3dtemp2 , grid%em_ph_2                , 'h' , grid%msft , &
787                     ids, ide, jds, jde, kds, kde, ims, ime, jms, jme, kms, kme, ips, ipe, jps, jpe, kps, kpe )
788       CALL couple ( grid%em_mu_2 , grid%em_mub , qbdy3dtemp2 , grid%moist(:,:,:,P_QV) , 't' , grid%msft , &
789                     ids, ide, jds, jde, kds, kde, ims, ime, jms, jme, kms, kme, ips, ipe, jps, jpe, kps, kpe )
790 
791       DO j = jps , jpe
792          DO i = ips , ipe
793             mbdy2dtemp2(i,1,j) = grid%em_mu_2(i,j)
794          END DO
795       END DO
796 
797       IF(grid_fdda .EQ. 1)THEN
798 ! for fdda
799          DO j = jps , jpe
800             DO k = kps , kpe
801                DO i = ips , ipe
802                   grid%fdda3d(i,k,j,p_u_ndg_new) = grid%em_u_2(i,k,j)
803                   grid%fdda3d(i,k,j,p_v_ndg_new) = grid%em_v_2(i,k,j)
804                   grid%fdda3d(i,k,j,p_t_ndg_new) = grid%em_t_2(i,k,j)
805                   grid%fdda3d(i,k,j,p_q_ndg_new) = grid%moist(i,k,j,P_QV)
806                   grid%fdda3d(i,k,j,p_ph_ndg_new) = grid%em_ph_2(i,k,j)
807                END DO
808             END DO
809          END DO
810 
811          DO j = jps , jpe
812             DO i = ips , ipe
813                grid%fdda2d(i,1,j,p_mu_ndg_new) = grid%em_mu_2(i,j)
814             END DO
815          END DO
816       ENDIF
817 
818       !  During all of the loops after the first loop, we first compute the boundary
819       !  tendencies with the current data values (*bdy3dtemp2 arrays) and the previously 
820       !  saved information stored in the *bdy3dtemp1 arrays.
821 
822       CALL stuff_bdytend ( ubdy3dtemp2 , ubdy3dtemp1 , REAL(interval_seconds) ,                 &
823                                                             grid%em_u_btxs, grid%em_u_btxe,     &
824                                                             grid%em_u_btys, grid%em_u_btye,     &
825                                                             'U' , &
826                                                             spec_bdy_width      , &
827                                                             ids , ide , jds , jde , kds , kde , &
828                                                             ims , ime , jms , jme , kms , kme , &
829                                                             ips , ipe , jps , jpe , kps , kpe )
830       CALL stuff_bdytend ( vbdy3dtemp2 , vbdy3dtemp1 , REAL(interval_seconds) ,                 &
831                                                             grid%em_v_btxs, grid%em_v_btxe,     &
832                                                             grid%em_v_btys, grid%em_v_btye,     &
833                                                             'V' , &
834                                                             spec_bdy_width      , &
835                                                             ids , ide , jds , jde , kds , kde , &
836                                                             ims , ime , jms , jme , kms , kme , &
837                                                             ips , ipe , jps , jpe , kps , kpe )
838       CALL stuff_bdytend ( tbdy3dtemp2 , tbdy3dtemp1 , REAL(interval_seconds) ,                 &
839                                                             grid%em_t_btxs, grid%em_t_btxe,     &
840                                                             grid%em_t_btys, grid%em_t_btye,     &
841                                                             'T' , &
842                                                             spec_bdy_width      , &
843                                                             ids , ide , jds , jde , kds , kde , &
844                                                             ims , ime , jms , jme , kms , kme , &
845                                                             ips , ipe , jps , jpe , kps , kpe )
846       CALL stuff_bdytend ( pbdy3dtemp2 , pbdy3dtemp1 , REAL(interval_seconds) ,                 &
847                                                             grid%em_ph_btxs, grid%em_ph_btxe,   &
848                                                             grid%em_ph_btys, grid%em_ph_btye,   &
849                                                             'W' , &
850                                                             spec_bdy_width      , &
851                                                             ids , ide , jds , jde , kds , kde , &
852                                                             ims , ime , jms , jme , kms , kme , &
853                                                             ips , ipe , jps , jpe , kps , kpe )
854       CALL stuff_bdytend ( qbdy3dtemp2 , qbdy3dtemp1 , REAL(interval_seconds) ,                 &
855                                                             grid%moist_btxs(:,:,:,P_QV), grid%moist_btxe(:,:,:,P_QV), &
856                                                             grid%moist_btys(:,:,:,P_QV), grid%moist_btye(:,:,:,P_QV), &
857                                                             'T' , &
858                                                             spec_bdy_width      , &
859                                                             ids , ide , jds , jde , kds , kde , &
860                                                             ims , ime , jms , jme , kms , kme , &
861                                                             ips , ipe , jps , jpe , kps , kpe )
862       CALL stuff_bdytend ( mbdy2dtemp2 , mbdy2dtemp1 , REAL(interval_seconds) ,                 &
863                                                             grid%em_mu_btxs, grid%em_mu_btxe,   &
864                                                             grid%em_mu_btys, grid%em_mu_btye,   &
865                                                             'M' , &
866                                                             spec_bdy_width      , &
867                                                             ids , ide , jds , jde , 1 , 1 , &
868                                                             ims , ime , jms , jme , 1 , 1 , &
869                                                             ips , ipe , jps , jpe , 1 , 1 )
870 
871       !  Both pieces of the boundary data are now available to be written (initial time and tendency).
872       !  This looks ugly, these date shifting things.  What's it for?  We want the "Times" variable
873       !  in the lateral BDY file to have the valid times of when the initial fields are written.
874       !  That's what the loop-2 thingy is for with the start date.  We increment the start_date so
875       !  that the starting time in the attributes is the second time period.  Why you may ask.  I
876       !  agree, why indeed.
877 
878       CALL domain_clockprint ( 150, grid, &
879              'DEBUG assemble_output:  clock before 1st current_date set,' )
880       WRITE (wrf_err_message,*) &
881         'DEBUG assemble_output:  before 1st currTime set, current_date = ',TRIM(current_date)
882       CALL wrf_debug ( 150 , wrf_err_message )
883       CALL domain_clock_set( grid, current_date(1:19) )
884       CALL domain_clockprint ( 150, grid, &
885              'DEBUG assemble_output:  clock after 1st current_date set,' )
886 
887       temp24= current_date
888       temp24b=start_date
889       start_date = current_date
890       CALL geth_newdate ( temp19 , temp24b(1:19) , (loop-2) * model_config_rec%interval_seconds )
891       current_date = temp19 //  '.0000'
892       CALL domain_clockprint ( 150, grid, &
893              'DEBUG assemble_output:  clock before 2nd current_date set,' )
894       WRITE (wrf_err_message,*) &
895         'DEBUG assemble_output:  before 2nd currTime set, current_date = ',TRIM(current_date)
896       CALL wrf_debug ( 150 , wrf_err_message )
897       CALL domain_clock_set( grid, current_date(1:19) )
898       CALL domain_clockprint ( 150, grid, &
899              'DEBUG assemble_output:  clock after 2nd current_date set,' )
900       IF(grid%id .EQ. 1)THEN
901         print *,'LBC valid between these times ',current_date, ' ',start_date
902         CALL output_boundary ( id, grid , config_flags , ierr )
903       ENDIF
904 ! for fdda
905       IF(grid_fdda .EQ. 1) THEN
906          CALL output_aux_model_input10 ( id2, grid , config_flags , ierr )
907       END IF
908       current_date = temp24
909       start_date = temp24b
910       CALL domain_clockprint ( 150, grid, &
911              'DEBUG assemble_output:  clock before 3rd current_date set,' )
912       WRITE (wrf_err_message,*) &
913         'DEBUG assemble_output:  before 3rd currTime set, current_date = ',TRIM(current_date)
914       CALL wrf_debug ( 150 , wrf_err_message )
915       CALL domain_clock_set( grid, current_date(1:19) )
916       CALL domain_clockprint ( 150, grid, &
917              'DEBUG assemble_output:  clock after 3rd current_date set,' )
918 
919       !  OK, for all of the loops, we output the initialzation data, which would allow us to
920       !  start the model at any of the available analysis time periods.
921 
922 !     WRITE ( loop_char , FMT = '(I4.4)' ) loop
923 !     CALL open_w_dataset ( id1, 'wrfinput'//loop_char , grid , config_flags , output_model_input , "DATASET=INPUT", ierr )
924 !     IF ( ierr .NE. 0 ) THEN
925 !       CALL wrf_error_fatal( 'real: error opening wrfinput'//loop_char//' for writing' )
926 !     ENDIF
927 
928 !     CALL calc_current_date ( grid%id , 0. )
929 !     CALL output_model_input ( id1, grid , config_flags , ierr )
930 !     CALL close_dataset ( id1 , config_flags , "DATASET=INPUT" )
931       !  Is this or is this not the last time time?  We can remove some unnecessary
932       !  stores if it is not.
933       IF     ( loop .LT. time_loop_max ) THEN
934 
935          !  We need to save the 3d data to compute a difference during the next loop.  Couple the
936          !  3d fields with total mu (mub + mu_2) and the stagger-specific map scale factor.
937          !  We load up the boundary data again for use in the next loop.
938 
939          DO j = jps , jpe
940             DO k = kps , kpe
941                DO i = ips , ipe
942                   ubdy3dtemp1(i,k,j) = ubdy3dtemp2(i,k,j)
943                   vbdy3dtemp1(i,k,j) = vbdy3dtemp2(i,k,j)
944                   tbdy3dtemp1(i,k,j) = tbdy3dtemp2(i,k,j)
945                   pbdy3dtemp1(i,k,j) = pbdy3dtemp2(i,k,j)
946                   qbdy3dtemp1(i,k,j) = qbdy3dtemp2(i,k,j)
947                END DO
948             END DO
949          END DO
950 
951          DO j = jps , jpe
952             DO i = ips , ipe
953                mbdy2dtemp1(i,1,j) = mbdy2dtemp2(i,1,j)
954             END DO
955          END DO
956 
957       IF(grid_fdda .EQ. 1)THEN
958 ! for fdda
959          DO j = jps , jpe
960             DO k = kps , kpe
961                DO i = ips , ipe
962                   grid%fdda3d(i,k,j,p_u_ndg_old) = grid%fdda3d(i,k,j,p_u_ndg_new)
963                   grid%fdda3d(i,k,j,p_v_ndg_old) = grid%fdda3d(i,k,j,p_v_ndg_new)
964                   grid%fdda3d(i,k,j,p_t_ndg_old) = grid%fdda3d(i,k,j,p_t_ndg_new)
965                   grid%fdda3d(i,k,j,p_q_ndg_old) = grid%fdda3d(i,k,j,p_q_ndg_new)
966                   grid%fdda3d(i,k,j,p_ph_ndg_old) = grid%fdda3d(i,k,j,p_ph_ndg_new)
967                END DO
968             END DO
969          END DO
970 
971          DO j = jps , jpe
972             DO i = ips , ipe
973                grid%fdda2d(i,1,j,p_mu_ndg_old) = grid%fdda2d(i,1,j,p_mu_ndg_new)
974             END DO
975          END DO
976       ENDIF
977 
978          !  There are 2 components to the lateral boundaries.  First, there is the starting
979          !  point of this time period - just the outer few rows and columns.
980 
981          CALL stuff_bdy     ( ubdy3dtemp1 , grid%em_u_bxs, grid%em_u_bxe, grid%em_u_bys, grid%em_u_bye, &
982                                                               'U' , spec_bdy_width      , &
983                                                                     ids , ide , jds , jde , kds , kde , &
984                                                                     ims , ime , jms , jme , kms , kme , &
985                                                                     ips , ipe , jps , jpe , kps , kpe )
986          CALL stuff_bdy     ( vbdy3dtemp1 , grid%em_v_bxs, grid%em_v_bxe, grid%em_v_bys, grid%em_v_bye, &
987                                                               'V' , spec_bdy_width      , &
988                                                                     ids , ide , jds , jde , kds , kde , &
989                                                                     ims , ime , jms , jme , kms , kme , &
990                                                                     ips , ipe , jps , jpe , kps , kpe )
991          CALL stuff_bdy     ( tbdy3dtemp1 , grid%em_t_bxs, grid%em_t_bxe, grid%em_t_bys, grid%em_t_bye, &
992                                                               'T' , spec_bdy_width      , &
993                                                                     ids , ide , jds , jde , kds , kde , &
994                                                                     ims , ime , jms , jme , kms , kme , &
995                                                                     ips , ipe , jps , jpe , kps , kpe )
996          CALL stuff_bdy     ( pbdy3dtemp1 , grid%em_ph_bxs, grid%em_ph_bxe, grid%em_ph_bys, grid%em_ph_bye, &
997                                                               'W' , spec_bdy_width      , &
998                                                                     ids , ide , jds , jde , kds , kde , &
999                                                                     ims , ime , jms , jme , kms , kme , &
1000                                                                     ips , ipe , jps , jpe , kps , kpe )
1001          CALL stuff_bdy     ( qbdy3dtemp1 , grid%moist_bxs(:,:,:,P_QV), grid%moist_bxe(:,:,:,P_QV),     &
1002                                             grid%moist_bys(:,:,:,P_QV), grid%moist_bye(:,:,:,P_QV),     &
1003                                                               'T' , spec_bdy_width      ,               &
1004                                                                     ids , ide , jds , jde , kds , kde , &
1005                                                                     ims , ime , jms , jme , kms , kme , &
1006                                                                     ips , ipe , jps , jpe , kps , kpe )
1007          CALL stuff_bdy     ( mbdy2dtemp1 , grid%em_mu_bxs, grid%em_mu_bxe, grid%em_mu_bys, grid%em_mu_bye, &
1008                                                               'M' , spec_bdy_width      , &
1009                                                                     ids , ide , jds , jde , 1 , 1 , &
1010                                                                     ims , ime , jms , jme , 1 , 1 , &
1011                                                                     ips , ipe , jps , jpe , 1 , 1 )
1012 
1013       ELSE IF ( loop .EQ. time_loop_max ) THEN
1014 
1015          !  If this is the last time through here, we need to close the files.
1016 
1017          IF(grid%id .EQ. 1)CALL close_dataset ( id , config_flags , "DATASET=BOUNDARY" )
1018          IF(grid_fdda .EQ. 1)CALL close_dataset ( id2 , config_flags , "DATASET=AUXINPUT10" )
1019         IF(sst_update .EQ. 1)THEN
1020          CALL close_dataset ( id5 , config_flags , "DATASET=AUXINPUT5" )
1021         ENDIF
1022 
1023       END IF
1024 
1025    END IF
1026 
1027 END SUBROUTINE assemble_output