-
Notifications
You must be signed in to change notification settings - Fork 1
/
NORTRIP_reading_functions.f90
606 lines (509 loc) · 20.7 KB
/
NORTRIP_reading_functions.f90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
!----------------------------------------------------------------------
! Various functions for manipulating and reading text in tab format
!----------------------------------------------------------------------
!----------------------------------------------------------------------
function match_string_char_2048(match_str,unit_in,unit_output,default_char)
!Finds a leading string and returns the string that follows it
!Tab delimitted before and after
implicit none
character(2048) match_string_char_2048
character (*) match_str,default_char
character(2048) temp_str1,temp_str2,temp_str
integer unit_in,unit_output
integer index_val
temp_str1=''
temp_str2='Not available'
temp_str2=trim(default_char)
rewind(unit_in)
do while (index(temp_str1,match_str).eq.0)
read(unit_in,'(a)',end=10) temp_str
if (temp_str(1:1).eq.'#'.or.temp_str(1:1).eq.'!') goto 5 !If first character is ! or # then ignore and go to next
index_val=index(temp_str,achar(9))
temp_str1=temp_str(1:index_val-1)
temp_str=temp_str(index_val+1:)
index_val=index(temp_str,achar(9))
if (index_val.gt.0) then
temp_str2=temp_str(1:index_val-1)
else
temp_str2=temp_str
endif
5 end do
match_string_char_2048=trim(temp_str2)
if (unit_output.ge.0) then
write(unit_output,'(A40,A3,A)') trim(match_str),' = ',adjustl(trim(match_string_char_2048))
endif
return
10 write(unit_output,'(A)') 'WARNING: No match found to "'//trim(match_str)//'" in input files. Returning: '//trim(default_char)
match_string_char_2048=default_char
end function match_string_char_2048
!----------------------------------------------------------------------
!----------------------------------------------------------------------
function match_string_char(match_str,unit_in,unit_output,default_char)
!Finds a leading string and returns the string that follows it
!Tab delimitted before and after
implicit none
character(256) match_string_char
character (*) match_str,default_char
character(256) temp_str1,temp_str2,temp_str
integer unit_in,unit_output
integer index_val
temp_str1=''
temp_str2='Not available'
temp_str2=trim(default_char)
rewind(unit_in)
do while (index(temp_str1,match_str).eq.0)
read(unit_in,'(a)',end=10) temp_str
if (temp_str(1:1).eq.'#'.or.temp_str(1:1).eq.'!') goto 5 !If first character is ! or # then ignore and go to next
index_val=index(temp_str,achar(9))
temp_str1=temp_str(1:index_val-1)
temp_str=temp_str(index_val+1:)
index_val=index(temp_str,achar(9))
if (index_val.gt.0) then
temp_str2=temp_str(1:index_val-1)
else
temp_str2=temp_str
endif
5 end do
match_string_char=trim(temp_str2)
if (unit_output.ge.0) then
write(unit_output,'(A40,A3,A)') trim(match_str),' = ',adjustl(trim(match_string_char))
endif
return
10 write(unit_output,'(A)') 'WARNING: No match found to "'//trim(match_str)//'" in input files. Returning: '//trim(default_char)
match_string_char=default_char
end function match_string_char
!----------------------------------------------------------------------
!----------------------------------------------------------------------
function match_string_val(match_str,unit_in,unit_output,default_val)
!Finds a leading string and returns the real variable that follows it
!Tab delimitted before and after
implicit none
real match_string_val,default_val
character (*) match_str
character(256) temp_str1,temp_str2,temp_str
integer unit_in,unit_output
integer index_val
match_string_val=default_val
temp_str1=''
temp_str2='Not available'
rewind(unit_in)
do while (index(temp_str1,match_str).eq.0)
read(unit_in,'(a)',end=10) temp_str
if (temp_str(1:1).eq.'#'.or.temp_str(1:1).eq.'!') goto 5 !If first character is ! or # then ignore and go to next
index_val=index(temp_str,achar(9))
!write(*,*) index_val,temp_str
temp_str1=temp_str(1:index_val-1)
temp_str=temp_str(index_val+1:)
index_val=index(temp_str,achar(9))
!write(*,*) index_val,temp_str
if (index_val.gt.0) then
temp_str2=temp_str(1:index_val-1)
else
temp_str2=temp_str
endif
5 end do
!write(*,*) index_val,temp_str2
if (LEN(trim(temp_str2)).gt.0) then
read(temp_str2,*,err=15) match_string_val
else
goto 15
endif
if (unit_output.ge.0) then
write(unit_output,'(A40,A3,ES10.2)') trim(match_str),' = ',match_string_val
endif
return
10 write(unit_output,'(A,f12.3)') 'WARNING: No match found to "'//trim(match_str)//'" in input files. Returning default value: ',match_string_val
return
15 write(unit_output,'(A,f12.3)') 'WARNING: No value found for "'//trim(match_str)//'" in input files. Setting to default: ',match_string_val
end function match_string_val
!----------------------------------------------------------------------
!----------------------------------------------------------------------
function match_string_int(match_str,unit_in,unit_output,default_int)
!Finds a leading string and returns the integer variable that follows it
!Tab delimitted before and after
implicit none
integer match_string_int,default_int
character (*) match_str
character(256) temp_str1,temp_str2,temp_str
integer unit_in,unit_output
integer index_val
match_string_int=default_int
temp_str1=''
temp_str2='Not available'
rewind(unit_in)
do while (index(temp_str1,match_str).eq.0)
read(unit_in,'(a)',end=10) temp_str
if (temp_str(1:1).eq.'#'.or.temp_str(1:1).eq.'!') goto 5 !If first character is ! or # then ignore and go to next
index_val=index(temp_str,achar(9))
temp_str1=temp_str(1:index_val-1)
temp_str=temp_str(index_val+1:)
index_val=index(temp_str,achar(9))
if (index_val.gt.0) then
temp_str2=temp_str(1:index_val-1)
else
temp_str2=temp_str
endif
5 end do
if (LEN(trim(temp_str2)).gt.0) then
read(temp_str2,*,err=15) match_string_int
else
goto 15
endif
if (unit_output.ge.0) then
write(unit_output,'(A40,A3,I10)') trim(match_str),' = ',match_string_int
endif
return
10 write(unit_output,'(A,i)') 'WARNING: No match found to "'//trim(match_str)//'" in input files. Setting to ',match_string_int
return
15 write(unit_output,'(A,i)') 'WARNING: No value found for "'//trim(match_str)//'" in input files. Setting to ',match_string_int
end function match_string_int
!----------------------------------------------------------------------
!----------------------------------------------------------------------
function read_string_val(unit_in,unit_output)
!Reads a leading string and returns the real variable that follows it
!Tab delimitted between.
implicit none
integer n_val
real read_string_val
character(256) temp_str1,temp_str2,temp_str
integer unit_in,unit_output
integer index_val
read_string_val=-999
temp_str1=''
temp_str2='Not available'
read(unit_in,'(a)',end=10) temp_str
index_val=index(temp_str,achar(9))
temp_str1=temp_str(1:index_val-1)
temp_str=temp_str(index_val+1:)
index_val=index(temp_str,achar(9))
if (index_val.gt.0) then
temp_str2=temp_str(1:index_val-1)
else
temp_str2=temp_str
endif
read(temp_str2,*) read_string_val
if (unit_output.ge.0) then
write(unit_output,'(A40,A3,es10.2)') trim(temp_str1),' = ',read_string_val
endif
return
10 write(unit_output,*) 'ERROR: End of file read during read_string_val'
end function read_string_val
!----------------------------------------------------------------------
!----------------------------------------------------------------------
subroutine string_split_tab(unit_in,unit_output,out_str)
!Reads the next string in unit_in and returns the string that follows the first tab
implicit none
integer unit_in,unit_output
integer index_val
character(256) temp_str
character(256) out_str(2)
read(unit_in,'(a)',ERR=10) temp_str
index_val=index(temp_str,achar(9))
out_str(1)=temp_str(1:index_val-1)
out_str(2)=temp_str(index_val+1:)
return
10 write(unit_output,*) 'ERROR: End of file read during string_split_tab'
end subroutine string_split_tab
!----------------------------------------------------------------------
!----------------------------------------------------------------------
subroutine read_line_int1(unit_in,unit_output,val1)
!Reads a leading string and returns the integer variable that follows it
!Tab delimitted between.
implicit none
character(256) header_str,val_str,temp_str
integer unit_in,unit_output
integer index_val
integer val1
read(unit_in,'(a)',end=10) temp_str
index_val=index(temp_str,achar(9))
header_str=temp_str(1:index_val-1)
val_str=temp_str(index_val+1:)
!Only take the first value
index_val=index(val_str,achar(9))
if (index_val.gt.0) then
val_str=val_str(1:index_val-1)
endif
read(val_str,*) val1
if (unit_output.ge.0) then
write(unit_output,'(A40,A3,i10)') trim(header_str),' = ',val1
endif
return
10 write(unit_output,*) 'ERROR: End of file read during read_line_int1'
end subroutine read_line_int1
!----------------------------------------------------------------------
!----------------------------------------------------------------------
subroutine read_line_int2(unit_in,unit_output,val1,val2)
!Reads a leading string and returns the real variable that follows it
!Tab delimitted between.
implicit none
character(256) header_str,val_str,temp_str
integer unit_in,unit_output
integer index_val
integer val1,val2
read(unit_in,'(a)',end=10) temp_str
index_val=index(temp_str,achar(9))
header_str=temp_str(1:index_val-1)
val_str=temp_str(index_val+1:)
read(val_str,*) val1,val2
if (unit_output.ge.0) then
write(unit_output,'(A40,A3,2i10)') trim(header_str),' = ',val1,val2
endif
return
10 write(unit_output,*) 'ERROR: End of file read during read_line_int2'
end subroutine read_line_int2
!----------------------------------------------------------------------
!----------------------------------------------------------------------
subroutine read_line_val1(unit_in,unit_output,val1)
!Reads a leading string and returns the real variable that follows it
!Tab delimitted between.
implicit none
character(256) header_str,val_str,temp_str
integer unit_in,unit_output
integer index_val
real val1
read(unit_in,'(a)',end=10) temp_str
index_val=index(temp_str,achar(9))
header_str=temp_str(1:index_val-1)
val_str=temp_str(index_val+1:)
!Only take the first value
index_val=index(val_str,achar(9))
if (index_val.gt.0) then
val_str=val_str(1:index_val-1)
endif
read(val_str,*) val1
if (unit_output.ge.0) then
write(unit_output,'(A40,A3,es10.2)') trim(header_str),' = ',val1
endif
return
10 write(unit_output,*) 'ERROR: End of file read during read_line_val1'
end subroutine read_line_val1
!----------------------------------------------------------------------
!----------------------------------------------------------------------
subroutine read_line_val2(unit_in,unit_output,val1,val2)
!Reads a leading string and returns the real variable that follows it
!Tab delimitted between.
implicit none
character(256) header_str,val_str,temp_str
integer unit_in,unit_output
integer index_val
real val1,val2
read(unit_in,'(a)',end=10) temp_str
index_val=index(temp_str,achar(9))
header_str=temp_str(1:index_val-1)
val_str=temp_str(index_val+1:)
read(val_str,*) val1,val2
if (unit_output.ge.0) then
write(unit_output,'(A40,A3,2es10.2)') trim(header_str),' = ',val1,val2
endif
return
10 write(unit_output,*) 'ERROR: End of file read during read_line_val2'
end subroutine read_line_val2
!----------------------------------------------------------------------
!----------------------------------------------------------------------
subroutine read_line_val3(unit_in,unit_output,val1,val2,val3)
!Reads a leading string and returns the real variable that follows it
!Tab delimitted between.
implicit none
character(256) header_str,val_str,temp_str
integer unit_in,unit_output
integer index_val
real val1,val2,val3
read(unit_in,'(a)',end=10) temp_str
index_val=index(temp_str,achar(9))
header_str=temp_str(1:index_val-1)
val_str=temp_str(index_val+1:)
read(val_str,*) val1,val2,val3
if (unit_output.ge.0) then
write(unit_output,'(A40,A3,3es10.2)') trim(header_str),' = ',val1,val2,val3
endif
return
10 write(unit_output,*) 'ERROR: End of file read during read_line_val3'
end subroutine read_line_val3
!----------------------------------------------------------------------
!----------------------------------------------------------------------
subroutine read_line_val1or3(unit_in,unit_output,val1,val2,val3)
!Reads a leading string and returns the real variable that follows it
!Tab delimitted between.
implicit none
character(256) header_str,val_str,temp_str
integer unit_in,unit_output
integer index_val,verify_val,i
real val1,val2,val3
read(unit_in,'(a)',end=10) temp_str
index_val=index(temp_str,achar(9))
header_str=temp_str(1:index_val-1)
val_str=temp_str(index_val+1:)
read(val_str,*) val1
!Go to next tab
index_val=index(val_str,achar(9))
if (index_val.eq.0) then
val2=0;val3=0
else
val_str=val_str(index_val+1:)
!Check for any more numbers. This is the most redickuless thing I have ever done but do not know what else to do
index_val=0
do i=48,57
index_val=index_val+index(val_str,achar(i))
!write(*,*) i,index_val,achar(i)
enddo
if (index_val.eq.0) then
val2=0;val3=0
else
!Assume there are two more
read(val_str,*) val2,val3
endif
endif
if (unit_output.ge.0) then
write(unit_output,'(A40,A3,3es10.2)') trim(header_str),' = ',val1,val2,val3
endif
return
10 write(unit_output,*) 'ERROR: End of file read during read_line_val3'
end subroutine read_line_val1or3
!----------------------------------------------------------------------
!----------------------------------------------------------------------
subroutine read_line_val4(unit_in,unit_output,val1,val2,val3,val4)
!Reads a leading string and returns the real variable that follows it
!Tab delimitted between.
implicit none
character(256) header_str,val_str,temp_str
integer unit_in,unit_output
integer index_val
real val1,val2,val3,val4
read(unit_in,'(a)',end=10) temp_str
index_val=index(temp_str,achar(9))
header_str=temp_str(1:index_val-1)
val_str=temp_str(index_val+1:)
read(val_str,*) val1,val2,val3,val4
if (unit_output.ge.0) then
write(unit_output,'(A40,A3,4es10.2)') trim(header_str),' = ',val1,val2,val3,val4
endif
return
10 write(unit_output,*) 'ERROR: End of file read during read_line_val4'
end subroutine read_line_val4
!----------------------------------------------------------------------
!----------------------------------------------------------------------
subroutine read_line_val5(unit_in,unit_output,val1,val2,val3,val4,val5)
!Reads a leading string and returns the real variable that follows it
!Tab delimitted between.
implicit none
character(256) header_str,val_str,temp_str
integer unit_in,unit_output
integer index_val
real val1,val2,val3,val4,val5
read(unit_in,'(a)',end=10) temp_str
index_val=index(temp_str,achar(9))
header_str=temp_str(1:index_val-1)
val_str=temp_str(index_val+1:)
read(val_str,*) val1,val2,val3,val4,val5
if (unit_output.ge.0) then
write(unit_output,'(A40,A3,5es10.2)') trim(header_str),' = ',val1,val2,val3,val4,val5
endif
return
10 write(unit_output,*) 'ERROR: End of file read during read_line_val5'
end subroutine read_line_val5
!----------------------------------------------------------------------
!----------------------------------------------------------------------
subroutine match_string_multi_int(match_str,unit_in,unit_output,val,n_val)
!Finds a leading string and returns all the integer variables that follows it
!Tab delimitted before and free format after
implicit none
integer n_val
integer val(n_val)
character (*) match_str
character(256) temp_str1,temp_str2,temp_str
integer unit_in,unit_output
integer index_val
val=-999
temp_str1=''
temp_str2='Not available'
rewind(unit_in)
do while (index(temp_str1,match_str).eq.0)
read(unit_in,'(a)',end=10) temp_str
if (temp_str(1:1).eq.'#'.or.temp_str(1:1).eq.'!') goto 5 !If first character is ! or # then ignore and go to next
index_val=index(temp_str,achar(9))
temp_str1=temp_str(1:index_val-1)
temp_str=temp_str(index_val+1:)
index_val=index(temp_str,achar(9))
!if (index_val.gt.0) then
! temp_str2=temp_str(1:index_val-1)
!else
! temp_str2=temp_str
!endif
5 end do
if (LEN(trim(temp_str)).gt.0) then
read(temp_str,*) val(1:n_val)
else
goto 15
endif
if (unit_output.ge.0) then
write(unit_output,'(A40,A3,<n_val>I10)') trim(match_str),' = ',val
endif
return
10 write(unit_output,*) 'WARNING: No match found to "'//trim(match_str)//'" in input files. Set to -999'
return
15 write(unit_output,*) 'WARNING: No values for "'//trim(match_str)//'" in input files'
end subroutine match_string_multi_int
!----------------------------------------------------------------------
!----------------------------------------------------------------------
subroutine match_string_multi_val(match_str,unit_in,unit_output,val,n_val)
!Finds a leading string and returns all the integer variables that follows it
!Tab delimitted before and free format after
!Not used
implicit none
integer n_val
real val(n_val)
character (*) match_str
character(256) temp_str1,temp_str2,temp_str
integer unit_in,unit_output
integer index_val
val=-999.
temp_str1=''
temp_str2='Not available'
rewind(unit_in)
do while (index(temp_str1,match_str).eq.0)
read(unit_in,'(a)',end=10) temp_str
if (temp_str(1:1).eq.'#'.or.temp_str(1:1).eq.'!') goto 5 !If first character is ! or # then ignore and go to next
index_val=index(temp_str,achar(9))
temp_str1=temp_str(1:index_val-1)
temp_str=temp_str(index_val+1:)
index_val=index(temp_str,achar(9))
!if (index_val.gt.0) then
! temp_str2=temp_str(1:index_val-1)
!else
! temp_str2=temp_str
!endif
5 end do
if (LEN(trim(temp_str)).gt.0) then
read(temp_str,*) val(1:n_val)
else
goto 15
endif
if (unit_output.ge.0) then
write(unit_output,'(A40,A3,<n_val>es10.2)') trim(match_str),' = ',val
endif
return
10 write(unit_output,*) 'WARNING: No match found to "'//trim(match_str)//'" in input files. Set to -999'
return
15 write(unit_output,*) 'WARNING: No values for "'//trim(match_str)//'" in input files'
end subroutine match_string_multi_val
!----------------------------------------------------------------------
!----------------------------------------------------------------------
function replace_string_char(replace_str,match_str,read_str)
!Finds a leading string and returns the string that follows it
!Tab delimitted before and after
implicit none
character(256) replace_string_char
character (*) match_str,replace_str,read_str
character(256) temp_str1,temp_str2
integer index_start,index_stop
replace_string_char=read_str
index_start=index(read_str,trim(match_str))
if (index_start.ne.0) then
index_stop=index_start+len(match_str)
temp_str1=read_str(1:index_start-1)
temp_str2=read_str(index_stop:len(read_str))
replace_string_char=trim(temp_str1)//trim(replace_str)//trim(temp_str2)
endif
!write(*,'(A)') trim(replace_string_char)
end function replace_string_char
!----------------------------------------------------------------------