-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStrNum.f90
753 lines (734 loc) · 23.9 KB
/
StrNum.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
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
!------------------------------------------------------------
!---StrNum.F90:提供字符串,数字和文本文件处理的功能子程序
!---吴徐平2013-07-22([email protected])
!------------------------------------------------------------
module StrNum
implicit none
interface StrToNum !---字符串转换为数字数StrToNum(InStr,Num,Error)
module procedure StrToReal4
module procedure StrToReal8
module procedure StrToInteger1
module procedure StrToInteger2
module procedure StrToInteger4
end interface
interface KeepRealChar !---保留InStr中的浮点数相关的字符,其它字符全部变为空格KeepRealChar(InStr)
module procedure KeepRealChar
end interface
interface KeepNumChar !---保留InStr中的浮点数相关的字符,其它字符全部变为空格KeepNumChar(InStr)
module procedure KeepNumChar
end interface
interface TrimIndex !---识别InStr中左右有效可见字符(33-126)的索引TrimIndex(InStr,LeftIndex,RightIndex,Error)
module procedure TrimIndex
end interface
interface StringSplit !---字符串分割StringSplit(InStr,delimiter,StrArray,nsize)
module procedure StringSplit
end interface
interface StrReplace !---字符串替换StrReplace(InStr,OldChar,NewChar,OutStr)
module procedure StrReplace
end interface
interface StrToRealArray !---字符串变为浮点数组StrToRealArray(InStr,RealArray,nsize)
module procedure StrToRealArray4
module procedure StrToRealArray8
end interface
interface IsRealArrayString !---测试字符串是否可以转为RealArray数组:IsRealArrayString(InStr,Error)
module procedure IsRealArrayString
end interface
interface GetFileRowColumn !---获取文本文件FileName行列信息:GetFileRowColumn(FileName,HeadLine,Row,Column)
module procedure GetFileRowColumn
end interface
interface LoadFromFile !---文件数据的读取LoadFromFile(FileName,Array2D,Row,Column,Error)
module procedure LoadFromFile4
module procedure LoadFromFile8
end interface
!------------------------------------------------------------
contains
subroutine StrToReal4(InStr,Num,Error)
!------------------------------------------------------------
!---将字符串InStr转为Num数字类型
!---如果Error == 0 ::表示InStr可以转为Num,否则转换错误
!---吴徐平2013-07-20([email protected])
!------------------------------------------------------------
Implicit None
Character(Len = *), Intent( IN ) :: InStr
Character(Len = LEN(InStr)):: Str_temp
Real(kind = 4),Intent( INOUT ) :: Num
Integer,Intent( INOUT ) :: Error
Integer::LeftIndex,RightIndex
!-----------------
Num=0
Error=0
!-----------------
if (LEN(TRIM(InStr))>0 ) then
Str_temp=InStr !为了不修改原始字符串的内容
call KeepRealChar(Str_temp) !只保留浮点数相关的字符
call TrimIndex(Str_temp,LeftIndex,RightIndex,Error)
!-----------------
if (Error==0 ) then
Read( Str_temp(LeftIndex:RightIndex) , * ,iostat=Error) Num
else
Error=Error+1
end if
else
Error=Error+1
end if
!-----------------
end subroutine StrToReal4
subroutine StrToReal8(InStr,Num,Error)
!------------------------------------------------------------
!---将字符串InStr转为Num数字类型
!---如果Error == 0 ::表示InStr可以转为Num,否则转换错误
!---吴徐平2013-07-20([email protected])
!------------------------------------------------------------
Implicit None
Character(Len = *), Intent( IN ) :: InStr
Character(Len = LEN(InStr)):: Str_temp
Real(kind = 8),Intent( INOUT ) :: Num
Integer,Intent( INOUT ) :: Error
Integer::LeftIndex,RightIndex
!-----------------
Num=0
Error=0
!-----------------
if (LEN(TRIM(InStr))>0 ) then
Str_temp=InStr !为了不修改原始字符串的内容
call KeepRealChar(Str_temp) !只保留浮点数相关的字符
call TrimIndex(Str_temp,LeftIndex,RightIndex,Error)
!-----------------
if (Error==0 ) then
Read( Str_temp(LeftIndex:RightIndex) , * ,iostat=Error) Num
else
Error=Error+1
end if
!-----------------
else
Error=Error+1
end if
end subroutine StrToReal8
subroutine StrToInteger1(InStr,Num,Error)
!------------------------------------------------------------
!---将字符串InStr转为Num数字类型
!---如果Error == 0 ::表示InStr可以转为Num,否则转换错误
!---吴徐平2013-07-20([email protected])
!------------------------------------------------------------
Implicit None
Character(Len = *), Intent( IN ) :: InStr
Character(Len = LEN(InStr)):: Str_temp
Integer(kind = 1),Intent( INOUT ) :: Num
Integer,Intent( INOUT ) :: Error
Integer::LeftIndex,RightIndex
!-----------------
Num=0
Error=0
!-----------------
if (LEN(TRIM(InStr))>0 ) then
Str_temp=InStr !为了不修改原始字符串的内容
call KeepRealChar(Str_temp) !只保留浮点数相关的字符
call TrimIndex(Str_temp,LeftIndex,RightIndex,Error)
!-----------------
if (Error==0 ) then
Read( Str_temp(LeftIndex:RightIndex) , * ,iostat=Error) Num
else
Error=Error+1
end if
!-----------------
else
Error=Error+1
end if
end subroutine StrToInteger1
subroutine StrToInteger2(InStr,Num,Error)
!------------------------------------------------------------
!---将字符串InStr转为Num数字类型
!---如果Error == 0 ::表示InStr可以转为Num,否则转换错误
!---吴徐平2013-07-20([email protected])
!------------------------------------------------------------
Implicit None
Character(Len = *), Intent( IN ) :: InStr
Character(Len = LEN(InStr)):: Str_temp
Integer(kind = 2),Intent( INOUT ) :: Num
Integer,Intent( INOUT ) :: Error
Integer::LeftIndex,RightIndex
!-----------------
Num=0
Error=0
!-----------------
if (LEN(TRIM(InStr))>0 ) then
Str_temp=InStr !为了不修改原始字符串的内容
call KeepRealChar(Str_temp) !只保留浮点数相关的字符
call TrimIndex(Str_temp,LeftIndex,RightIndex,Error)
!-----------------
if (Error==0 ) then
Read( Str_temp(LeftIndex:RightIndex) , * ,iostat=Error) Num
else
Error=Error+1
end if
!-----------------
else
Error=Error+1
end if
!-----------------
end subroutine StrToInteger2
subroutine StrToInteger4(InStr,Num,Error)
!------------------------------------------------------------
!---将字符串InStr转为Num数字类型
!---如果Error == 0 ::表示InStr可以转为Num,否则转换错误
!---吴徐平2013-07-20([email protected])
!------------------------------------------------------------
Implicit None
Character(Len = *), Intent( IN ) :: InStr
Character(Len = LEN(InStr)):: Str_temp
Integer(kind = 4),Intent( INOUT ) :: Num
Integer,Intent( INOUT ) :: Error
Integer::LeftIndex,RightIndex
!-----------------
Num=0
Error=0
!-----------------
if (LEN(TRIM(InStr))>0 ) then
!-----------------
Str_temp=InStr !为了不修改原始字符串的内容
call KeepRealChar(Str_temp) !只保留浮点数相关的字符
call TrimIndex(Str_temp,LeftIndex,RightIndex,Error)
!-----------------
if (Error==0 ) then
Read( Str_temp(LeftIndex:RightIndex) , * ,iostat=Error) Num
else
Error=Error+1
end if
!-----------------
else
Error=Error+1
end if
end subroutine StrToInteger4
subroutine KeepRealChar(InStr)
!------------------------------------------------------------
!---保留InStr中的浮点数相关的字符,其它字符全部变为空格
!---吴徐平2013-07-20([email protected])
!------------------------------------------------------------
Implicit None
Character(Len =*),Intent( INOUT ) :: InStr
Character(Len =17):: RealChar='+-.0123456789eEdD'
Character(Len =4):: StartChar='eEdD'
Character(Len =6):: EndChar='eEdD+-'
Character(Len =7):: SingleChar='eEdD+-.'
!------------------------------------------------------------
Integer ::i,j,k,flag,Error
!------------------------------------------------------------
do i=1,LEN(InStr)
flag=0
!-------------------------------
do j=1,LEN(RealChar)
!-------------------------------
if (InStr(i:i)==RealChar(j:j)) then
flag=flag+1 !-识别为RealChar浮点数字符
Exit
end if
!------------------------------
end do
!-------------------------------
if (flag==0) then
InStr(i:i)=' ' !-非RealChar浮点数字符,置为空格
end if
!------------------------------
end do
!------------------------------------------------------------
!---第一个有效字符不能为StartChar='eEdD'
do while(.TRUE.)
call TrimIndex(InStr,i,j,Error)
if (Error==0)then
!-------------------------------
flag=0
!------------------------------
do k=1,LEN(StartChar)
!-------------------------------
if (InStr(i:i)==StartChar(k:k)) then
flag=flag+1 !-第一个有效字符不能为eEdD
Exit
end if
!------------------------------
end do
!------------------------------
if (flag>0)then
InStr(i:i)=' ' !将该字符置为空格
else
EXIT !-第一个有效字符不是eEdD
end if
!------------------------------
else
EXIT
end if
end do
!------------------------------------------------------------
!---最后一个有效字符不能为EndChar='eEdD+-'
do while(.TRUE.)
call TrimIndex(InStr,i,j,Error)
if (Error==0)then
!-------------------------------
flag=0
!------------------------------
do k=1,LEN(EndChar)
!-------------------------------
if (InStr(j:j)==EndChar(k:k)) then
flag=flag+1 !-最后一个有效字符不能为EndChar='eEdD+-'
Exit
end if
!------------------------------
end do
!------------------------------
if (flag>0)then
InStr(j:j)=' ' !将该字符置为空格
else
EXIT !-最后一个有效字符不是EndChar='eEdD+-'
end if
!------------------------------
else
EXIT
end if
end do
!------------------------------------------------------------
!---如果只含有一个有效字符,则不能是SingleChar='eEdD+-.'
do while(.TRUE.)
call TrimIndex(InStr,i,j,Error)
if ((Error==0) .AND. (i==j))then
!-------------------------------
flag=0
!------------------------------
do k=1,LEN(SingleChar)
!-------------------------------
if (InStr(i:i)==SingleChar(k:k)) then
flag=flag+1 !-有效字符不能为SingleChar
Exit
end if
!------------------------------
end do
!------------------------------
if (flag>0)then
InStr(i:i)=' ' !将该字符置为空格
else
EXIT !-有效字符不是SingleChar
end if
!------------------------------
else
EXIT
end if
end do
!------------------------------------------------------------
end subroutine KeepRealChar
subroutine KeepNumChar(InStr)
!------------------------------------------------------------
!---保留InStr中的数字字符,其它字符全部变为空格
!---吴徐平2013-07-20([email protected])
!------------------------------------------------------------
Implicit None
Character(Len =*),Intent( INOUT ) :: InStr
Character(Len =10):: NumChar='0123456789'
!------------------------------------------------------------
Integer ::i,j,flag
!------------------------------------------------------------
do i=1,LEN(InStr)
flag=0
!-------------------------------
do j=1,LEN(NumChar)
!-------------------------------
if (InStr(i:i)==NumChar(j:j)) then
flag=flag+1 !-识别为NumChar字符
Exit
end if
!------------------------------
end do
!-------------------------------
if (flag==0) then
InStr(i:i)=' ' !-非NumChar字符,置为空格
end if
!------------------------------
end do
!------------------------------------------------------------
end subroutine KeepNumChar
subroutine TrimIndex(InStr,LeftIndex,RightIndex,Error)
!------------------------------------------------------------
!---识别InStr中左右有效可见字符(33-126)的索引
!---如果Error==0,则识别正确
!---吴徐平2013-07-20([email protected])
!------------------------------------------------------------
Implicit None
Character(Len =*),Intent( IN ) :: InStr
Integer,Intent( OUT)::LeftIndex,RightIndex,Error
!------------------------------------------------------------
Integer ::i
LeftIndex=0
RightIndex=LEN(InStr)+1
!------------------------------------------------------------
if (LEN(TRIM(InStr))>0) then
do i=1,LEN(InStr),1
if ((IACHAR(InStr(i:i)) >32 ).AND.(IACHAR(InStr(i:i)) <127) ) then
LeftIndex=i !-左边有效可见字符(33-126)的索引
EXIT
end if
end do
!------------------------------------------------------------
do i=LEN(InStr),1,-1
if ((IACHAR(InStr(i:i)) >32 ).AND.(IACHAR(InStr(i:i)) <127 )) then
RightIndex=i !-右边有效可见字符(33-126)的索引
EXIT
end if
end do
!--------------------------
if ((LeftIndex>0 ).AND. (LeftIndex<=RightIndex) .AND. (RightIndex<=LEN(InStr)))then
Error=0 !-操作正确
else
Error=-1 !-操作有误
end if
!--------------------------
else
Error=-1 !-字符串全部为空格或是空字符串
end if
end subroutine TrimIndex
subroutine StringSplit(InStr,delimiter,StrArray,nsize)
!---将字符串InStr进行分割,结果放入StrArray中
!---delimiter::分隔符号,例如';,,' 使用;和,分割字符串
!---nsize:分割数目
!---吴徐平2011-04-29([email protected])
!----------------------------------------------
implicit none
character(len = *) , Intent( IN ) :: InStr
character(len = *) , Intent( IN ) :: delimiter
character(len = LEN(InStr)),dimension(LEN(InStr)),Intent( OUT ) :: StrArray
integer, Intent( OUT ) :: nsize ! Effective Size of StrArray
integer:: i,j ! loop variable
integer:: istart ! split index for Start Position
nsize=0
istart=1
do i=1,LEN(InStr)
do j=1,LEN(delimiter)
if (InStr(i:i) == delimiter(j:j)) then
if (istart == i) then
istart=i+1 ! ---可防止分隔符相连的情况
end if
if (istart<i) then
nsize=nsize+1
StrArray(nsize)=InStr(istart:i-1)
istart=i+1
end if
end if
end do
end do
! ---匹配最后一个子字符串
if (nsize>0) then
if (istart<LEN(InStr)) then
nsize=nsize+1
StrArray(nsize)=InStr(istart:LEN(InStr))
end if
end if
! ---如果无可分割的子字符串,则包含整个字符串为数组的第一元素
if ( (nsize<1) .AND. (LEN(TRIM(InStr)) > 0 )) then
nsize=1
StrArray(1)=InStr
end if
end subroutine StringSplit
subroutine StrReplace(InStr,OldChar,NewChar,OutStr)
!------------------------------------------------------------
!---将字符串InStr中的字符串OldChar替换成NewChar
!---结果放入字符串OutStr中
!---吴徐平2013-07-20([email protected])
!------------------------------------------------------------
implicit none
character(len = *) , Intent( IN ) :: InStr
character(len = *) , Intent( IN ) :: OldChar
character(len = LEN(OldChar)) , Intent( IN ) ::NewChar
character(len = LEN(InStr)) , Intent( INOUT ) :: OutStr
integer :: i ! loop variable
OutStr=InStr
i=INDEX(OutStr,OldChar)
do while(i>0)
OutStr(i:i+LEN(OldChar)-1)=NewChar
i=INDEX(OutStr,OldChar)
end do
end subroutine StrReplace
subroutine StrToRealArray4(InStr,RealArray,nsize)
!------------------------------------------------------------
Implicit None
Character(Len = *), Intent( IN ) :: InStr
Character(Len = LEN(InStr)):: Str_temp
Integer:: i,j,Error,nsize
Real::Num
character(len =LEN(InStr)),dimension(LEN(InStr)):: StrArray
Real(kind=4),dimension(LEN(InStr)),Intent(OUT) :: RealArray
character(len = 4):: delimiter=' ;, '
Error=0
nsize=0
j=0
Str_temp=InStr
call KeepRealChar(Str_temp)
!----------------------
call StringSplit(Str_temp,delimiter,StrArray,nsize)
if (nsize>=1)then
do i=1,nsize
call KeepRealChar(StrArray(i))
call StrToNum(StrArray(i),Num,Error)
if (Error==0) then
j=j+1
RealArray(j)=Num
end if
end do
end if
nsize=j
!------------------------------------------------------
end subroutine StrToRealArray4
subroutine StrToRealArray8(InStr,RealArray,nsize)
!------------------------------------------------------------
Implicit None
Character(Len = *), Intent( IN ) :: InStr
Character(Len = LEN(InStr)):: Str_temp
Integer:: i,j,Error,nsize
Real::Num
character(len =LEN(InStr)),dimension(LEN(InStr)):: StrArray
Real(kind=8),dimension(LEN(InStr)),Intent(OUT) :: RealArray
character(len = 4):: delimiter=' ;, '
Error=0
nsize=0
j=0
Str_temp=InStr
call KeepRealChar(Str_temp)
!----------------------
call StringSplit(Str_temp,delimiter,StrArray,nsize)
if (nsize>=1)then
do i=1,nsize
call KeepRealChar(StrArray(i))
write(*,*) StrArray(i)
call StrToNum(StrArray(i),Num,Error)
if (Error==0) then
j=j+1
RealArray(j)=Num
end if
end do
end if
nsize=j
!------------------------------------------------------
end subroutine StrToRealArray8
subroutine IsRealArrayString(InStr,Error)
!------------------------------------------------------------
!---测试字符串InStr转为RealArray类型的数组
!---Error == 0 ::表示InStr可以转为RealArray数组,否则不能转换
!---吴徐平2011-04-29([email protected])
!------------------------------------------------------------
Implicit None
Character(Len = *), Intent( IN ) :: InStr
Integer ,Intent( OUT ) :: Error
Real,dimension(LEN(InStr)):: RealArray
!------------------------------------------
Integer::nsize
Error=0
nsize=0
!------------------------------------------
call StrToRealArray(InStr,RealArray,nsize)
if (nsize>=1)then
Error=0 !可以转为RealArray
else
Error=-1 !不可以转为RealArray
end if
!------------------------------------------------------
end subroutine IsRealArrayString
subroutine GetFileRowColumn(FileName,HeadLine,Row,Column)
!------------------------------------------------------------
!---获取文本文件FileName的行数Row
!---吴徐平2013-07-20([email protected])
!------------------------------------------------------------
Implicit None
Character(Len = *), Intent( IN ) :: FileName
Integer, Intent( out ) :: HeadLine !---文件头的行数
Integer , Intent( out ) :: Row !---文件行数Row
Integer, Intent( out ) :: Column !---最大列数Column
Character(Len = 1000) :: CLine
Integer:: IOStatus=0
Real,dimension(LEN(CLine)):: RealArray
Integer:: nsize=0
!---------------------------------------------
Row=0
HeadLine=0
Column=0
!---------------------------------------------
!---获取Row和Column
close(9001)
!---先测试出文件行数和数据的最大列数
open(unit=9001,file=FileName,status='OLD')
Read( 9001 ,'(A1000)',iostat=IOStatus) CLine
Do While (IOStatus == 0 )
Row = Row + 1
!---------------------------------------------
call StrToRealArray(CLine,RealArray,nsize)
!---------------------------------------------
if (nsize>0 .AND.Column<nsize) then
Column=nsize
end if
!---------------------------------------------
Read( 9001 ,'(A1000)',iostat=IOStatus) CLine
!---------------------------------------------
End Do
close(9001)
!---------------------------------------------
close(9001)
!---------------------------------------------
!---测试文件头HeadLine
open(unit=9001,file=FileName,status='OLD')
Read( 9001 ,'(A1000)',iostat=IOStatus) CLine
!---------------------------------------------
call StrToRealArray(CLine,RealArray,nsize)
!---------------------------------------------
Do While (IOStatus==0 .AND. nsize < Column )
!---------------------------------------------
HeadLine=HeadLine+1
!---------------------------------------------
Read( 9001 ,'(A1000)',iostat=IOStatus) CLine
!---------------------------------------------
call StrToRealArray(CLine,RealArray,nsize)
!---------------------------------------------
End Do
close(9001)
!---------------------------------------------
end subroutine GetFileRowColumn
subroutine LoadFromFile4(FileName,Array2D,Row,Column,Error)
!------------------------------------------------------------
!---获取文本文件FileName的数据
!---Array2D::大小(Row,Column),存放数据的二维可分配内存大小的数组
!---Row::文件行数,Column::文件数据列数
!---Error==0::表示读取文本文件中的数据正确,否则有误
!---吴徐平2013-07-22([email protected])
!------------------------------------------------------------
Implicit None
Character(Len = *), Intent( IN ) :: FileName
Integer ,Intent( OUT ) ::Row,Column
Real(kind=4),allocatable,Intent( OUT ) :: Array2D(:,:)
Character(Len = 1000) :: CLine
Real,dimension(LEN(CLine)):: RealArray
Integer,Intent( OUT ):: Error
Integer :: TotalRow,HeadLine,nsize,i,j,IOStatus
!---------------------------------------
Row=0
Column=0
TotalRow=0
HeadLine=0
nsize = 0
i = 0
j=0
Error = 0
IOStatus = 0
RealArray=0.0
!---------------------------------------
call GetFileRowColumn(FileName,HeadLine,TotalRow,Column)
Row=TotalRow-HeadLine !只包含数据的行数
!---------------------------------------
if (Row>0 .AND. Column>0 ) then
!--------------------------
if (Allocated(Array2D)) then
deallocate(Array2D)
end if
allocate(Array2D(Row,Column),stat=Error)
Array2D=0.0 !初始化为0
!------------------------
if (Error==0) then
!--------------------------
close(9002)
!---------------------------------------
open(unit=9002,file=FileName,status='OLD')
!---------------------------------------
do i=1,TotalRow
Read( 9002 , '(A1000)' ,iostat=IOStatus) CLine
if (IOStatus==0 .AND. i>HeadLine) then
!---------------------------------------
call StrToRealArray(CLine,RealArray,nsize)
!---------------------------------------
if (nsize >0) then
!---------------------------------------
do j=1,nsize
Array2D(i-HeadLine,j)=RealArray(j)
end do
!---------------------------------------
end if
!---------------------------------------
end if
end do
!---------------------------------------
close(9002)
!---------------------------------------
else
Error=-1 !分配内存失败
end if
!---------------------------------------
else
Error=-1 !文本文件中没有数据
end if
!---------------------------------------
end subroutine LoadFromFile4
subroutine LoadFromFile8(FileName,Array2D,Row,Column,Error)
!------------------------------------------------------------
!---获取文本文件FileName的数据
!---Array2D::大小(Row,Column),存放数据的二维可分配内存大小的数组
!---Row::文件行数,Column::文件数据列数
!---Error==0::表示读取文本文件中的数据正确,否则有误
!---吴徐平2013-07-22([email protected])
!------------------------------------------------------------
Implicit None
Character(Len = *), Intent( IN ) :: FileName
Integer ,Intent( OUT ) ::Row,Column
Real(kind=8),allocatable,Intent( OUT ) :: Array2D(:,:)
Character(Len = 1000) :: CLine
Real,dimension(LEN(CLine)):: RealArray
Integer,Intent( OUT ):: Error
Integer :: TotalRow,HeadLine,nsize,i,j,IOStatus
!---------------------------------------
Row=0
Column=0
TotalRow=0
HeadLine=0
nsize = 0
i = 0
j=0
Error = 0
IOStatus = 0
RealArray=0.0
!---------------------------------------
call GetFileRowColumn(FileName,HeadLine,TotalRow,Column)
Row=TotalRow-HeadLine !只包含数据的行数
!---------------------------------------
if (Row>0 .AND. Column>0 ) then
!--------------------------
if (Allocated(Array2D)) then
deallocate(Array2D) !释放内存,重新分配
end if
allocate(Array2D(Row,Column),stat=Error)
Array2D=0.0 !初始化为0
!------------------------
if (Error==0) then
!--------------------------
close(9002)
!---------------------------------------
open(unit=9002,file=FileName,status='OLD')
!---------------------------------------
do i=1,TotalRow
Read( 9002 , '(A1000)' ,iostat=IOStatus) CLine
if (IOStatus==0 .AND. i>HeadLine) then
!---------------------------------------
call StrToRealArray(CLine,RealArray,nsize)
!---------------------------------------
if (nsize >0) then
!---------------------------------------
do j=1,nsize
Array2D(i-HeadLine,j)=RealArray(j)
end do
!---------------------------------------
end if
!---------------------------------------
end if
end do
!---------------------------------------
close(9002)
!---------------------------------------
else
Error=-1 !分配内存失败
end if
!---------------------------------------
else
Error=-1 !文本文件中没有数据
end if
!---------------------------------------
end subroutine LoadFromFile8
end module StrNum