3 real*4, parameter :: sclht = 287.04 * 256.0 / 9.81
4 real*4, parameter :: eps = 0.622
5 real*4, parameter :: ezero = 6.112
6 real*4, parameter :: eslcon1 = 17.67
7 real*4, parameter :: eslcon2 = 29.65
8 real*4, parameter :: gamma = 287.04/1004.
9 real*4, parameter :: gammamd = 0.608-0.887
10 real*4, parameter :: pi = 3.1415926 ! Value used in WRF.
11 real*4, parameter :: earth_radius = 6370.0 ! Value used in WRF.
12 real*4, parameter :: deg_to_rad = pi/180.0
13 real*4, parameter :: rad_to_deg = 1.0/deg_to_rad
16 real*4, allocatable, dimension(:) :: znw, znu, znfac
17 integer, allocatable, dimension(:,:) :: landmask
18 real*4, allocatable, dimension(:,:) :: wrf_psfc, wrf_t2, wrf_q2, wrf_u10, wrf_v10, wrf_rh2, wrf_xlat, wrf_xlong
19 real*4, allocatable, dimension(:,:,:) :: wrf_t, wrf_u, wrf_v, wrf_p, wrf_pb, wrf_ph, wrf_phb, wrf_ght, wrf_q, wrf_rh, wrf_tdp
24 function readwrf(filename, west_east_dim, south_north_dim, dx, dy, cen_lat, cen_lon, &
25 stand_lon, true1, true2, mapproj, idim, jdim, kdim, idim_stag, jdim_stag, &
26 ratio, miycors, mjxcors)
33 integer :: mapproj, west_east_dim, south_north_dim, idim, jdim, kdim, idim_stag, jdim_stag
34 real*4 :: dx, dy, cen_lat, cen_lon, stand_lon, true1, true2, ratio, miycors, mjxcors
35 character (len=*) :: filename
39 integer :: status, ncid, n_dimensions, n_variables, n_attributes, unltd_dim_id
40 integer :: xtype, ndims, n_atts, i, j, xtype_att, l, att_num, dimid
42 integer, dimension(NF_MAX_VAR_DIMS) :: dimids
43 real*4 :: q, e, es, elog, gammam
44 character (len=NF_MAX_NAME) :: varname, attname, dimname
46 ! Set for compatibility with
51 status = nf_open(trim(filename), 0, ncid)
53 if (status /= NF_NOERR) then
54 write(6,*) 'Error :: readwrf() : Could not open file '//trim(filename)
59 status = nf_inq(ncid, n_dimensions, n_variables, n_attributes, unltd_dim_id)
61 if (status == NF_NOERR) then
64 status = nf_inq_attname(ncid, NF_GLOBAL, i, attname)
65 if (status == NF_NOERR) then
67 status = nf_inq_att(ncid, NF_GLOBAL, attname, xtype, l)
68 if (status == NF_NOERR) then
69 if (index(trim(attname),'WEST-EAST_GRID_DIMENSION') /= 0) then
70 status = nf_get_att_int(ncid, NF_GLOBAL, attname, west_east_dim)
71 if (status /= NF_NOERR) write(6,*) 'Error getting west-east dimension'
72 else if (index(trim(attname),'SOUTH-NORTH_GRID_DIMENSION') /= 0) then
73 status = nf_get_att_int(ncid, NF_GLOBAL, attname, south_north_dim)
74 if (status /= NF_NOERR) write(6,*) 'Error getting south-north dimension'
75 else if (index(trim(attname),'MAP_PROJ') /= 0) then
76 status = nf_get_att_int(ncid, NF_GLOBAL, attname, mapproj)
77 if (status /= NF_NOERR) write(6,*) 'Error getting map_projection'
78 else if (index(trim(attname),'DX') /= 0) then
79 status = nf_get_att_real(ncid, NF_GLOBAL, attname, dx)
80 if (status /= NF_NOERR) write(6,*) 'Error getting dx'
82 else if (index(trim(attname),'DY') /= 0) then
83 status = nf_get_att_real(ncid, NF_GLOBAL, attname, dy)
84 if (status /= NF_NOERR) write(6,*) 'Error getting dy'
86 else if (index(trim(attname),'CEN_LAT') /= 0 .and. len_trim(attname) == 7) then
87 status = nf_get_att_real(ncid, NF_GLOBAL, attname, cen_lat)
88 if (status /= NF_NOERR) write(6,*) 'Error getting cen_lat'
89 else if (index(trim(attname),'CEN_LON') /= 0) then
90 status = nf_get_att_real(ncid, NF_GLOBAL, attname, cen_lon)
91 if (status /= NF_NOERR) write(6,*) 'Error getting cen_lon'
92 else if (index(trim(attname),'STAND_LON') /= 0) then
93 status = nf_get_att_real(ncid, NF_GLOBAL, attname, stand_lon)
94 if (status /= NF_NOERR) write(6,*) 'Error getting stand_lon'
95 else if (index(trim(attname),'TRUELAT1') /= 0) then
96 status = nf_get_att_real(ncid, NF_GLOBAL, attname, true1)
97 if (status /= NF_NOERR) write(6,*) 'Error getting true1'
98 else if (index(trim(attname),'TRUELAT2') /= 0) then
99 status = nf_get_att_real(ncid, NF_GLOBAL, attname, true2)
100 if (status /= NF_NOERR) write(6,*) 'Error getting true2'
107 status = nf_inq_var(ncid, i, varname, xtype, ndims, dimids, n_atts)
109 if (status == NF_NOERR) then
110 if (index(trim(varname),'P_TOP') /= 0) then !{
112 status = nf_get_var_real(ncid, i, ptop)
114 else if (index(trim(varname),'LANDMASK') /= 0) then !{
117 status = nf_inq_dim(ncid, dimids(j), dimname, l)
118 if (status == NF_NOERR) then
119 if (index(trim(dimname),'west_east') /= 0) then
121 else if (index(trim(dimname),'west-east') /= 0) then
123 else if (index(trim(dimname),'south_north') /= 0) then
125 else if (index(trim(dimname),'south-north') /= 0) then
131 allocate(landmask(idim, jdim))
132 status = nf_get_var_real(ncid, i, landmask)
134 else if (index(trim(varname),'XLAT') /= 0 .and. len_trim(varname) == 4) then !{
137 status = nf_inq_dim(ncid, dimids(j), dimname, l)
138 if (status == NF_NOERR) then
139 if (index(trim(dimname),'west_east') /= 0) then
141 else if (index(trim(dimname),'west-east') /= 0) then
143 else if (index(trim(dimname),'south_north') /= 0) then
145 else if (index(trim(dimname),'south-north') /= 0) then
151 allocate(wrf_xlat(idim, jdim))
152 status = nf_get_var_real(ncid, i, wrf_xlat)
154 else if (index(trim(varname),'XLONG') /= 0 .and. len_trim(varname) == 5) then !{
157 status = nf_inq_dim(ncid, dimids(j), dimname, l)
158 if (status == NF_NOERR) then
159 if (index(trim(dimname),'west_east') /= 0) then
161 else if (index(trim(dimname),'west-east') /= 0) then
163 else if (index(trim(dimname),'south_north') /= 0) then
165 else if (index(trim(dimname),'south-north') /= 0) then
171 allocate(wrf_xlong(idim, jdim))
172 status = nf_get_var_real(ncid, i, wrf_xlong)
174 else if (index(trim(varname),'T') /= 0 .and. len_trim(varname) == 1) then !{
177 status = nf_inq_dim(ncid, dimids(j), dimname, l)
178 if (status == NF_NOERR) then
179 if (index(trim(dimname),'west_east') /= 0) then
181 else if (index(trim(dimname),'west-east') /= 0) then
183 else if (index(trim(dimname),'south_north') /= 0) then
185 else if (index(trim(dimname),'south-north') /= 0) then
187 else if (index(trim(dimname),'bottom_top') /= 0) then
189 else if (index(trim(dimname),'bottom-top') /= 0) then
195 allocate(wrf_t(idim, jdim, kdim))
196 status = nf_get_var_real(ncid, i, wrf_t)
198 else if (index(trim(varname),'U') /= 0 .and. len_trim(varname) == 1) then !{
201 status = nf_inq_dim(ncid, dimids(j), dimname, l)
202 if (status == NF_NOERR) then
203 if (index(trim(dimname),'west_east_stag') /= 0) then
205 else if (index(trim(dimname),'west-east_stag') /= 0) then
207 else if (index(trim(dimname),'south_north') /= 0) then
209 else if (index(trim(dimname),'south-north') /= 0) then
211 else if (index(trim(dimname),'bottom_top') /= 0) then
213 else if (index(trim(dimname),'bottom-top') /= 0) then
219 allocate(wrf_u(idim_stag, jdim, kdim))
220 status = nf_get_var_real(ncid, i, wrf_u)
222 else if (index(trim(varname),'V') /= 0 .and. len_trim(varname) == 1) then !{
225 status = nf_inq_dim(ncid, dimids(j), dimname, l)
226 if (status == NF_NOERR) then
227 if (index(trim(dimname),'west_east') /= 0) then
229 else if (index(trim(dimname),'west-east') /= 0) then
231 else if (index(trim(dimname),'south_north_stag') /= 0) then
233 else if (index(trim(dimname),'south-north_stag') /= 0) then
235 else if (index(trim(dimname),'bottom_top') /= 0) then
237 else if (index(trim(dimname),'bottom-top') /= 0) then
243 allocate(wrf_v(idim, jdim_stag, kdim))
244 status = nf_get_var_real(ncid, i, wrf_v)
246 else if (index(trim(varname),'PSFC') /= 0 .and. len_trim(varname) == 4) then !{
249 status = nf_inq_dim(ncid, dimids(j), dimname, l)
250 if (status == NF_NOERR) then
251 if (index(trim(dimname),'west_east') /= 0) then
253 else if (index(trim(dimname),'west-east') /= 0) then
255 else if (index(trim(dimname),'south_north') /= 0) then
257 else if (index(trim(dimname),'south-north') /= 0) then
263 allocate(wrf_psfc(idim, jdim))
264 status = nf_get_var_real(ncid, i, wrf_psfc)
266 else if (index(trim(varname),'T2') /= 0 .and. len_trim(varname) == 2) then !{
269 status = nf_inq_dim(ncid, dimids(j), dimname, l)
270 if (status == NF_NOERR) then
271 if (index(trim(dimname),'west_east') /= 0) then
273 else if (index(trim(dimname),'west-east') /= 0) then
275 else if (index(trim(dimname),'south_north') /= 0) then
277 else if (index(trim(dimname),'south-north') /= 0) then
283 allocate(wrf_t2(idim, jdim))
284 status = nf_get_var_real(ncid, i, wrf_t2)
286 else if (index(trim(varname),'Q2') /= 0 .and. len_trim(varname) == 2) then !{
289 status = nf_inq_dim(ncid, dimids(j), dimname, l)
290 if (status == NF_NOERR) then
291 if (index(trim(dimname),'west_east') /= 0) then
293 else if (index(trim(dimname),'west-east') /= 0) then
295 else if (index(trim(dimname),'south_north') /= 0) then
297 else if (index(trim(dimname),'south-north') /= 0) then
303 allocate(wrf_q2(idim, jdim))
304 status = nf_get_var_real(ncid, i, wrf_q2)
306 else if (index(trim(varname),'U10') /= 0 .and. len_trim(varname) == 3) then !{
309 status = nf_inq_dim(ncid, dimids(j), dimname, l)
310 if (status == NF_NOERR) then
311 if (index(trim(dimname),'west_east') /= 0) then
313 else if (index(trim(dimname),'west-east') /= 0) then
315 else if (index(trim(dimname),'south_north') /= 0) then
317 else if (index(trim(dimname),'south-north') /= 0) then
323 allocate(wrf_u10(idim, jdim))
324 status = nf_get_var_real(ncid, i, wrf_u10)
326 else if (index(trim(varname),'V10') /= 0 .and. len_trim(varname) == 3) then !{
329 status = nf_inq_dim(ncid, dimids(j), dimname, l)
330 if (status == NF_NOERR) then
331 if (index(trim(dimname),'west_east') /= 0) then
333 else if (index(trim(dimname),'west-east') /= 0) then
335 else if (index(trim(dimname),'south_north') /= 0) then
337 else if (index(trim(dimname),'south-north') /= 0) then
343 allocate(wrf_v10(idim, jdim))
344 status = nf_get_var_real(ncid, i, wrf_v10)
346 else if (index(trim(varname),'QVAPOR') /= 0 .and. len_trim(varname) == 6) then !{
349 status = nf_inq_dim(ncid, dimids(j), dimname, l)
350 if (status == NF_NOERR) then
351 if (index(trim(dimname),'west_east') /= 0) then
353 else if (index(trim(dimname),'west-east') /= 0) then
355 else if (index(trim(dimname),'south_north') /= 0) then
357 else if (index(trim(dimname),'south-north') /= 0) then
359 else if (index(trim(dimname),'bottom_top') /= 0) then
361 else if (index(trim(dimname),'bottom-top') /= 0) then
367 allocate(wrf_q(idim, jdim, kdim))
368 status = nf_get_var_real(ncid, i, wrf_q)
370 else if (index(trim(varname),'PB') /= 0 .and. len_trim(varname) == 2) then !{
373 status = nf_inq_dim(ncid, dimids(j), dimname, l)
374 if (status == NF_NOERR) then
375 if (index(trim(dimname),'west_east') /= 0) then
377 else if (index(trim(dimname),'west-east') /= 0) then
379 else if (index(trim(dimname),'south_north') /= 0) then
381 else if (index(trim(dimname),'south-north') /= 0) then
383 else if (index(trim(dimname),'bottom_top') /= 0) then
385 else if (index(trim(dimname),'bottom-top') /= 0) then
391 allocate(wrf_pb(idim, jdim, kdim))
392 status = nf_get_var_real(ncid, i, wrf_pb)
394 else if (index(trim(varname),'P') /= 0 .and. len_trim(varname) == 1) then !{
397 status = nf_inq_dim(ncid, dimids(j), dimname, l)
398 if (status == NF_NOERR) then
399 if (index(trim(dimname),'west_east') /= 0) then
401 else if (index(trim(dimname),'west-east') /= 0) then
403 else if (index(trim(dimname),'south_north') /= 0) then
405 else if (index(trim(dimname),'south-north') /= 0) then
407 else if (index(trim(dimname),'bottom_top') /= 0) then
409 else if (index(trim(dimname),'bottom-top') /= 0) then
415 allocate(wrf_p(idim, jdim, kdim))
416 status = nf_get_var_real(ncid, i, wrf_p)
418 else if (index(trim(varname),'PHB') /= 0 .and. len_trim(varname) == 3) then !{
421 status = nf_inq_dim(ncid, dimids(j), dimname, l)
422 if (status == NF_NOERR) then
423 if (index(trim(dimname),'west_east') /= 0) then
425 else if (index(trim(dimname),'west-east') /= 0) then
427 else if (index(trim(dimname),'south_north') /= 0) then
429 else if (index(trim(dimname),'south-north') /= 0) then
431 else if (index(trim(dimname),'bottom_top_stag') /= 0) then
433 else if (index(trim(dimname),'bottom-top_stag') /= 0) then
439 allocate(wrf_phb(idim, jdim, kdim_stag))
440 status = nf_get_var_real(ncid, i, wrf_phb)
442 else if (index(trim(varname),'PH') /= 0 .and. len_trim(varname) == 2) then !{
445 status = nf_inq_dim(ncid, dimids(j), dimname, l)
446 if (status == NF_NOERR) then
447 if (index(trim(dimname),'west_east') /= 0) then
449 else if (index(trim(dimname),'west-east') /= 0) then
451 else if (index(trim(dimname),'south_north') /= 0) then
453 else if (index(trim(dimname),'south-north') /= 0) then
455 else if (index(trim(dimname),'bottom_top_stag') /= 0) then
457 else if (index(trim(dimname),'bottom-top_stag') /= 0) then
463 allocate(wrf_ph(idim, jdim, kdim_stag))
464 status = nf_get_var_real(ncid, i, wrf_ph)
466 else if (index(trim(varname),'ZNU') /= 0 .and. len_trim(varname) == 3) then !{
469 status = nf_inq_dim(ncid, dimids(j), dimname, l)
470 if (status == NF_NOERR) then
471 if (index(trim(dimname),'bottom_top') /= 0) then
473 else if (index(trim(dimname),'bottom-top') /= 0) then
480 status = nf_get_var_real(ncid, i, znu)
482 else if (index(trim(varname),'ZNW') /= 0 .and. len_trim(varname) == 3) then !{
485 status = nf_inq_dim(ncid, dimids(j), dimname, l)
486 if (status == NF_NOERR) then
487 if (index(trim(dimname),'bottom-top_stag') /= 0) then
489 else if (index(trim(dimname),'bottom_top_stag') /= 0) then
495 allocate(znw(kdim_stag))
496 status = nf_get_var_real(ncid, i, znw)
507 allocate(znfac(kdim))
510 znfac(kk)=(znw(kk)-znu(kk))/(znw(kk)-znw(kk+1))
516 wrf_ph(ii,jj,kk)=exp(-(wrf_ph(ii,jj,kk)+wrf_phb(ii,jj,kk))/(9.81*sclht))
521 allocate(wrf_ght(idim, jdim, kdim))
526 wrf_ght(ii,jj,kk)=znfac(kk)*wrf_ph(ii,jj,kk+1)+(1.-znfac(kk))*wrf_ph(ii,jj,kk)
527 wrf_ght(ii,jj,kk)=-sclht*log(wrf_ght(ii,jj,kk))
535 ! wrf_p(ii,jj,kk)=wrf_p(ii,jj,kk) + wrf_pb(ii,jj,kk)
543 ! gammam=gamma*(1.+gammamd*.001*wrf_q(ii,jj,kk))
544 ! wrf_t(ii,jj,kk)=(wrf_t(ii,jj,kk)+300.)*(wrf_p(ii,jj,kk)/100000.)**gammam
549 allocate(wrf_rh(idim, jdim, kdim))
554 ! e = wrf_q(ii,jj,kk)*(wrf_p(ii,jj,kk)/100.)/(eps+wrf_q(ii,jj,kk))
555 ! es = ezero * exp( eslcon1*(wrf_t(ii,jj,kk)-273.15)/(wrf_t(ii,jj,kk)-eslcon2) )
556 ! wrf_rh(ii,jj,kk)=100.*(e*((wrf_p(ii,jj,kk)/100.)-es))/(es*((wrf_p(ii,jj,kk)/100.)-e))
561 allocate(wrf_tdp(idim, jdim, kdim))
566 ! q=max(wrf_q(ii,jj,kk),1.e-15)
567 ! e=q*(wrf_p(ii,jj,kk)/100.)/(eps+q)
569 ! wrf_tdp(ii,jj,kk)=(eslcon2*elog-eslcon1*273.15)/(elog-eslcon1)
574 allocate(wrf_rh2(idim, jdim))
578 e = wrf_q2(ii,jj)*(wrf_psfc(ii,jj)/100.)/(eps+wrf_q2(ii,jj))
579 es = ezero * exp( eslcon1*(wrf_t2(ii,jj)-273.15)/(wrf_t2(ii,jj)-eslcon2) )
580 wrf_rh2(ii,jj)=100.*(e*((wrf_psfc(ii,jj)/100.)-es))/(es*((wrf_psfc(ii,jj)/100.)-e))
586 end module readwrf_module