Skip to content

Commit

Permalink
Merge pull request #4 from fritzt/development
Browse files Browse the repository at this point in the history
Export fields on first call to HCO_GC_RUN
  • Loading branch information
jimmielin committed Dec 10, 2020
2 parents 4e6715d + 96a8bca commit 99b804b
Showing 1 changed file with 58 additions and 73 deletions.
131 changes: 58 additions & 73 deletions hemco_interface.F90
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ subroutine HCOI_Chunk_Init()

HcoConfig%amIRoot = masterproc

HcoConfig%amIRoot = .true. ! for debug only so verbosity is higher
!HcoConfig%amIRoot = .true. ! for debug only so verbosity is higher
HcoConfig%MetField = 'MERRA2'
HcoConfig%GridRes = ''

Expand Down Expand Up @@ -610,6 +610,7 @@ subroutine HCOI_Chunk_Init()
! Additional exports: Verify if we need to add additional exports
! for integration with CESM-GC. (hplin, 4/15/20)
!-----------------------------------------------------------------------

! Do additional exports!
if(chem_is('GEOS-Chem')) then
do N = 0, 72
Expand Down Expand Up @@ -1106,6 +1107,9 @@ subroutine HCO_GC_Run(GC, IMPORT, EXPORT, Clock, RC)
! HEMCO return code
integer :: HMRC

logical, save :: FIRST = .True.
logical :: doExport = .False.

! Assume success
RC = ESMF_SUCCESS
HMRC = HCO_SUCCESS
Expand Down Expand Up @@ -1346,67 +1350,30 @@ subroutine HCO_GC_Run(GC, IMPORT, EXPORT, Clock, RC)

! Build history / pbuf field name (HCO_NO, HCO_CO, etc.)
exportName = 'HCO_' // trim(HcoConfig%ModelSpc(spcID)%SpcName)
doExport = ( FIRST .or. associated(HcoState%Spc(spcID)%Emis%Val) )
! if(masterproc) write(iulog,*) "HEMCO_CAM: Begin exporting " // trim(exportName)

! Get HEMCO emissions flux [kg/m2/s].
! For performance optimization ... tap into HEMCO structure directly (ugly ugly)
! No need to flip vertical here. The regridder will do it for us
if(associated(HcoState%Spc(spcID)%Emis%Val)) then
if( associated(HcoState%Spc(spcID)%Emis%Val) ) then
exportFldHco(my_IS:my_IE,my_JS:my_JE,1:LM) = HcoState%Spc(spcID)%Emis%Val(1:HI,1:HJ,1:LM)
! do K = 1, LM
! do J = my_JS, my_JE
! HJ = J - my_JS + 1

! exportFldHco(my_IS:my_IE,J,K) = HcoState%Spc(spcID)%Emis%Val(1:HI,J,K)
! enddo
! enddo

if(masterproc) then
write(iulog,*) "HEMCO_CAM: Retrieved from HCO " // trim(exportName)
endif
else
if(masterproc) then
write(iulog,*) "HEMCO_CAM: No emissions HCO " // trim(exportName)
endif
if(masterproc) write(iulog,*) "HEMCO_CAM: Retrieved from HCO " // trim(exportName)
!write(6,*) "HEMCO_CAM: Retrieved from HCO " // trim(exportName)
endif

! Regrid exportFldHco to CAM grid...
call HCO_Grid_HCO2CAM_3D(exportFldHco, exportFldCAM, doRegrid=associated(HcoState%Spc(spcID)%Emis%Val))
! if(masterproc) write(iulog,*) "HEMCO_CAM: Regridded " // trim(exportName)
call HCO_Grid_HCO2CAM_3D(exportFldHco, exportFldCAM, doRegrid=doExport)

if(associated(HcoState%Spc(spcID)%Emis%Val)) then
if( doExport ) then
! Write to history on CAM mesh
call HCO_Export_History_CAM3D(exportName, exportFldCAM)
! if(masterproc) write(iulog,*) "HEMCO_CAM: Exported to history " // trim(exportName)

! Write to physics buffer (pass model name)
call HCO_Export_Pbuf_CAM3D(HcoConfig%ModelSpc(spcID)%SpcName, spcID, exportFldCAM)
endif

! do K = 1, LM
! HL = K ! map to HEMCO index
! do J = my_JS, my_JE
! HJ = J - my_JS + 1
! do I = my_IS, my_IE
! HI = I - my_IS + 1

! ! Maybe no need to call GetHcoVal and just directly grab the pointer
! ! instead?
! ! call GetHcoVal(HcoState, ExtState, spcID, HI, HJ, HL, FND, emis=TMP)

! ! For performance optimization

! if(FND) then
! exportFldHco(I,J,K) = TMP

! if(masterproc .and. TMP < 0.0_r8) then
! write(iulog) "> **ERR** HEMCO_CAM negative value HI,HJ,HL,spcID,val", HI, HJ, HL, spcID, TMP
! endif
! endif
! enddo
! enddo
! enddo
! if(masterproc) write(iulog,*) "HEMCO_CAM: Retrieved from HCO " // trim(exportName)
enddo


Expand All @@ -1428,10 +1395,12 @@ subroutine HCO_GC_Run(GC, IMPORT, EXPORT, Clock, RC)

! Grab the pointer if available
call HCO_GetPtr(HcoState, exportNameTmp, Ptr2D, HMRC, FOUND=FND)
if(HMRC == HCO_SUCCESS .and. FND) then
doExport = (FIRST .or. (HMRC == HCO_SUCCESS .and. FND))
if ( HMRC == HCO_SUCCESS .and. FND ) then
exportFldHco(:,:,1) = Ptr2D ! Copy data in

call HCO_Grid_HCO2CAM_3D(exportFldHco, exportFldCAM)
endif
call HCO_Grid_HCO2CAM_3D(exportFldHco, exportFldCAM)
if ( doExport ) then
call HCO_Export_History_CAM3D(exportName, exportFldCAM)
call HCO_Export_Pbuf_CAM3D(exportNameTmp, -1, exportFldCAM)
endif
Expand All @@ -1447,10 +1416,12 @@ subroutine HCO_GC_Run(GC, IMPORT, EXPORT, Clock, RC)

! Grab the pointer if available
call HCO_GetPtr(HcoState, exportNameTmp, Ptr2D, HMRC, FOUND=FND)
if(HMRC == HCO_SUCCESS .and. FND) then
doExport = (FIRST .or. (HMRC == HCO_SUCCESS .and. FND))
if ( HMRC == HCO_SUCCESS .and. FND ) then
exportFldHco(:,:,1) = Ptr2D ! Copy data in

call HCO_Grid_HCO2CAM_3D(exportFldHco, exportFldCAM)
endif
call HCO_Grid_HCO2CAM_3D(exportFldHco, exportFldCAM)
if ( doExport ) then
call HCO_Export_History_CAM3D(exportName, exportFldCAM)
call HCO_Export_Pbuf_CAM3D(exportNameTmp, -1, exportFldCAM)
endif
Expand All @@ -1467,10 +1438,12 @@ subroutine HCO_GC_Run(GC, IMPORT, EXPORT, Clock, RC)

! Grab the pointer if available
call HCO_GetPtr(HcoState, exportNameTmp, Ptr2D, HMRC, FOUND=FND)
if(HMRC == HCO_SUCCESS .and. FND) then
doExport = (FIRST .or. (HMRC == HCO_SUCCESS .and. FND))
if ( HMRC == HCO_SUCCESS .and. FND ) then
exportFldHco(:,:,1) = Ptr2D ! Copy data in

call HCO_Grid_HCO2CAM_3D(exportFldHco, exportFldCAM)
endif
call HCO_Grid_HCO2CAM_3D(exportFldHco, exportFldCAM)
if ( doExport ) then
call HCO_Export_History_CAM3D(exportName, exportFldCAM)
call HCO_Export_Pbuf_CAM3D(exportNameTmp, -1, exportFldCAM)
endif
Expand All @@ -1486,16 +1459,17 @@ subroutine HCO_GC_Run(GC, IMPORT, EXPORT, Clock, RC)

! Grab the pointer if available
call HCO_GetPtr(HcoState, exportNameTmp, Ptr2D, HMRC, FOUND=FND)
if(HMRC == HCO_SUCCESS .and. FND) then
doExport = (FIRST .or. (HMRC == HCO_SUCCESS .and. FND))
if ( HMRC == HCO_SUCCESS .and. FND ) then
exportFldHco(:,:,1) = Ptr2D ! Copy data in

! This is required as the physics buffer cannot store
! `HCO_surf_salinity` as it is too long.. We thus export as
! `HCO_salinity`
write(exportNameTmp, '(a)') 'salinity'
exportName = 'HCO_' // trim(exportNameTmp)

call HCO_Grid_HCO2CAM_3D(exportFldHco, exportFldCAM)
endif
call HCO_Grid_HCO2CAM_3D(exportFldHco, exportFldCAM)
if ( doExport ) then
call HCO_Export_History_CAM3D(exportName, exportFldCAM)
call HCO_Export_Pbuf_CAM3D(exportNameTmp, -1, exportFldCAM)
endif
Expand All @@ -1511,14 +1485,15 @@ subroutine HCO_GC_Run(GC, IMPORT, EXPORT, Clock, RC)

! Grab the pointer if available
call HCO_GetPtr(HcoState, exportNameTmp, Ptr2D, HMRC, FOUND=FND)
if(HMRC == HCO_SUCCESS .and. FND) then
doExport = (FIRST .or. (HMRC == HCO_SUCCESS .and. FND))
if ( HMRC == HCO_SUCCESS .and. FND ) then
exportFldHco(:,:,1) = Ptr2D ! Copy data in

! See above comment about `HCO_surf_salinity`
write(exportNameTmp, '(a)') 'iodide'
exportName = 'HCO_' // trim(exportNameTmp)

call HCO_Grid_HCO2CAM_3D(exportFldHco, exportFldCAM)
endif
call HCO_Grid_HCO2CAM_3D(exportFldHco, exportFldCAM)
if ( doExport ) then
call HCO_Export_History_CAM3D(exportName, exportFldCAM)
call HCO_Export_Pbuf_CAM3D(exportNameTmp, -1, exportFldCAM)
endif
Expand All @@ -1534,10 +1509,12 @@ subroutine HCO_GC_Run(GC, IMPORT, EXPORT, Clock, RC)

! Grab the pointer if available
call HCO_GetPtr(HcoState, exportNameTmp, Ptr2D, HMRC, FOUND=FND)
if(HMRC == HCO_SUCCESS .and. FND) then
doExport = (FIRST .or. (HMRC == HCO_SUCCESS .and. FND))
if ( HMRC == HCO_SUCCESS .and. FND ) then
exportFldHco(:,:,1) = Ptr2D ! Copy data in

call HCO_Grid_HCO2CAM_3D(exportFldHco, exportFldCAM)
endif
call HCO_Grid_HCO2CAM_3D(exportFldHco, exportFldCAM)
if ( doExport ) then
call HCO_Export_History_CAM3D(exportName, exportFldCAM)
call HCO_Export_Pbuf_CAM3D(exportNameTmp, -1, exportFldCAM)
endif
Expand All @@ -1553,10 +1530,12 @@ subroutine HCO_GC_Run(GC, IMPORT, EXPORT, Clock, RC)

! Grab the pointer if available
call HCO_GetPtr(HcoState, exportNameTmp, Ptr2D, HMRC, FOUND=FND)
if(HMRC == HCO_SUCCESS .and. FND) then
doExport = (FIRST .or. (HMRC == HCO_SUCCESS .and. FND))
if ( HMRC == HCO_SUCCESS .and. FND ) then
exportFldHco(:,:,1) = Ptr2D ! Copy data in

call HCO_Grid_HCO2CAM_3D(exportFldHco, exportFldCAM)
endif
call HCO_Grid_HCO2CAM_3D(exportFldHco, exportFldCAM)
if ( doExport ) then
call HCO_Export_History_CAM3D(exportName, exportFldCAM)
call HCO_Export_Pbuf_CAM3D(exportNameTmp, -1, exportFldCAM)
endif
Expand All @@ -1572,10 +1551,12 @@ subroutine HCO_GC_Run(GC, IMPORT, EXPORT, Clock, RC)

! Grab the pointer if available
call HCO_GetPtr(HcoState, exportNameTmp, Ptr2D, HMRC, FOUND=FND)
if(HMRC == HCO_SUCCESS .and. FND) then
doExport = (FIRST .or. (HMRC == HCO_SUCCESS .and. FND))
if ( HMRC == HCO_SUCCESS .and. FND ) then
exportFldHco(:,:,1) = Ptr2D ! Copy data in

call HCO_Grid_HCO2CAM_3D(exportFldHco, exportFldCAM)
endif
call HCO_Grid_HCO2CAM_3D(exportFldHco, exportFldCAM)
if ( doExport ) then
call HCO_Export_History_CAM3D(exportName, exportFldCAM)
call HCO_Export_Pbuf_CAM3D(exportNameTmp, -1, exportFldCAM)
endif
Expand All @@ -1591,10 +1572,12 @@ subroutine HCO_GC_Run(GC, IMPORT, EXPORT, Clock, RC)

! Grab the pointer if available
call HCO_GetPtr(HcoState, exportNameTmp, Ptr2D, HMRC, FOUND=FND)
if(HMRC == HCO_SUCCESS .and. FND) then
doExport = (FIRST .or. (HMRC == HCO_SUCCESS .and. FND))
if ( HMRC == HCO_SUCCESS .and. FND ) then
exportFldHco(:,:,1) = Ptr2D ! Copy data in

call HCO_Grid_HCO2CAM_3D(exportFldHco, exportFldCAM)
endif
call HCO_Grid_HCO2CAM_3D(exportFldHco, exportFldCAM)
if ( doExport ) then
call HCO_Export_History_CAM3D(exportName, exportFldCAM)
call HCO_Export_Pbuf_CAM3D(exportNameTmp, -1, exportFldCAM)
endif
Expand Down Expand Up @@ -1623,6 +1606,8 @@ subroutine HCO_GC_Run(GC, IMPORT, EXPORT, Clock, RC)
last_HCO_day = now_day
last_HCO_second = now_s

IF ( FIRST ) FIRST = .False.

RC = ESMF_SUCCESS

end subroutine HCO_GC_Run
Expand Down

0 comments on commit 99b804b

Please sign in to comment.