0

I'm expanding upon my previously asked question.

Before I explain, here is my C# code:

static Dictionary<string, string> phenom = new Dictionary<string, string> 
{
    {"-", "Light"},
    {"\\+", "Heavy"},
    {"\bVC\b","In the Vicinity"},
    // descriptor
    {"MI","Shallow"},
    {"PR","Partial"},
    {"BC","Patches"},
    {"DR","Low Drifting"},
    {"BL","Blowing"},
    {"SH","Showers"},
    {"TS","Thunderstorm"},
    {"FZ","Freezing"},
    // precipitation
    {"DZ","Drizzle"},
    {"RA","Rain"},
    {"SN","Snow"},
    {"SG","Snow Grains"},
    {"IC","Ice Crystals"},
    {"PL","Ice Pellets"},
    {"GR","Hail"},
    {"GS","Small Hail/Snow Pellets"},
    {"UP","Uknown Precipitation"},
    // obscuration
    {"BR","Mist"},
    {"FG","Fog"},
    {"FU","Smoke"},
    {"VA","Volcanic Ash"},
    {"DU","Widespread Dust"},
    {"SA","Sand"},
    {"HZ","Haze"},
    {"PY","Spray"},
    // other
    {"PO","Well-Developed Dust/Sand Whirls"},
    {"SQ","Squalls"},
    {"FC","Funnel Cloud Tornado Waterspout"},
    {"SS","Sandstorm"},
    {"DS","Duststorm"}
};

public static string Process(String metar)
{
    metar = Regex.Replace(metar, "(?<=A[0-9]{4}).*", "");
    StringBuilder _string = new StringBuilder();

    var results = from result in phenom
                  where Regex.Match(metar, result.Key, RegexOptions.Singleline).Success
                  select result;

    foreach (var result in results)
    {
        if (result.Key == "DZ" || result.Key == "RA" || result.Key == "SN" || result.Key == "SG" || result.Key == "PL")
        {
            switch (result.Key)
            {
                case "+":
                    _string.Append("Heavy ");
                    break;
                case "-":
                    _string.Append("Light");
                    break;
                case "VC":
                    _string.Append("In the Vicinity ");
                    break;
                default:
                    break;
            }
        }

        _string.AppendFormat("{0} ", result.Value);
    }

    return _string.ToString();
}

Basically, this code parses the weather phenomenons in an airport METAR weather report. Take the following METAR for example:

KAMA 230623Z AUTO 05016KT 7SM +TSRAGR FEW070 BKN090 OVC110 16/14 A3001 RMK AO2 PK WND 06026/0601 LTG DSNT E-SW RAB04 TSB02E17 P0005 T01560144

Notice the +TSRAGR...this is the portion I want to focus on. The code I currently have works fine...but not close to what I actually want. It spits out this: "Heavy Thunderstorm Rain Hail".

The actual decoding differs slightly. Referenced from this manual (see the 5th page).

The intensity indicators (+ and -) always come before the first precipitation. In the above METAR, it would be RA. So, ideally it should spit out "Thunderstorm Heavy Rain Hail", instead. However, it is not. There are other times when the phenomena is not as intense...sometimes it may only be -RA, in which case should only return "Light Rain".

Also to note, as the manual I referenced says, the intensity identifiers are not coded with the IC, GR, GS or UP precipitation types, which explains my attempt at checking the key value before appending the intensity, but failed.

If someone could point me in the right direction on how to append the intensity identifiers in the right location, I'd much appreciate it.

TL;DR...basically, I some code logic to decide where to put prefixes before specific items from a dictionary list.

2
  • 3
    TL;DR can you give us the short version? You need a regex for what?
    – zx81
    Jun 24, 2014 at 5:20
  • Why don't you define the regex to extract what you want instead of fiddling with the input string? Jun 24, 2014 at 11:32

1 Answer 1

0

Given the structure of your code, I would recommend pre-parsing your input string to "move" the intensity indicators to after the terms they don't modify, so the words come out in the order that makes the most sense.

Then, I might consider adding a few keys to your dictionary to cover the +/- versions of each type of precipitation that can have an intensity indicator, so your overall code is simplified.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.