[Fortran] 纯文本查看 复制代码
Integer Function daysinyear(year, month)
Integer :: year, month
Integer :: daysinmonth(12) = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ]
If (((mod(year,4)==0) .And. (mod(year,100)/=0)) .Or. (mod(year,400)==0)) Then
daysinmonth(2) = 29
Else
daysinmonth(2) = 28
End If
daysinyear = daysinmonth(month)
End Function daysinyear
Program weather
Implicit None
Integer l, i, j, m, daysinyear, k, a
Integer, Allocatable :: site(:), lat(:), lon(:), year(:), month(:), day(:)
real, Allocatable :: rain(:)
Integer :: h, pre1, pre2
Character (45) :: filename = 'SURF_CLI_CHN_MUL_DAY-PRE-13011-123456.txt'
Character (8) :: outfile = '12345678', f = '12345678'
! character(6)::filename1='123456.txt'
Character (Len=20) :: line
!================================================================================================
!批量读入文件
!================================================================================================
Do l = 2010, 2015 !内部文件读写,将filename 第8-11 字符换为相应年份
Do j = 1, 12
Write (filename(32:37), '(i6)') l*100 + j
Open (11, File='D:\气象数据\降水\'//filename, Action='read')
!=================================================================================================
!创建输出文件
!=================================================================================================
Do k = 1, daysinyear(l, j)
Write (outfile(1:8), '(i8)') l*10000 + j*100 + k
! print *,outfile
Open (15, File='D:\气象数据\pre\'//outfile//'.txt')
!===============================================================================================
! 获取文件行数
!===============================================================================================
a = 0
Do
Read (11, *, End=100) line
a = a + 1
End Do
100 m = a !//跳过第一行
Rewind (11)
!=================================================================================================
Allocate (site(m), lat(m), lon(m), year(m), month(m), day(m), rain(m))
!=================================================================================================
Do i = 1, m
Read (11, *) site(i), lat(i), lon(i), h, year(i), month(i), day(i), pre1, pre2, rain(i)
rain(i)=rain(i)/10
Write (f(1:8), '(i8)') year(i)*10000 + month(i)*100 + day(i)
!print *,f
If (rain(i)==3270) Then
rain(i) = 0
End If
!print *, site(i),lat(i),lon(i) , year(i) , month(i) , day(i) , rain(i)
! print *,site(i), lat(i), lon(i), year(i), month(i), day(i),rain(i)
!=================================================================================================
!==================================================================================
End Do
Do i = 1, m
if(l*10000 + j*100 + k.eq.year(i)*10000 + month(i)*100 + day(i))then
Write (15, "(i5,2x,i4,2x,i5,2x,i4,2x,i2,2x,i2,2x,f5.2)") site(i), lat(i), lon(i), year(i), month(i), day(i), rain(i)
print *,site(i), lat(i), lon(i), year(i), month(i), day(i),rain(i)
end if
end do
Deallocate (site, lat, lon, year, month, day, rain)
End Do
End Do
End Do
Close (11)
End Program weather