| 1 | MODULE 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 | ! ------------ |
|---|
| 18 | use numerics, only: dp, di, k4 |
|---|
| 19 | |
|---|
| 20 | ! DECLARATION |
|---|
| 21 | ! ----------- |
|---|
| 22 | implicit none |
|---|
| 23 | |
|---|
| 24 | contains |
|---|
| 25 | !+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
|---|
| 26 | |
|---|
| 27 | !======================================================================= |
|---|
| 28 | SUBROUTINE 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 | ! ------------ |
|---|
| 46 | use geometry, only: ngrid, nslope, nsoil, nsoil_PCM, nday, lonlat2vect, nlon, nlat |
|---|
| 47 | use io_netcdf, only: xios_day_name2, xios_yr_name1, xios_yr_name2, open_nc, close_nc, get_var_nc, get_dim_nc |
|---|
| 48 | use soil, only: do_soil |
|---|
| 49 | use frost, only: compute_frost4PCM |
|---|
| 50 | use stoppage, only: stop_clean |
|---|
| 51 | use display, only: print_msg |
|---|
| 52 | use utility, only: real2str |
|---|
| 53 | |
|---|
| 54 | ! DECLARATION |
|---|
| 55 | ! ----------- |
|---|
| 56 | implicit none |
|---|
| 57 | |
|---|
| 58 | ! ARGUMENTS |
|---|
| 59 | ! --------- |
|---|
| 60 | real(dp), dimension(:), intent(out) :: ps_avg |
|---|
| 61 | real(dp), dimension(:,:), intent(out) :: tsurf_avg, tsurf_avg_yr1, h2o_surfdensity_avg, ps_ts, q_h2o_ts, q_co2_ts |
|---|
| 62 | real(dp), dimension(:,:,:), intent(out) :: tsoil_avg, h2o_soildensity_avg |
|---|
| 63 | real(dp), dimension(:,:,:,:), intent(out) :: tsoil_ts |
|---|
| 64 | real(dp), dimension(:,:,:), intent(out) :: minPCM_h2operice, minPCM_co2perice, minPCM_h2ofrost, minPCM_co2frost |
|---|
| 65 | |
|---|
| 66 | ! LOCAL VARIABLES |
|---|
| 67 | ! --------------- |
|---|
| 68 | logical(k4) :: here |
|---|
| 69 | integer(di) :: islope, isoil, iday |
|---|
| 70 | real(dp), dimension(:,:), allocatable :: var_read_2d |
|---|
| 71 | real(dp), dimension(:,:,:), allocatable :: var_read_3d |
|---|
| 72 | real(dp), dimension(:,:,:,:), allocatable :: var_read_4d |
|---|
| 73 | character(:), allocatable :: num ! To read slope variables |
|---|
| 74 | real(dp), dimension(ngrid,nsoil,nslope,nday) :: h2o_soildensity_ts |
|---|
| 75 | ! CODE |
|---|
| 76 | ! ---- |
|---|
| 77 | ! Initialization |
|---|
| 78 | minPCM_h2operice(:,:,:) = 0._dp |
|---|
| 79 | minPCM_co2perice(:,:,:) = 0._dp |
|---|
| 80 | minPCM_h2ofrost(:,:,:) = 0._dp |
|---|
| 81 | minPCM_co2frost(:,:,:) = 0._dp |
|---|
| 82 | if (nslope == 1) then ! No slope |
|---|
| 83 | allocate(character(0) :: num) |
|---|
| 84 | else ! Using slopes |
|---|
| 85 | allocate(character(8) :: num) |
|---|
| 86 | end if |
|---|
| 87 | |
|---|
| 88 | !~~~~~~~~~~~~~~~~~~~~~~~~ Year 1 - Yearly data ~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 89 | inquire(file = xios_yr_name1,exist = here) |
|---|
| 90 | if (.not. here) call stop_clean(__FILE__,__LINE__,'cannot find required file "'//xios_yr_name1//'"!',1) |
|---|
| 91 | |
|---|
| 92 | ! Open the NetCDF file of XIOS outputs |
|---|
| 93 | call print_msg('> Reading "'//xios_yr_name1//'"') |
|---|
| 94 | call open_nc(xios_yr_name1,'read') |
|---|
| 95 | |
|---|
| 96 | ! Allocate and read the variables |
|---|
| 97 | allocate(var_read_2d(nlon,nlat),var_read_3d(nlon,nlat,nsoil_PCM)) |
|---|
| 98 | do islope = 1,nslope |
|---|
| 99 | if (nslope /= 1) then |
|---|
| 100 | num = ' ' |
|---|
| 101 | write(num,'(i2.2)') islope |
|---|
| 102 | num = '_slope'//num |
|---|
| 103 | end if |
|---|
| 104 | call get_var_nc('co2ice'//num,var_read_2d) ; call lonlat2vect(nlon,nlat,ngrid,var_read_2d,minPCM_co2frost(:,islope,1)) |
|---|
| 105 | call get_var_nc('h2o_ice_s'//num,var_read_2d) ; call lonlat2vect(nlon,nlat,ngrid,var_read_2d,minPCM_h2ofrost(:,islope,1)) |
|---|
| 106 | call get_var_nc('watercap'//num,var_read_2d) ; call lonlat2vect(nlon,nlat,ngrid,var_read_2d,minPCM_h2operice(:,islope,1)) |
|---|
| 107 | call get_var_nc('perennial_co2ice'//num,var_read_2d); call lonlat2vect(nlon,nlat,ngrid,var_read_2d,minPCM_co2perice(:,islope,1)) |
|---|
| 108 | call get_var_nc('tsurf'//num,var_read_2d) ; call lonlat2vect(nlon,nlat,ngrid,var_read_2d,tsurf_avg_yr1(:,islope)) |
|---|
| 109 | end do |
|---|
| 110 | |
|---|
| 111 | ! Close the NetCDF file of XIOS outputs |
|---|
| 112 | call close_nc(xios_yr_name1) |
|---|
| 113 | |
|---|
| 114 | !~~~~~~~~~~~~~~~~~~~~~~~~ Year 2 - Yearly data ~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 115 | inquire(file = xios_yr_name2,exist = here) |
|---|
| 116 | if (.not. here) call stop_clean(__FILE__,__LINE__,'cannot find required file "'//xios_yr_name2//'"!',1) |
|---|
| 117 | ! Open the NetCDF file of XIOS outputs |
|---|
| 118 | call print_msg('> Reading "'//xios_yr_name2//'"') |
|---|
| 119 | call open_nc(xios_yr_name2,'read') |
|---|
| 120 | |
|---|
| 121 | ! Allocate and read the variables |
|---|
| 122 | call get_var_nc('ps',var_read_2d); call lonlat2vect(nlon,nlat,ngrid,var_read_2d,ps_avg) |
|---|
| 123 | do islope = 1,nslope |
|---|
| 124 | if (nslope /= 1) then |
|---|
| 125 | num=' ' |
|---|
| 126 | write(num,'(i2.2)') islope |
|---|
| 127 | num = '_slope'//num |
|---|
| 128 | end if |
|---|
| 129 | call get_var_nc('tsurf'//num,var_read_2d) ; call lonlat2vect(nlon,nlat,ngrid,var_read_2d,tsurf_avg(:,islope)) |
|---|
| 130 | call get_var_nc('co2ice'//num,var_read_2d) ; call lonlat2vect(nlon,nlat,ngrid,var_read_2d,minPCM_co2frost(:,islope,2)) |
|---|
| 131 | call get_var_nc('h2o_ice_s'//num,var_read_2d) ; call lonlat2vect(nlon,nlat,ngrid,var_read_2d,minPCM_h2ofrost(:,islope,2)) |
|---|
| 132 | call get_var_nc('watercap'//num,var_read_2d) ; call lonlat2vect(nlon,nlat,ngrid,var_read_2d,minPCM_h2operice(:,islope,2)) |
|---|
| 133 | call get_var_nc('perennial_co2ice'//num,var_read_2d); call lonlat2vect(nlon,nlat,ngrid,var_read_2d,minPCM_co2perice(:,islope,2)) |
|---|
| 134 | if (do_soil) then |
|---|
| 135 | call get_var_nc('soiltemp'//num,var_read_3d) |
|---|
| 136 | do isoil = 1,nsoil_PCM |
|---|
| 137 | call lonlat2vect(nlon,nlat,ngrid,var_read_3d(:,:,isoil),tsoil_avg(:,isoil,islope)) |
|---|
| 138 | end do |
|---|
| 139 | do isoil = nsoil_PCM + 1,nsoil |
|---|
| 140 | tsoil_avg(:,isoil,islope) = tsoil_avg(:,nsoil_PCM,islope) ! Explicit initialization because dimension with size nsoil > nsoil_PCM |
|---|
| 141 | end do |
|---|
| 142 | call get_var_nc('waterdensity_surface'//num,var_read_2d); call lonlat2vect(nlon,nlat,ngrid,var_read_2d,h2o_surfdensity_avg(:,islope)) |
|---|
| 143 | end if |
|---|
| 144 | end do |
|---|
| 145 | ! Close the NetCDF file of XIOS outputs |
|---|
| 146 | call close_nc(xios_yr_name2) |
|---|
| 147 | |
|---|
| 148 | !~~~~~~~~~~~~~~~~~~~~~~~~~ Year 2 - Daily data ~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 149 | inquire(file = xios_day_name2,exist = here) |
|---|
| 150 | if (.not. here) call stop_clean(__FILE__,__LINE__,'cannot find required file "'//xios_day_name2//'"!',1) |
|---|
| 151 | ! Open the NetCDF file of XIOS outputs |
|---|
| 152 | call print_msg('> Reading "'//xios_day_name2//'"') |
|---|
| 153 | call open_nc(xios_day_name2,'read') |
|---|
| 154 | |
|---|
| 155 | ! Allocate and read the variables |
|---|
| 156 | deallocate(var_read_2d,var_read_3d) |
|---|
| 157 | allocate(var_read_3d(nlon,nlat,nday),var_read_4d(nlon,nlat,nsoil_PCM,nday)) |
|---|
| 158 | call get_var_nc('ps',var_read_3d) |
|---|
| 159 | do iday = 1,nday |
|---|
| 160 | call lonlat2vect(nlon,nlat,ngrid,var_read_3d(:,:,iday),ps_ts(:,iday)) |
|---|
| 161 | end do |
|---|
| 162 | call get_var_nc('h2o_layer1',var_read_3d) |
|---|
| 163 | do iday = 1,nday |
|---|
| 164 | call lonlat2vect(nlon,nlat,ngrid,var_read_3d(:,:,iday),q_h2o_ts(:,iday)) |
|---|
| 165 | end do |
|---|
| 166 | call get_var_nc('co2_layer1',var_read_3d) |
|---|
| 167 | do iday = 1,nday |
|---|
| 168 | call lonlat2vect(nlon,nlat,ngrid,var_read_3d(:,:,iday),q_co2_ts(:,iday)) |
|---|
| 169 | end do |
|---|
| 170 | if (do_soil) then |
|---|
| 171 | do islope = 1,nslope |
|---|
| 172 | if (nslope /= 1) then |
|---|
| 173 | num=' ' |
|---|
| 174 | write(num,'(i2.2)') islope |
|---|
| 175 | num = '_slope'//num |
|---|
| 176 | end if |
|---|
| 177 | call get_var_nc('soiltemp'//num,var_read_4d) |
|---|
| 178 | do iday = 1,nday |
|---|
| 179 | do isoil = 1,nsoil_PCM |
|---|
| 180 | call lonlat2vect(nlon,nlat,ngrid,var_read_4d(:,:,isoil,iday),tsoil_ts(:,isoil,islope,iday)) |
|---|
| 181 | end do |
|---|
| 182 | do isoil = nsoil_PCM + 1,nsoil |
|---|
| 183 | tsoil_ts(:,isoil,islope,iday) = tsoil_ts(:,nsoil_PCM,islope,iday) ! Explicit initialization because dimension with size nsoil > nsoil_PCM |
|---|
| 184 | end do |
|---|
| 185 | end do |
|---|
| 186 | call get_var_nc('waterdensity_soil'//num,var_read_4d) |
|---|
| 187 | do iday = 1,nday |
|---|
| 188 | do isoil = 1,nsoil_PCM |
|---|
| 189 | call lonlat2vect(nlon,nlat,ngrid,var_read_4d(:,:,isoil,iday),h2o_soildensity_ts(:,isoil,islope,iday)) |
|---|
| 190 | end do |
|---|
| 191 | do isoil = nsoil_PCM + 1,nsoil |
|---|
| 192 | h2o_soildensity_ts(:,isoil,islope,iday) = h2o_soildensity_ts(:,nsoil_PCM,islope,iday) ! Explicit initialization because dimension with size nsoil > nsoil_PCM |
|---|
| 193 | end do |
|---|
| 194 | end do |
|---|
| 195 | end do |
|---|
| 196 | h2o_soildensity_avg(:,:,:) = sum(h2o_soildensity_ts,4)/nday |
|---|
| 197 | end if |
|---|
| 198 | deallocate(var_read_3d,var_read_4d,num) |
|---|
| 199 | |
|---|
| 200 | ! Close the NetCDF file of XIOS outputs |
|---|
| 201 | call close_nc(xios_day_name2) |
|---|
| 202 | |
|---|
| 203 | !~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 204 | ! Compute frost from yearly minima |
|---|
| 205 | call compute_frost4PCM(minPCM_h2ofrost(:,:,2),minPCM_co2frost(:,:,2)) |
|---|
| 206 | |
|---|
| 207 | END SUBROUTINE load_xios_data |
|---|
| 208 | !======================================================================= |
|---|
| 209 | |
|---|
| 210 | END MODULE xios_data |
|---|