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