1 | MODULE read_data_PCM_mod |
---|
2 | |
---|
3 | use netcdf, only: nf90_open, NF90_NOWRITE, nf90_noerr, nf90_strerror, & |
---|
4 | nf90_get_var, nf90_inq_varid, nf90_inq_dimid, & |
---|
5 | nf90_inquire_dimension, nf90_close |
---|
6 | |
---|
7 | implicit none |
---|
8 | |
---|
9 | character(13), parameter :: modname = 'read_data_PCM' |
---|
10 | character(256) :: msg ! for reading |
---|
11 | integer :: fID, vID ! for reading |
---|
12 | |
---|
13 | !======================================================================= |
---|
14 | contains |
---|
15 | !======================================================================= |
---|
16 | |
---|
17 | SUBROUTINE read_data_PCM(filename,timelen,iim_input,jjm_input,ngrid,nslope,vmr_co2_PCM_phys,ps_timeseries, & |
---|
18 | min_co2_ice,min_h2o_ice,tsurf_avg,tsoil_avg,tsurf_PCM,tsoil_PCM,q_co2,q_h2o, & |
---|
19 | watersurf_density_avg,watersoil_density) |
---|
20 | use comsoil_h, only: nsoilmx |
---|
21 | use comsoil_h_PEM, only: soil_pem |
---|
22 | use constants_marspem_mod, only: m_co2, m_noco2 |
---|
23 | |
---|
24 | implicit none |
---|
25 | |
---|
26 | !======================================================================= |
---|
27 | ! |
---|
28 | ! Purpose: Read initial confitions file from the PCM |
---|
29 | ! |
---|
30 | ! Authors: RV & LL |
---|
31 | !======================================================================= |
---|
32 | |
---|
33 | include "dimensions.h" |
---|
34 | |
---|
35 | !======================================================================= |
---|
36 | ! Arguments: |
---|
37 | character(*), intent(in) :: filename ! File name |
---|
38 | integer, intent(in) :: timelen ! number of times stored in the file |
---|
39 | integer, intent(in) :: iim_input, jjm_input, ngrid, nslope ! number of points in the lat x lon dynamical grid, number of subgrid slopes |
---|
40 | ! Ouputs |
---|
41 | real, dimension(ngrid,nslope), intent(out) :: min_co2_ice ! Minimum of co2 ice per slope of the year [kg/m^2] |
---|
42 | real, dimension(ngrid,nslope), intent(out) :: min_h2o_ice ! Minimum of h2o ice per slope of the year [kg/m^2] |
---|
43 | real, dimension(ngrid,timelen), intent(out) :: vmr_co2_PCM_phys ! Physics x Times co2 volume mixing ratio retrieve from the PCM [m^3/m^3] |
---|
44 | real, dimension(ngrid,timelen), intent(out) :: ps_timeseries ! Surface Pressure [Pa] |
---|
45 | real, dimension(ngrid,timelen), intent(out) :: q_co2 ! CO2 mass mixing ratio in the first layer [kg/m^3] |
---|
46 | real, dimension(ngrid,timelen), intent(out) :: q_h2o ! H2O mass mixing ratio in the first layer [kg/m^3] |
---|
47 | !SOIL |
---|
48 | real, dimension(ngrid,nslope), intent(out) :: tsurf_avg ! Average surface temperature of the concatenated file [K] |
---|
49 | real, dimension(ngrid,nsoilmx,nslope), intent(out) :: tsoil_avg ! Average soil temperature of the concatenated file [K] |
---|
50 | real, dimension(ngrid,nslope,timelen), intent(out) :: tsurf_PCM ! Surface temperature of the concatenated file, time series [K] |
---|
51 | real, dimension(ngrid,nsoilmx,nslope,timelen), intent(out) :: tsoil_PCM ! Soil temperature of the concatenated file, time series [K] |
---|
52 | real, dimension(ngrid,nslope), intent(out) :: watersurf_density_avg ! Water density at the surface [kg/m^3] |
---|
53 | real, dimension(ngrid,nsoilmx,nslope,timelen), intent(out) :: watersoil_density ! Water density in the soil layer, time series [kg/m^3] |
---|
54 | !======================================================================= |
---|
55 | ! Local Variables |
---|
56 | integer :: i, j, l, t ! loop variables |
---|
57 | real :: A, B, mmean ! Molar Mass of co2 and no co2, A;B intermediate variables to compute the mean molar mass of the layer |
---|
58 | integer :: islope ! loop for variables |
---|
59 | character(2) :: num ! for reading sloped variables |
---|
60 | real, dimension(iim_input + 1,jjm_input + 1,nslope,timelen) :: h2o_ice_s_dyn ! h2o ice per slope of the concatenated file [kg/m^2] |
---|
61 | real, dimension(iim_input + 1,jjm_input + 1,nslope,timelen) :: watercap |
---|
62 | real, dimension(iim_input + 1,jjm_input + 1,nslope,timelen) :: perennial_co2ice |
---|
63 | real, dimension(iim_input + 1,jjm_input + 1,timelen) :: vmr_co2_PCM ! CO2 volume mixing ratio in the first layer [mol/m^3] |
---|
64 | real, dimension(iim_input + 1,jjm_input + 1,timelen) :: ps_PCM ! Surface Pressure [Pa] |
---|
65 | real, dimension(iim_input + 1,jjm_input + 1,nslope) :: min_co2_ice_dyn |
---|
66 | real, dimension(iim_input + 1,jjm_input + 1,nslope) :: min_h2o_ice_dyn |
---|
67 | real, dimension(iim_input + 1,jjm_input + 1,nslope) :: tsurf_avg_dyn ! Average surface temperature of the concatenated file [K] |
---|
68 | real, dimension(iim_input + 1,jjm_input + 1,nsoilmx,nslope) :: tsoil_avg_dyn ! Average soil temperature of the concatenated file [K] |
---|
69 | real, dimension(iim_input + 1,jjm_input + 1,nslope,timelen) :: tsurf_PCM_dyn ! Surface temperature of the concatenated file, time series [K] |
---|
70 | real, dimension(iim_input + 1,jjm_input + 1,nsoilmx,nslope,timelen) :: tsoil_PCM_dyn ! Soil temperature of the concatenated file, time series [K] |
---|
71 | real, dimension(iim_input + 1,jjm_input + 1,timelen) :: q_co2_dyn ! CO2 mass mixing ratio in the first layer [kg/m^3] |
---|
72 | real, dimension(iim_input + 1,jjm_input + 1,timelen) :: q_h2o_dyn ! H2O mass mixing ratio in the first layer [kg/m^3] |
---|
73 | real, dimension(iim_input + 1,jjm_input + 1,nslope,timelen) :: co2_ice_slope_dyn ! co2 ice amount per slope of the year [kg/m^2] |
---|
74 | real, dimension(iim_input + 1,jjm_input + 1,nslope,timelen) :: watersurf_density_dyn ! Water density at the surface, time series [kg/m^3] |
---|
75 | real, dimension(iim_input + 1,jjm_input + 1,nslope) :: watersurf_density_dyn_avg ! Water density at the surface, dynamic grid, yearly averaged [kg/m^3] |
---|
76 | real, dimension(iim_input + 1,jjm_input + 1,nsoilmx,nslope,timelen) :: watersoil_density_dyn ! Water density in the soil layer, time series [kg/m^3] |
---|
77 | !----------------------------------------------------------------------- |
---|
78 | ! Open the NetCDF file |
---|
79 | write(*,*) "Opening "//filename//"..." |
---|
80 | call error_msg(NF90_OPEN(filename,NF90_NOWRITE,fID),"open",filename) |
---|
81 | |
---|
82 | ! Download the data from the file |
---|
83 | call get_var3("ps",ps_PCM) |
---|
84 | write(*,*) "Data for surface pressure downloaded!" |
---|
85 | |
---|
86 | call get_var3("co2_layer1",q_co2_dyn) |
---|
87 | write(*,*) "Data for vmr co2 downloaded!" |
---|
88 | |
---|
89 | call get_var3("h2o_layer1",q_h2o_dyn) |
---|
90 | write(*,*) "Data for vmr h2o downloaded!" |
---|
91 | |
---|
92 | if (nslope == 1) then ! There is no slope |
---|
93 | call get_var3("co2ice",co2_ice_slope_dyn(:,:,1,:)) |
---|
94 | write(*,*) "Data for co2_ice downloaded!" |
---|
95 | |
---|
96 | call get_var3("h2o_ice_s",h2o_ice_s_dyn(:,:,1,:)) |
---|
97 | write(*,*) "Data for h2o_ice_s downloaded!" |
---|
98 | |
---|
99 | call get_var3("tsurf",tsurf_PCM_dyn(:,:,1,:)) |
---|
100 | write(*,*) "Data for tsurf downloaded!" |
---|
101 | |
---|
102 | #ifndef CPP_STD |
---|
103 | call get_var3("watercap",watercap(:,:,1,:)) |
---|
104 | write(*,*) "Data for watercap downloaded!" |
---|
105 | |
---|
106 | call get_var3("perennial_co2ice",perennial_co2ice(:,:,1,:)) |
---|
107 | write(*,*) "Data for perennial_co2ice downloaded!" |
---|
108 | |
---|
109 | if (soil_pem) then |
---|
110 | call get_var4("soiltemp",tsoil_PCM_dyn(:,:,:,1,:)) |
---|
111 | write(*,*) "Data for soiltemp downloaded!" |
---|
112 | |
---|
113 | call get_var4("waterdensity_soil",watersoil_density_dyn(:,:,:,1,:)) |
---|
114 | write(*,*) "Data for waterdensity_soil downloaded!" |
---|
115 | |
---|
116 | call get_var3("waterdensity_surface",watersurf_density_dyn(:,:,1,:)) |
---|
117 | write(*,*) "Data for waterdensity_surface downloaded!" |
---|
118 | endif !soil_pem |
---|
119 | #endif |
---|
120 | else ! We use slopes |
---|
121 | do islope = 1,nslope |
---|
122 | write(num,'(i2.2)') islope |
---|
123 | call get_var3("co2ice_slope"//num,co2_ice_slope_dyn(:,:,islope,:)) |
---|
124 | enddo |
---|
125 | write(*,*) "Data for co2_ice downloaded!" |
---|
126 | |
---|
127 | do islope = 1,nslope |
---|
128 | write(num,'(i2.2)') islope |
---|
129 | call get_var3("h2o_ice_s_slope"//num,h2o_ice_s_dyn(:,:,islope,:)) |
---|
130 | enddo |
---|
131 | write(*,*) "Data for h2o_ice_s downloaded!" |
---|
132 | |
---|
133 | do islope = 1,nslope |
---|
134 | write(num,'(i2.2)') islope |
---|
135 | call get_var3("tsurf_slope"//num,tsurf_PCM_dyn(:,:,islope,:)) |
---|
136 | enddo |
---|
137 | write(*,*) "Data for tsurf downloaded!" |
---|
138 | |
---|
139 | #ifndef CPP_STD |
---|
140 | do islope = 1,nslope |
---|
141 | write(num,'(i2.2)') islope |
---|
142 | call get_var3("watercap_slope"//num,watercap(:,:,islope,:)) |
---|
143 | enddo |
---|
144 | write(*,*) "Data for watercap downloaded!" |
---|
145 | |
---|
146 | do islope = 1,nslope |
---|
147 | write(num,'(i2.2)') islope |
---|
148 | call get_var3("perennial_co2ice_slope"//num,perennial_co2ice(:,:,islope,:)) |
---|
149 | enddo |
---|
150 | write(*,*) "Data for perennial_co2ice downloaded!" |
---|
151 | |
---|
152 | if (soil_pem) then |
---|
153 | do islope = 1,nslope |
---|
154 | write(num,'(i2.2)') islope |
---|
155 | call get_var4("soiltemp_slope"//num,tsoil_PCM_dyn(:,:,:,islope,:)) |
---|
156 | enddo |
---|
157 | write(*,*) "Data for soiltemp downloaded!" |
---|
158 | |
---|
159 | do islope = 1,nslope |
---|
160 | write(num,'(i2.2)') islope |
---|
161 | call get_var4("waterdensity_soil_slope"//num,watersoil_density_dyn(:,:,:,islope,:)) |
---|
162 | enddo |
---|
163 | write(*,*) "Data for waterdensity_soil downloaded!" |
---|
164 | |
---|
165 | do islope = 1,nslope |
---|
166 | write(num,'(i2.2)') islope |
---|
167 | call get_var3("waterdensity_surface"//num,watersurf_density_dyn(:,:,islope,:)) |
---|
168 | enddo |
---|
169 | write(*,*) "Data for waterdensity_surface downloaded!" |
---|
170 | endif !soil_pem |
---|
171 | #endif |
---|
172 | endif |
---|
173 | |
---|
174 | ! Close the NetCDF file |
---|
175 | write(*,*) "Closing "//filename//"..." |
---|
176 | call error_msg(nf90_close(fID),"close",filename) |
---|
177 | |
---|
178 | ! Compute the minimum over the year for each point |
---|
179 | write(*,*) "Computing the min of h2o_ice..." |
---|
180 | where (h2o_ice_s_dyn < 0.) h2o_ice_s_dyn = 0. |
---|
181 | min_h2o_ice_dyn = minval(h2o_ice_s_dyn + watercap,4) |
---|
182 | write(*,*) "Computing the min of co2_ice..." |
---|
183 | where (co2_ice_slope_dyn < 0.) co2_ice_slope_dyn = 0. |
---|
184 | min_co2_ice_dyn = minval(co2_ice_slope_dyn + perennial_co2ice,4) |
---|
185 | |
---|
186 | ! Compute averages over the year for each point |
---|
187 | write(*,*) "Computing the average of tsurf..." |
---|
188 | tsurf_avg_dyn = sum(tsurf_PCM_dyn,4)/timelen |
---|
189 | |
---|
190 | if (soil_pem) then |
---|
191 | write(*,*) "Computing average of tsoil..." |
---|
192 | tsoil_avg_dyn = sum(tsoil_PCM_dyn,5)/timelen |
---|
193 | write(*,*) "Computing average of waterdensity_surface..." |
---|
194 | watersurf_density_dyn_avg = sum(watersurf_density_dyn,4)/timelen |
---|
195 | endif |
---|
196 | |
---|
197 | ! By definition, we get rid of the negative values |
---|
198 | A = (1./m_co2 - 1./m_noco2) |
---|
199 | B = 1./m_noco2 |
---|
200 | do i = 1,iim + 1 |
---|
201 | do j = 1,jjm_input + 1 |
---|
202 | do t = 1, timelen |
---|
203 | if (q_co2_dyn(i,j,t) < 0) then |
---|
204 | q_co2_dyn(i,j,t) = 1.e-10 |
---|
205 | else if (q_co2_dyn(i,j,t) > 1) then |
---|
206 | q_co2_dyn(i,j,t) = 1. |
---|
207 | endif |
---|
208 | if (q_h2o_dyn(i,j,t) < 0) then |
---|
209 | q_h2o_dyn(i,j,t) = 1.e-10 |
---|
210 | else if (q_h2o_dyn(i,j,t) > 1) then |
---|
211 | q_h2o_dyn(i,j,t) = 1. |
---|
212 | endif |
---|
213 | mmean = 1/(A*q_co2_dyn(i,j,t) + B) |
---|
214 | vmr_co2_PCM(i,j,t) = q_co2_dyn(i,j,t)*mmean/m_co2 |
---|
215 | enddo |
---|
216 | enddo |
---|
217 | enddo |
---|
218 | |
---|
219 | #ifndef CPP_1D |
---|
220 | call gr_dyn_fi(timelen,iim_input + 1,jjm_input + 1,ngrid,vmr_co2_PCM,vmr_co2_PCM_phys) |
---|
221 | call gr_dyn_fi(timelen,iim_input + 1,jjm_input + 1,ngrid,ps_PCM,ps_timeseries) |
---|
222 | call gr_dyn_fi(timelen,iim_input + 1,jjm_input + 1,ngrid,q_co2_dyn,q_co2) |
---|
223 | call gr_dyn_fi(timelen,iim_input + 1,jjm_input + 1,ngrid,q_h2o_dyn,q_h2o) |
---|
224 | |
---|
225 | do islope = 1,nslope |
---|
226 | call gr_dyn_fi(1,iim_input + 1,jjm_input + 1,ngrid,min_co2_ice_dyn(:,:,islope),min_co2_ice(:,islope)) |
---|
227 | call gr_dyn_fi(1,iim_input + 1,jjm_input + 1,ngrid,min_h2o_ice_dyn(:,:,islope),min_h2o_ice(:,islope)) |
---|
228 | if (soil_pem) then |
---|
229 | do l = 1,nsoilmx |
---|
230 | call gr_dyn_fi(1,iim_input + 1,jjm_input + 1,ngrid,tsoil_avg_dyn(:,:,l,islope),tsoil_avg(:,l,islope)) |
---|
231 | do t = 1,timelen |
---|
232 | call gr_dyn_fi(1,iim_input + 1,jjm_input + 1,ngrid,tsoil_PCM_dyn(:,:,l,islope,t),tsoil_PCM(:,l,islope,t)) |
---|
233 | call gr_dyn_fi(1,iim_input + 1,jjm_input + 1,ngrid,watersoil_density_dyn(:,:,l,islope,t),watersoil_density(:,l,islope,t)) |
---|
234 | enddo |
---|
235 | enddo |
---|
236 | call gr_dyn_fi(1,iim_input + 1,jjm_input + 1,ngrid,watersurf_density_dyn_avg(:,:,islope),watersurf_density_avg(:,islope)) |
---|
237 | endif ! soil_pem |
---|
238 | do t = 1,timelen |
---|
239 | call gr_dyn_fi(1,iim_input + 1,jjm_input + 1,ngrid,tsurf_PCM_dyn(:,:,islope,t),tsurf_PCM(:,islope,t)) |
---|
240 | enddo |
---|
241 | enddo |
---|
242 | |
---|
243 | call gr_dyn_fi(nslope,iim_input + 1,jjm_input + 1,ngrid,tsurf_avg_dyn,tsurf_avg) |
---|
244 | #else |
---|
245 | vmr_co2_PCM_phys(1,:) = vmr_co2_PCM(1,1,:) |
---|
246 | ps_timeseries(1,:) = ps_PCM(1,1,:) |
---|
247 | q_co2(1,:) = q_co2_dyn(1,1,:) |
---|
248 | q_h2o(1,:) = q_h2o_dyn(1,1,:) |
---|
249 | min_co2_ice(1,:) = min_co2_ice_dyn(1,1,:) |
---|
250 | min_h2o_ice(1,:) = min_h2o_ice_dyn(1,1,:) |
---|
251 | if (soil_pem) then |
---|
252 | tsoil_avg(1,:,:) = tsoil_avg_dyn(1,1,:,:) |
---|
253 | tsoil_PCM(1,:,:,:) = tsoil_PCM_dyn(1,1,:,:,:) |
---|
254 | watersoil_density(1,:,:,:) = watersoil_density_dyn(1,1,:,:,:) |
---|
255 | watersurf_density_avg(1,:) = watersurf_density_dyn_avg(1,1,:) |
---|
256 | endif ! soil_pem |
---|
257 | tsurf_PCM(1,:,:) = tsurf_PCM_dyn(1,1,:,:) |
---|
258 | tsurf_avg(1,:) = tsurf_avg_dyn(1,1,:) |
---|
259 | #endif |
---|
260 | |
---|
261 | END SUBROUTINE read_data_PCM |
---|
262 | |
---|
263 | !======================================================================= |
---|
264 | |
---|
265 | SUBROUTINE check_dim(n1,n2,str1,str2) |
---|
266 | |
---|
267 | implicit none |
---|
268 | |
---|
269 | integer, intent(in) :: n1, n2 |
---|
270 | character(len = *), intent(in) :: str1, str2 |
---|
271 | |
---|
272 | character(256) :: s1, s2 |
---|
273 | |
---|
274 | if (n1 /= n2) then |
---|
275 | s1 = 'value of '//trim(str1)//' =' |
---|
276 | s2 = ' read in starting file differs from parametrized '//trim(str2)//' =' |
---|
277 | write(msg,'(10x,a,i4,2x,a,i4)')trim(s1),n1,trim(s2),n2 |
---|
278 | call abort_gcm(trim(modname),trim(msg),1) |
---|
279 | endif |
---|
280 | |
---|
281 | END SUBROUTINE check_dim |
---|
282 | |
---|
283 | !======================================================================= |
---|
284 | |
---|
285 | SUBROUTINE get_var1(var,v) |
---|
286 | |
---|
287 | implicit none |
---|
288 | |
---|
289 | character(len = *), intent(in) :: var |
---|
290 | real, dimension(:), intent(out) :: v |
---|
291 | |
---|
292 | call error_msg(NF90_INQ_VARID(fID,var,vID),"inq",var) |
---|
293 | call error_msg(NF90_GET_VAR(fID,vID,v),"get",var) |
---|
294 | |
---|
295 | END SUBROUTINE get_var1 |
---|
296 | |
---|
297 | !======================================================================= |
---|
298 | |
---|
299 | SUBROUTINE get_var3(var,v) ! on U grid |
---|
300 | |
---|
301 | implicit none |
---|
302 | |
---|
303 | character(len = *), intent(in) :: var |
---|
304 | real, dimension(:,:,:), intent(out) :: v |
---|
305 | |
---|
306 | call error_msg(NF90_INQ_VARID(fID,var,vID),"inq",var) |
---|
307 | call error_msg(NF90_GET_VAR(fID,vID,v),"get",var) |
---|
308 | |
---|
309 | END SUBROUTINE get_var3 |
---|
310 | |
---|
311 | !======================================================================= |
---|
312 | |
---|
313 | SUBROUTINE get_var4(var,v) |
---|
314 | |
---|
315 | implicit none |
---|
316 | |
---|
317 | character(len = *), intent(in) :: var |
---|
318 | real, dimension(:,:,:,:), intent(out) :: v |
---|
319 | |
---|
320 | call error_msg(NF90_INQ_VARID(fID,var,vID),"inq",var) |
---|
321 | call error_msg(NF90_GET_VAR(fID,vID,v),"get",var) |
---|
322 | |
---|
323 | END SUBROUTINE get_var4 |
---|
324 | |
---|
325 | !======================================================================= |
---|
326 | |
---|
327 | SUBROUTINE error_msg(ierr,typ,nam) |
---|
328 | |
---|
329 | implicit none |
---|
330 | |
---|
331 | integer, intent(in) :: ierr !--- NetCDF ERROR CODE |
---|
332 | character(len = *), intent(in) :: typ !--- TYPE OF OPERATION |
---|
333 | character(len = *), intent(in) :: nam !--- FIELD/FILE NAME |
---|
334 | |
---|
335 | if (ierr == nf90_noerr) return |
---|
336 | select case(typ) |
---|
337 | case('inq'); msg="Field <"//trim(nam)//"> is missing" |
---|
338 | case('get'); msg="Reading failed for <"//trim(nam)//">" |
---|
339 | case('put'); msg="Writing failed for <"//trim(nam)//">" |
---|
340 | case('open'); msg="File opening failed for <"//trim(nam)//">" |
---|
341 | case('close'); msg="File closing failed for <"//trim(nam)//">" |
---|
342 | case default |
---|
343 | write(*,*) 'There is no message for this error.' |
---|
344 | error stop |
---|
345 | end select |
---|
346 | call abort_gcm(trim(modname),trim(msg),ierr) |
---|
347 | |
---|
348 | END SUBROUTINE error_msg |
---|
349 | |
---|
350 | END MODULE read_data_PCM_mod |
---|