SABER
model_aro.inc
Go to the documentation of this file.
1 !----------------------------------------------------------------------
2 ! Subroutine: model_aro_coord
3 !> Load AROME coordinates
4 !----------------------------------------------------------------------
5 subroutine model_aro_coord(model,mpl,nam)
6 
7 implicit none
8 
9 ! Passed variables
10 class(model_type),intent(inout) :: model !< Model
11 type(mpl_type),intent(inout) :: mpl !< MPI data
12 type(nam_type),intent(in) :: nam !< Namelist
13 
14 ! Local variables
15 integer :: img,ilon,ilat,il0,il0_bot,il0_top,ie,its
16 integer :: ncid,nlon_id,nlat_id,nlev_id,pp_id,lon_id,lat_id,cmask_id,a_id,b_id,fld_id,mask_id
17 integer,allocatable :: mask_counter(:)
18 real(kind_real) :: dx,dy
19 real(kind_real),allocatable :: lon(:,:),lat(:,:),cmask(:,:),a(:),b(:),fld_loc(:,:),fld(:,:)
20 character(len=3) :: ilchar
21 character(len=1024) :: filename
22 character(len=1024),parameter :: subr = 'model_aro_coord'
23 
24 ! Open file and get dimensions
25 model%ntile = 1
26 call mpl%ncerr(subr,nf90_open(trim(nam%datadir)//'/grid.nc',nf90_share,ncid))
27 call mpl%ncerr(subr,nf90_inq_dimid(ncid,'X',nlon_id))
28 call mpl%ncerr(subr,nf90_inq_dimid(ncid,'Y',nlat_id))
29 call mpl%ncerr(subr,nf90_inquire_dimension(ncid,nlon_id,len=model%nlon))
30 call mpl%ncerr(subr,nf90_inquire_dimension(ncid,nlat_id,len=model%nlat))
31 model%nmg = model%nlon*model%nlat
32 call mpl%ncerr(subr,nf90_inq_dimid(ncid,'Z',nlev_id))
33 call mpl%ncerr(subr,nf90_inquire_dimension(ncid,nlev_id,len=model%nlev))
34 
35 ! Allocation
36 call model%alloc
37 allocate(lon(model%nlon,model%nlat))
38 allocate(lat(model%nlon,model%nlat))
39 allocate(cmask(model%nlon,model%nlat))
40 allocate(a(model%nlev+1))
41 allocate(b(model%nlev+1))
42 
43 ! Read data and close file
44 call mpl%ncerr(subr,nf90_inq_varid(ncid,'longitude',lon_id))
45 call mpl%ncerr(subr,nf90_inq_varid(ncid,'latitude',lat_id))
46 call mpl%ncerr(subr,nf90_inq_varid(ncid,'cmask',cmask_id))
47 call mpl%ncerr(subr,nf90_inq_varid(ncid,'hybrid_coef_A',a_id))
48 call mpl%ncerr(subr,nf90_inq_varid(ncid,'hybrid_coef_B',b_id))
49 call mpl%ncerr(subr,nf90_inq_varid(ncid,'Projection_parameters',pp_id))
50 call mpl%ncerr(subr,nf90_get_var(ncid,lon_id,lon))
51 call mpl%ncerr(subr,nf90_get_var(ncid,lat_id,lat))
52 call mpl%ncerr(subr,nf90_get_var(ncid,cmask_id,cmask))
53 call mpl%ncerr(subr,nf90_get_var(ncid,a_id,a))
54 call mpl%ncerr(subr,nf90_get_var(ncid,b_id,b))
55 call mpl%ncerr(subr,nf90_get_att(ncid,pp_id,'x_resolution',dx))
56 call mpl%ncerr(subr,nf90_get_att(ncid,pp_id,'y_resolution',dy))
57 call mpl%ncerr(subr,nf90_close(ncid))
58 
59 ! Convert to radian
60 lon = lon*deg2rad
61 lat = lat*deg2rad
62 
63 ! Model grid
64 img = 0
65 do ilon=1,model%nlon
66  do ilat=1,model%nlat
67  img = img+1
68  model%mg_to_tile(img) = 1
69  model%mg_to_lon(img) = ilon
70  model%mg_to_lat(img) = ilat
71  model%lon(img) = lon(ilon,ilat)
72  model%lat(img) = lat(ilon,ilat)
73  select case (trim(zone))
74  case ('C')
75  model%mask(img,:) = (cmask(ilon,ilat)>0.75)
76  case ('C+I')
77  model%mask(img,:) = (cmask(ilon,ilat)>0.25)
78  case ('C+I+E')
79  model%mask(img,:) = .true.
80  case default
81  call mpl%abort(subr,'wrong AROME zone')
82  end select
83  end do
84 end do
85 model%area = dx*dy/req**2
86 
87 ! Specific mask
88 select case (trim(nam%mask_type))
89 case ("hyd")
90  ! Based on an external file
91 
92  ! Allocation
93  allocate(fld_loc(model%nlon,model%nlat))
94 
95  ! Open file
96  call mpl%ncerr(subr,nf90_open(trim(nam%datadir)//'/'//trim(nam%prefix)//'_hyd.nc',nf90_nowrite,ncid))
97 
98  ! Read mask
99  do il0=1,nam%nl
100  ! Get id
101  write(ilchar,'(i3.3)') nam%levs(il0)
102  call mpl%ncerr(subr,nf90_inq_varid(ncid,'S'//ilchar//'MASK',mask_id))
103 
104  ! Read data
105  call mpl%ncerr(subr,nf90_get_var(ncid,mask_id,fld_loc))
106 
107  ! Pack data
108  img = 0
109  do ilon=1,model%nlon
110  do ilat=1,model%nlat
111  img = img+1
112  model%mask(img,il0) = (fld_loc(ilon,ilat)>nam%mask_th(1))
113  end do
114  end do
115  end do
116 
117  ! Close file
118  call mpl%ncerr(subr,nf90_close(ncid))
119 
120  ! Release memory
121  deallocate(fld_loc)
122 case ("lwc_fog","lwc_clear","lwc_stratus")
123  ! Based on the ensemble-mean LWC
124 
125  ! Allocation
126  allocate(fld_loc(model%nlon,model%nlat))
127  allocate(fld(model%nmg,model%nl0))
128  allocate(mask_counter(model%nmga))
129 
130  ! Initialization
131  mask_counter = 0
132  select case (trim(nam%mask_type))
133  case("lwc_fog")
134  il0_bot = nam%nl
135  il0_top = nam%nl-2
136  case("lwc_clear")
137  il0_bot = nam%nl
138  il0_top = 1
139  case("lwc_stratus")
140  il0_bot = nam%nl-30
141  il0_top = nam%nl-66
142  end select
143 
144  ! Setup mask
145  do ie=1,nam%ens1_ne
146  ! Set file name
147  write(filename,'(a,i6.6)') 'ens1_',ie
148 
149  ! Open file
150  call mpl%ncerr(subr,nf90_open(trim(nam%datadir)//'/'//trim(filename)//'.nc',nf90_nowrite,ncid))
151 
152  ! Read LWC
153  do il0=1,nam%nl
154  ! Get id
155  write(ilchar,'(i3.3)') nam%levs(il0)
156  call mpl%ncerr(subr,nf90_inq_varid(ncid,'S'//ilchar//'CLOUD_WATER',fld_id))
157 
158  ! Read data
159  call mpl%ncerr(subr,nf90_get_var(ncid,fld_id,fld_loc))
160 
161  ! Pack data
162  img = 0
163  do ilon=1,model%nlon
164  do ilat=1,model%nlat
165  img = img+1
166  fld(img,il0) = real(fld_loc(ilon,ilat),kind_real)
167  end do
168  end do
169  end do
170 
171  ! Close file
172  call mpl%ncerr(subr,nf90_close(ncid))
173 
174  ! Update mask
175  select case (trim(nam%mask_type))
176  case("lwc_fog")
177  do img=1,model%nmg
178  if (all(fld(img,il0_top:il0_bot)>nam%mask_th)) mask_counter(img) = mask_counter(img)+1
179  end do
180  case("lwc_clear")
181  do img=1,model%nmg
182  if (all(fld(img,il0_top:il0_bot)<nam%mask_th)) mask_counter(img) = mask_counter(img)+1
183  end do
184  case("lwc_stratus")
185  do img=1,model%nmg
186  if (count(fld(img,il0_top:il0_bot)>nam%mask_th)>3) mask_counter(img) = mask_counter(img)+1
187  end do
188  end select
189  end do
190 
191  ! Update mask
192  do img=1,model%nmg
193  if (real(mask_counter(img),kind_real)<0.9*real(nam%ens1_ne,kind_real)) model%mask(img,:) = .false.
194  end do
195 end select
196 
197 ! Vertical unit
198 do img=1,model%nmg
199  if (nam%logpres) then
200  model%vunit(img,1:nam%nl) = log(0.5*(a(nam%levs(1:nam%nl))+a(nam%levs(1:nam%nl)+1)) &
201  & +0.5*(b(nam%levs(1:nam%nl))+b(nam%levs(1:nam%nl)+1))*ps)
202  if (model%nl0>nam%nl) model%vunit(img,model%nl0) = log(ps)
203  else
204  model%vunit(img,:) = real(nam%levs(1:model%nl0),kind_real)
205  end if
206 end do
207 
208 ! Release memory
209 deallocate(lon)
210 deallocate(lat)
211 deallocate(cmask)
212 deallocate(a)
213 deallocate(b)
214 
215 end subroutine model_aro_coord
216 
217 !----------------------------------------------------------------------
218 ! Subroutine: model_aro_read
219 !> Read AROME field
220 !----------------------------------------------------------------------
221 subroutine model_aro_read(model,mpl,nam,filename,fld)
222 
223 implicit none
224 
225 ! Passed variables
226 class(model_type),intent(inout) :: model !< Model
227 type(mpl_type),intent(inout) :: mpl !< MPI data
228 type(nam_type),intent(in) :: nam !< Namelist
229 character(len=*),intent(in) :: filename !< File name
230 real(kind_real),intent(out) :: fld(model%nmga,model%nl0,nam%nv) !< Field
231 
232 ! Local variables
233 integer :: iv,il0,img,ilon,ilat,info,iv_q,iv_lwc
234 integer :: ncid,fld_id
235 real(kind_real) :: fld_mg(model%nmg,model%nl0)
236 real(kind_real),allocatable :: fld_tmp(:,:,:)
237 character(len=3) :: ilchar
238 character(len=1024),parameter :: subr = 'model_aro_read'
239 
240 if (mpl%main) then
241  ! Allocation
242  allocate(fld_tmp(model%nlon,model%nlat,model%nl0))
243 
244  ! Open file
245  call mpl%ncerr(subr,nf90_open(trim(nam%datadir)//'/'//trim(filename)//'.nc',nf90_nowrite,ncid))
246 end if
247 
248 do iv=1,nam%nv
249  if (mpl%main) then
250  do il0=1,nam%nl
251  ! Get id
252  write(ilchar,'(i3.3)') nam%levs(il0)
253  info = nf90_inq_varid(ncid,'S'//ilchar//trim(nam%variables(iv)),fld_id)
254 
255  ! Read data
256  if (info==nf90_noerr) then
257  ! 3D data
258  call mpl%ncerr(subr,nf90_get_var(ncid,fld_id,fld_tmp(:,:,il0)))
259  else
260  ! Check if the variable exists as 2D data
261  call mpl%ncerr(subr,nf90_inq_varid(ncid,nam%variables(iv),fld_id))
262 
263  ! 2D data
264  if (((trim(nam%lev2d)=='first').and.(il0==1)).or.((trim(nam%lev2d)=='last').and.(il0==model%nl0))) then
265  fld_tmp = mpl%msv%valr
266  call mpl%ncerr(subr,nf90_get_var(ncid,fld_id,fld_tmp(:,:,il0)))
267  end if
268  end if
269  end do
270 
271  ! Pack
272  do il0=1,model%nl0
273  do img=1,model%nmg
274  ilon = model%mg_to_lon(img)
275  ilat = model%mg_to_lat(img)
276  fld_mg(img,il0) = fld_tmp(ilon,ilat,il0)
277  end do
278  end do
279  end if
280  call mpl%glb_to_loc(model%nl0,model%nmga,model%nmg,model%mga_to_mg,fld_mg,fld(:,:,iv))
281 end do
282 
283 if (mpl%main) then
284  ! Close file
285  call mpl%ncerr(subr,nf90_close(ncid))
286 
287  ! Release memory
288  deallocate(fld_tmp)
289 end if
290 
291 ! Post-processing
292 iv_q = mpl%msv%vali
293 iv_lwc = mpl%msv%vali
294 do iv=1,nam%nv
295  if (trim(nam%variables(iv))=='HUMI_SPECIFI') iv_q = iv
296  if (trim(nam%variables(iv))=='CLOUD_WATER') iv_lwc = iv
297 end do
298 select case (trim(nam%variable_change))
299 case ('logq')
300  if (mpl%msv%isnot(iv_q)) then
301  fld(:,:,iv_q) = log(max(fld(:,:,iv_q),qmin))
302  else
303  call mpl%abort(subr,'specific humidity not found')
304  end if
305 case ('logqtot')
306  if ((mpl%msv%isnot(iv_q)).and.(mpl%msv%isnot(iv_lwc))) then
307  fld(:,:,iv_q) = log(max(fld(:,:,iv_q)+fld(:,:,iv_lwc),qmin))
308  fld(:,:,iv_lwc) = mpl%msv%valr
309  else
310  call mpl%abort(subr,'specific humidity, or LWC not found')
311  end if
312 end select
313 
314 end subroutine model_aro_read
main
int main(int argc, char **argv)
Definition: bump_main.cc:17
tools_const::ps
real(kind_real), parameter, public ps
Definition: tools_const.F90:19
tools_const::req
real(kind_real), parameter, public req
Definition: tools_const.F90:17
tools_const::deg2rad
real(kind_real), parameter, public deg2rad
Definition: tools_const.F90:15
type_model::qmin
real(kind_real), parameter qmin
Definition: type_model.F90:26
type_model::zone
character(len=1024) zone
Definition: type_model.F90:27
tools_kinds::kind_real
integer, parameter, public kind_real
Definition: tools_kinds.F90:18
type_rng::a
integer(kind=int64), parameter a
Definition: type_rng.F90:18