4 logical :: valid = .false.
10 real :: startLon = 0.0
12 real, dimension(:,:), pointer :: lats => null()
13 real, dimension(:,:), pointer :: lons => null()
14 end type target_mesh_type
20 integer function target_mesh_setup(mesh, lat2d, lon2d) result(stat)
24 type (target_mesh_type), intent(out) :: mesh
25 real, dimension(:,:), target, optional :: lat2d
26 real, dimension(:,:), target, optional :: lon2d
33 character (len=64) :: spec
34 real, parameter :: pi_const = 2.0 * asin(1.0)
39 ! If 2-d arrays of latitude and longitude are provided, we can just
40 ! point to those arrays rather than generate lat/lon values based on
41 ! a specified target domain
43 if (present(lat2d) .and. present(lon2d)) then
46 mesh % nLat = size(lat2d,2)
47 mesh % nLon = size(lon2d,1)
57 ! Try to parse nLat, nLon from target_domain file
59 inquire(file='target_domain', exist=exists)
62 write(0,*) 'Reading target domain specification from file ''target_domain'''
64 mesh % startLat = -90.0
66 mesh % startLon = -180.0
71 open(22, file='target_domain', form='formatted')
72 read(22,fmt='(a)',iostat=iostatus) spec
74 do while (iostatus >= 0)
76 eqIdx = index(spec, '=')
78 if (spec(1:eqIdx-1) == 'nlat') then
79 read(spec(eqIdx+1:len_trim(spec)),fmt=*) mesh % nLat
80 write(0,*) 'Setting nlat = ', mesh % nLat
81 else if (spec(1:eqIdx-1) == 'nlon') then
82 read(spec(eqIdx+1:len_trim(spec)),fmt=*) mesh % nLon
83 write(0,*) 'Setting nlon = ', mesh % nLon
84 else if (spec(1:eqIdx-1) == 'startlat') then
85 read(spec(eqIdx+1:len_trim(spec)),fmt=*) mesh % startLat
86 write(0,*) 'Setting startlat = ', mesh % startLat
87 else if (spec(1:eqIdx-1) == 'endlat') then
88 read(spec(eqIdx+1:len_trim(spec)),fmt=*) mesh % endLat
89 write(0,*) 'Setting endlat = ', mesh % endLat
90 else if (spec(1:eqIdx-1) == 'startlon') then
91 read(spec(eqIdx+1:len_trim(spec)),fmt=*) mesh % startLon
92 write(0,*) 'Setting startlon = ', mesh % startLon
93 else if (spec(1:eqIdx-1) == 'endlon') then
94 read(spec(eqIdx+1:len_trim(spec)),fmt=*) mesh % endLon
95 write(0,*) 'Setting endlon = ', mesh % endLon
97 write(0,*) 'Unrecognized keyword on line ', j, ' of file ''target_domain'': '//spec(1:eqIdx-1)
103 write(0,*) 'Syntax error on line ', j, ' of file ''target_domain'': ''='' not found'
108 read(22,fmt='(a)',iostat=iostatus) spec
114 write(0,*) 'Target domain specification file ''target_domain'' not found.'
115 write(0,*) 'Default 0.5-degree global target domain will be used.'
118 mesh % startLat = -90.0
120 mesh % startLon = -180.0
121 mesh % endLon = 180.0
127 allocate(mesh % lats(1, mesh % nLat))
128 allocate(mesh % lons(mesh % nLon, 1))
130 delta = (mesh % endLat - mesh % startLat) / real(mesh % nLat)
132 mesh % lats(1,i+1) = mesh % startLat + 0.5 * delta + real(i) * delta
133 mesh % lats(1,i+1) = mesh % lats(1,i+1) * pi_const / 180.0
136 delta = (mesh % endLon - mesh % startLon) / real(mesh % nLon)
138 mesh % lons(i+1,1) = mesh % startLon + 0.5 * delta + real(i) * delta
139 mesh % lons(i+1,1) = mesh % lons(i+1,1) * pi_const / 180.0
142 mesh % valid = .true.
144 end function target_mesh_setup
147 integer function target_mesh_free(mesh) result(stat)
151 type (target_mesh_type), intent(inout) :: mesh
156 mesh % valid = .false.
159 mesh % startLat = 0.0
161 mesh % startLon = 0.0
165 ! When irank == 0, we allocated the lats and lons arrays
166 ! internally and should therefore deallocate them
168 if (mesh % irank == 0) then
169 if (associated(mesh % lats)) deallocate(mesh % lats)
170 if (associated(mesh % lons)) deallocate(mesh % lons)
173 end function target_mesh_free
176 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
179 ! Purpose: Remove all space and tab characters from a string, thus
180 ! compressing the string to the left.
181 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
182 subroutine despace(string)
187 character (len=*), intent(inout) :: string
190 integer :: i, j, length, iquoted
197 ! Check for a quote mark
198 if (string(i:i) == '"' .or. string(i:i) == '''') iquoted = mod(iquoted+1,2)
200 ! Check for non-space, non-tab character, or if we are inside quoted
202 if ((string(i:i) /= ' ' .and. string(i:i) /= achar(9)) .or. iquoted == 1) then
203 string(j:j) = string(i:i)
212 end subroutine despace
214 end module target_mesh