filwZ 发表于 2014-3-6 16:46:24

关于求平均值的问题

求助大家一个问题
我的资料是52年的雨量资料,每一天都有,但是有的天数是缺失的,现在需要将一个月的前15天求一个平均值,这个月剩下的天数求一个平均。
但是需要雨量大于12mm,资料里雨量30000多的表示缺失,记为0.

请位大神教下怎么求。

楚香饭 发表于 2014-3-6 17:21:19

本帖最后由 chuxf 于 2014-3-7 13:34 编辑

Program www_fcode_cn
Implicit None
integer :: ierr
integer :: i , year , mon , day
integer :: yearlast , monlast , daylast , c
real :: d , a
Open( 12 , File = '57517(4年).txt' )
Open( 13 , File = 'out.txt' )
read(12,*)
yearlast = -1
monlast = -1
daylast = -1
a = 0.0
c = 0
Do
    Read( 12 , * , ioStat=ierr ) i , year , mon , day , d
    if ( ( ierr== 0 ) .and. (year == yearlast) .and. (mon == monlast) .and. ( (day>15)==(daylast>15) ) ) then !// 同一个半月
      if ( ( d > 12.0 ) .and. ( d < 3000.0 ) ) then
      a = a + d
      c = c + 1
      end if
    else
      if( c> 0 ) then
      a = a / c
      write(13,*) yearlast , monlast , a , c
      end if
      a = 0.0
      c = 0
      if ( ( d > 12.0 ) .and. ( d < 3000.0 ) ) then
      c = 1
      a = d
      end if
    end if
    yearlast = year
    monlast = mon
    daylast = day
    if ( ierr /= 0 ) Exit
End Do
Close( 12 )
Close( 13 )
End Program www_fcode_cn

filwZ 发表于 2014-3-6 17:56:52

非常谢谢。。大神。

filwZ 发表于 2014-3-7 10:32:24


yearlast = -1
monlast = -1
daylast = -1

和后面的那句
( (day>15)==(daylast>15) ) )
看不懂啊,能解释下吗?

filwZ 发表于 2014-3-7 10:34:01

chuxf 发表于 2014-3-6 17:21
Program www_fcode_cn
Implicit None
integer :: ierr


能解释下吗?谢谢!

楚香饭 发表于 2014-3-7 11:03:29

这个程序用本次读取的年月日和上一次读取的年月日对比,以便判断是否需要重新进行平均。(你要求每半月进行一次平均)
yearlast = -1
monlast = -1
daylast = -1
这是让一开始的上一次读取的年月日是一个不可能存在的年月日,以便第一行读取后,本次读取的年月日,一定与这个不同,于是程序会认为是一个新的平均部分。

( (day>15)==(daylast>15) ) )
这句话的意思是,day 和 daylast 是否同时大于15,或者是否同时小于15。如果不是的话,就说明需要重新平均了。如果是的话,就继续本次平均

filwZ 发表于 2014-3-7 11:15:49

chuxf 发表于 2014-3-7 11:03
这个程序用本次读取的年月日和上一次读取的年月日对比,以便判断是否需要重新进行平均。(你要求每半月进行 ...

我把你的那个写进去,调试不出结果。。。好难啊

楚香饭 发表于 2014-3-7 11:21:10

filwZ 发表于 2014-3-7 11:15
我把你的那个写进去,调试不出结果。。。好难啊

建议您发送部分数据(而不是截图)

如果遇到错误提示,请给出来。这样别人才能判断是为什么。

filwZ 发表于 2014-3-7 12:46:05

不好意思,我把那个写到屏幕的让他写到一个txt里,运行完没有错误,但是也没有结果输出。麻烦你了。。

楚香饭 发表于 2014-3-7 13:36:59

filwZ 发表于 2014-3-7 12:46
不好意思,我把那个写到屏幕的让他写到一个txt里,运行完没有错误,但是也没有结果输出。麻烦你了。。 ...

别人给你写的代码,你不能完全拷贝,要根据自己的情况稍作修改。别人的代码只是思路,而不是确定的解决方案。

问题就出在第一行的表头上面。之前的代码没有考虑第一行的表头。

请使用二楼的新代码。
页: [1] 2 3
查看完整版本: 关于求平均值的问题