File: C:\NOAA\NEMS_11731\src\atmos\gfs\phys\gfs_physics_grid_comp_mod.f

1     !! !module: gfs_physics_grid_comp_mod --- 
2     !                       esmf gridded component of gfs physics
3     !
4     ! !description: gfs physics gridded component main module.
5     !
6     ! !revision history:
7     !
8     !  july     2007     shrinivas moorthi
9     !  november 2007     hann-ming henry juang 
10     !  may      2009     jun wang, add write grid component
11     !  october  2009     jun wang, output every time step, add no quilting option 
12     !  oct 09   2009     sarah lu, 3D Gaussian grid (DistGrid5) added
13     !  oct 12   2009     sarah lu, set the association between imp/exp states
14     !                    and internal state grid_fld; reset start_step
15     !  oct 17 2009      Sarah Lu, add debug print to check imp/exp state
16     !  dec 10 2009      Sarah Lu, add debug print to chekc fcld
17     !  dec 15 2009      Sarah Lu, add debug print to chekc 3d diag fld (fcld, dqdt)
18     !  Feb 05 2010      Jun Wang, set init time for restart
19     !  Mar 02 2010      Sarah Lu, associate export state with internal state in init
20     !  Apr 11 2010      Sarah Lu, debug print removed
21     !  Aug 25 2010      Jun Wang, output half dfi filted fields
22     !  Oct 16 2010      Sarah Lu, retrieve fscav from exp state
23     !  Dec 23 2010      Sarah Lu, modify fscav initialization 
24     !                           
25     !
26     ! !interface:
27     !
28           module gfs_physics_grid_comp_mod
29      
30     !!uses:
31     !------
32           use esmf_mod
33     
34           use gfs_physics_err_msg_mod,        ONLY: gfs_physics_err_msg,        &
35                                                     gfs_physics_err_msg_final
36           use gfs_physics_initialize_mod,     ONLY: gfs_physics_initialize
37           use gfs_physics_run_mod,            ONLY: gfs_physics_run
38           use gfs_physics_finalize_mod,       ONLY: gfs_physics_finalize
39           USE gfs_physics_getcf_mod,          ONLY: gfs_physics_getcf
40           USE gfs_physics_internal_state_mod, ONLY: gfs_physics_internal_state, &
41                                                     gfs_phy_wrap
42           USE mpi_def,                        ONLY: mpi_comm_all,quilting
43           USE layout1,                        ONLY: me
44           USE date_def,                       ONLY: idate, fhour
45           USE namelist_physics_def,           ONLY: fhini, fhmax, lssav,ndfi,ldfi
46     !jw
47           USE gfs_physics_output,             ONLY: point_physics_output_gfs
48     !
49           use GFS_Phy_States_Mod,             ONLY: gfs_physics_import2internal, &  
50                                                     gfs_physics_internal2export  
51     !
52           implicit none
53     
54           private   ! by default, data is private to this module
55     
56           public gfs_phy_setservices	! only set service is public
57     
58     !eop
59     !-------------------------------------------------------------------------
60     
61     
62           contains
63     
64     
65     !----------------------------------------------------------------------
66     !bop
67     !
68     ! !routine: gfs_phy_setservices --- 
69     !           set services for gfs physics gridded component.
70     ! 
71     ! !interface:
72     !
73           subroutine gfs_phy_setservices (gc_gfs_phy, rc)
74      
75     ! !arguments:
76     !------------
77     
78           type(esmf_gridcomp), intent(in)  :: gc_gfs_phy 	! gridded component
79           integer,             intent(out) :: rc    	! return code
80          
81     ! !description: set services (register) for the gfs physics grid component.
82     !         
83     !eop         
84     !----------------------------------------------------------------------
85       
86           integer                            :: rc1     = esmf_success
87     
88     ! initializing the error signal variable rc.
89     !-------------------------------------------
90           rc = esmf_success
91     
92     ! register services for this component
93     ! ------------------------------------
94     
95     ! register the initialize subroutine.  since it is just one subroutine
96     ! for the initialize, use esmf_singlephase.  the second argument is
97     ! a pre-defined subroutine type, such as esmf_setinit, esmf_setrun, 
98     ! esmf_setfinal.
99     !---------------------------------------------------------------------
100           call esmf_logwrite("set entry point for initialize",              &
101                               esmf_log_info, rc = rc1)
102           call esmf_gridcompsetentrypoint (gc_gfs_phy, 			&
103                                            esmf_setinit,  			&
104                                            gfs_phy_initialize,    		&
105                                            esmf_singlephase, rc1)
106           call gfs_physics_err_msg(rc1,'set entry point for initialize',rc)
107     
108     ! register the run subroutine.
109     !-----------------------------
110           call esmf_logwrite("set entry point for run",              	&
111                                esmf_log_info, rc = rc1)
112           call esmf_gridcompsetentrypoint (gc_gfs_phy, 			&
113                                            esmf_setrun,   			&
114                                            gfs_phy_run,           		&
115                                            esmf_singlephase, rc1)
116           call gfs_physics_err_msg(rc1,'set entry point for run',rc)
117     
118     
119     ! register the finalize subroutine.
120     !----------------------------------
121           call esmf_logwrite("set entry point for finalize",                &
122                             esmf_log_info, rc = rc1)
123           call esmf_gridcompsetentrypoint (gc_gfs_phy, 			&
124                                            esmf_setfinal, 			&
125                                            gfs_phy_finalize,       		&
126                                            esmf_singlephase, rc1)
127           call gfs_physics_err_msg(rc1,'set entry point for finalize',rc)
128     
129     ! check the error signal variable and print out the result.
130     !----------------------------------------------------------
131           call gfs_physics_err_msg_final(rc1,				&
132                             'setservice for gfs physics grid comp.',rc)
133     
134           end subroutine gfs_phy_setservices
135     
136     
137     
138     
139     
140     !----------------------------------------------------------------------
141     !bop
142     ! !routine:  gfs_phy_initialize --- initialize routine to initialize 
143     !                                   and set up the gfs running job.
144     !
145     ! !description: this subroutine initializes the gfs running before
146     !               the main running loop.
147     !
148     !
149     ! !revision history:
150     !
151     !  november 2004     weiyu yang initial code.
152     !  may      2005     weiyu yang for the updated gfs version.
153     !  february 2006     moorthi
154     !  february 2007     h.-m. h. juang
155     !  may      2009     j. wang
156     !
157     ! !interface:
158     !
159     
160     ! this argument list is a standard list for all the initialize,
161     ! the run and finalize routines for an esmf system.
162     !--------------------------------------------------------------
163           subroutine gfs_phy_initialize(gc_gfs_phy, 			&
164                                        imp_gfs_phy, exp_gfs_phy, clock, rc)
165     
166     ! user code, for computations related to the esmf interface states.
167     !------------------------------------------------------------------
168     !*    use gfs_physics_states_mod
169           use gfs_physics_grid_create_mod
170     !
171     ! !input/output variables and parameters:
172     !----------------------------------------
173     
174           type(esmf_gridcomp), intent(inout) :: gc_gfs_phy 
175           type(esmf_state),    intent(inout) :: imp_gfs_phy
176           type(esmf_state),    intent(inout) :: exp_gfs_phy
177           type(esmf_clock),    intent(inout) :: clock
178     
179     !
180     ! !output variables and parameters:
181     !----------------------------------
182     
183           integer, intent(out) :: rc  
184     
185     ! !eop
186     !------------------------------------------------------------------------- 
187      
188     ! !working arrays and local parameters.  
189     !--------------------------------------
190           type(gfs_phy_wrap)                :: wrap         
191     ! this wrap is a derived type which contains
192     ! only a pointer to the internal state.  it is needed
193     ! for using different architectures or compliers.
194           type(gfs_physics_internal_state), pointer  :: int_state    
195           type(esmf_vm)                      :: vm_local     
196     !jw
197           type(esmf_state)                   :: imp_wrt_state
198           type(esmf_timeinterval)            :: timestep     
199           type(esmf_timeinterval)            :: runduration  
200           type(esmf_time)                    :: starttime    
201           type(esmf_time)                    :: stoptime    
202           type(esmf_time)                    :: currtime     
203           type(esmf_timeinterval)            :: reftimeinterval 
204           type(esmf_delayout)                :: mydelayout   
205           integer(kind=esmf_kind_i4)         :: yy, mm, dd   ! time variables for date
206           integer(kind=esmf_kind_i4)         :: hh, mns, sec ! time variables for time
207           integer                            :: advancecount4, timestep_sec
208           integer                            :: atm_timestep_s, phy_timestep_s
209           integer(esmf_kind_i8)              :: advancecount
210     
211           TYPE(ESMF_DistGrid)                :: DistGrid5    ! the ESMF DistGrid.
212     
213           integer                            :: rc1 
214           integer                            :: rcfinal, grib_inp
215           integer                            :: ifhmax
216           integer                            :: runduration_hour 
217     
218           type(ESMF_FieldBundle)        :: Bundle     ! debug check
219     
220     ! initialize the error signal variables.
221     !---------------------------------------
222           rc1     = esmf_success
223           rcfinal = esmf_success
224     
225     ! allocate the internal state pointer.
226     !-------------------------------------
227           call esmf_logwrite("allocate the internal state",                 &
228                              esmf_log_info, rc = rc1)
229     
230           allocate(int_state, stat = rc1)
231     
232           call gfs_physics_err_msg(rc1,' - allocate the internal state',rc)
233     
234           wrap%int_state => int_state
235     
236     ! attach internal state to the gfs physics grid component.
237     !-------------------------------------------------
238           call esmf_logwrite("set up the internal state",                   &
239                             esmf_log_info, rc = rc1)
240     
241           call esmf_gridcompsetinternalstate(gc_gfs_phy, wrap, rc1)
242     
243           call gfs_physics_err_msg(rc1,'set up the internal state',rc)
244     
245     ! use esmf utilities to get information from the configuration file.
246     ! the function is similar to reading the namelist in the original gfs.
247     !---------------------------------------------------------------------
248           call esmf_logwrite("getting information from the configure file", &
249                             esmf_log_info, rc = rc1)
250     
251           call gfs_physics_getcf(gc_gfs_phy, int_state,  rc = rc1)
252     
253           call gfs_physics_err_msg(rc1,'get configure file information',rc)
254     !jws
255     !-----------------------------------------------------------------------
256     !***  retrieve the import state of the write gridded component
257     !***  from the physics export state.
258     !-----------------------------------------------------------------------
259           call esmf_logwrite("Retrieve Write Import State from Physics Export State", &
260                             esmf_log_info, rc = rc1)
261      
262           CALL ESMF_StateGet(state      =exp_gfs_phy                        &  !<-- The Physics export state
263                             ,itemName   ='Write Import State'               &  !<-- Name of the state to get from Physics export state
264                             ,nestedState=imp_wrt_state                      &  !<-- Extract Write component import state from Physics export
265                             ,rc         =RC1)
266     !
267           CALL gfs_physics_err_msg(rc1,"Retrieve Write Import State from Physics Export State",RC)
268     !jwe
269     !
270     ! initialize time interval to the parameter from the configure file.
271     !-------------------------------------------------------------------
272     !     call esmf_logwrite("set up time step interval",                   &
273     !                       esmf_log_info, rc = rc1)
274     
275     !     call esmf_clockget(clock,            				&
276     !                        timestep    = timestep,                	&
277     !                        rc          = rc1)
278     
279     !     call esmf_timeintervalget(timestep,                               &
280     !                               s  = atm_timestep_s,                    &
281     !                               rc = rc1)
282     
283     !     phy_timestep_s = nint(int_state%nam_gfs_phy%deltim)
284     
285     !     int_state%nam_gfs_phy%deltim = atm_timestep_s /			&
286     !           min( 1, atm_timestep_s / phy_timestep_s  )
287     
288     !     call gfs_physics_err_msg(rc1,'set up time step interval',rc)
289     
290     ! get the start time from reading the surface file.
291     !----------------------------------------------------------
292           call esmf_logwrite("getting the start time",                      &
293                              esmf_log_info, rc = rc1)
294     
295     
296           call gfs_physics_start_time_get(					&
297                             yy, mm, dd, hh, mns, sec, int_state%kfhour,     &
298     !--get init time
299                             fhini,                                          &
300                             int_state%n3, int_state%nam_gfs_phy%sfc_ini,rc1)
301      
302           call gfs_physics_err_msg(rc1,'getting the start time',rc)
303      
304           advancecount4    = nint(real(int_state%kfhour) * 3600.0 /         &
305                                   int_state%nam_gfs_phy%deltim)
306           int_state%phour  = advancecount4 * 				&
307                              int_state%nam_gfs_phy%deltim / 3600.0
308           int_state%kfhour = nint(int_state%phour)
309     
310     ! initialize the clock with the start time based on the information
311     ! from calling starttimeget.
312     !------------------------------------------
313           call esmf_logwrite("set up the esmf time",                        &
314                              esmf_log_info, rc = rc1)
315     
316     ! in dynamics, we had already timeset, this is redo, update later.
317     
318           call esmf_timeset(starttime, yy = yy, mm = mm,  dd = dd,          &
319                                   h  = hh, m  = mns, s  = sec, rc = rc1)
320     
321           call gfs_physics_err_msg(rc1,'set up the esmf time',rc)
322     
323           call esmf_logwrite("set up the reference time interval",          &
324                             esmf_log_info, rc = rc1)
325     
326           call esmf_timeintervalset(reftimeinterval, h = int_state%kfhour,  &
327                                m = 0, rc = rc1)
328     
329     ! re-set up the start time based on the kfhour value in the sigma file.
330     !----------------------------------------------------------------------
331     !starttime = starttime + reftimeinterval
332     
333           call gfs_physics_err_msg(rc1,					&
334                              'set up the reference time interval',rc)
335     
336     ! set the esmf clock which will control the gfs run do loop.
337     !--------------------------------------------------------------
338     
339     ! in dynamics, clock is set, this is redo, update later
340     
341           currtime = starttime + reftimeinterval
342           call esmf_clockset(clock, 					&
343                              currtime = currtime,                      	&
344                              rc = rc1)
345     !
346     ! get the grid component vm.
347     ! this esmf_gridcompget vm can be used at any where you need it.
348     !---------------------------------------------------------------
349           call esmf_logwrite("get the local vm", esmf_log_info, rc = rc1)
350     
351           call esmf_vmgetcurrent(vm_local, rc = rc1)
352     
353           call gfs_physics_err_msg(rc1,'get the vm',rc)
354     
355     
356     ! set up parameters of mpi communications.
357     ! use esmf utility to get pe identification and total number of pes.
358     !-------------------------------------------------------------------
359           call esmf_logwrite("get me and nodes from vm", 			&
360                               esmf_log_info, rc = rc1)
361     
362           call esmf_vmget(vm_local, localpet = int_state%me,    		&
363                                mpicommunicator = mpi_comm_all,        	&
364                                petcount = int_state%nodes,			&
365                                rc       = rc1)
366           me = int_state%me
367     
368           call gfs_physics_err_msg(rc1,'get me and nodes from vm',rc)
369     
370     ! initialize the gfs, including set up the internal state
371     ! variables and some local parameter short names, aloocate
372     ! internal state arrays.
373     !---------------------------------------------------------
374           call esmf_logwrite("run the gfs_physics_initialize", 		&
375                              esmf_log_info, rc = rc1)
376     
377     ! ----------------- gfs physics related initialize --------------------
378     ! ----------------------------------------------------------------------
379     !      write(0,*)'in after init, size fhour_idate=',size(int_state%fhour_idate,1), &
380     !        size(int_state%fhour_idate,2),'idate size=',size(idate)
381           call gfs_physics_initialize(int_state, rc1)
382     ! ----------------------------------------------------------------------
383     !
384           call gfs_physics_err_msg(rc1,'run the gfs_physics_initialize',rc)
385     
386           call esmf_clockget(clock, timestep    = timestep,            	&
387                              runduration = runduration,              	&
388                              starttime   = starttime,                	&
389                              currtime    = currtime,                 	&
390                              rc          = rc1)
391     !
392     !
393           call esmf_timeintervalget(runduration,                            &
394                                     h = runduration_hour, rc = rc1)
395     !
396     !
397     !moor ifhmax = nint(int_state%nam_gfs_phy%fhmax)
398           ifhmax = nint(fhmax)
399           if(runduration_hour <= 0    .or.                  		&
400               ifhmax /= 0             .and.                 		&
401               ifhmax <= int_state%kfhour + runduration_hour) then
402               ifhmax            = nint(fhmax)
403               runduration_hour  = nint(fhmax) - nint(fhini)
404               call esmf_timeintervalset(runduration,                        &
405                                         h = runduration_hour, rc = rc1)
406           end if
407           if (runduration_hour < 0) then
408             print *,' fhini=',fhini, ' > fhmax=',fhmax,' job aborted'
409             call mpi_quit(444)
410           endif
411           stoptime = currtime  + runduration
412                                
413           call esmf_clockset(clock, stoptime = stoptime,               	&
414                              rc       = rc1)
415     !
416           call esmf_timeintervalget(timestep, s = timestep_sec, rc = rc1)
417                                
418           if(me==0)print *,' timestep_sec=',timestep_sec,' rc1=',rc1
419     !!
420           if (me.eq.0) then
421             call out_para(real(timestep_sec))
422           endif
423     !!
424           if (me==0) then
425             print *,' gsm physics will forecast ',runduration_hour,' hours',  &
426                     ' from hour ',int_state%kfhour,' to hour ',               &
427                      runduration_hour+int_state%kfhour
428           endif
429     !
430     !
431           call synchro
432     
433     !
434     ! Create 3D Gaussian grid                                   
435     !-----------------------
436     !
437           call gfs_physics_grid_create_Gauss3D(vm_local,int_state,DistGrid5,rc1) 
438     
439           call gfs_physics_err_msg(rc1,'gfs_physics_grid_create_gauss',rc)     
440     
441     !
442     ! Define Physics Import and Export states   
443     !
444           if ( .not. int_state%grid_aldata ) then
445             call gfs_physics_import2internal( imp_gfs_phy,    &         
446                                               int_state, rc = rc1)      
447             call gfs_physics_err_msg(rc1,'PHY INIT-call import2internal',rc)
448           endif
449     
450           call gfs_physics_internal2export ( int_state,        &
451                                              exp_gfs_phy, rc = rc1)
452           call gfs_physics_err_msg(rc1,'PHY INIT-call internal2export',rc)
453     
454     !! debug check
455     !     call ESMF_StatePrint( exp_gfs_phy, rc=rc)
456     
457     !
458     
459     ! set pointer the gfs export fields in the internal state 
460     ! to the esmf exprot state which is the public interface
461     ! for other esmf grid components.
462     !-------------------------------------------------------
463     !     call esmf_logwrite("internal state link to esmf export state", 	&
464     !                       esmf_log_info, rc = rc1)
465     
466           int_state%fhour_idate(1,1)=int_state%kfhour
467           int_state%fhour_idate(1,2:5)=idate(1:4)
468     
469     !      call gfs_physics_internal2export(gc_gfs_phy, int_state,  	&
470     !                                          exp_gfs_phy, rc = rc1)
471     
472     !     call gfs_physics_err_msg(rc1,'internal state to esmf export state',rc)
473     
474     !-------------------------------------------------------
475     !jw send all the head info to write tasks
476     !-------------------------------------------------------
477     !
478             call point_physics_output_gfs(int_state,imp_wrt_state)
479     !
480     !*******************************************************************
481     ! print out the final error signal variable and put it to rc.
482     !------------------------------------------------------------
483           call gfs_physics_err_msg_final(rcfinal,				&
484                             'initialize from gfs physics grid comp.',rc)
485     
486           end subroutine gfs_phy_initialize
487     
488     
489     
490     
491     
492     !----------------------------------------------------------------------
493     !bop
494     !
495     ! !routine: gfs_phy_run --- 
496     !           main grid component routine to run the gfs physics.
497     !
498     ! !description: this subroutine will run the most part computations 
499     !               of the gfs physics.
500     !
501     ! !revision history:
502     !
503     !  november 2004     weiyu yang initial code.
504     !  may      2005     weiyu yang for the updated gfs version.
505     !  february 2006     moorthi
506     !  december 2007     juang
507     !  oct 12 2009       Sarah Lu, call gfs_physics_import2internal_mgrid and
508     !                    gfs_physics_internal2export_mgrid to associate imp/exp
509     !                    states with internal state grid_fld
510     !  oct 17 2009       Sarah Lu, debug print added to track imp/exp states
511     !  mar 05 2010       Sarah Lu, internal2export_mgrid is called in init step
512     !
513     ! !interface:
514     !
515     
516           subroutine gfs_phy_run(gc_gfs_phy, 				&
517                                 imp_gfs_phy, exp_gfs_phy, clock, rc)
518     
519     !*     use gfs_physics_states_mod
520     !
521     ! !input variables and parameters:
522     !---------------------------------
523           type(esmf_gridcomp), intent(inout) :: gc_gfs_phy   
524           type(esmf_state),    intent(in)    :: imp_gfs_phy 
525      
526     ! !output variables and parameters:
527     !----------------------------------
528           type(esmf_clock),    intent(inout) :: clock
529           type(esmf_timeinterval)            :: timestep, donetime    
530           type(esmf_time)                    :: starttime    
531           type(esmf_time)                    :: currtime     
532           type(esmf_time)                    :: stoptime     
533           type(esmf_state),    intent(inout) :: exp_gfs_phy
534           integer,             intent(out)   :: rc   
535     !
536     !eop
537     !-------------------------------------------------------------------------
538     
539     !
540     ! !working arrays and local parameters.
541     !--------------------------------------
542           type(gfs_phy_wrap)                :: wrap         
543     ! this wrap is a derived type which contains
544     ! only a pointer to the internal state.  it is needed
545     ! for using different architectures or compliers.
546           type(gfs_physics_internal_state), pointer   :: int_state   
547           integer                                     :: rc1          
548           integer                                     :: rcfinal     
549            real(8) :: zhour1
550     !
551     !jw
552           type(esmf_state)                   :: imp_wrt_state
553     !lu
554           type(ESMF_Field)                   :: Field
555           type(ESMF_FieldBundle)             :: Bundle
556           integer                            :: i
557           character*10                       :: vname
558           real                               :: fscav
559     !
560     ! initialize the error signal variables.
561     !---------------------------------------
562           rc1     = esmf_success
563           rcfinal = esmf_success
564     
565     ! retrieve the esmf internal state.
566     !---------------------------------- 
567           call esmf_logwrite("get the internal state in the run routine", 	&
568                             esmf_log_info, rc = rc1)
569     
570           call esmf_gridcompgetinternalstate(gc_gfs_phy, wrap, rc1)
571     
572           call gfs_physics_err_msg(rc1,					&
573                       'get the internal state in the run routine',rc)
574     
575     ! pointing the local internal state pointer to the esmf internal state pointer.
576     !------------------------------------------------------------------------------
577           int_state => wrap%int_state
578     
579     ! get the esmf import state and over-write the gfs internal state.
580     ! update the initial condition arrays in the internal state based on
581     ! the information of the esmf import state. 
582     !------------------------------------------------------------------
583           call esmf_logwrite("esmf import state to internal state", 	&
584                             esmf_log_info, rc = rc1)
585     !
586     ! the pointer/copy option (Sarah Lu)
587     !  get the esmf import state and over-write the gfs internal state         
588     !  for one-copy option, import2internal is called in the init step
589     !  for two-copy option, import2internal is called every time step        
590     !
591     !*    call gfs_physics_import2internal(gc_gfs_phy, imp_gfs_phy, 	&
592     !*                                        int_state, rc = rc1)
593     !
594     !       print *,'in gfs phys grid comp, run'
595     
596           if ( int_state%grid_aldata ) then
597             call gfs_physics_import2internal( imp_gfs_phy,    &         
598                                                 int_state, rc = rc1)      
599             call gfs_physics_err_msg(rc1,'import2internal',rc)     
600           endif                                                         
601     
602           idate(1:4)=int_state%fhour_idate(1,2:5)
603           fhour     =int_state%fhour_idate(1,1)
604     
605           call gfs_physics_err_msg(rc1,'esmf import state to internal state',rc)
606     
607     !
608     ! get clock times
609     ! ------------------
610           call esmf_clockget(clock,            				&
611                              timestep    = timestep,                	&
612                              starttime   = starttime,                 	&
613                              currtime    = currtime,                 	&
614                              stoptime    = stoptime,                	&
615                              rc          = rc1)
616     
617           call gfs_physics_err_msg(rc1,'esmf clockget',rc)
618     
619           donetime = currtime-starttime
620           int_state%kdt = nint(donetime/timeStep) + 1
621     
622           if(me==0)print *,' in physics kdt=',int_state%kdt
623     
624     !     if( currtime .eq. stoptime ) then
625     !         print *,' currtime equals to stoptime '
626     !         int_state%end_step=.true.
627     !     endif
628     !
629     !-----------------------------------------------------------------------
630     !***  retrieve the import state of the write gridded component
631     !***  from the physics export state.
632     !-----------------------------------------------------------------------
633           call esmf_logwrite("Retrieve Write Import State from Physics Export State", &
634                             esmf_log_info, rc = rc1)
635     
636           CALL ESMF_StateGet(state      =exp_gfs_phy                        &  !<-- The Physics export state
637                             ,itemName   ='Write Import State'               &  !<-- Name of the state to get from Physics export state
638                             ,nestedState=imp_wrt_state                      &  !<-- Extract Write component import state from Physics export
639                             ,rc         =RC1)
640     !
641           CALL gfs_physics_err_msg(rc1,"Retrieve Write Import State from Physics Export State",RC)
642     !-----------------------------------------------------------------------
643     !      CALL ESMF_AttributeSet(state    =imp_wrt_state                    &  !<-- The Write component import state
644     !                           ,name     ='zhour'                          &  !<-- Name of the var
645     !                           ,value    =int_state%zhour                  &  !<-- The var being inserted into the impo
646     !                            ,rc       =RC)
647           if(ldfi.and.int_state%kdt==ndfi) then
648              CALL ESMF_AttributeSet(state    =imp_wrt_state                    &  !<-- The Write component import state
649                                    ,name     ='zhour'                          &  !<-- Name of the var
650                                    ,value    =int_state%zhour_dfi              &  !<-- The var being inserted into the import state
651                                    ,rc       =RC)
652           else
653              CALL ESMF_AttributeSet(state    =imp_wrt_state                    &  !<-- The Write component import state
654                                    ,name     ='zhour'                          &  !<-- Name of the var
655                                    ,value    =int_state%zhour                  &  !<-- The var being inserted into the impo
656                                    ,rc       =RC)
657     
658           endif
659     !       write(0,*)'in physgrid comp,kdt=',int_state%kdt,'zhour=',int_state%zhour,'zhour_dfi=',  &
660     !           int_state%zhour_dfi,'ldfi=',ldfi,'ndfi=',ndfi,'rc=',rc
661     !
662     !-----------------------------------------------------------------------
663     !***  retrieve the scavenging coefficients from physics export state
664     !-----------------------------------------------------------------------
665     !
666            if ( int_state%start_step ) then                             
667             CALL ESMF_StateGet(exp_gfs_phy, 'tracers', Bundle, rc = RC1 )
668             CALL gfs_physics_err_msg(rc1,"Retrieve tracer bundle from phy_exp",RC)
669     
670             do I = 1, int_state%ntrac
671     
672              vname = trim(int_state%gfs_phy_tracer%vname(i))
673              call ESMF_FieldBundleGet(Bundle, NAME=vname, FIELD=Field, rc = RC1 )
674              CALL gfs_physics_err_msg(rc1,"Retrieve field from tracer bundle ",RC)
675              CALL ESMF_AttributeGet(Field, NAME="ScavengingFractionPerKm", &
676                                     value = fscav , rc=RC1)
677              if ( RC1 == ESMF_SUCCESS) THEN
678                int_state%gfs_phy_tracer%fscav(i) = fscav
679              else
680                int_state%gfs_phy_tracer%fscav(i) = 0.
681              endif
682     !	 print *, 'phys_gc: fscav:', i, vname, int_state%gfs_phy_tracer%fscav(i)
683             enddo
684             int_state%start_step = .false. 
685            endif
686     
687     !
688     !
689     ! run the gfs.
690     !--------------------------
691           call esmf_logwrite("run the gfs_physics_run", 			&
692                              esmf_log_info, rc = rc1)
693     
694           call gfs_physics_run(int_state, rc = rc1)
695     !
696           call gfs_physics_err_msg(rc1,'run the gfs_physics_run',rc)
697     !
698     !
699     ! transfer the gfs export fields in the internal state 
700     ! to the esmf exprot state which is the public interface
701     ! for other esmf grid components.
702     !-------------------------------------------------------
703          call esmf_logwrite("internal state to esmf export state", 	&
704                            esmf_log_info, rc = rc1)
705     
706     ! the pointer/copy option (Sarah Lu)
707     !  point export state to internal state grid_fld in init step
708     
709     !     if ( int_state%start_step ) then                             
710     !       call gfs_physics_internal2export( int_state,             & 
711     !                                          exp_gfs_phy, rc = rc1)  
712     !       call gfs_physics_err_msg(rc1,'internal2export',rc)  
713     !
714     !       int_state%start_step = .false. 
715     !     endif                                                     
716     
717     !     call gfs_physics_err_msg(rc1,'internal state to esmf export state',rc)
718     
719     !
720     !-----------------------------------------------------------------------
721     !***  retrieve the import state of the write gridded component
722     !***  from the physics export state.
723     !-----------------------------------------------------------------------
724     !      call esmf_logwrite("Retrieve Write Import State from Physics Export State", &
725     !                        esmf_log_info, rc = rc1)
726     !
727     !      CALL ESMF_StateGet(state      =exp_gfs_phy                        &  !<-- The Physics export state
728     !                        ,itemName   ='Write Import State'               &  !<-- Name of the state to get from Physics export state
729     !                        ,nestedState=imp_wrt_state                      &  !<-- Extract Write component import state from Physics export
730     !                        ,rc         =RC1)
731     !!
732     !      CALL gfs_physics_err_msg(rc1,"Retrieve Write Import State from Physics Export State",RC)
733     !-----------------------------------------------------------------------
734     !      CALL ESMF_AttributeSet(state    =imp_wrt_state                    &  !<-- The Write component import state
735     !                            ,name     ='zhour'                          &  !<-- Name of the var
736     !                            ,value    =int_state%zhour                  &  !<-- The var being inserted into the import state
737     !                            ,rc       =RC)
738     !
739     !*******************************************************************
740     !
741     ! print out the final error signal information and put it to rc.
742     !---------------------------------------------------------------
743           call gfs_physics_err_msg_final(rcfinal,				&
744                             'run from gfs physics grid comp.',rc)
745     
746           end subroutine gfs_phy_run
747     
748     
749     !----------------------------------------------------------------------
750     !bop
751     !
752     ! !routine: finalize --- finalizing routine to finish the 
753     !                        gfs running job.
754     !
755     ! !description: this subroutine will finish the gfs computations,
756     ! !             and will release the memory space.
757     !
758     ! !revision history:
759     !
760     !  november 2004     weiyu yang initial code.
761     !  may      2005     weiyu yang for the updated gfs version.
762     !  february 2006     moorthi
763     !  february 2007     juang for dynamics only
764     !  july     2007     juang for physics only
765     !
766     ! !interface:
767     
768           subroutine gfs_phy_finalize(gc_gfs_phy, 				&
769                                      imp_gfs_phy, exp_gfs_phy, clock, rc)
770     
771     !
772     ! !input variables and parameters:
773     !---------------------------------
774           type(esmf_gridcomp), intent(inout)  :: gc_gfs_phy
775           type(esmf_state),    intent(inout)  :: imp_gfs_phy
776           type(esmf_state),    intent(inout)  :: exp_gfs_phy
777           type(esmf_clock),    intent(inout)  :: clock
778     
779     ! !output variables and parameters:
780     !----------------------------------
781           integer,             intent(out)    :: rc
782     
783     ! !working arrays and local parameters.
784     !--------------------------------------
785           type(gfs_phy_wrap)                            :: wrap   
786           type(gfs_physics_internal_state), pointer     :: int_state  
787           integer                                       :: rc1        
788           integer                                       :: rcfinal   
789     
790     !eop
791     !-------------------------------------------------------------------------
792     
793     ! initialize the error signal variables.
794     !---------------------------------------
795           rc1     = esmf_success
796           rcfinal = esmf_success
797     
798     ! retrieve the esmf internal state.
799     !----------------------------------
800          call esmf_logwrite(						&
801                           "get the internal state in the finalize routine", &
802                            esmf_log_info, rc = rc1)
803     
804          call esmf_gridcompgetinternalstate(gc_gfs_phy, wrap, rc1)
805     
806          call gfs_physics_err_msg(rc1,					&
807                   'get the internal state in the finalize routine',rc)
808     
809     ! point the local internal state pointer to the esmf internal state pointer.
810     !------------------------------------------------------------------------------
811           int_state => wrap%int_state
812     
813     ! run the gfs finalize routine to release the memory space, etc. 
814     !----------------------------------------------------------------------------
815           call esmf_logwrite("run the gfs_physics_finalize", 		&
816                              esmf_log_info, rc = rc1)
817     
818           call gfs_physics_finalize(int_state, rc = rc1)
819     
820           call gfs_physics_err_msg(rc1,'run the gfs_physics_finalize',rc)
821     
822     ! print out the final error signal information and put it to rc.
823     !---------------------------------------------------------------
824           call gfs_physics_err_msg_final(rcfinal,				&
825                             'finalize from gfs physics grid comp.',rc)
826     
827           end subroutine gfs_phy_finalize
828     
829     ! end of the gfs esmf grid component module.
830     !-------------------------------------------
831           end module gfs_physics_grid_comp_mod
832