如何处理重叠的时间数组和数据

时间:2019-03-19 02:09:22

标签: arrays python-3.x string sum

我需要具体确定哪种降水类型进入此字符串,具体请参见第225行

雪,颗粒,冷冻或混合。有时候,我通过为每个循环构建一个空数组并对其求和来克服这个问题,现在我遇到的问题是什么是“混合降水”,什么不是

我来到需要将其扩展为一个数组的地方,而不是我使用np.sum函数(请参阅第298行)将其扩展为一个数组的12个不​​同的数组。问题是我很难区分是降水与雪,颗粒和冰冻是混合降水,还是由于降水的组合而没有(n)。

1 TAF CYYZ 142340Z 1500/1606 10010KT 6SM -SN BR BKN040 OVC090 
2 TEMPO 1500/1504 4SM -PLSN BR BKN020 OVC040 
3 PROB30 1500/1503 VRB20G30KT 2SM +TSRA BR BKN015 OVC030CB 
4 FM150400 18012KT P6SM BKN050 
5 TEMPO 1504/1507 3SM -SHRA BR BKN015 OVC030 
6 BECMG 1504/1506 22015KT 
7 FM150700 23015G25KT P6SM BKN040 
8 TEMPO 1510/1512 BKN020 BKN040 
9 FM151200 23015G25KT P6SM OVC015 
10 BECMG 1514/1516 23018G30KT 
11 FM152000 24018G30KT 5SM -RA BR OVC012 
12 FM160200 27015G25KT P6SM BKN020 BKN040'

我想知道我是否可以使用A,B,C,D,E代替数字 AAAA,巴巴

第264行的compare_function是我需要帮助的一个

例如

n           1
snow        10
pellets     100
freezing    1000
mix         10000

n           1
n + n           2
n + n + n   3
n + n + n + n   4

s   10
s+s 20
s+s+s   30
s+s+s+s 40
s+s+n+n 22
s+n 11
s+n+n   12
s+s+n   21
s+s+s+n 31
s+s+n+n 22

p   100
p+p 200
p+p+p   300
p+p+p+p 400
p+p+p+n 301
p+p+n+n 202
p+n+n+n 103

f   1000
f+f 2000
f+f+f   3000
f+f+f+f 4000
f+f+f+n 3001
f+f+n+n 2002
f+n+n+n 1003

m   10000
m+m 20000
m+m+m   30000
m+m+m+m 40000
p+f+s+n 1111
P+f+n+n 1102
p+f 1100
p+f+n   1101
s+p 110
s+f 1010
s+m 10010
f+p+p   1200
f+s+s   1020
p+f+s   1110

我认为这可能是一种方法,但我仍然可能缺少一些组合

winter_type={'s': 10|20|30|40|22|11|12|21|31|22,
'p': 100|200|300|400|301|202|103,
'f': 1000|2000|3000|4000|3001|2002|1003,
'm': 10000|20000|30000|40000|1111|1102|1100|1101|110|1010|10010|1200|1020|1110
'n' : 1|2|3|4}

这里是应该使用pyTAF从字符串中提取降水的时间

['2019-03-15 00:00', '2019-03-15 01:00', '2019-03-15 02:00', '2019-03-15 03:00', '2019-03-15 04:00', '2019-03-15 05:00', '2019-03-15 06:00', '2019-03-15 07:00', '2019-03-15 08:00', '2019-03-15 09:00', '2019-03-15 10:00', '2019-03-15 11:00', '2019-03-15 12:00', '2019-03-15 13:00', '2019-03-15 14:00', '2019-03-15 15:00', '2019-03-15 16:00', '2019-03-15 17:00', '2019-03-15 18:00', '2019-03-15 19:00', '2019-03-15 20:00', '2019-03-15 21:00', '2019-03-15 22:00', '2019-03-15 23:00', '2019-03-16 00:00', '2019-03-16 01:00', '2019-03-16 02:00', '2019-03-16 03:00', '2019-03-16 04:00', '2019-03-16 05:00']

这是结果时间

['2019-03-15 00:00', '2019-03-15 00:00', '2019-03-15 00:00', '2019-03-15 04:00', '2019-03-15 04:00', '2019-03-15 04:00', '2019-03-15 07:00', '2019-03-15 10:00', '2019-03-15 12:00', '2019-03-15 14:00', '2019-03-15 20:00', '2019-03-16 02:00'] ['2019-03-15 04:00', '2019-03-15 04:00', '2019-03-15 03:00', '2019-03-15 07:00', '2019-03-15 07:00', '2019-03-15 06:00', '2019-03-15 12:00', '2019-03-15 12:00', '2019-03-15 20:00', '2019-03-15 16:00', '2019-03-16 02:00', '2019-03-16 06:00']

['m', 'm', 'm', 'm', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n']

这是原始代码

# Import Libraries 
import pytaf
import re 
from datetime import datetime, timedelta 
from dateutil import parser
import numpy as np 

 """WARNING DO NOT PUT TAF IN FOR KBFM WILL NOT WORK"""

 #values = "TAF AMD KORD 150028Z 1500/1606 24024G38KT P6SM VCSH OVC025 TEMPO 1500/1503 4SM -RA FM150300 24016G28KT P6SM VCSH OVC018 FM150600 24014G21KT P6SM VCSH BKN008 OVC012 FM151200 28015G24KT P6SM VCSH OVC020 TEMPO 1514/1517 4SM -RASN FM151800 29017G28KT P6SM BKN035 TEMPO 1519/1520 4SM -RASN "

 values ='TAF CYYZ 142340Z 1500/1606 10010KT 6SM -SN BR BKN040 OVC090 TEMPO 1500/1504 4SM -PLSN BR BKN020 OVC040 PROB30 1500/1503 VRB20G30KT 2SM +TSRA BR BKN015 OVC030CB FM150400 18012KT P6SM BKN050 TEMPO 1504/1507 3SM -SHRA BR BKN015 OVC030 BECMG 1504/1506 22015KT FM150700 23015G25KT P6SM BKN040 TEMPO 1510/1512 BKN020 BKN040 FM151200 23015G25KT P6SM OVC015 BECMG 1514/1516 23018G30KT FM152000 24018G30KT 5SM -RA BR OVC012 FM160200 27015G25KT P6SM BKN020 BKN040'

def extractor_function(values):

#read values i.e. taf into the pytaf program 
def read_taf_function(values):
    taf = pytaf.TAF(values)
    return(taf)

taf=read_taf_function(values)

# initailzing vars for time year and month (for leap years and tafs the have XX00/XX24 where XX is the the same day)
leap_year = 2020, 2024, 2028
month = [1,2,3,4,5,6,7,8,8,9,10,12]
dd  = [31,28,31,30,31,30,31,31,30,31,30,31]

#

#utc time 
#current_utc_time=datetime.utcnow()
#print(current_utc_time)

# header functiom 
def header_time_information_function(taf):
    index_time_stamp = []
    yyyy = 2019
    mm =  3
    dd = 15
    begintime = []
    endtime = []
    res2=[]
    res3=[] 
    """ 
    Function built to extract time from pytaf using any Terminal Aerodrome Forecast
    Extracts - The Issuance Time, Start Time, End Time
    """   
    taf_header = taf._taf_header

    # if statement to know if leap year or not
    if mm==1 or mm==3 or mm==5 or mm==7 or mm==8 or mm==10 or mm==12:
        if dd==31 and int(taf_header["valid_till_hours"])==24:
            dd=1
            hh=int(taf_header["valid_till_hours"])==00
            res2 = datetime(yyyy, mm, int(taf_header['valid_from_date']), int(taf_header['valid_from_hours']), 00).strftime('%Y-%m-%d %H:%M')
            res3 = datetime(yyyy, mm, dd, hh, 00).strftime('%Y-%m-%d %H:%M')
        elif dd<31 and int(taf_header["valid_till_hours"])==24:
            dd=dd+1
            hh=int(taf_header["valid_till_hours"])==00
            res2 = datetime(yyyy, mm, int(taf_header['valid_from_date']), int(taf_header['valid_from_hours']), 00).strftime('%Y-%m-%d %H:%M')
            res3 = datetime(yyyy, mm, dd, hh, 00).strftime('%Y-%m-%d %H:%M')
        elif dd<31 and not int(taf_header["valid_till_hours"])==24:
            res2 = datetime(yyyy, mm, int(taf_header['valid_from_date']), int(taf_header['valid_from_hours']), 00).strftime('%Y-%m-%d %H:%M')
            res3 = datetime(yyyy, mm, int(taf_header["valid_till_date"]), int(taf_header["valid_till_hours"]), 00).strftime('%Y-%m-%d %H:%M')         


    elif mm==4 or mm==6 or mm==9 or mm==11:
        if dd==30 and int(taf_header["valid_till_hours"])==24:
            dd=1
            hh=int(taf_header["valid_till_hours"])==00
            res2 = datetime(yyyy, mm, int(taf_header['valid_from_date']), int(taf_header['valid_from_hours']), 00).strftime('%Y-%m-%d %H:%M')
            res3 = datetime(yyyy, mm, dd, hh, 00).strftime('%Y-%m-%d %H:%M')
        elif dd<30 and int(taf_header["valid_till_hours"])==24:
            dd=dd+1
            hh=int(taf_header["valid_till_hours"])==00
            res2 = datetime(yyyy, mm, int(taf_header['valid_from_date']), int(taf_header['valid_from_hours']), 00).strftime('%Y-%m-%d %H:%M')
            res3 = datetime(yyyy, mm, dd, hh, 00).strftime('%Y-%m-%d %H:%M')
        elif dd<30 and not int(taf_header["valid_till_hours"])==24:
            res2 = datetime(yyyy, mm, int(taf_header['valid_from_date']), int(taf_header['valid_from_hours']), 00).strftime('%Y-%m-%d %H:%M')
            res3 = datetime(yyyy, mm, int(taf_header["valid_till_date"]), int(taf_header["valid_till_hours"]), 00).strftime('%Y-%m-%d %H:%M')     

    elif mm==2:
      if yyyy==2020 or yyyy==2024: 
          if dd==29 and int(taf_header["valid_till_hours"])==24:
              dd=1
              hh=int(taf_header["valid_till_hours"])==00
              res2 = datetime(yyyy, mm, int(taf_header['valid_from_date']), int(taf_header['valid_from_hours']), 00).strftime('%Y-%m-%d %H:%M')
              res3 = datetime(yyyy, mm, dd, hh, 00).strftime('%Y-%m-%d %H:%M')

          elif dd<29 and int(taf_header["valid_till_hours"])==24:
              dd=dd+1
              hh=int(taf_header["valid_till_hours"])==00
              res2 = datetime(yyyy, mm, int(taf_header['valid_from_date']), int(taf_header['valid_from_hours']), 00).strftime('%Y-%m-%d %H:%M')
              res3 = datetime(yyyy, mm, dd, hh, 00).strftime('%Y-%m-%d %H:%M')

          elif dd<29 and not int(taf_header["valid_till_hours"])==24:
              res2 = datetime(yyyy, mm, int(taf_header['valid_from_date']), int(taf_header['valid_from_hours']), 00).strftime('%Y-%m-%d %H:%M')
              res3 = datetime(yyyy, mm, int(taf_header["valid_till_date"]), int(taf_header["valid_till_hours"]), 00).strftime('%Y-%m-%d %H:%M')     

      else:
          if dd==28 and int(taf_header["valid_till_hours"])==24:
              dd=1
              hh=int(taf_header["valid_till_hours"])==00
              res2 = datetime(yyyy, mm, int(taf_header['valid_from_date']), int(taf_header['valid_from_hours']), 00).strftime('%Y-%m-%d %H:%M')
              res3 = datetime(yyyy, mm, dd, hh, 00).strftime('%Y-%m-%d %H:%M')
          elif dd<28 and int(taf_header["valid_till_hours"])==24:
              dd=dd+1
              hh=int(taf_header["valid_till_hours"])==00
              res2 = datetime(yyyy, mm, int(taf_header['valid_from_date']), int(taf_header['valid_from_hours']), 00).strftime('%Y-%m-%d %H:%M')
              res3 = datetime(yyyy, mm, dd, hh, 00).strftime('%Y-%m-%d %H:%M')
          elif dd<28 and not int(taf_header["valid_till_hours"])==24:
              res2 = datetime(yyyy, mm, int(taf_header['valid_from_date']), int(taf_header['valid_from_hours']), 00).strftime('%Y-%m-%d %H:%M')
              res3 = datetime(yyyy, mm, int(taf_header["valid_till_date"]), int(taf_header["valid_till_hours"]), 00).strftime('%Y-%m-%d %H:%M')     

    begintime = res2
    endtime = res3
    return(begintime,endtime)

def timex_function(taf):
    index_time_stamp = []
    index_time_stamp.append(1)
    yyyy = 2019
    mm =  3
    dd = 15
    dumby=header_time_information_function(taf)
    insertfromtime = []
    begintime=list()
    begintime.append(dumby[0])
    endtime=list()
    endtime.append(dumby[1])
    # initilzing vars
    regex = r"PROB\d{0,2}"
    weather_groups=taf._weather_groups
    #for val in weather_groups:
    #    print(val)

    """For Loop with assoicated if and if else to  Pull Each Time (DD/HH:MM) in Each From Tempo Prob Becming Line in the Terminal Aerodrome Forecast"""

    for i in range(1,len(weather_groups)):

        # initilzing vars
        res1=[]
        res2=[]
        fmtype=[]
        #print(str(weather_groups[i]["header"]))
        fmtype = str(weather_groups[i]["header"]["type"])

        if fmtype == "FM":
            res1 = datetime(yyyy, mm, int(weather_groups[i]["header"]["from_date"]), int(weather_groups[i]["header"]["from_hours"]), int( weather_groups[i]["header"]["from_minutes"])).strftime('%Y-%m-%d %H:%M')
            begintime.append(res1)
            endtime.append('-9999')
            insertfromtime.append(res1)
            index_time_stamp.append(1)

        elif fmtype == "TEMPO":
            res1 = datetime(yyyy, mm, int(weather_groups[i]["header"]["from_date"]), int(weather_groups[i]["header"]["from_hours"]),00).strftime('%Y-%m-%d %H:%M')
            res2 = datetime(yyyy, mm, int(weather_groups[i]["header"]["till_date"]), int(weather_groups[i]["header"]["till_hours"]),00).strftime('%Y-%m-%d %H:%M')
            begintime.append(res1)
            endtime.append(res2)
            index_time_stamp.append(2)

        elif re.search(regex,fmtype):
            res1 = datetime(yyyy, mm, int(weather_groups[i]["header"]["from_date"]), int(weather_groups[i]["header"]["from_hours"]), 00).strftime('%Y-%m-%d %H:%M')
            res2 = datetime(yyyy, mm, int(weather_groups[i]["header"]["till_date"]), int(weather_groups[i]["header"]["till_hours"]), 00).strftime('%Y-%m-%d %H:%M')
            begintime.append(res1)
            endtime.append(res2)
            index_time_stamp.append(2)

        elif fmtype =="BECMG":
            res1 = datetime(yyyy, mm, int(weather_groups[i]["header"]["from_date"]), int(weather_groups[i]["header"]["from_hours"]), 00).strftime('%Y-%m-%d %H:%M')
            res2 = datetime(yyyy, mm, int(weather_groups[i]["header"]["till_date"]), int(weather_groups[i]["header"]["till_hours"]), 00).strftime('%Y-%m-%d %H:%M')
            begintime.append(res1)
            endtime.append(res2)
            index_time_stamp.append(2)
        fmtype=[] 

    if index_time_stamp[len(index_time_stamp)-1]==1: 
        firstendtime =insertfromtime[0]  
        insertfromtime = insertfromtime[1:len(insertfromtime)]
        endtime_shorten = endtime[0:len(endtime)-1]
        reference00 = [i for i in range(len(endtime_shorten))]    
        val3 = 0
        for val1,val2 in zip(endtime_shorten,reference00):
        #print(val1)
            if val1=='-9999':
                print(val3)
                endtime_shorten[val2]=insertfromtime[val3]
                val3 = val3 + 1 

        endtime = list(endtime_shorten) + [endtime[len(endtime)-1]]
        endtime = [firstendtime] + list(endtime[1:len(endtime)-1]) + [endtime[0]]

    elif index_time_stamp[len(index_time_stamp)-1]==2: 
        firstendtime =insertfromtime[0]  
        insertfromtime = list(insertfromtime[1:len(insertfromtime)]) + [endtime[0]]
        print(insertfromtime)
        endtime = endtime[1:len(endtime)]
        reference00 = [i for i in range(len(endtime))]
        val3 = 0
        for val1,val2 in zip(endtime,reference00):
            print(val1,val2,endtime[val2])
            if val1=='-9999':
                endtime[val2]=insertfromtime[val3]
                val3 = val3 + 1 

        endtime = [firstendtime] + list(endtime[0:len(endtime)]) 

    print(begintime,endtime)
    return(begintime,endtime)


def precip_extraction_function():

    """
    Function built for extracting precip group 
    """

    # initilzing vars

    precip_tf = []
    precip_type = []
    precip_groups = taf._raw_weather_groups
    precip = {
        'snow': r'\sSN\s|\s-SN\s|\s+SN\s',
        'pellets': r'\sPL\s|\s-PL\s|\s+PL\s|\sIC\s|\s-IC\s|\s+IC\s',
        'freezing': r'\sFZRA\s|\s-FZRA\s|\s+FZRA\s|\sFZDZ\s|\s-FZDZ\s|\s+FZDZ\s',
        'mix': r'RAPL|PLRA|SNPL|PLSN|SNFZDZ|SNFZRA|RASN|SNRA|DZSN|SNDZ'}

    for g in precip_groups:
        precip_tf.append(' '.join([k for k, v in precip.items() if re.search(v, g)]))

    for values in precip_tf:
        if values == 'snow':
            precip_type.append(100)
        elif values == 'pellets':
            precip_type.append(200)
        elif values == 'freezing':
           precip_type.append(300)
        elif values == 'mix':
            precip_type.append(400)
        elif values == '':
           precip_type.append(-999)

    return(precip_type)

print(precip_extraction_function())

def datetime_range(start, end, delta):
    current = start

    while current < end:
        yield current
        current += delta

def time_range_function(): 
    results=header_time_information_function(taf)
    startime=datetime.strptime(results[0],"%Y-%m-%d %H:%M")
    endtime=datetime.strptime(results[1], '%Y-%m-%d %H:%M')
    reference_time = [dt.strftime('%Y-%m-%d %H:%M') for dt in datetime_range(startime, endtime, timedelta(hours=1))]
    return(reference_time)

def comparing_function():

    # winter precip is [number of line, number of reference time]  
    number_of_lines=precip_extraction_function()
    reference_time = time_range_function()
    reference_time_index = [i for i in range(len(reference_time))]
    winter_precip = np.zeros((len(number_of_lines), len(reference_time))) 
    times=timex_function(taf)
    gotime=times[0]
    nogotime=times[1]
    precip_tf = precip_extraction_function()
    line_reference_index = [i for i in range(len(precip_tf))]

    # for loop to loop thru the TAF lines 
    for values01,values02,values03,values04 in zip(gotime,nogotime,precip_tf,line_reference_index): 
        startime=datetime.strptime(values01,'%Y-%m-%d %H:%M')
        endtime=datetime.strptime(values02, '%Y-%m-%d %H:%M')
        # for loop to loop thru the reference time 
        for values05,values06 in zip(reference_time,reference_time_index): 
            currenttime=datetime.strptime(values05, '%Y-%m-%d %H:%M') 

            if parser.parse(str(startime))<=parser.parse(str(currenttime))<parser.parse(str(endtime)):

                if values03==-999:
                    winter_precip[values04,values06] = 1
                elif values03==100: 
                    winter_precip[values04,values06] = 10
                elif values03==200:
                    winter_precip[values04,values06] = 100
                elif values03==300:
                    winter_precip[values04,values06] = 1000
                elif values03==400:
                    winter_precip[values04,values06]= 10000
    print(winter_precip)
    winter_totals = np.sum(winter_precip,axis=0)
    print(winter_totals)
    winter_weather = list()
    for values in winter_totals:
      if values==10 and values==13:
          winter_weather.append('s')
      elif values>=100 and values<1000:
          winter_weather.append('p')
      elif values>=1000 and values<10000:
          winter_weather.append('f')
      elif values>=10000:
          winter_weather.append('m')
      elif values<10:
          winter_weather.append('n')
    print(len(winter_weather))
    return(winter_weather)



def icao_station_info_function():
    station_id = taf._taf_header['icao_code']
    return(station_id)

return(comparing_function(),time_range_function(),icao_station_info_function())




return(timex_function(taf))

print(extractor_function(values))

0 个答案:

没有答案