此外,观测数据常常具有“间隔等时连续”的特点,如果出现少测或者乱序的情况,可以采用“打点法”来补全数据,同时排序。“打点法”的基本思想在于:对号入座,亦即“一个萝卜一个坑”。人为给定正常顺序后,在对应位置填入相应数据即可。思路源于fcode群友,在此一并感谢。
相应数据文件:
input.txt 时间数据以-和:作为分隔符,且有少测,乱序。

output.txt 时间数据以空格作为分隔符,便于表控读入。

Program www_fcode_cn
Implicit None
Integer, External :: GetFileN !GetFileN函数用于获取文件行号
Integer :: nRow, i, j
Character(len=100) :: str
Integer r(8), pre(0:59) !pre用于存放将使用的数据
Open (12, File='/Users/input.txt')
Open (13, File='/Users/output.txt')
nRow = GetFileN(12)
Write (*, *) '文件共', nRow, '行!'
Do i = 1, nRow
Read (12, '(A100)') str
Do j = 1, len_trim(str)
If ((str(j:j))=='-' .Or. (str(j:j)==':')) Then
str(j:j) = ' '
End If ! if程序块用于将短杠和冒号换成空格,空格作为数据分割符
End Do
Write (13, *) str
End Do
Close (12)
Rewind (13)
pre = -99 ! 设定一个人为的缺测值
Do i = 1, nRow
Read (13, *) r(1:8)
pre(r(6)) = r(8) !r(6),r(8)分别存放 分钟和降水量。”对号入座”,填充数据
End Do
! 此后,pre中存放的数据将包括缺测值和观测数据,且按照时间已经排好序,在输出或者处理过程中
! 可以if判断,仅仅使用非缺测值(-99),或者将所有值输入到文件,来补全数据。
! 这种方法也可用于多年、月、日、时等数据的补全。缺测值可在计算有效数据的均值后,再重新赋予。
Close (13)
Write (*, '(10(1x,i4))') pre
End Program www_fcode_cn
Integer Function GetFileN( iFileUnit )
! 子程序GetFileN由Fortran Coder群主提供,群号2338021,特此感谢。
! // 此函数应在打开文件后立即调用。调用后读取位置返回文件起始位置
Implicit None
Integer , Intent( IN ) :: iFileUnit
character( Len = 1 ) :: cDummy
integer :: ierr
GetFileN = 0
Rewind( iFileUnit )
Do
Read( iFileUnit , * , ioStat = ierr ) cDummy
If( ierr /= 0 ) Exit
GetFileN = GetFileN + 1
End Do
Rewind( iFileUnit )
End Function GetFileN



