V3FIT
All Classes Namespaces Files Functions Variables Enumerations Macros Pages
write_array_generic.f
1  MODULE write_array_generic
2  USE stel_kinds
3 C-----------------------------------------------
4 C L o c a l V a r i a b l e s
5 C-----------------------------------------------
6  REAL(rprec), PARAMETER :: zero = 0
7  INTEGER :: start_count, end_count, length, line
8  CHARACTER(LEN=*), PARAMETER :: adyes = "yes", adno = "no"
9  CHARACTER(LEN=3) :: adv
10 
11  INTERFACE write_array
12  MODULE PROCEDURE write_array_section_real,
13  1 write_array_section_logical,
14  2 write_array_section_integer
15  END INTERFACE
16 
17  PRIVATE :: parse_name
18 
19  CONTAINS
20 
21  SUBROUTINE write_array_section_real(iunit, name, array, n1,
22  1 ndim2, low_index)
23  IMPLICIT NONE
24 C-----------------------------------------------
25 C D u m m y A r g u m e n t s
26 C-----------------------------------------------
27  INTEGER, INTENT(in) :: iunit, n1
28  INTEGER, INTENT(in), OPTIONAL :: ndim2, low_index
29  CHARACTER(len=*), INTENT(in) :: name
30  REAL(rprec), DIMENSION(n1), INTENT(in) :: array
31  REAL(rprec) :: temp
32  CHARACTER(len=LEN_TRIM(name)+20) :: parse
33  CHARACTER(len=40+LEN_TRIM(name)) :: line1,line2
34 C-----------------------------------------------
35 !
36 ! this subroutine writes repeated namelist entries in n*value format to save space
37 ! in namelist file connected to "iunit"
38 !
39 ! n1 = extent of 1st dimension of array
40 ! ndim2 = row (2nd dimension) of array, if present
41 !
42  parse = name
43  CALL parse_name (parse, n1, ndim2, low_index)
44 
45  IF (n1 <= 0) THEN
46  RETURN
47  ELSE IF (n1 == 1) THEN
48  WRITE (line2, 990) array(1)
49  line1 = trim(parse) // adjustl(line2)
50  WRITE (iunit,980) trim(line1)
51 
52  ELSE IF (all(array(1) == array(2:n1))) THEN
53  WRITE (line1, *) n1
54  WRITE (line2, 990) array(1)
55  line1 = trim(parse) // trim(adjustl(line1))
56  1 // '*' // adjustl(line2)
57  WRITE (iunit,980) trim(line1)
58 
59  ELSE
60 ! Look for repeats within the array
61  start_count = 1
62  line = 1
63  WRITE(iunit, 980, advance="no") trim(parse)
64  DO WHILE (start_count .le. n1)
65  temp = array(start_count)
66  end_count = start_count
67  DO WHILE (end_count .lt. n1)
68  IF (array(end_count+1) .ne. temp) EXIT
69  end_count = end_count+1
70  END DO
71 ! Limit no. records/line and start new record for each array
72 ! If maximum packing desired, eliminate the end_count==n in following test
73  IF (line==3 .or. end_count.eq.n1) THEN
74  adv = "yes"
75  line = 0
76  ELSE
77  adv = "no"
78  line = line+1
79  END IF
80  length = end_count - start_count + 1
81  IF (length > 1) THEN
82  WRITE (line1, *) length
83  WRITE (line2, 990) temp
84  line1 = trim(adjustl(line1)) // '*' // adjustl(line2)
85  WRITE (iunit,980, advance=trim(adv)) trim(line1)
86  ELSE
87  WRITE (line1, 990) temp
88  WRITE (iunit, 980, advance=trim(adv))trim(adjustl(line1))
89  END IF
90  start_count = end_count+1
91  END DO
92 
93  980 FORMAT(2x,a)
94  990 FORMAT(1p,e21.14)
95 
96  END IF
97 
98  END SUBROUTINE write_array_section_real
99 
100 
101  SUBROUTINE write_array_section_logical(iunit, name, larray, n1,
102  1 ndim2, low_index)
103  IMPLICIT NONE
104 C-----------------------------------------------
105 C D u m m y A r g u m e n t s
106 C-----------------------------------------------
107  INTEGER, INTENT(in) :: iunit, n1
108  INTEGER, INTENT(in), OPTIONAL :: ndim2, low_index
109  CHARACTER(LEN=*), INTENT(in) :: name
110  LOGICAL, DIMENSION(n1), INTENT(in) :: larray
111  LOGICAL :: ltemp
112  CHARACTER(len=LEN_TRIM(name)+20) :: parse
113 C-----------------------------------------------
114 !
115 ! this subroutine writes repeated namelist entries in n*value format to save space
116 ! in namelist file connected to "iunit"
117 !
118 ! n1 = extent of 1st dimension of array
119 ! ndim2 = row (2nd dimension) of array, if present
120 !
121  parse = name
122  CALL parse_name (parse, n1, ndim2, low_index)
123 
124  IF (n1 <= 0) THEN
125  RETURN
126  ELSE IF (n1 == 1) THEN
127  WRITE(iunit,990) trim(parse), larray(1)
128 
129  ELSE IF (all(larray(1) .eqv. larray(2:n1))) THEN
130  WRITE(iunit,993) trim(parse), n1, larray(1)
131 
132  ELSE
133 ! Look for repeats within the array
134  start_count = 1
135  line = 1
136  WRITE(iunit, 980, advance="no") trim(parse)
137  DO WHILE (start_count .le. n1)
138  ltemp = larray(start_count)
139  end_count = start_count
140  DO WHILE (end_count .lt. n1)
141  IF (larray(end_count+1) .neqv. ltemp) EXIT
142  end_count = end_count+1
143  END DO
144 ! Limit no. records/line and start new record for each array
145 ! If maximum packing desired, eliminate the end_count==n in following test
146  IF (line==20 .or. end_count.eq.n1) THEN
147  adv = "yes"
148  line = 0
149  ELSE
150  adv = "no"
151  line = line+1
152  END IF
153  length = end_count - start_count + 1
154  IF (length > 1) THEN
155  WRITE (iunit, 982, advance=trim(adv)) length, ltemp
156  ELSE
157  WRITE (iunit, 984, advance=trim(adv)) ltemp
158  END IF
159  start_count = end_count+1
160  END DO
161 
162  980 FORMAT(2x,a)
163  982 FORMAT(2x,i4,'*',l1)
164  984 FORMAT(2x,l1)
165  990 FORMAT(2x,a,l1)
166  993 FORMAT(2x,a,i4,'*',l1)
167 
168  END IF
169 
170  END SUBROUTINE write_array_section_logical
171 
172  SUBROUTINE write_array_section_integer(iunit, name, iarray, n1,
173  1 ndim2, low_index)
174  IMPLICIT NONE
175 C-----------------------------------------------
176 C D u m m y A r g u m e n t s
177 C-----------------------------------------------
178  INTEGER, INTENT(in) :: iunit, n1
179  INTEGER, INTENT(in), OPTIONAL :: ndim2, low_index
180  CHARACTER(len=*), INTENT(in) :: name
181  INTEGER, DIMENSION(n1), INTENT(in) :: iarray
182 ! INTEGER, DIMENSION(:), INTENT(in) :: iarray
183  INTEGER :: temp
184  CHARACTER(len=LEN_TRIM(name)+20) :: parse
185  CHARACTER(len=40+LEN_TRIM(name)) :: line1,line2
186 C-----------------------------------------------
187 !
188 ! this subroutine writes repeated namelist entries in n*value format to save space
189 ! in namelist file connected to "iunit"
190 !
191 ! n1 = extent of 1st dimension of array
192 ! ndim2 = row (2nd dimension) of array, if present
193 !
194  parse = name
195  CALL parse_name (parse, n1, ndim2, low_index)
196 
197  IF (n1 <= 0) THEN
198  RETURN
199  ELSE IF (n1 == 1) THEN
200  WRITE (line1, *) iarray(1)
201  line2 = trim(parse) // adjustl(line1)
202  WRITE (iunit, 980) trim(line2)
203 
204  ELSE IF (all(iarray(1) == iarray(2:n1))) THEN
205  WRITE (line1, *) n1
206  WRITE (line2, *) iarray(1)
207  line1 = trim(parse) // trim(adjustl(line1))
208  1 // '*' // adjustl(line2)
209  WRITE (iunit,980) trim(line1)
210 
211  ELSE
212 ! Look for repeats within the array
213  start_count = 1
214  line = 1
215  WRITE(iunit, 980, advance="no") trim(parse)
216  DO WHILE (start_count .le. n1)
217  temp = iarray(start_count)
218  end_count = start_count
219  DO WHILE (end_count .lt. n1)
220  IF (iarray(end_count+1) .ne. temp) EXIT
221  end_count = end_count+1
222  END DO
223 ! Limit no. records/line and start new record for each array
224 ! If maximum packing desired, eliminate the end_count==n in following test
225  IF (line==8 .or. end_count.eq.n1) THEN
226  adv = "yes"
227  line = 0
228  ELSE
229  adv = "no"
230  line = line+1
231  END IF
232  length = end_count - start_count + 1
233  IF (length > 1) THEN
234  WRITE (line1, *) length
235  WRITE (line2, *) temp
236  line1 = trim(adjustl(line1)) // '*' // adjustl(line2)
237  WRITE (iunit,980, advance=trim(adv)) trim(line1)
238  ELSE
239  WRITE (line1, *) temp
240  WRITE (iunit, 980, advance=trim(adv))
241  1 trim(adjustl(line1))
242  END IF
243  start_count = end_count+1
244  END DO
245 
246  980 FORMAT(2x,a)
247 
248  END IF
249 
250  END SUBROUTINE write_array_section_integer
251 
252 
253  SUBROUTINE parse_name (name, n1, ndim2, low_index)
254  CHARACTER(LEN=*), INTENT(inout) :: name
255  INTEGER, INTENT(in) :: n1
256  INTEGER, INTENT(in), OPTIONAL :: ndim2, low_index
257  CHARACTER(LEN=*), PARAMETER :: fmt(5) = (/ '(a,i1)',
258  1 '(a,i2)', '(a,i3)', '(a,i4)', '(a,i5)' /)
259  CHARACTER(len=LEN(name)+100) :: parse
260  CHARACTER(len=100) :: fmt_string
261  INTEGER :: i1, istart, iend
262 
263  istart = 1
264  IF (PRESENT(low_index)) istart = low_index
265  i1 = indexformat(istart)
266  WRITE (parse, fmt(i1)) trim(name) // "(", istart
267 
268  iend = istart + n1 - 1
269  i1 = indexformat(iend)
270  WRITE (parse, fmt(i1)) trim(parse) // ":", iend
271 
272  IF (PRESENT(ndim2)) THEN
273  i1 = indexformat(ndim2)
274  WRITE (parse, fmt(i1)) trim(parse) // ",", ndim2
275  END IF
276 
277  name = trim(parse) // ")="
278 
279  END SUBROUTINE parse_name
280 
281  INTEGER FUNCTION indexformat(number)
282  INTEGER, INTENT(in) :: number
283  INTEGER :: i1
284 
285  SELECT CASE (abs(number))
286  CASE (1000:9999)
287  i1 = 4
288  CASE (100:999)
289  i1 = 3
290  CASE (10:99)
291  i1 = 2
292  CASE DEFAULT
293  i1 = 1
294  END SELECT
295 
296  IF (number < 0) i1 = i1 + 1
297  indexformat = i1
298 
299  END FUNCTION indexformat
300 
301  END MODULE write_array_generic
write_array_generic::write_array
Definition: write_array_generic.f:11