【问题标题】:How to Calculate Dry and Wet Spell in Python?如何在 Python 中计算干法和湿法?
【发布时间】:2013-11-12 15:18:27
【问题描述】:

我有一个包含四列的随机时间序列数据,例如:年、月、日、降水。我想计算不同拼写长度的干/湿拼写。我正在寻找一种更方便的方法来做到这一点,而目前正在使用如下一些丑陋的代码:

import numpy as np
data = np.loadtxt('Data Series.txt', usecols=(1,3))
dry = np.zeros(12)
wet = np.zeros(12)

rows,cols = data.shape #reading number of rows and columns into variables

for i in xrange (0,rows):
    for m in xrange(0,12):
        if data[i,1] == 0 and data[i-1,1] == 0 and data[i-2,1] == 0:
            if data[i,0] == m+1:
                dry[m] += 1.0
        if data[i,1] > 0 and data[i-1,1] > 0 and data[i-2,1] > 0:
            if data[i,0] == m+1:
                wet[m] += 1.0
print '3 Days Dry Spell\n', dry
print '3 Days Wet Spell\n', wet

现在,如果我想计算 4、5、6 天的咒语,那么“如果 data[i,1] == 0 and data[i-1,1] == 0.....” 变成一个巨大的。任何人都可以帮助我,以便我可以只给出拼写长度而不是这条长而丑陋的线吗?

【问题讨论】:

  • 仅在列表列表中包含复杂数据通常是个坏主意。这是一个这样的例子。这里有趣的数据都在一大组列表的第二项中。这使得处理起来很棘手。我要么从一开始就提取我想要的数据,要么将其放入对象中。
  • 您似乎正在尝试计算干旱指数。检查 [journals.ametsoc.org/doi/abs/10.1175/…paper) 比较一些众所周知的索引。如果您有时间特别关注帕尔默指数,以及他如何定义干湿期

标签: python arrays for-loop iteration time-series


【解决方案1】:

你可能想尝试这样的事情:

# first extract precipitation data for later use
precipitation = [data[i][1] for i in xrange(0, rows)]

# then test the range (i, i+m)
all_dry = all([(data==0) for data in precipitation[i:i+m]])
all_wet = not any([(data==0) for data in precipitation[i:i+m]])
# of course you can also use
all_wet = all([(data>0) for data in precipitation[i:i+m]])

但请注意,这种方法在测试相邻天数时会引入冗余计算,因此可能不适合处理大量数据。

已编辑:

好吧,这次让我们寻找更有效的方法。

# still extract precipitation data for later use first
precipitation = [data[i][1] for i in xrange(0, rows)]

# let's start our calculations by counting the longest consecutive dry days 
consecutive_dry = [1 if data == 0 else 0 for data in precipitation]
for i in xrange(1, len(consecutive_dry))
    if consecutive_dry[i] == 1:
        consecutive_dry[i] += consecutive_dry[i - 1]

# then you will see, if till day i there're m consecutive dry days, then:
consecutive_dry[i] >= m    # here is the test

# ...and it would be same for wet day testings.

这显然比上面的方法更有效:为了测试总共 N 天,连续范围 M,前一个需要 O(N * M) 操作来计算,而这需要 O(N)。

再次编辑:

这是原始代码的编辑版本。由于您的代码可以运行,它也应该在您的 PC 或其他设备上运行。

import numpy as np
data = np.loadtxt('Data Series.txt', usecols=(1,3))
dry = np.zeros(12)
wet = np.zeros(12)

rows,cols = data.shape #reading number of rows and columns into variables

# prepare 
precipitation = [data[i][1] for i in xrange(0, rows)]

# collecting data for consecutive dry days
consecutive_dry = [1 if data == 0 else 1 for data in precipitation]
for i in xrange(1, len(consecutive_dry))
    if consecutive_dry[i] == 1:
        consecutive_dry[i] += consecutive_dry[i - 1]

# ...and for wet days
consecutive_wet = [1 if data > 0 else 0 for data in precipitation]
for i in xrange(1, len(consecutive_wet))
    if consecutive_wet[i] == 1:
        consecutive_wet[i] += consecutive_wet[i - 1]

# set your day range here. 
day_range = 3

for i in xrange (0,rows):
    if consecutive_dry[i] >= day_range:
        month_id = data[i,0]
        dry[month_id - 1] += 1
    if consecutive_wet[i] >= day_range:
        month_id = data[i,0]
        wet[month_id - 1] += 1

print '3 Days Dry Spell\n', dry
print '3 Days Wet Spell\n', wet

请试试这个,如果有任何问题,请告诉我。

【讨论】:

  • 谢谢你,Starrify。但是,我仍然不清楚每个月连续3天的出现次数是如何计算的?
  • @user30337 我的两个样本都提供了测试一天是否是连续出现的干/湿天的结束的方法。因此,只需将代码中的if data[i,1] == 0 and data[i-1,1] == 0 and data[i-2,1] == 0: 行重写为我的方法即可。 :) 如果您仍然不清楚该怎么做,请告诉我,我会尝试提供一些像您这样的全功能代码。
  • 抱歉,我无法使用这两个中的任何一个运行。你能再清除一次吗?假设,我有 2 年的降雨数据。因此,一月有 61 天。我想看看在那些 1 月的日子里连续发生了 3 次干旱天,其他月份也如此。最后这些值将存储在 (1x12) 数组中。
  • 我现在要睡觉了。请在有时间的时候尝试回答我。感谢您的热心帮助。 @Starrify
  • 它给出了一个错误消息,例如“IndexError: 0-d arrays can only use a single () or a list of newaxes (and a single ...) as a index”...我有尝试了其他几种方法,但不能。 :(
【解决方案2】:

我发现以下计算平均干燥期的方法很方便。我在这里编写代码,因为它可能对其他人有用:

import numpy as np
import itertools as itr

#Import daily rainfall time series#
rain_series = np.loadtxt('daily_rainfall_timeseries.txt')

#separate the group of zero values (dry days) in a list of lists#
d = [list(x[1]) for x in itr.groupby(rain_series, lambda x: x > 0) if not x[0]]

#Count the lengths of different dry spells#
d_len = [len(f) for f in d]

#Calculate the mean dry period#
mean_dry_spell = np.mean(d_len)

【讨论】:

    猜你喜欢
    • 2013-10-07
    • 1970-01-01
    • 1970-01-01
    • 2018-07-08
    • 1970-01-01
    • 1970-01-01
    • 2019-08-26
    • 2021-07-01
    • 1970-01-01
    相关资源
    最近更新 更多