Decoding Cloud details and more understanding Re with python 3.7 - decoding METAR
I have began understanding re more and more, but still have struggles in that field. For example I was capable of creating such a pattern search using Re:
_vis=re.search(r"s(d4)s((?:d4[NWSE]))?(?:R(d2[LR]?)/(d4[NDU]))?",metar)
And I am happy, but for example I wasn't able to put in that pattern to search also a word "CAVOK". When i tried to use it many of phrases were just not searched at all. Also i've happen to find a METAR class for python, but I'm having problems understanding how it works. Here is the sample:
WEATHER_RE = re.compile(r"""^(?P<int>(-|+|VC)*)
(?P<desc>(MI|PR|BC|DR|BL|SH|TS|FZ)+)?
(?P<prec>(DZ|RA|SN|SG|IC|PL|GR|GS|UP|/)*)
(?P<obsc>BR|FG|FU|VA|DU|SA|HZ|PY)?
(?P<other>PO|SQ|FC|SS|DS|NSW|/+)?
(?P<int2>[-+])?s+""",
re.VERBOSE)
def _handleWeather(self, d):
"""
Parse a present-weather group.
The following attributes are set:
weather [list of tuples]
. intensity [string]
. description [string]
. precipitation [string]
. obscuration [string]
. other [string]
"""
inteni = d['int']
if not inteni and d['int2']:
inteni = d['int2']
desci = d['desc']
preci = d['prec']
obsci = d['obsc']
otheri = d['other']
self.weather.append((inteni, desci, preci, obsci, otheri))
handlers = [(WEATHER_RE, _handleWeather, True)]
Ive done it completely differently, and my code looks like a 10year old would do, but still don't understand how this works. This is how ive treated the cloud section. Each phrase may be independently, and as you can guess after doing it like Ive did it looks like rubish and I need to make it more efficent:
_sky_few=re.search(r"(FEW)(d3)",metar)
_sky_sct=re.search(r"(SCT)(d3)",metar)
_sky_bkn=re.search(r"(BKN)(d3)",metar)
_sky_ovc=re.search(r"(OVC)(d3)",metar)
_sky_skc=re.search(r"SKC",metar)
_sky_nsc=re.search(r"NSC",metar)
_sky_vv=re.search(r"VV(d3)",metar)
_cavok=re.search(r"CAVOK",metar)
and in that metar class they've done it like this:
SKY_RE= re.compile(r"""^(?P<cover>VV|CLR|SKC|SCK|NSC|NCD|BKN|SCT|FEW|[O0]VC|///)
(?P<height>[dO]2,4|///)?
(?P<cloud>([A-Z][A-Z]+|///))?s+""",
re.VERBOSE)
Would be grateful for you assistance.
edit:
Ok, I've managed to get a little hold of it. I first thought that things in <> are already a variables... and it turns out that it is not. Now in a simpler manner I've managed to do the following:
metar = "METAR XXXX 241100Z VRB20KTG60 9999 FEW020TCU SCT030 BKN060 OVC080 09/M00 Q1005 RMK 090 053 3/3="
SKY_RE= re.compile(r"(?P<cover>FEW|SCT|BKN|OVC|[0]VC|///)(?P<height>[dO]3|///)",re.VERBOSE)
SKY_RE.findall(metar)
and i'm getting the following list:
[('FEW', '020'), ('SCT', '030'), ('BKN', '060'), ('OVC', '080')]
how could I get it to variables in a few simple commands that later I could get convert into each list and then translate using dictionary? So that I could have 2 lists without again using regex but answering to its indexes via for loop for i in a for example:
[FEW, SCT, BKN, OVC]
[020, 030, 060, 080]
python-3.7
add a comment |
I have began understanding re more and more, but still have struggles in that field. For example I was capable of creating such a pattern search using Re:
_vis=re.search(r"s(d4)s((?:d4[NWSE]))?(?:R(d2[LR]?)/(d4[NDU]))?",metar)
And I am happy, but for example I wasn't able to put in that pattern to search also a word "CAVOK". When i tried to use it many of phrases were just not searched at all. Also i've happen to find a METAR class for python, but I'm having problems understanding how it works. Here is the sample:
WEATHER_RE = re.compile(r"""^(?P<int>(-|+|VC)*)
(?P<desc>(MI|PR|BC|DR|BL|SH|TS|FZ)+)?
(?P<prec>(DZ|RA|SN|SG|IC|PL|GR|GS|UP|/)*)
(?P<obsc>BR|FG|FU|VA|DU|SA|HZ|PY)?
(?P<other>PO|SQ|FC|SS|DS|NSW|/+)?
(?P<int2>[-+])?s+""",
re.VERBOSE)
def _handleWeather(self, d):
"""
Parse a present-weather group.
The following attributes are set:
weather [list of tuples]
. intensity [string]
. description [string]
. precipitation [string]
. obscuration [string]
. other [string]
"""
inteni = d['int']
if not inteni and d['int2']:
inteni = d['int2']
desci = d['desc']
preci = d['prec']
obsci = d['obsc']
otheri = d['other']
self.weather.append((inteni, desci, preci, obsci, otheri))
handlers = [(WEATHER_RE, _handleWeather, True)]
Ive done it completely differently, and my code looks like a 10year old would do, but still don't understand how this works. This is how ive treated the cloud section. Each phrase may be independently, and as you can guess after doing it like Ive did it looks like rubish and I need to make it more efficent:
_sky_few=re.search(r"(FEW)(d3)",metar)
_sky_sct=re.search(r"(SCT)(d3)",metar)
_sky_bkn=re.search(r"(BKN)(d3)",metar)
_sky_ovc=re.search(r"(OVC)(d3)",metar)
_sky_skc=re.search(r"SKC",metar)
_sky_nsc=re.search(r"NSC",metar)
_sky_vv=re.search(r"VV(d3)",metar)
_cavok=re.search(r"CAVOK",metar)
and in that metar class they've done it like this:
SKY_RE= re.compile(r"""^(?P<cover>VV|CLR|SKC|SCK|NSC|NCD|BKN|SCT|FEW|[O0]VC|///)
(?P<height>[dO]2,4|///)?
(?P<cloud>([A-Z][A-Z]+|///))?s+""",
re.VERBOSE)
Would be grateful for you assistance.
edit:
Ok, I've managed to get a little hold of it. I first thought that things in <> are already a variables... and it turns out that it is not. Now in a simpler manner I've managed to do the following:
metar = "METAR XXXX 241100Z VRB20KTG60 9999 FEW020TCU SCT030 BKN060 OVC080 09/M00 Q1005 RMK 090 053 3/3="
SKY_RE= re.compile(r"(?P<cover>FEW|SCT|BKN|OVC|[0]VC|///)(?P<height>[dO]3|///)",re.VERBOSE)
SKY_RE.findall(metar)
and i'm getting the following list:
[('FEW', '020'), ('SCT', '030'), ('BKN', '060'), ('OVC', '080')]
how could I get it to variables in a few simple commands that later I could get convert into each list and then translate using dictionary? So that I could have 2 lists without again using regex but answering to its indexes via for loop for i in a for example:
[FEW, SCT, BKN, OVC]
[020, 030, 060, 080]
python-3.7
add a comment |
I have began understanding re more and more, but still have struggles in that field. For example I was capable of creating such a pattern search using Re:
_vis=re.search(r"s(d4)s((?:d4[NWSE]))?(?:R(d2[LR]?)/(d4[NDU]))?",metar)
And I am happy, but for example I wasn't able to put in that pattern to search also a word "CAVOK". When i tried to use it many of phrases were just not searched at all. Also i've happen to find a METAR class for python, but I'm having problems understanding how it works. Here is the sample:
WEATHER_RE = re.compile(r"""^(?P<int>(-|+|VC)*)
(?P<desc>(MI|PR|BC|DR|BL|SH|TS|FZ)+)?
(?P<prec>(DZ|RA|SN|SG|IC|PL|GR|GS|UP|/)*)
(?P<obsc>BR|FG|FU|VA|DU|SA|HZ|PY)?
(?P<other>PO|SQ|FC|SS|DS|NSW|/+)?
(?P<int2>[-+])?s+""",
re.VERBOSE)
def _handleWeather(self, d):
"""
Parse a present-weather group.
The following attributes are set:
weather [list of tuples]
. intensity [string]
. description [string]
. precipitation [string]
. obscuration [string]
. other [string]
"""
inteni = d['int']
if not inteni and d['int2']:
inteni = d['int2']
desci = d['desc']
preci = d['prec']
obsci = d['obsc']
otheri = d['other']
self.weather.append((inteni, desci, preci, obsci, otheri))
handlers = [(WEATHER_RE, _handleWeather, True)]
Ive done it completely differently, and my code looks like a 10year old would do, but still don't understand how this works. This is how ive treated the cloud section. Each phrase may be independently, and as you can guess after doing it like Ive did it looks like rubish and I need to make it more efficent:
_sky_few=re.search(r"(FEW)(d3)",metar)
_sky_sct=re.search(r"(SCT)(d3)",metar)
_sky_bkn=re.search(r"(BKN)(d3)",metar)
_sky_ovc=re.search(r"(OVC)(d3)",metar)
_sky_skc=re.search(r"SKC",metar)
_sky_nsc=re.search(r"NSC",metar)
_sky_vv=re.search(r"VV(d3)",metar)
_cavok=re.search(r"CAVOK",metar)
and in that metar class they've done it like this:
SKY_RE= re.compile(r"""^(?P<cover>VV|CLR|SKC|SCK|NSC|NCD|BKN|SCT|FEW|[O0]VC|///)
(?P<height>[dO]2,4|///)?
(?P<cloud>([A-Z][A-Z]+|///))?s+""",
re.VERBOSE)
Would be grateful for you assistance.
edit:
Ok, I've managed to get a little hold of it. I first thought that things in <> are already a variables... and it turns out that it is not. Now in a simpler manner I've managed to do the following:
metar = "METAR XXXX 241100Z VRB20KTG60 9999 FEW020TCU SCT030 BKN060 OVC080 09/M00 Q1005 RMK 090 053 3/3="
SKY_RE= re.compile(r"(?P<cover>FEW|SCT|BKN|OVC|[0]VC|///)(?P<height>[dO]3|///)",re.VERBOSE)
SKY_RE.findall(metar)
and i'm getting the following list:
[('FEW', '020'), ('SCT', '030'), ('BKN', '060'), ('OVC', '080')]
how could I get it to variables in a few simple commands that later I could get convert into each list and then translate using dictionary? So that I could have 2 lists without again using regex but answering to its indexes via for loop for i in a for example:
[FEW, SCT, BKN, OVC]
[020, 030, 060, 080]
python-3.7
I have began understanding re more and more, but still have struggles in that field. For example I was capable of creating such a pattern search using Re:
_vis=re.search(r"s(d4)s((?:d4[NWSE]))?(?:R(d2[LR]?)/(d4[NDU]))?",metar)
And I am happy, but for example I wasn't able to put in that pattern to search also a word "CAVOK". When i tried to use it many of phrases were just not searched at all. Also i've happen to find a METAR class for python, but I'm having problems understanding how it works. Here is the sample:
WEATHER_RE = re.compile(r"""^(?P<int>(-|+|VC)*)
(?P<desc>(MI|PR|BC|DR|BL|SH|TS|FZ)+)?
(?P<prec>(DZ|RA|SN|SG|IC|PL|GR|GS|UP|/)*)
(?P<obsc>BR|FG|FU|VA|DU|SA|HZ|PY)?
(?P<other>PO|SQ|FC|SS|DS|NSW|/+)?
(?P<int2>[-+])?s+""",
re.VERBOSE)
def _handleWeather(self, d):
"""
Parse a present-weather group.
The following attributes are set:
weather [list of tuples]
. intensity [string]
. description [string]
. precipitation [string]
. obscuration [string]
. other [string]
"""
inteni = d['int']
if not inteni and d['int2']:
inteni = d['int2']
desci = d['desc']
preci = d['prec']
obsci = d['obsc']
otheri = d['other']
self.weather.append((inteni, desci, preci, obsci, otheri))
handlers = [(WEATHER_RE, _handleWeather, True)]
Ive done it completely differently, and my code looks like a 10year old would do, but still don't understand how this works. This is how ive treated the cloud section. Each phrase may be independently, and as you can guess after doing it like Ive did it looks like rubish and I need to make it more efficent:
_sky_few=re.search(r"(FEW)(d3)",metar)
_sky_sct=re.search(r"(SCT)(d3)",metar)
_sky_bkn=re.search(r"(BKN)(d3)",metar)
_sky_ovc=re.search(r"(OVC)(d3)",metar)
_sky_skc=re.search(r"SKC",metar)
_sky_nsc=re.search(r"NSC",metar)
_sky_vv=re.search(r"VV(d3)",metar)
_cavok=re.search(r"CAVOK",metar)
and in that metar class they've done it like this:
SKY_RE= re.compile(r"""^(?P<cover>VV|CLR|SKC|SCK|NSC|NCD|BKN|SCT|FEW|[O0]VC|///)
(?P<height>[dO]2,4|///)?
(?P<cloud>([A-Z][A-Z]+|///))?s+""",
re.VERBOSE)
Would be grateful for you assistance.
edit:
Ok, I've managed to get a little hold of it. I first thought that things in <> are already a variables... and it turns out that it is not. Now in a simpler manner I've managed to do the following:
metar = "METAR XXXX 241100Z VRB20KTG60 9999 FEW020TCU SCT030 BKN060 OVC080 09/M00 Q1005 RMK 090 053 3/3="
SKY_RE= re.compile(r"(?P<cover>FEW|SCT|BKN|OVC|[0]VC|///)(?P<height>[dO]3|///)",re.VERBOSE)
SKY_RE.findall(metar)
and i'm getting the following list:
[('FEW', '020'), ('SCT', '030'), ('BKN', '060'), ('OVC', '080')]
how could I get it to variables in a few simple commands that later I could get convert into each list and then translate using dictionary? So that I could have 2 lists without again using regex but answering to its indexes via for loop for i in a for example:
[FEW, SCT, BKN, OVC]
[020, 030, 060, 080]
python-3.7
python-3.7
edited Nov 15 '18 at 1:07
Marcus
asked Nov 14 '18 at 6:16
MarcusMarcus
33
33
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53294169%2fdecoding-cloud-details-and-more-understanding-re-with-python-3-7-decoding-meta%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53294169%2fdecoding-cloud-details-and-more-understanding-re-with-python-3-7-decoding-meta%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown