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

Last change on this file since 4157 was 4157, checked in by jbclement, 12 days 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
Line 
1MODULE xios_data
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!-----------------------------------------------------------------------
15
16! DEPENDENCIES
17! ------------
18use numerics, only: dp, di, k4
19
20! DECLARATION
21! -----------
22implicit none
23
24contains
25!+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
26
27!=======================================================================
28SUBROUTINE load_xios_data(ps_avg,ps_ts,tsurf_avg,tsurf_avg_yr1,tsoil_avg,tsoil_ts,h2o_surfdensity_avg,h2o_soildensity_avg, &
29                          q_h2o_ts,q_co2_ts,minPCM_h2operice,minPCM_co2perice,minPCM_h2ofrost,minPCM_co2frost)
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!-----------------------------------------------------------------------
43
44! DEPENDENCIES
45! ------------
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
51use display,    only: print_msg, LVL_NFO
52use utility,    only: real2str
53
54! DECLARATION
55! -----------
56implicit none
57
58! ARGUMENTS
59! ---------
60real(dp), dimension(:),       intent(out) :: ps_avg
61real(dp), dimension(:,:),     intent(out) :: tsurf_avg, tsurf_avg_yr1, h2o_surfdensity_avg, ps_ts, q_h2o_ts, q_co2_ts
62real(dp), dimension(:,:,:),   intent(out) :: tsoil_avg, h2o_soildensity_avg
63real(dp), dimension(:,:,:,:), intent(out) :: tsoil_ts
64real(dp), dimension(:,:,:),   intent(out) :: minPCM_h2operice, minPCM_co2perice, minPCM_h2ofrost, minPCM_co2frost
65
66! LOCAL VARIABLES
67! ---------------
68logical(k4)                                  :: here
69integer(di)                                  :: islope, isoil, iday
70real(dp), dimension(:),       allocatable    :: var_read_1d
71real(dp), dimension(:,:),     allocatable    :: var_read_2d
72real(dp), dimension(:,:,:),   allocatable    :: var_read_3d
73real(dp), dimension(:,:,:,:), allocatable    :: var_read_4d
74character(8)                                 :: num ! Slope suffix to read variables
75
76! CODE
77! ----
78! Initialization
79minPCM_h2operice(:,:,:) = 0._dp
80minPCM_co2perice(:,:,:) = 0._dp
81minPCM_h2ofrost(:,:,:) = 0._dp
82minPCM_co2frost(:,:,:) = 0._dp
83num = ''
84
85!~~~~~~~~~~~~~~~~~~~~~~~~ Year 1 - Yearly data ~~~~~~~~~~~~~~~~~~~~~~~~~
86inquire(file = xios_yr_name1,exist = here)
87if (.not. here) call stop_clean(__FILE__,__LINE__,'cannot find required file "'//xios_yr_name1//'"!',1)
88
89! Open the NetCDF file of XIOS outputs
90call print_msg('> Reading "'//xios_yr_name1//'"',LVL_NFO)
91call open_nc(xios_yr_name1,'read')
92
93! Allocate and read the variables
94allocate(var_read_2d(nlon,nlat),var_read_3d(nlon,nlat,nsoil_PCM))
95do islope = 1,nslope
96    if (nslope == 1) then
97        num = ''
98    else
99        write(num,'("_slope",i2.2)') islope
100    end if
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))
106end do
107
108! Close the NetCDF file of XIOS outputs
109call close_nc(xios_yr_name1)
110
111!~~~~~~~~~~~~~~~~~~~~~~~~ Year 2 - Yearly data ~~~~~~~~~~~~~~~~~~~~~~~~~
112inquire(file = xios_yr_name2,exist = here)
113if (.not. here) call stop_clean(__FILE__,__LINE__,'cannot find required file "'//xios_yr_name2//'"!',1)
114! Open the NetCDF file of XIOS outputs
115call print_msg('> Reading "'//xios_yr_name2//'"',LVL_NFO)
116call open_nc(xios_yr_name2,'read')
117
118! Allocate and read the variables
119call get_var_nc('ps',var_read_2d); call lonlat2vect(var_read_2d,ps_avg)
120do islope = 1,nslope
121    if (nslope == 1) then
122        num = ''
123    else
124        write(num,'("_slope",i2.2)') islope
125    end if
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))
131    if (do_soil) then
132        call get_var_nc('soiltemp'//trim(num),var_read_3d)
133        do isoil = 1,nsoil_PCM
134            call lonlat2vect(var_read_3d(:,:,isoil),tsoil_avg(:,isoil,islope))
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
139        call get_var_nc('waterdensity_surface'//trim(num),var_read_2d); call lonlat2vect(var_read_2d,h2o_surfdensity_avg(:,islope))
140    end if
141end do
142
143! Close the NetCDF file of XIOS outputs
144call close_nc(xios_yr_name2)
145
146!~~~~~~~~~~~~~~~~~~~~~~~~~ Year 2 - Daily data ~~~~~~~~~~~~~~~~~~~~~~~~~
147inquire(file = xios_day_name2,exist = here)
148if (.not. here) call stop_clean(__FILE__,__LINE__,'cannot find required file "'//xios_day_name2//'"!',1)
149! Open the NetCDF file of XIOS outputs
150call print_msg('> Reading "'//xios_day_name2//'"',LVL_NFO)
151call open_nc(xios_day_name2,'read')
152
153! Allocate and read the variables
154deallocate(var_read_2d,var_read_3d)
155allocate(var_read_1d(ngrid),var_read_3d(nlon,nlat,nday),var_read_4d(nlon,nlat,nsoil_PCM,nday))
156call get_var_nc('ps',var_read_3d)
157do iday = 1,nday
158    call lonlat2vect(var_read_3d(:,:,iday),ps_ts(:,iday))
159end do
160call get_var_nc('h2o_layer1',var_read_3d)
161do iday = 1,nday
162    call lonlat2vect(var_read_3d(:,:,iday),q_h2o_ts(:,iday))
163end do
164call get_var_nc('co2_layer1',var_read_3d)
165do iday = 1,nday
166    call lonlat2vect(var_read_3d(:,:,iday),q_co2_ts(:,iday))
167end do
168if (do_soil) then
169    h2o_soildensity_avg(:,:,:) = 0._dp
170    do islope = 1,nslope
171        if (nslope == 1) then
172            num = ''
173        else
174            write(num,'("_slope",i2.2)') islope
175        end if
176        call get_var_nc('soiltemp'//trim(num),var_read_4d)
177        do iday = 1,nday
178            do isoil = 1,nsoil_PCM
179                call lonlat2vect(var_read_4d(:,:,isoil,iday),tsoil_ts(:,isoil,islope,iday))
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
185        call get_var_nc('waterdensity_soil'//trim(num),var_read_4d)
186        do iday = 1,nday
187            do isoil = 1,nsoil_PCM
188                call lonlat2vect(var_read_4d(:,:,isoil,iday),var_read_1d)
189                h2o_soildensity_avg(:,isoil,islope) = h2o_soildensity_avg(:,isoil,islope) + var_read_1d
190            end do
191        end do
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
195    end do
196    h2o_soildensity_avg(:,:,:) = h2o_soildensity_avg(:,:,:)/real(nday,dp)
197end if
198deallocate(var_read_1d,var_read_3d,var_read_4d)
199
200! Close the NetCDF file of XIOS outputs
201call close_nc(xios_day_name2)
202
203!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
204! Compute frost from yearly minima
205where (minPCM_h2ofrost(:,:,:) < 0._dp) minPCM_h2ofrost(:,:,:) = 0._dp ! Enforcing positivity
206where (minPCM_co2frost(:,:,:) < 0._dp) minPCM_co2frost(:,:,:) = 0._dp ! Enforcing positivity
207call compute_frost4PCM(minPCM_h2ofrost(:,:,2),minPCM_co2frost(:,:,2))
208
209END SUBROUTINE load_xios_data
210!=======================================================================
211
212END MODULE xios_data
Note: See TracBrowser for help on using the repository browser.