使用shell脚本计算闰年日历中的时间序列的年平均值(Compute annual average from a times series in leap year calender using sh

编程入门 行业动态 更新时间:2024-10-18 14:22:19
使用shell脚本计算闰年日历中的时间序列的年平均值(Compute annual average from a times series in leap year calender using shell script)

我有一个10年(1995-2004)的时间序列每日数据集,其中一些缺失值为9999.00。 我想计算每年的年平均值,而不考虑缺失值。

我可以通过以下命令考虑365天日历来实现它

awk '!/\9999.00/{sum += $1; count++} NR%365==0{print count ? (sum) :9999.00;sum=count=0}'ifile

但我无法用闰年日历修改。 我还需要添加另一列多年。 我的愿望输出是

1995 annual_average 1996 annual_average 1997 annual_average ....

例如:我有以下1995 - 2000年的数据。 如果是闰年,我需要计算每3行而不是365行和4行的平均值而不是366行:

3 3 4 9999.00 4 9999.00 13 3 9999.00 9999.00 9999.00 9999.00 9999.00 3 4 2 2 2.6 5.1 4.5

试用命令:

awk '!/\9999.00/{sum += $1; count++} NR%3==0{print count ? (sum) :9999.00;sum=count=0}'ifile

欲望输出:

1995 3.33 1996 8.5 it is a leap year, so average of 4 lines without considering missing values (4+13)/2 1997 3 1998 9999.00 1999 3 2000 3.55 leap year

I have a time series daily dataset for 10 years (1995-2004) with some missing values as 9999.00. I would like to compute annual average for each year without considering the missing value.

I could able to make it by considering 365 days calendar with following command

awk '!/\9999.00/{sum += $1; count++} NR%365==0{print count ? (sum) :9999.00;sum=count=0}'ifile

But I can't able to modify with leap year calendar. I also need to add another column with years. My desire output is as

1995 annual_average 1996 annual_average 1997 annual_average ....

For example: I have following data from 1995-2000. I need to compute average of every 3 lines instead of 365 and 4 lines instead of 366 if it is a leap year:

3 3 4 9999.00 4 9999.00 13 3 9999.00 9999.00 9999.00 9999.00 9999.00 3 4 2 2 2.6 5.1 4.5

Trial command:

awk '!/\9999.00/{sum += $1; count++} NR%3==0{print count ? (sum) :9999.00;sum=count=0}'ifile

Desire output:

1995 3.33 1996 8.5 it is a leap year, so average of 4 lines without considering missing values (4+13)/2 1997 3 1998 9999.00 1999 3 2000 3.55 leap year

最满意答案

此代码适用于您的示例数据。 当然,您需要调整target :

BEGIN { year = 0; target = 3; } $1 < 9990.00 { sum += $1; count++; } NR == target { if (count == 0) { print "9999"; } else { print sum / count; } sum = 0; count = 0; year++; if (year % 4 == 1) { target += 4; } else { target += 3; } }

哦,请记住,非常简单的闰年计算将会失败多年,尽管不是你提到的年份。

This code works for your sample data. Of course, you will need to adjust the target values:

BEGIN { year = 0; target = 3; } $1 < 9990.00 { sum += $1; count++; } NR == target { if (count == 0) { print "9999"; } else { print sum / count; } sum = 0; count = 0; year++; if (year % 4 == 1) { target += 4; } else { target += 3; } }

Oh, and keep in mind that the very simplistic leap year calculation will fail for some years, although not the years you mentioned.

更多推荐

本文发布于:2023-08-07 05:44:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1462685.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:闰年   序列   年平均   脚本   日历

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!