MODULE climate_rec !----------------------------------------------------------------------- ! NAME ! climate_rec ! ! DESCRIPTION ! Write the restart files to save the climate state. ! ! AUTHORS & DATE ! JB Clement, 12/2025 ! ! NOTES ! !----------------------------------------------------------------------- ! DEPENDENCIES ! ------------ use numerics, only: dp, di, k4 use io_netcdf, only: open_nc, close_nc, check_nc, put_var_nc, get_dim_nc, get_var_nc, start_name, start1D_name, startfi_name, startevo_name ! DECLARATION ! ----------- implicit none ! VARIABLES ! --------- logical(k4), protected, private :: is_restartevo = .false. ! Flag to know if "restartevo.nc" exists contains !+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ !======================================================================= SUBROUTINE write_restart(ps4PCM,q4PCM,teta4PCM,air_mass4PCM) !----------------------------------------------------------------------- ! NAME ! write_restart ! ! DESCRIPTION ! Write the file "restart.nc". ! ! AUTHORS & DATE ! JB Clement, 12/2025 ! ! NOTES ! !----------------------------------------------------------------------- ! DEPENDENCIES ! ------------ use geometry, only: vect2dyngrd, nlon, nlat, nlayer use tracers, only: qnames, nq use stoppage, only: stop_clean use display, only: print_msg, LVL_NFO ! DECLARATION ! ----------- implicit none ! ARGUMENTS ! --------- real(dp), dimension(:), intent(in) :: ps4PCM real(dp), dimension(:,:), intent(in) :: teta4PCM, air_mass4PCM real(dp), dimension(:,:,:), intent(in) :: q4PCM ! LOCAL VARIABLES ! --------------- integer(di) :: cstat, l, i real(dp), dimension(nlon + 1,nlat) :: ps_dyn real(dp), dimension(nlon + 1,nlat,nlayer) :: var_dyn ! CODE ! ---- ! Copy "start.nc" into "restart.nc" call print_msg('> Writing "re'//start_name//'"',LVL_NFO) call execute_command_line('cp '//start_name//' re'//start_name,cmdstat = cstat) if (cstat > 0) then call stop_clean(__FILE__,__LINE__,'command execution failed!',1) else if (cstat < 0) then call stop_clean(__FILE__,__LINE__,'command execution not supported!',1) end if ! Rewrite the variables modified by the PEM call open_nc('re'//start_name,'write') ! Surface pressure call vect2dyngrd(ps4PCM,ps_dyn) call put_var_nc('ps',ps_dyn,1) ! Potential temperature do l = 1,nlayer call vect2dyngrd(teta4PCM(:,l),var_dyn(:,:,l)) end do call put_var_nc('teta',var_dyn,1) ! Air mass do l = 1,nlayer call vect2dyngrd(air_mass4PCM(:,l),var_dyn(:,:,l),extensive = .true.) end do call put_var_nc('masse',var_dyn,1) ! Tracers do i = 1,nq do l = 1,nlayer call vect2dyngrd(q4PCM(:,l,i),var_dyn(:,:,l)) end do call put_var_nc(qnames(i),var_dyn,1) end do ! Close call close_nc('re'//start_name) END SUBROUTINE write_restart !======================================================================= !======================================================================= SUBROUTINE write_restartfi(h2o_ice4PCM,co2_ice4PCM,tsurf4PCM,tsoil4PCM,inertiesoil4PCM,albedo4PCM,emissivity4PCM) !----------------------------------------------------------------------- ! NAME ! write_restartfi ! ! DESCRIPTION ! Write the file "restartfi.nc". ! ! AUTHORS & DATE ! JB Clement, 01/2026 ! C. Metz, 04/2026 ! ! NOTES ! !----------------------------------------------------------------------- ! DEPENDENCIES ! ------------ use orbit, only: obliquity, aphelion, perihelion, date_peri use frost, only: h2o_frost4PCM, co2_frost4PCM use stoppage, only: stop_clean use display, only: print_msg, LVL_NFO ! DECLARATION ! ----------- implicit none ! ARGUMENTS ! --------- real(dp), dimension(:,:), intent(in) :: h2o_ice4PCM, co2_ice4PCM, albedo4PCM, emissivity4PCM, tsurf4PCM real(dp), dimension(:,:,:), intent(in) :: tsoil4PCM, inertiesoil4PCM ! LOCAL VARIABLES ! --------------- integer(di) :: cstat integer(di) :: nindex ! Size of dimension 'index' real(dp), dimension(:), allocatable :: controle ! CODE ! ---- ! Copy "startfi.nc" into "restartfi.nc" call print_msg('> Writing "re'//startfi_name//'"',LVL_NFO) call execute_command_line('cp '//startfi_name//' re'//startfi_name,cmdstat = cstat) if (cstat > 0) then call stop_clean(__FILE__,__LINE__,'command execution failed!',1) else if (cstat < 0) then call stop_clean(__FILE__,__LINE__,'command execution not supported!',1) end if ! Load the variable 'controle' to modify it with new values call open_nc('re'//startfi_name,'read') call get_dim_nc('index',nindex) allocate(controle(nindex)) call get_var_nc('controle',controle) call close_nc('re'//startfi_name) ! Rewrite the variables modified by the PEM call open_nc('re'//startfi_name,'write') ! Orbital parameters (controle) controle(18) = obliquity ! Obliquity controle(15) = perihelion ! Perihelion controle(16) = aphelion ! Aphelion controle(17) = date_peri ! Date of perihelion call put_var_nc('controle',controle) deallocate(controle) ! Variables that have been modified call put_var_nc('watercap',h2o_ice4PCM,1) call put_var_nc('h2o_ice',h2o_frost4PCM,1) call put_var_nc('co2',co2_frost4PCM,1) call put_var_nc('perennial_co2ice',co2_ice4PCM,1) call put_var_nc('tsurf',tsurf4PCM,1) call put_var_nc('tsoil',tsoil4PCM,1) call put_var_nc('inertiesoil',inertiesoil4PCM,1) call put_var_nc('albedo',albedo4PCM,1) call put_var_nc('emis',emissivity4PCM,1) ! Close call close_nc('re'//startfi_name) END SUBROUTINE write_restartfi !======================================================================= !======================================================================= SUBROUTINE create_startevo() !----------------------------------------------------------------------- ! NAME ! create_startevo ! ! DESCRIPTION ! Create a NetCDF file for the PEM. ! ! AUTHORS & DATE ! JB Clement, 01/2026 ! ! NOTES ! !----------------------------------------------------------------------- ! DEPENDENCIES ! ------------ use netcdf, only: nf90_double, nf90_clobber, nf90_unlimited, nf90_global, & nf90_create, nf90_enddef, nf90_def_dim, nf90_def_var, nf90_put_att use geometry, only: dim_init, ngrid, nsoil, nslope, longitudes, latitudes, cell_area use stoppage, only: stop_clean use soil, only: do_soil, mlayer use display, only: print_msg, LVL_NFO ! DECLARATION ! ----------- implicit none ! LOCAL VARIABLES ! --------------- integer(di) :: ncid ! File ID integer(di) :: varid ! Variable ID integer(di) :: dim_ngrid, dim_nsoil, dim_nslope, dim_time ! CODE ! ---- ! Check if dimensions are well initialized if (.not. dim_init) call stop_clean(__FILE__,__LINE__,'dimensions are not initilized in the PEM!',1) ! Create file call print_msg('> Creating "re'//startevo_name//'"',LVL_NFO) call check_nc(nf90_create('re'//startevo_name,nf90_clobber,ncid),'creating re'//startevo_name) ! Enter define mode ! Define dimensions call check_nc(nf90_def_dim(ncid,'Time',nf90_unlimited,dim_time),'defining dimension Time') call check_nc(nf90_def_dim(ncid,'physical_points',ngrid,dim_ngrid),'defining dimension physical_points') call check_nc(nf90_def_dim(ncid,'subsurface_layers',nsoil,dim_nsoil),'defining dimension subsurface_layers') call check_nc(nf90_def_dim(ncid,'nslope',nslope,dim_nslope),'defining dimension nslope') ! Define variables call check_nc(nf90_def_var(ncid,'Time',nf90_double,(/dim_time/),varid),'defining variable Time') call check_nc(nf90_put_att(ncid,varid,'title','Year of simulation'),'putting title attribute for Time') call check_nc(nf90_put_att(ncid,varid,'units','Planetary year'),'putting units attribute for Time') call check_nc(nf90_def_var(ncid,'longitude',nf90_double,(/dim_ngrid/),varid),'defining variable longitude') call check_nc(nf90_put_att(ncid,varid,'title','Longitudes of the grid'),'putting title attribute for longitude') call check_nc(nf90_def_var(ncid,'latitude',nf90_double,(/dim_ngrid/),varid),'defining variable latitude') call check_nc(nf90_put_att(ncid,varid,'title','Latitudes of the grid'),'putting title attribute for latitude') call check_nc(nf90_def_var(ncid,'cell_area',nf90_double,(/dim_ngrid/),varid),'defining variable cell_area') call check_nc(nf90_put_att(ncid,varid,'title','Cell area'),'putting title attribute for cell_area') call check_nc(nf90_def_var(ncid,'h2o_ice',nf90_double,(/dim_ngrid,dim_nslope,dim_time/),varid),'defining variable h2o_ice') call check_nc(nf90_put_att(ncid,varid,'title','H2O ice'),'putting title attribute for h2o_ice') call check_nc(nf90_def_var(ncid,'co2_ice',nf90_double,(/dim_ngrid,dim_nslope,dim_time/),varid),'defining variable co2_ice') call check_nc(nf90_put_att(ncid,varid,'title','CO2 ice'),'putting title attribute for co2_ice') if (do_soil) then call check_nc(nf90_def_var(ncid,'soildepth',nf90_double,(/dim_nsoil/),varid),'defining variable soildepth') call check_nc(nf90_put_att(ncid,varid,'title','Depths of soil layers'),'putting title attribute for soildepth') call check_nc(nf90_def_var(ncid,'tsoil',nf90_double,(/dim_ngrid,dim_nsoil,dim_nslope,dim_time/),varid),'defining variable tsoil') call check_nc(nf90_put_att(ncid,varid,'title','Soil temperature'),'putting title attribute for tsoil') call check_nc(nf90_def_var(ncid,'TI',nf90_double,(/dim_ngrid,dim_nsoil,dim_nslope,dim_time/),varid),'defining variable inertiedat') call check_nc(nf90_put_att(ncid,varid,'title','Thermal inertie of PEM'),'putting title attribute for TI') call check_nc(nf90_def_var(ncid,'inertiedat',nf90_double,(/dim_ngrid,dim_nsoil,dim_time/),varid),'defining variable longitude') call check_nc(nf90_put_att(ncid,varid,'title','Soil thermal inertia'),'putting title attribute for inertiedat') end if ! Global attributes call check_nc(nf90_put_att(ncid,nf90_global,'title','PEM start file'),'putting global attribute') ! Leave define mode and putting variables defining dimensions call check_nc(nf90_enddef(ncid),'leaving define mode') call put_var_nc('longitude',longitudes) call put_var_nc('latitude',latitudes) call put_var_nc('cell_area',cell_area) call put_var_nc('soildepth',mlayer) call close_nc('re'//startevo_name) ! File creation done is_restartevo = .true. END SUBROUTINE create_startevo !======================================================================= !======================================================================= SUBROUTINE write_restartevo(h2o_ice,co2_ice,tsoil,TI) !----------------------------------------------------------------------- ! NAME ! write_restartevo ! ! DESCRIPTION ! Write the file "restartevo.nc". ! ! AUTHORS & DATE ! JB Clement, 01/2026 ! ! NOTES ! !----------------------------------------------------------------------- ! DEPENDENCIES ! ------------ use evolution, only: pem_ini_date, n_yr_sim use soil, only: do_soil, inertiedat use stoppage, only: stop_clean use display, only: print_msg, LVL_NFO ! DECLARATION ! ----------- implicit none ! ARGUMENTS ! --------- real(dp), dimension(:,:), intent(in) :: h2o_ice, co2_ice real(dp), dimension(:,:,:), intent(in) :: tsoil, TI ! LOCAL VARIABLES ! --------------- integer(di) :: itime ! Time index to record variables ! CODE ! ---- ! Create the file call create_startevo() call print_msg('> Writing "re'//startevo_name//'"',LVL_NFO) if (.not. is_restartevo) call stop_clean(__FILE__,__LINE__,'The file"'//startevo_name//'" has not been created',1) ! Writing time counter call open_nc('re'//startevo_name,'write',itime) call put_var_nc('Time',pem_ini_date + n_yr_sim,itime) ! Writing other variables call put_var_nc('h2o_ice',h2o_ice,itime) call put_var_nc('co2_ice',co2_ice,itime) if (do_soil) then call put_var_nc('tsoil',tsoil,itime) call put_var_nc('TI',TI,itime) call put_var_nc('inertiedat',inertiedat,itime) end if ! Close call close_nc('re'//startevo_name) END SUBROUTINE write_restartevo !======================================================================= END MODULE climate_rec