1 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2 ! MODULE GRIDINFO_MODULE
4 ! This module handles (i.e., acquires, stores, and makes available) all data
5 ! describing the model domains to be processed.
6 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
10 use misc_definitions_module
14 integer, parameter :: MAX_DOMAINS = 21
17 integer :: iproj_type, n_domains, io_form_output, dyn_opt
18 integer, dimension(MAX_DOMAINS) :: parent_grid_ratio, parent_id, ixdim, jydim
19 integer, dimension(MAX_DOMAINS) :: subgrid_ratio_x, subgrid_ratio_y
20 real :: known_lat, known_lon, pole_lat, pole_lon, stand_lon, truelat1, truelat2, &
21 known_x, known_y, dxkm, dykm, phi, lambda, ref_lat, ref_lon, ref_x, ref_y, &
23 real, dimension(MAX_DOMAINS) :: parent_ll_x, parent_ll_y, parent_ur_x, parent_ur_y
24 character (len=MAX_FILENAME_LEN) :: geog_data_path, opt_output_from_geogrid_path, opt_geogrid_tbl_path
26 character (len=128), dimension(MAX_DOMAINS) :: geog_data_res
27 character (len=1) :: gridtype
28 logical :: do_tiled_output
29 logical, dimension(MAX_DOMAINS) :: grid_is_active
30 integer :: debug_level
34 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
35 ! Name: get_grid_params
37 ! Purpose: This subroutine retrieves all parameters regarding the model domains
38 ! to be processed by geogrid.exe. This includes map parameters, domain
39 ! size and location, and nest information.
40 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
41 subroutine get_grid_params()
46 integer :: i, j, max_dom, funit, io_form_geogrid, interval_seconds
47 real :: dx, dy, rparent_gridpts
48 integer, dimension(MAX_DOMAINS) :: i_parent_start, j_parent_start, &
49 s_we, e_we, s_sn, e_sn, &
50 start_year, start_month, start_day, start_hour, start_minute, start_second, &
51 end_year, end_month, end_day, end_hour, end_minute, end_second
52 character (len=128) :: map_proj
53 character (len=128), dimension(MAX_DOMAINS) :: start_date, end_date
54 character (len=3) :: wrf_core
55 logical :: is_used, nest_outside
56 logical, dimension(MAX_DOMAINS) :: active_grid
58 namelist /share/ wrf_core, max_dom, start_date, end_date, &
59 start_year, end_year, start_month, end_month, &
60 start_day, end_day, start_hour, end_hour, &
61 start_minute, end_minute, start_second, end_second, &
63 io_form_geogrid, opt_output_from_geogrid_path, debug_level, active_grid, &
64 subgrid_ratio_x, subgrid_ratio_y
65 namelist /geogrid/ parent_id, parent_grid_ratio, &
66 i_parent_start, j_parent_start, s_we, e_we, s_sn, e_sn, &
67 map_proj, ref_x, ref_y, ref_lat, ref_lon, &
68 pole_lat, pole_lon, truelat1, truelat2, stand_lon, &
69 dx, dy, geog_data_res, geog_data_path, opt_geogrid_tbl_path
71 ! Set defaults for namelist variables
76 geog_data_path = 'NOT_SPECIFIED'
90 geog_data_res(i) = 'default'
92 parent_grid_ratio(i) = INVALID
109 start_date(i) = '0000-00-00_00:00:00'
110 end_date(i) = '0000-00-00_00:00:00'
111 active_grid(i) = .true.
112 subgrid_ratio_x(i) = 1
113 subgrid_ratio_y(i) = 1
115 opt_output_from_geogrid_path = './'
116 opt_geogrid_tbl_path = 'geogrid/'
117 interval_seconds = INVALID
119 ! Read parameters from Fortran namelist
121 inquire(unit=funit, opened=is_used)
122 if (.not. is_used) exit
124 open(funit,file='namelist.wps',status='old',form='formatted',err=1000)
129 ! BUG: More properly handle debug_level in module_debug
130 if (debug_level.gt.100) then
131 call set_debug_level(DEBUG)
133 call set_debug_level(WARN)
136 call mprintf(.true.,LOGFILE,'Using the following namelist variables:')
137 call mprintf(.true.,LOGFILE,'&SHARE')
138 call mprintf(.true.,LOGFILE,' WRF_CORE = %s',s1=wrf_core)
139 call mprintf(.true.,LOGFILE,' MAX_DOM = %i',i1=max_dom)
140 call mprintf(.true.,LOGFILE,' START_YEAR = %i',i1=start_year(1))
142 call mprintf(.true.,LOGFILE,' = %i',i1=start_year(i))
144 call mprintf(.true.,LOGFILE,' START_MONTH = %i',i1=start_month(1))
146 call mprintf(.true.,LOGFILE,' = %i',i1=start_month(i))
148 call mprintf(.true.,LOGFILE,' START_DAY = %i',i1=start_day(1))
150 call mprintf(.true.,LOGFILE,' = %i',i1=start_day(i))
152 call mprintf(.true.,LOGFILE,' START_HOUR = %i',i1=start_hour(1))
154 call mprintf(.true.,LOGFILE,' = %i',i1=start_hour(i))
156 call mprintf(.true.,LOGFILE,' START_MINUTE = %i',i1=start_minute(1))
158 call mprintf(.true.,LOGFILE,' = %i',i1=start_minute(i))
160 call mprintf(.true.,LOGFILE,' START_SECOND = %i',i1=start_second(1))
162 call mprintf(.true.,LOGFILE,' = %i',i1=start_second(i))
164 call mprintf(.true.,LOGFILE,' END_YEAR = %i',i1=end_year(1))
166 call mprintf(.true.,LOGFILE,' = %i',i1=end_year(i))
168 call mprintf(.true.,LOGFILE,' END_MONTH = %i',i1=end_month(1))
170 call mprintf(.true.,LOGFILE,' = %i',i1=end_month(i))
172 call mprintf(.true.,LOGFILE,' END_DAY = %i',i1=end_day(1))
174 call mprintf(.true.,LOGFILE,' = %i',i1=end_day(i))
176 call mprintf(.true.,LOGFILE,' END_HOUR = %i',i1=end_hour(1))
178 call mprintf(.true.,LOGFILE,' = %i',i1=end_hour(i))
180 call mprintf(.true.,LOGFILE,' END_MINUTE = %i',i1=end_minute(1))
182 call mprintf(.true.,LOGFILE,' = %i',i1=end_minute(i))
184 call mprintf(.true.,LOGFILE,' END_SECOND = %i',i1=end_second(1))
186 call mprintf(.true.,LOGFILE,' = %i',i1=end_second(i))
188 call mprintf(.true.,LOGFILE,' START_DATE = %s',s1=start_date(1))
190 call mprintf(.true.,LOGFILE,' = %s',s1=start_date(i))
192 call mprintf(.true.,LOGFILE,' END_DATE = %s',s1=end_date(1))
194 call mprintf(.true.,LOGFILE,' = %s',s1=end_date(i))
196 call mprintf(.true.,LOGFILE,' INTERVAL_SECONDS = %i',i1=interval_seconds)
197 call mprintf(.true.,LOGFILE,' IO_FORM_GEOGRID = %i',i1=io_form_geogrid)
198 call mprintf(.true.,LOGFILE,' OPT_OUTPUT_FROM_GEOGRID_PATH = %s',s1=opt_output_from_geogrid_path)
199 call mprintf(.true.,LOGFILE,' SUBGRID_RATIO_X = %i',i1=subgrid_ratio_x(1))
201 call mprintf(.true.,LOGFILE,' = %i',i1=subgrid_ratio_x(i))
203 call mprintf(.true.,LOGFILE,' SUBGRID_RATIO_Y = %i',i1=subgrid_ratio_y(1))
205 call mprintf(.true.,LOGFILE,' = %i',i1=subgrid_ratio_y(i))
208 call mprintf(.true.,LOGFILE,' DEBUG_LEVEL = %i',i1=debug_level)
209 call mprintf(.true.,LOGFILE,' ACTIVE_GRID = %l',l1=active_grid(1))
211 call mprintf(.true.,LOGFILE,' = %l',l1=active_grid(i))
213 call mprintf(.true.,LOGFILE,'/')
215 call mprintf(.true.,LOGFILE,'&GEOGRID')
216 call mprintf(.true.,LOGFILE,' PARENT_ID = %i',i1=parent_id(1))
218 call mprintf(.true.,LOGFILE,' = %i',i1=parent_id(i))
220 call mprintf(.true.,LOGFILE,' PARENT_GRID_RATIO = %i',i1=parent_grid_ratio(1))
222 call mprintf(.true.,LOGFILE,' = %i',i1=parent_grid_ratio(i))
224 call mprintf(.true.,LOGFILE,' I_PARENT_START = %i',i1=i_parent_start(1))
226 call mprintf(.true.,LOGFILE,' = %i',i1=i_parent_start(i))
228 call mprintf(.true.,LOGFILE,' J_PARENT_START = %i',i1=j_parent_start(1))
230 call mprintf(.true.,LOGFILE,' = %i',i1=j_parent_start(i))
232 call mprintf(.true.,LOGFILE,' S_WE = %i',i1=s_we(1))
234 call mprintf(.true.,LOGFILE,' = %i',i1=s_we(i))
236 call mprintf(.true.,LOGFILE,' E_WE = %i',i1=e_we(1))
238 call mprintf(.true.,LOGFILE,' = %i',i1=e_we(i))
240 call mprintf(.true.,LOGFILE,' S_SN = %i',i1=s_sn(1))
242 call mprintf(.true.,LOGFILE,' = %i',i1=s_sn(i))
244 call mprintf(.true.,LOGFILE,' E_SN = %i',i1=e_sn(1))
246 call mprintf(.true.,LOGFILE,' = %i',i1=e_sn(i))
248 call mprintf(.true.,LOGFILE,' GEOG_DATA_RES = %s',s1=geog_data_res(1))
250 call mprintf(.true.,LOGFILE,' = %s',s1=geog_data_res(i))
252 call mprintf(.true.,LOGFILE,' DX = %f',f1=dx)
253 call mprintf(.true.,LOGFILE,' DY = %f',f1=dy)
254 call mprintf(.true.,LOGFILE,' MAP_PROJ = %s',s1=map_proj)
255 call mprintf(.true.,LOGFILE,' POLE_LAT = %f',f1=pole_lat)
256 call mprintf(.true.,LOGFILE,' POLE_LON = %f',f1=pole_lon)
257 call mprintf(.true.,LOGFILE,' REF_LAT = %f',f1=ref_lat)
258 call mprintf(.true.,LOGFILE,' REF_LON = %f',f1=ref_lon)
259 call mprintf(.true.,LOGFILE,' REF_X = %f',f1=ref_x)
260 call mprintf(.true.,LOGFILE,' REF_Y = %f',f1=ref_y)
261 call mprintf(.true.,LOGFILE,' TRUELAT1 = %f',f1=truelat1)
262 call mprintf(.true.,LOGFILE,' TRUELAT2 = %f',f1=truelat2)
263 call mprintf(.true.,LOGFILE,' STAND_LON = %f',f1=stand_lon)
264 call mprintf(.true.,LOGFILE,' GEOG_DATA_PATH = %s',s1=geog_data_path)
265 call mprintf(.true.,LOGFILE,' OPT_GEOGRID_TBL_PATH = %s',s1=opt_geogrid_tbl_path)
266 call mprintf(.true.,LOGFILE,'/')
276 ! Convert wrf_core to uppercase letters
278 if (ichar(wrf_core(i:i)) >= 97) wrf_core(i:i) = char(ichar(wrf_core(i:i))-32)
281 ! Before doing anything else, we must have a valid grid type
283 if (wrf_core == 'ARW') then
286 else if (wrf_core == 'NMM') then
291 ! Next, if this is NMM, we need to subtract 1 from the specified E_WE and E_SN;
292 ! for some reason, these two variables need to be set to 1 larger than they
293 ! really ought to be in the WRF namelist, so, to be consistent, we will do
294 ! the same in the WPS namelist
295 if (gridtype == 'E') then
297 e_we(i) = e_we(i) - 1
298 e_sn(i) = e_sn(i) - 1
302 call mprintf(gridtype /= 'C' .and. gridtype /= 'E', ERROR, &
303 'A valid wrf_core must be specified in the namelist. '// &
304 'Currently, only "ARW" and "NMM" are supported.')
306 call mprintf(max_dom > MAX_DOMAINS, ERROR, &
307 'In namelist, max_dom must be <= %i. To run with more'// &
308 ' than %i domains, increase the MAX_DOMAINS parameter.', &
309 i1=MAX_DOMAINS, i2=MAX_DOMAINS)
311 ! Every domain must have a valid parent id
313 call mprintf(parent_id(i) <= 0 .or. parent_id(i) >= i, ERROR, &
314 'In namelist, the parent_id of domain %i must be in '// &
315 'the range 1 to %i.', i1=i, i2=i-1)
318 ! Check for valid geog_data_path
320 do i=1,len(geog_data_path)
321 geog_data_path(j:j) = geog_data_path(i:i)
322 if (geog_data_path(i:i) /= ' ') j = j + 1
324 if (geog_data_path(1:1) == ' ') then
325 call mprintf(.true.,ERROR,'In namelist, geog_data_path must be specified.')
327 j = len_trim(geog_data_path)
328 if (j >= MAX_FILENAME_LEN) then
329 call mprintf(.true.,ERROR, &
330 'In namelist, geog_data_path must be strictly less '// &
331 'than 128 characters in length.')
333 if (geog_data_path(j:j) /= '/') then
334 geog_data_path(j+1:j+1) = '/'
338 ! Paths need to end with a /
339 j = len_trim(opt_geogrid_tbl_path)
340 if (opt_geogrid_tbl_path(j:j) /= '/') then
341 opt_geogrid_tbl_path(j+1:j+1) = '/'
344 j = len_trim(opt_output_from_geogrid_path)
345 if (opt_output_from_geogrid_path(j:j) /= '/') then
346 opt_output_from_geogrid_path(j+1:j+1) = '/'
349 ! Handle IOFORM+100 to do tiled IO
350 if (io_form_geogrid > 100) then
351 do_tiled_output = .true.
352 io_form_geogrid = io_form_geogrid - 100
354 do_tiled_output = .false.
357 ! Check for valid io_form_geogrid
360 io_form_geogrid /= BINARY .and. &
363 io_form_geogrid /= NETCDF .and. &
366 io_form_geogrid /= GRIB1 .and. &
369 call mprintf(.true.,WARN,'Valid io_form_geogrid values are:')
371 call mprintf(.true.,WARN,' %i (=BINARY)',i1=BINARY)
374 call mprintf(.true.,WARN,' %i (=NETCDF)',i1=NETCDF)
377 call mprintf(.true.,WARN,' %i (=GRIB1)',i1=GRIB1)
379 call mprintf(.true.,ERROR,'No valid value for io_form_geogrid was specified in the namelist.')
381 io_form_output = io_form_geogrid
383 ! Convert map_proj to uppercase letters
385 if (ichar(map_proj(i:i)) >= 97) map_proj(i:i) = char(ichar(map_proj(i:i))-32)
388 ! Assign parameters to module variables
389 if ((index(map_proj, 'LAMBERT') /= 0) .and. &
390 (len_trim(map_proj) == len('LAMBERT'))) then
393 else if ((index(map_proj, 'MERCATOR') /= 0) .and. &
394 (len_trim(map_proj) == len('MERCATOR'))) then
395 iproj_type = PROJ_MERC
397 else if ((index(map_proj, 'POLAR') /= 0) .and. &
398 (len_trim(map_proj) == len('POLAR'))) then
401 else if ((index(map_proj, 'ROTATED_LL') /= 0) .and. &
402 (len_trim(map_proj) == len('ROTATED_LL'))) then
403 iproj_type = PROJ_ROTLL
405 else if ((index(map_proj, 'LAT-LON') /= 0) .and. &
406 (len_trim(map_proj) == len('LAT-LON'))) then
407 iproj_type = PROJ_CASSINI
410 call mprintf(.true.,ERROR,&
411 'In namelist, invalid map_proj specified. Valid '// &
412 'projections are "lambert", "mercator", "polar", '// &
413 '"lat-lon", and "rotated_ll".')
416 ! For Cassini / lat-lon projections
417 if (iproj_type == PROJ_CASSINI) then
419 ! If no dx,dy specified, assume global grid
420 if (dx == NAN .and. dy == NAN) then
421 dlondeg = 360. / (e_we(1)-s_we(1)) ! Here, we really do not want e_we-s_we+1
422 dlatdeg = 180. / (e_sn(1)-s_sn(1)) ! Here, we really do not want e_we-s_we+1
425 known_lon = stand_lon + dlondeg/2.
426 known_lat = -90. + dlatdeg/2.
427 dxkm = EARTH_RADIUS_M * PI * 2.0 / (e_we(1)-s_we(1))
428 dykm = EARTH_RADIUS_M * PI / (e_sn(1)-s_sn(1))
430 ! If dx,dy specified, however, assume regional grid
434 dxkm = dlondeg * EARTH_RADIUS_M * PI * 2.0 / 360.0
435 dykm = dlatdeg * EARTH_RADIUS_M * PI * 2.0 / 360.0
436 if (known_lat == NAN .or. known_lon == NAN) then
437 call mprintf(.true.,ERROR,'For lat-lon projection, if dx/dy are specified, '// &
438 'a regional domain is assumed, and a ref_lat,ref_lon must also be specified')
443 ! Manually set truelat2 = truelat1 if truelat2 not specified for Lambert
444 if (iproj_type == PROJ_LC .and. truelat2 == NAN) then
445 call mprintf ((truelat1 == NAN), ERROR, "No TRUELAT1 specified for Lambert conformal projection.")
452 ! For C grid, let ixdim and jydim be the number of velocity points in
453 ! each direction; for E grid, we will put the row and column back
454 ! later; maybe this should be changed to be more clear, though.
456 ixdim(i) = e_we(i) - s_we(i) + 1
457 jydim(i) = e_sn(i) - s_sn(i) + 1
460 if (gridtype == 'E') then
461 phi = dykm*real(jydim(1)-1)/2.
462 lambda = dxkm*real(ixdim(1)-1)
465 ! If the user hasn't supplied a known_x and known_y, assume the center of domain 1
466 if (gridtype == 'E' .and. (known_x /= NAN .or. known_y /= NAN)) then
467 call mprintf(.true.,WARN, &
468 'Namelist variables ref_x and ref_y cannot be used for NMM grids.'// &
469 ' (ref_lat, ref_lon) will refer to the center of the coarse grid.')
470 else if (gridtype == 'C') then
471 if (known_x == NAN .and. known_y == NAN) then
472 known_x = ixdim(1) / 2.
473 known_y = jydim(1) / 2.
474 else if (known_x == NAN .or. known_y == NAN) then
475 call mprintf(.true.,ERROR, &
476 'In namelist.wps, neither or both of ref_x, ref_y must be specified.')
480 ! Checks specific to E grid
481 if (gridtype == 'E') then
483 ! E grid supports only the rotated lat/lon projection
484 if (iproj_type /= PROJ_ROTLL) then
485 call mprintf(.true., WARN, &
486 'For the NMM core, projection type must be rotated '// &
487 'lat/lon (map_proj=rotated_ll)')
488 call mprintf(.true.,WARN,'Projection will be set to rotated_ll')
489 iproj_type = PROJ_ROTLL
492 ! In the following check, add back the 1 that we had to subtract above
493 ! for the sake of being consistent with WRF namelist
494 call mprintf(mod(e_sn(1)+1,2) /= 0, ERROR, &
495 'For the NMM core, E_SN must be an even number for grid %i.', i1=1)
498 call mprintf((parent_grid_ratio(i) /= 3), WARN, &
499 'For the NMM core, the parent_grid_ratio must be 3 for '// &
500 'domain %i. A ratio of 3 will be assumed.', i1=i)
501 parent_grid_ratio(i) = 3
504 ! Check that nests have an acceptable number of grid points in each dimension
506 ! rparent_gridpts = real(ixdim(i)+2)/real(parent_grid_ratio(i))
507 ! if (floor(rparent_gridpts) /= ceiling(rparent_gridpts)) then
508 ! call mprintf(.true.,ERROR,'For nest %i, e_we must be 3n-2 '// &
509 ! 'for some integer n > 1.', &
512 ! rparent_gridpts = real(jydim(i)+2)/real(parent_grid_ratio(i))
513 ! if (floor(rparent_gridpts) /= ceiling(rparent_gridpts)) then
514 ! call mprintf(.true.,ERROR,'For nest %i, e_sn must be 3n-2 '// &
515 ! 'for some odd integer n > 1.', &
525 ! Checks specific to C grid
526 else if (gridtype == 'C') then
528 ! C grid does not support the rotated lat/lon projection
529 call mprintf((iproj_type == PROJ_ROTLL), ERROR, &
530 'Rotated lat/lon projection is not supported for the ARW core. '// &
531 'Valid projecitons are "lambert", "mercator", "polar", and "lat-lon".')
533 ! Check that nests have an acceptable number of grid points in each dimension
535 rparent_gridpts = real(ixdim(i)-1)/real(parent_grid_ratio(i))
536 if (floor(rparent_gridpts) /= ceiling(rparent_gridpts)) then
537 call mprintf(.true.,ERROR,'For nest %i, (e_we-s_we+1) must be one greater '// &
538 'than an interger multiple of the parent_grid_ratio of %i.', &
539 i1=i, i2=parent_grid_ratio(i))
541 rparent_gridpts = real(jydim(i)-1)/real(parent_grid_ratio(i))
542 if (floor(rparent_gridpts) /= ceiling(rparent_gridpts)) then
543 call mprintf(.true.,ERROR,'For nest %i, (e_sn-s_sn+1) must be one greater '// &
544 'than an interger multiple of the parent_grid_ratio of %i.', &
545 i1=i, i2=parent_grid_ratio(i))
549 ! Check that a nest does not extend outside of its parent grid
550 nest_outside = .false.
552 if (i_parent_start(i) >= ixdim(parent_id(i))) then
553 call mprintf(.true.,WARN,'Nest %i cannot have i_parent_start outside of parent domain.',i1=i)
554 nest_outside = .true.
555 else if (i_parent_start(i) + (ixdim(i) - 1)/parent_grid_ratio(i) > ixdim(parent_id(i))) then
556 call mprintf(.true.,WARN,'Nest %i extends beyond its parent grid in the west-east direction.',i1=i)
557 call mprintf(.true.,WARN,' Maximum allowable e_we for current i/j_parent_start is %i.', &
558 i1=(ixdim(parent_id(i))-i_parent_start(i))*parent_grid_ratio(i)+1 )
559 nest_outside = .true.
561 if (j_parent_start(i) >= jydim(parent_id(i))) then
562 call mprintf(.true.,WARN,'Nest %i cannot have j_parent_start outside of parent domain.',i1=i)
563 nest_outside = .true.
564 else if (j_parent_start(i) + (jydim(i) - 1)/parent_grid_ratio(i) > jydim(parent_id(i))) then
565 call mprintf(.true.,WARN,'Nest %i extends beyond its parent grid in the south-north direction.',i1=i)
566 call mprintf(.true.,WARN,' Maximum allowable e_sn for current i/j_parent_start is %i.', &
567 i1=(jydim(parent_id(i))-j_parent_start(i))*parent_grid_ratio(i)+1 )
568 nest_outside = .true.
571 if (nest_outside) then
572 call mprintf(.true.,ERROR,'One or more nested domains extend beyond their parent domains.')
576 parent_ll_x(i) = real(i_parent_start(i))
577 parent_ll_y(i) = real(j_parent_start(i))
578 parent_ur_x(i) = real(i_parent_start(i))+real(ixdim(i))/real(parent_grid_ratio(i))-1.
579 parent_ur_y(i) = real(j_parent_start(i))+real(jydim(i))/real(parent_grid_ratio(i))-1.
580 grid_is_active(i) = active_grid(i)
587 1000 call mprintf(.true.,ERROR,'Error opening file namelist.wps')
589 end subroutine get_grid_params
591 end module gridinfo_module