source: trunk/LMDZ.COMMON/libf/evolution/xios_data.F90 @ 4157

Last change on this file since 4157 was 4157, checked in by jbclement, 3 weeks ago

PEM:

  • Separate variables ownership between the module "planet" for persistent climate state and the program "pem" for transient workflow logic. It provides a meaningful structure.
  • Add lifecycle helpers for clear allocation/deallocation logic.
  • Simplify string suffix for slopes variables.

JBC

File size: 8.4 KB
RevLine 
[3989]1MODULE xios_data
[3991]2!-----------------------------------------------------------------------
3! NAME
4!     xios_data
5!
6! DESCRIPTION
7!     Read XIOS output data and process it for PEM initialization.
8!
9! AUTHORS & DATE
10!     JB Clement, 2025
11!
12! NOTES
13!
14!-----------------------------------------------------------------------
[3977]15
[3991]16! DEPENDENCIES
17! ------------
[4065]18use numerics, only: dp, di, k4
[3977]19
[3991]20! DECLARATION
21! -----------
[3977]22implicit none
23
24contains
[3991]25!+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
[3989]26
27!=======================================================================
[4065]28SUBROUTINE load_xios_data(ps_avg,ps_ts,tsurf_avg,tsurf_avg_yr1,tsoil_avg,tsoil_ts,h2o_surfdensity_avg,h2o_soildensity_avg, &
[4071]29                          q_h2o_ts,q_co2_ts,minPCM_h2operice,minPCM_co2perice,minPCM_h2ofrost,minPCM_co2frost)
[3991]30!-----------------------------------------------------------------------
31! NAME
32!     load_xios_data
33!
34! DESCRIPTION
35!     Reads yearly and daily XIOS data, computes frost and ice tendencies.
36!
37! AUTHORS & DATE
38!     JB Clement, 2025
39!
40! NOTES
41!
42!-----------------------------------------------------------------------
[3977]43
[3991]44! DEPENDENCIES
45! ------------
[4065]46use geometry,   only: ngrid, nslope, nsoil, nsoil_PCM, nday, lonlat2vect, nlon, nlat
47use io_netcdf,  only: xios_day_name2, xios_yr_name1, xios_yr_name2, open_nc, close_nc, get_var_nc, get_dim_nc
48use soil,       only: do_soil
49use frost,      only: compute_frost4PCM
50use stoppage,   only: stop_clean
[4110]51use display,    only: print_msg, LVL_NFO
[4065]52use utility,    only: real2str
[3977]53
[3991]54! DECLARATION
55! -----------
[3977]56implicit none
57
[3991]58! ARGUMENTS
59! ---------
[4065]60real(dp), dimension(:),       intent(out) :: ps_avg
[4071]61real(dp), dimension(:,:),     intent(out) :: tsurf_avg, tsurf_avg_yr1, h2o_surfdensity_avg, ps_ts, q_h2o_ts, q_co2_ts
[4065]62real(dp), dimension(:,:,:),   intent(out) :: tsoil_avg, h2o_soildensity_avg
63real(dp), dimension(:,:,:,:), intent(out) :: tsoil_ts
[4071]64real(dp), dimension(:,:,:),   intent(out) :: minPCM_h2operice, minPCM_co2perice, minPCM_h2ofrost, minPCM_co2frost
[3977]65
[3991]66! LOCAL VARIABLES
67! ---------------
[4065]68logical(k4)                                  :: here
69integer(di)                                  :: islope, isoil, iday
[4136]70real(dp), dimension(:),       allocatable    :: var_read_1d
[4065]71real(dp), dimension(:,:),     allocatable    :: var_read_2d
72real(dp), dimension(:,:,:),   allocatable    :: var_read_3d
73real(dp), dimension(:,:,:,:), allocatable    :: var_read_4d
[4157]74character(8)                                 :: num ! Slope suffix to read variables
[4090]75
[3991]76! CODE
77! ----
[3977]78! Initialization
[4071]79minPCM_h2operice(:,:,:) = 0._dp
80minPCM_co2perice(:,:,:) = 0._dp
81minPCM_h2ofrost(:,:,:) = 0._dp
82minPCM_co2frost(:,:,:) = 0._dp
[4157]83num = ''
[3977]84
85!~~~~~~~~~~~~~~~~~~~~~~~~ Year 1 - Yearly data ~~~~~~~~~~~~~~~~~~~~~~~~~
[4065]86inquire(file = xios_yr_name1,exist = here)
87if (.not. here) call stop_clean(__FILE__,__LINE__,'cannot find required file "'//xios_yr_name1//'"!',1)
88
[3977]89! Open the NetCDF file of XIOS outputs
[4110]90call print_msg('> Reading "'//xios_yr_name1//'"',LVL_NFO)
[4065]91call open_nc(xios_yr_name1,'read')
[3977]92
93! Allocate and read the variables
94allocate(var_read_2d(nlon,nlat),var_read_3d(nlon,nlat,nsoil_PCM))
95do islope = 1,nslope
[4157]96    if (nslope == 1) then
97        num = ''
98    else
99        write(num,'("_slope",i2.2)') islope
[4065]100    end if
[4157]101    call get_var_nc('co2ice'//trim(num),var_read_2d)          ; call lonlat2vect(var_read_2d,minPCM_co2frost(:,islope,1))
102    call get_var_nc('h2o_ice_s'//trim(num),var_read_2d)       ; call lonlat2vect(var_read_2d,minPCM_h2ofrost(:,islope,1))
103    call get_var_nc('watercap'//trim(num),var_read_2d)        ; call lonlat2vect(var_read_2d,minPCM_h2operice(:,islope,1))
104    call get_var_nc('perennial_co2ice'//trim(num),var_read_2d); call lonlat2vect(var_read_2d,minPCM_co2perice(:,islope,1))
105    call get_var_nc('tsurf'//trim(num),var_read_2d)           ; call lonlat2vect(var_read_2d,tsurf_avg_yr1(:,islope))
[4065]106end do
[3977]107
108! Close the NetCDF file of XIOS outputs
[4065]109call close_nc(xios_yr_name1)
[3977]110
111!~~~~~~~~~~~~~~~~~~~~~~~~ Year 2 - Yearly data ~~~~~~~~~~~~~~~~~~~~~~~~~
[4065]112inquire(file = xios_yr_name2,exist = here)
113if (.not. here) call stop_clean(__FILE__,__LINE__,'cannot find required file "'//xios_yr_name2//'"!',1)
[3977]114! Open the NetCDF file of XIOS outputs
[4110]115call print_msg('> Reading "'//xios_yr_name2//'"',LVL_NFO)
[4065]116call open_nc(xios_yr_name2,'read')
[3977]117
118! Allocate and read the variables
[4147]119call get_var_nc('ps',var_read_2d); call lonlat2vect(var_read_2d,ps_avg)
[3977]120do islope = 1,nslope
[4157]121    if (nslope == 1) then
122        num = ''
123    else
124        write(num,'("_slope",i2.2)') islope
[4065]125    end if
[4157]126    call get_var_nc('tsurf'//trim(num),var_read_2d)           ; call lonlat2vect(var_read_2d,tsurf_avg(:,islope))
127    call get_var_nc('co2ice'//trim(num),var_read_2d)          ; call lonlat2vect(var_read_2d,minPCM_co2frost(:,islope,2))
128    call get_var_nc('h2o_ice_s'//trim(num),var_read_2d)       ; call lonlat2vect(var_read_2d,minPCM_h2ofrost(:,islope,2))
129    call get_var_nc('watercap'//trim(num),var_read_2d)        ; call lonlat2vect(var_read_2d,minPCM_h2operice(:,islope,2))
130    call get_var_nc('perennial_co2ice'//trim(num),var_read_2d); call lonlat2vect(var_read_2d,minPCM_co2perice(:,islope,2))
[3989]131    if (do_soil) then
[4157]132        call get_var_nc('soiltemp'//trim(num),var_read_3d)
[3977]133        do isoil = 1,nsoil_PCM
[4147]134            call lonlat2vect(var_read_3d(:,:,isoil),tsoil_avg(:,isoil,islope))
[4065]135        end do
136        do isoil = nsoil_PCM + 1,nsoil
137            tsoil_avg(:,isoil,islope) = tsoil_avg(:,nsoil_PCM,islope) ! Explicit initialization because dimension with size nsoil > nsoil_PCM
138        end do
[4157]139        call get_var_nc('waterdensity_surface'//trim(num),var_read_2d); call lonlat2vect(var_read_2d,h2o_surfdensity_avg(:,islope))
[4065]140    end if
141end do
[4090]142
[3977]143! Close the NetCDF file of XIOS outputs
[4065]144call close_nc(xios_yr_name2)
[3977]145
146!~~~~~~~~~~~~~~~~~~~~~~~~~ Year 2 - Daily data ~~~~~~~~~~~~~~~~~~~~~~~~~
[4065]147inquire(file = xios_day_name2,exist = here)
148if (.not. here) call stop_clean(__FILE__,__LINE__,'cannot find required file "'//xios_day_name2//'"!',1)
[3977]149! Open the NetCDF file of XIOS outputs
[4110]150call print_msg('> Reading "'//xios_day_name2//'"',LVL_NFO)
[4065]151call open_nc(xios_day_name2,'read')
[3977]152
153! Allocate and read the variables
154deallocate(var_read_2d,var_read_3d)
[4136]155allocate(var_read_1d(ngrid),var_read_3d(nlon,nlat,nday),var_read_4d(nlon,nlat,nsoil_PCM,nday))
[4065]156call get_var_nc('ps',var_read_3d)
157do iday = 1,nday
[4147]158    call lonlat2vect(var_read_3d(:,:,iday),ps_ts(:,iday))
[4065]159end do
160call get_var_nc('h2o_layer1',var_read_3d)
161do iday = 1,nday
[4147]162    call lonlat2vect(var_read_3d(:,:,iday),q_h2o_ts(:,iday))
[4065]163end do
164call get_var_nc('co2_layer1',var_read_3d)
165do iday = 1,nday
[4147]166    call lonlat2vect(var_read_3d(:,:,iday),q_co2_ts(:,iday))
[4065]167end do
[3989]168if (do_soil) then
[4136]169    h2o_soildensity_avg(:,:,:) = 0._dp
[3977]170    do islope = 1,nslope
[4157]171        if (nslope == 1) then
172            num = ''
173        else
174            write(num,'("_slope",i2.2)') islope
[4065]175        end if
[4157]176        call get_var_nc('soiltemp'//trim(num),var_read_4d)
[4065]177        do iday = 1,nday
[3977]178            do isoil = 1,nsoil_PCM
[4147]179                call lonlat2vect(var_read_4d(:,:,isoil,iday),tsoil_ts(:,isoil,islope,iday))
[4065]180            end do
181            do isoil = nsoil_PCM + 1,nsoil
182                tsoil_ts(:,isoil,islope,iday) = tsoil_ts(:,nsoil_PCM,islope,iday) ! Explicit initialization because dimension with size nsoil > nsoil_PCM
183            end do
184        end do
[4157]185        call get_var_nc('waterdensity_soil'//trim(num),var_read_4d)
[4065]186        do iday = 1,nday
[3977]187            do isoil = 1,nsoil_PCM
[4147]188                call lonlat2vect(var_read_4d(:,:,isoil,iday),var_read_1d)
[4136]189                h2o_soildensity_avg(:,isoil,islope) = h2o_soildensity_avg(:,isoil,islope) + var_read_1d
[4065]190            end do
191        end do
[4138]192        do isoil = nsoil_PCM + 1,nsoil
193            h2o_soildensity_avg(:,isoil,islope) = h2o_soildensity_avg(:,nsoil_PCM,islope) ! Explicit initialization because dimension with size nsoil > nsoil_PCM
194        end do
[4065]195    end do
[4136]196    h2o_soildensity_avg(:,:,:) = h2o_soildensity_avg(:,:,:)/real(nday,dp)
[4065]197end if
[4157]198deallocate(var_read_1d,var_read_3d,var_read_4d)
[3977]199
200! Close the NetCDF file of XIOS outputs
[4065]201call close_nc(xios_day_name2)
[3977]202
203!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[3983]204! Compute frost from yearly minima
[4147]205where (minPCM_h2ofrost(:,:,:) < 0._dp) minPCM_h2ofrost(:,:,:) = 0._dp ! Enforcing positivity
206where (minPCM_co2frost(:,:,:) < 0._dp) minPCM_co2frost(:,:,:) = 0._dp ! Enforcing positivity
[4071]207call compute_frost4PCM(minPCM_h2ofrost(:,:,2),minPCM_co2frost(:,:,2))
[3983]208
[3989]209END SUBROUTINE load_xios_data
210!=======================================================================
[3977]211
[3989]212END MODULE xios_data
Note: See TracBrowser for help on using the repository browser.