1

Honestly I have no clue why this isn't working I'm specifically trying to identify which code is

  • snow (SN)
  • freezing rain (FZDZ,FZRA)
  • pellets (IC,PL, IP) and
  • mixed type which is anything from RAPL|PLRA|SNPL|PLSN|SNFZDZ|SNFZRA|RASN|SNRA|DZSN|SNDZ with the + or - or none symbol.

Thanks in advance.

Import Libraries

import pytaf
import re 

values = "TAF KZZZ 072336Z 0800/0900 11006KT P6SM SN OVC060 FM080100 11006KT P6SM SN OVC060 FM080200 11006KT P6SM SN OVC060"

taf = pytaf.TAF(values)

def precip_extraction_function(taf):

precip_groups=taf._raw_weather_groups


snow = re.compile(r"SN")
pellets = re.compile(r"/-PL/|/IC/")
freezing = re.compile(r"/FZRA/|/FZDZ/")
mix=re.compile(r"(RAPL|PLRA|SNPL|PLSN|SNFZDZ|SNFZRA|RASN|SNRA|DZSN|SNDZ)")
precip_tf=[]

for lines in precip_groups:
    print(lines)
        # initilzing vars
    if (bool(snow.match(lines))) and not (bool(pellets.match(lines)) or bool(freezing.match(lines))):
        precip_tf.append(100)
    elif (bool(pellets.match(lines))) and not (bool(snow.match(lines)) or bool(freezing.match(lines))):
        precip_tf.append(200)
    elif (bool(freezing.match(lines))) and not (bool(snow.match(lines)) or bool(pellets.match(lines))):
        precip_tf.append(300)
    elif (bool(mix.match(lines))) and not (bool(freezing.match(lines)) or bool(snow.match(lines)) or bool(pellets.match(lines))): 
        precip_tf.append(400)
    elif not (bool(mix.match(lines)) or bool(freezing.match(lines)) or bool(snow.match(lines)) or bool(pellets.match(lines))):
        precip_tf.append(-999)
return(precip_tf)

print(precip_extraction_function(taf))

1 Answer 1

1

re.match only matches at the beginning of the string. To match anywhere in the string, you need to use re.search instead. For example (I wasn't following the numerical codes you are appending based on the various precipitation combos, so the example below just outputs one or more precipitation types per group to illustrate):

from pytaf import TAF
import re 

values = "TAF KZZZ 072336Z 0800/0900 11006KT P6SM SN OVC060 FM080100 11006KT P6SM SN OVC060 FM080200 11006KT P6SM SN OVC060"

precip = {
    'snow': r'SN',
    'pellets': r'-PL|IC',
    'freezing': r'FZRA|FZDZ',
    'mix': r'RAPL|PLRA|SNPL|PLSN|SNFZDZ|SNFZRA|RASN|SNRA|DZSN|SNDZ'
}

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

print(precip_tf)
# ['snow', 'snow', 'snow']
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.