[Fortran] 纯文本查看 复制代码
program add_gamit_station_info
implicit none
integer :: count,stat,err,err2,i,stat2,stat3,k,stat4
character :: example_line*300,filename*30,line*300,site*4
character (len=300),allocatable :: example_line_array(:)
character (len=4),allocatable :: example_site_array(:)
open (11,file='list.txt')
close (11,status='delete')
call system ('dir /b *.info > list.txt')
open (11,file='list.txt')
open (22,file='station_cwb.info.db')
open (33,file='station_expt.info.db')
!---------------------------------------------> to check array size
do k=1,5,1
read (22,*,iostat=stat4) example_line
end do
count=0
stat=0
do while (stat==0)
read (22,'(a300)',iostat=stat) example_line
if (stat/=0) exit
count=count+1
end do
!---------------------------------------------> to check array size
rewind (22)
!---------------------------------------------> to allocate the example_line_array & example_site_array
allocate (example_line_array(count),stat=err)
if (err==0) then
write (*,*) 'The example_line_array allocated sucessfully.'
else if (err/=0) then
write (*,*) 'The example_line_array failed to allocate.'
end if
allocate (example_site_array(count),stat=err2)
if (err2==0) then
write (*,*) 'The example_site_array allocated sucessfully.'
else if (err/=0) then
write (*,*) 'The example_site_array failed to allocate.'
end if
!---------------------------------------------> to allocate the example_line_array & example_site_array
!---------------------------------------------> to assign array value from station_cwb.info into example_line_array
do k=1,5,1
read (22,*,iostat=stat4) example_line
end do
do i=1,count,1
read (22,'(a300)') example_line_array(i)
end do
!---------------------------------------------> to assign array value from file into example_line_array
!---------------------------------------------> to assign array value from example_line_array(2:5) into example_site_array
!where (example_site_array/=' ') example_site_array=example_line_array(2:5)
forall (i=1:count:1) example_site_array(i)=example_line_array(i)(2:5)
!---------------------------------------------> to assign array value from example_line_array(2:5) into example_site_array
do i=1,count,1
write (*,*) example_site_array(i)
end do
rewind (22)
!---------------------------------------------> to check whether is new site.
!---------------------------------------------> if it is a new site , we write down the name of site.
!---------------------------------------------> if it isn't a new site , we skip site.
stat2=0
do while (stat2==0)
read (11,*,iostat=stat2) filename
if (stat2/=0) exit
open (44,file=trim(filename))
do k=1,5,1
read (44,*,iostat=stat4) line
end do
stat3=0
do while (stat3==0)
read (44,'(a300)',iostat=stat3) line
if (stat3/=0) exit
site=line(2:5)
if (any(example_site_array==site)) cycle
write (33,'(a4)') site
end do
end do
!---------------------------------------------> to check whether is new site.
!---------------------------------------------> if it is a new site , we write down the name of site.
!---------------------------------------------> if it isn't a new site , we skip site.
close (11)
close (22)
close (33)
close (44)
end program