GRIDMAN
grid managment library
template.f
Go to the documentation of this file.
1 C> @file formats/template.f
2 C> Read/write coordinates in simple template file (DG template)
3 C GRIDMAN, grid managment library. Author: Vladislav Kotov, v.kotov@fz-juelich.de
4 
5 ! Copyright (c) 2017 Forschungszentrum Juelich GmbH
6 ! Vladislav Kotov
7 !
8 ! This file is part of GRIDMAN.
9 !
10 ! GRIDMAN is free software: you can redistribute it and/or modify
11 ! it under the terms of the GNU General Public License as published by
12 ! the Free Software Foundation, either version 3 of the License, or
13 ! (at your option) any later version.
14 !
15 ! GRIDMAN is distributed in the hope that it will be useful,
16 ! but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ! GNU General Public License for more details.
19 !
20 ! You should have received a copy of the GNU General Public License
21 ! along with GRIDMAN. If not, see <http://www.gnu.org/licenses/>.
22 
23 C*********************************************************************************
24 C> Read planar coordinates from a simple template file (DG template)
25 C>
26 C> Simple template file defines geometry as a set of plygons (closed or open).
27 C> See DG manual, I.16.1. Each lines defines a pair of coordinates (in Millimeter!),
28 C> polygons are separated by empty lines:
29 C>
30 C> X1_1 Y1_1
31 C> X2_2 Y2_2
32 C>
33 C> X1_3 Y1_3
34 C> X2_4 Y2_4
35 C> X2_5 Y2_5
36 C>
37 C> .....
38 C> X1_L-1 Y1_L-1
39 C> X1_L Y1_L
40 C>
41 C> end of file
42 C>
43 C> Lines containing '>>' are comments, they are ignored
44 C>
45 C> WARNING: arrays N, X, Y must NOT be allcoated -
46 C> they are allocated inside the subroutine.
47 C> Arrays which already exist will be re-allocated
48 C>
49 C> GRIDMAN_TEMPLATE_READ_ARRAY AND GRIDMAN_TEMPLATE_READ_GRID
50 C> are combined in the interface GRIDMAN_TEMPLATE_READ
51 C>
52 C*********************************************************************************
53  SUBROUTINE gridman_template_read_array(FNAME,M,N,L,X,Y,IERR)
55  IMPLICIT NONE
56  INTRINSIC trim,sum,index,len_trim
57 C> String containing the name of input file
58  CHARACTER(*),INTENT(IN) :: FNAME
59 C> Number of polygons
60  INTEGER(GRIDMAN_SP),INTENT(OUT) :: M
61 C> Number of vertices in each polygon, N(M)
62  INTEGER(GRIDMAN_SP),ALLOCATABLE :: N(:)
63 C> Total number of points, L=SUM(N)
64  INTEGER(GRIDMAN_SP),INTENT(OUT) :: L
65 C> X coordinates of the vertices, X(L)
66  REAL(GRIDMAN_DP),ALLOCATABLE :: X(:)
67 C> Y coordinates of the vertices, Y(L)
68  REAL(GRIDMAN_DP),ALLOCATABLE :: Y(:)
69 C> Error code
70  INTEGER,INTENT(OUT) :: IERR
71 
72  INTEGER(GRIDMAN_SP) :: IP,IV,IV1
73  INTEGER :: IO
74  CHARACTER(256) :: STR,STR0
75 
76  IF(gridman_dbg)
77  w WRITE(gridman_unit,*) "Starting GRIDMAN_TEMPLATE_READ_ARRAY"
78 
79  ierr=0
80  m=0
81  l=0
82 
83  OPEN (unit=3,status='OLD',access='SEQUENTIAL',
84  o form='FORMATTED',file=fname,iostat=io)
85  IF(io.NE.0) THEN
86  ierr=300
87  WRITE(gridman_unit,*) "ERROR in GRIDMAN_TEMPLATE_READ_ARRAY: ",
88  o "can not open ",trim(fname)
89  RETURN
90  END IF
91 
92  IF(gridman_dbg)
93  w WRITE(gridman_unit,*)
94  w "GRIDMAN_TEMPLATE_READ_ARRAY: count polygons"
95 
96 C COUNT POLYGONS
97  m=0
98  10 READ(3,'(A)',end=20,iostat=io) str
99  IF(io.NE.0) GOTO 100
100  IF(len_trim(str).LT.1) m=m+1 !M IS INCREMENTED IF EMPTY LINE
101  str0=str
102  GOTO 10
103  20 CONTINUE
104  IF(len_trim(str0).GT.1) m=m+1 !... OR END OF FILE AND
105  !PREVIOUS LINE WAS NOT EMPTY
106 
107  IF(gridman_dbg)
108  w WRITE(gridman_unit,*) "GRIDMAN_TEMPLATE_READ_ARRAY: M= ",m
109 
110 C ALLOCATE N
111  IF(m.LT.1) THEN
112  WRITE(gridman_unit,*) "ERROR in GRIDMAN_TEMPLATE_READ_ARRAY: ",
113  o "no polygons found in file ",trim(fname)
114  ierr=300
115  GOTO 200
116  END IF
117  IF(ALLOCATED(n)) DEALLOCATE(n,stat=io)
118  ALLOCATE(n(m),stat=io)
119  IF(io.NE.0) THEN
120  ierr=200
121  WRITE(gridman_unit,*) "ERROR in GRIDMAN_TEMPLATE_READ_ARRAY: ",
122  w "cannot perform allocation, M ",m
123  GOTO 200
124  END IF
125 
126  IF(gridman_dbg)
127  w WRITE(gridman_unit,*)
128  w "GRIDMAN_TEMPLATE_READ_ARRAY: count lengths"
129 
130 C COUNT LENGTH OF EACH POLYGON
131  rewind(3)
132  ip=1
133  n(ip)=0
134  11 READ(3,'(A)',end=21,iostat=io) str
135  IF(io.NE.0) GOTO 100
136 C IF NOT COMMENT THEN INCREMENT THE POLYGON LENGTH
137  IF(index(str,'>>').GT.0) GOTO 11 !SKIP COMMENT
138  IF(len_trim(str).LT.1) THEN
139 C IF EMPTY LINE THEN THE POLYGON INDEX IS INCREMENTED
140  ip=ip+1
141  IF(ip.LT.m+1) n(ip)=0
142  ELSE
143 C OTHERWISE THE POLYGON LENGTH IS INCREMENTED
144  n(ip)=n(ip)+1
145  END IF
146 
147  GOTO 11
148  21 CONTINUE
149 
150  IF(gridman_dbg)
151  w WRITE(gridman_unit,*) "GRIDMAN_TEMPLATE_READ_ARRAY: N= ",n
152 
153 C ALLOCATE ARRAYS OF COORDINATES
154  l=sum(n)
155  IF(l.LT.1) THEN
156  WRITE(gridman_unit,*) "ERROR in GRIDMAN_TEMPLATE_READ_ARRAY: ",
157  o "all polygons are empty in file ",trim(fname)
158  ierr=300
159  GOTO 200
160  END IF
161  rewind(3)
162  IF(ALLOCATED(x)) DEALLOCATE(x,stat=io)
163  IF(ALLOCATED(y)) DEALLOCATE(y,stat=io)
164  ALLOCATE(x(l),y(l),stat=io)
165  IF(io.NE.0) THEN
166  ierr=200
167  WRITE(gridman_unit,*) "ERROR in GRIDMAN_TEMPLATE_READ_ARRAY: ",
168  w "cannot perform allocation, L ",l
169  GOTO 200
170  END IF
171 
172  IF(gridman_dbg)
173  w WRITE(gridman_unit,*) "GRIDMAN_TEMPLATE_READ_ARRAY: L= ",l
174 
175  IF(gridman_dbg)
176  w WRITE(gridman_unit,*)
177  w "GRIDMAN_TEMPLATE_READ_ARRAY: read coordinates"
178 
179 C READ COORDINATES
180  ip=1
181  iv=0 !INDEX OF CURRENT VERTEX
182  iv1=0 !ONLY FOR DIAGNOSTIC
183  12 READ(3,'(A)',end=22,iostat=io) str
184  IF(io.NE.0) GOTO 100
185  IF(index(str,'>>').GT.0) GOTO 12 !SKIP COMMENT
186  IF(len_trim(str).LT.1) THEN
187 C INCREMENT THE POLYGON INDEX IF EMPTY LINE
188  IF(iv1.NE.n(ip)) THEN
189  WRITE(gridman_unit,*) "ERROR in GRIDMAN_TEMPLATE_READ_ARRAY: ",
190  w "internal error - counts do not match"
191  WRITE(gridman_unit,*) " IP, IV1, N ",ip,iv1,n(ip)
192  ierr=400
193  GOTO 200
194  END IF
195  iv1=0
196  ip=ip+1
197  ELSE
198 C OTHERWISE READ COORDINATES
199  iv=iv+1
200  iv1=iv1+1
201  IF(iv.GT.l) THEN
202  WRITE(gridman_unit,*) "ERROR in GRIDMAN_TEMPLATE_READ_ARRAY: ",
203  w "internal error - counts do not match"
204  WRITE(gridman_unit,*) " IP, IV, L ",ip,iv,l
205  ierr=400
206  GOTO 200
207  END IF
208  READ(str,*,iostat=io) x(iv),y(iv)
209  IF(io.NE.0) GOTO 100
210  END IF
211  GOTO 12
212  22 CONTINUE
213 
214  CLOSE(3)
215 
216  IF(gridman_dbg)
217  w WRITE(gridman_unit,*) "GRIDMAN_TEMPLATE_READ_ARRAY finished"
218 
219  RETURN
220 
221  100 ierr=300
222  WRITE(gridman_unit,*) "ERROR in GRIDMAN_TEMPLATE_READ_ARRAY: ",
223  w "cannot read template from file ",trim(fname)
224  200 IF(ALLOCATED(n)) DEALLOCATE(n,stat=io)
225  IF(ALLOCATED(x)) DEALLOCATE(x,stat=io)
226  IF(ALLOCATED(y)) DEALLOCATE(y,stat=io)
227  CLOSE(3)
228  m=0
229  l=0
230 
231  END SUBROUTINE gridman_template_read_array
232 
233 
234 C*********************************************************************************
235 C> Write coordinates into simple template file (DG template)
236 C>
237 C> See GRIDMAN_TEMPLATE_READ_ARRAY for description of the format
238 C>
239 C> GRIDMAN_TEMPLATE_WRITE_ARRAY and GRIDMAN_TEMPLATE_WRITE_GRID
240 C> are combined in the interface GRIDMAN_TEMPLATE_WRITE
241 C*********************************************************************************
242  SUBROUTINE gridman_template_write_array(FNAME,M,N,L,X,Y,IERR)
244  IMPLICIT NONE
245  INTRINSIC trim,sum
246 C> String containing the name of input file
247  CHARACTER(*),INTENT(IN) :: FNAME
248 C> Number of polygons
249  INTEGER(GRIDMAN_SP),INTENT(IN) :: M
250 C> Number of vertices in each polygon
251  INTEGER(GRIDMAN_SP),INTENT(IN) :: N(m)
252 C> Total number of points, must be L=SUM(N)
253  INTEGER(GRIDMAN_SP),INTENT(IN) :: L
254 C> X coordinates of the vertices
255  REAL(GRIDMAN_DP),INTENT(IN) :: X(l)
256 C> Y coordinates of the vertices
257  REAL(GRIDMAN_DP),INTENT(IN) :: Y(l)
258 C> Error code
259  INTEGER,INTENT(OUT) :: IERR
260 
261  INTEGER(GRIDMAN_SP) :: L1,IP,IV,IL
262  INTEGER :: IO
263 
264  IF(gridman_dbg)
265  w WRITE(gridman_unit,*) "Starting GRIDMAN_TEMPLATE_READ_ARRAY"
266 
267  ierr=0
268 
269  IF(m.LT.1) THEN
270  WRITE(gridman_unit,*) "ERROR in GRIDMAN_TEMPLATE_WRITE_ARRAY: ",
271  w "incorrect number of polygons, M ",m
272  ierr=100
273  RETURN
274  END IF
275 
276  l1=sum(n)
277  IF(l1.NE.l) THEN
278  WRITE(gridman_unit,*) "ERROR in GRIDMAN_TEMPLATE_WRITE_ARRAY: ",
279  w "incorrect number of points - L=/=sum(N)"
280  WRITE(gridman_unit,*) "L, SUM(N) ",l,l1
281  ierr=100
282  RETURN
283  END IF
284 
285  OPEN (unit=3,status='REPLACE',access='SEQUENTIAL',
286  o form='FORMATTED',file=fname,iostat=io)
287  IF(io.NE.0) THEN
288  ierr=300
289  WRITE(gridman_unit,*) "ERROR in GRIDMAN_TEMPLATE_WRITE_ARRAY: ",
290  o "can not open ",trim(fname)
291  RETURN
292  END IF
293 
294  il=0
295  DO ip=1,m
296  DO iv=1,n(ip)
297  il=il+1
298  IF(il.GT.l) THEN
299  WRITE(gridman_unit,*)
300  w "ERROR in GRIDMAN_TEMPLATE_WRITE_ARRAY: ",
301  w "internal error - too large index, IL, L ",il,l
302  ierr=400
303  CLOSE(3)
304  RETURN
305  END IF
306  WRITE(3,*,iostat=io) x(il), y(il)
307  IF(io.NE.0) GOTO 100
308  END DO
309  WRITE(3,*,iostat=io)
310  IF(io.NE.0) GOTO 100
311  END DO
312 
313  CLOSE(3)
314 
315  IF(gridman_dbg)
316  w WRITE(gridman_unit,*) "GRIDMAN_TEMPLATE_WRITE_ARRAY finished"
317 
318  RETURN
319 
320  100 ierr=300
321  CLOSE(3)
322  WRITE(gridman_unit,*) "ERROR in GRIDMAN_TEMPLATE_WRITE_ARRAY: ",
323  w "cannot write into file ",trim(fname)
324 
325  END SUBROUTINE gridman_template_write_array
integer, save, public gridman_unit
Index of the standard output unit.
Definition: gridman.f:116
integer, parameter, public gridman_dp
Kind parameter for real numbers.
Definition: gridman.f:93
subroutine gridman_template_read_array(FNAME, M, N, L, X, Y, IERR)
Read planar coordinates from a simple template file (DG template)
Definition: template.f:54
logical, save, public gridman_dbg
Switch for debugging mode.
Definition: gridman.f:122
subroutine gridman_template_write_array(FNAME, M, N, L, X, Y, IERR)
Write coordinates into simple template file (DG template)
Definition: template.f:243
Definition of data types, global constants and variables.
Definition: gridman.f:83
integer, parameter, public gridman_sp
Kind parameter for integer numbers.
Definition: gridman.f:95