private void ParseForecastData(TAFDto dto, forecast[] xml) { var lineNumber = 0; foreach (var line in xml) { var lineDto = new TAFLineDto(); lineDto.Altimeter = ParserHelpers.GetValue(line.altim_in_hgSpecified, line.altim_in_hg); lineDto.ForecastTimeEnd = ParserHelpers.ParseDateTime(line.fcst_time_to); lineDto.ForecastTimeStart = ParserHelpers.ParseDateTime(line.fcst_time_from); if (line.sky_condition != null) { lineDto.SkyCondition.AddRange(line.sky_condition .Select(sc => GetSkyCondition(sc))); } // First line may not have an change type if (lineNumber > 0 && !String.IsNullOrWhiteSpace(line.change_indicator)) { lineDto.ChangeIndicator = ChangeIndicatorType.GetByName(line.change_indicator); } if (line.icing_condition != null) { lineDto.IcingHazards.AddRange(line.icing_condition .Where(ic => !String.IsNullOrWhiteSpace(ic.icing_intensity)) .Select(ic => GetHazard(ic))); } if (line.turbulence_condition != null) { lineDto.TurbulenceHazards.AddRange(line.turbulence_condition .Where(tb => !String.IsNullOrWhiteSpace(tb.turbulence_intensity)) .Select(tb => GetHazard(tb))); } lineDto.VerticalVisibility = ParserHelpers.GetValue( line.vert_vis_ftSpecified, line.vert_vis_ft); lineDto.Visibility = ParserHelpers.GetValue( line.visibility_statute_miSpecified, line.visibility_statute_mi); lineDto.Weather = line.wx_string; lineDto.Wind = GetWind(line); lineDto.WindShear = GetWindSheer(line); dto.TAFLine.Add(lineDto); lineNumber++; } }
private ForecastDto GetForecast(string line, List <TAFCSVField> fieldOrder) { var dto = new ForecastDto() { GeographicData = new GeographicDataDto(), TAF = new List <TAFDto>() { new TAFDto() } }; var taf = dto.TAF[0]; TAFLineDto tafLine = new TAFLineDto(); var fields = line.Split(ParserConstants.CSVSplitCharacter); for (var idx = 0; idx < fieldOrder.Count && idx < fields.Length; idx++) { try { if (fieldOrder[idx] == TAFCSVField.fcst_time_from && String.IsNullOrWhiteSpace(fields[idx])) { // We've read all fields associated with this forecast so bail out early break; } // Skip all unknown and empty fields if (fieldOrder[idx] == TAFCSVField.Unknown || String.IsNullOrWhiteSpace(fields[idx])) { continue; } var fieldVal = fields[idx].Trim(); if (fieldOrder[idx] == TAFCSVField.raw_text) { taf.RawTAF = fieldVal; continue; } if (fieldOrder[idx] == TAFCSVField.altim_in_hg) { tafLine.Altimeter = ParserHelpers.ParseFloat(fieldVal); continue; } if (fieldOrder[idx] == TAFCSVField.bulletin_time) { taf.BulletinTime = ParserHelpers.ParseDateTime(fieldVal); continue; } if (fieldOrder[idx] == TAFCSVField.change_indicator) { tafLine.ChangeIndicator = ChangeIndicatorType.GetByName(fieldVal); continue; } if (fieldOrder[idx] == TAFCSVField.cloud_base_ft_agl) { tafLine.SkyCondition[tafLine.SkyCondition.Count - 1].CloudBase = ParserHelpers.ParseInt(fieldVal) ?? DefaultValue.Height; continue; } if (fieldOrder[idx] == TAFCSVField.sky_cover) { tafLine.SkyCondition.Add(new SkyConditionDto() { SkyCondition = SkyConditionType.ByName(fieldVal) }); continue; } if (fieldOrder[idx] == TAFCSVField.fcst_time_from) { // This should be made safe, we are assuming all new DateTime objects // have the same date if (tafLine.ForecastTimeStart > ParserConstants.DefaultDateTime) { taf.TAFLine.Add(tafLine); tafLine = new TAFLineDto(); } tafLine.ForecastTimeStart = ParserHelpers.ParseDateTime(fieldVal); continue; } if (fieldOrder[idx] == TAFCSVField.fcst_time_to) { tafLine.ForecastTimeEnd = ParserHelpers.ParseDateTime(fieldVal); continue; } if (fieldOrder[idx] == TAFCSVField.icing_intensity) { var intensity = ParserHelpers.ParseInt(fieldVal) ?? -1; tafLine.IcingHazards.Add(new HazardDto() { Intensity = IcingIntensity.ByValue(intensity) }); continue; } if (fieldOrder[idx] == TAFCSVField.icing_max_alt_ft_agl) { tafLine.IcingHazards[tafLine.IcingHazards.Count - 1].MaxAltitude = ParserHelpers.ParseInt(fieldVal) ?? DefaultValue.Height; continue; } if (fieldOrder[idx] == TAFCSVField.icing_min_alt_ft_agl) { tafLine.IcingHazards[tafLine.IcingHazards.Count - 1].MinAltitude = ParserHelpers.ParseInt(fieldVal) ?? DefaultValue.Height; continue; } if (fieldOrder[idx] == TAFCSVField.issue_time) { taf.IssuedTime = ParserHelpers.ParseDateTime(fieldVal); continue; } if (fieldOrder[idx] == TAFCSVField.max_or_min_temp_c) { var tempFloat = ParserHelpers.ParseFloat(fieldVal); if (tempFloat.HasValue) { if (tafLine.TemperatureRange == null) { tafLine.TemperatureRange = new TemperatureRangeDto() { MaxTemperature = tempFloat }; } else { if (tempFloat > tafLine.TemperatureRange.MaxTemperature) { tafLine.TemperatureRange.MinTemperature = tafLine.TemperatureRange.MaxTemperature; tafLine.TemperatureRange.MaxTemperature = tempFloat; } else { tafLine.TemperatureRange.MinTemperature = tempFloat; } } } continue; } if (fieldOrder[idx] == TAFCSVField.not_decoded) { tafLine.NotDecoded = fieldVal; continue; } if (fieldOrder[idx] == TAFCSVField.probability) { tafLine.Probability = ParserHelpers.ParseInt(fieldVal); continue; } if (fieldOrder[idx] == TAFCSVField.remarks) { taf.Remarks = fieldVal; continue; } if (fieldOrder[idx] == TAFCSVField.sfc_temp_c) { tafLine.Temperature = ParserHelpers.ParseFloat(fieldVal); continue; } if (fieldOrder[idx] == TAFCSVField.station_id) { dto.ICAO = fieldVal; continue; } if (fieldOrder[idx] == TAFCSVField.time_becoming) { tafLine.TimeBecoming = ParserHelpers.ParseDateTime(fieldVal); continue; } if (fieldOrder[idx] == TAFCSVField.turbulence_intensity) { var intensity = ParserHelpers.ParseInt(fieldVal) ?? -1; tafLine.TurbulenceHazards.Add(new HazardDto() { Intensity = TurbulenceIntensity.ByValue(intensity) }); continue; } if (fieldOrder[idx] == TAFCSVField.turbulence_max_alt_ft_agl) { tafLine.TurbulenceHazards[tafLine.TurbulenceHazards.Count - 1].MaxAltitude = ParserHelpers.ParseInt(fieldVal) ?? DefaultValue.Height; continue; } if (fieldOrder[idx] == TAFCSVField.turbulence_min_alt_ft_agl) { tafLine.TurbulenceHazards[tafLine.TurbulenceHazards.Count - 1].MinAltitude = ParserHelpers.ParseInt(fieldVal) ?? DefaultValue.Height; continue; } if (fieldOrder[idx] == TAFCSVField.valid_time) { ///TODO: Temperature valid time, need to determine how this should be handled continue; } if (fieldOrder[idx] == TAFCSVField.valid_time_from) { taf.ValidTimeStart = ParserHelpers.ParseDateTime(fieldVal); continue; } if (fieldOrder[idx] == TAFCSVField.valid_time_to) { taf.ValidTimeEnd = ParserHelpers.ParseDateTime(fieldVal); continue; } if (fieldOrder[idx] == TAFCSVField.vert_vis_ft) { tafLine.VerticalVisibility = ParserHelpers.ParseInt(fieldVal); continue; } if (fieldOrder[idx] == TAFCSVField.visibility_statute_mi) { tafLine.Visibility = ParserHelpers.ParseFloat(fieldVal); continue; } if (fieldOrder[idx] == TAFCSVField.elevation_m) { dto.GeographicData.Elevation = ParserHelpers.ParseFloat(fieldVal); continue; } if (fieldOrder[idx] == TAFCSVField.latitude) { dto.GeographicData.Latitude = ParserHelpers.ParseFloat(fieldVal); continue; } if (fieldOrder[idx] == TAFCSVField.longitude) { dto.GeographicData.Longitude = ParserHelpers.ParseFloat(fieldVal); continue; } if (fieldOrder[idx] == TAFCSVField.wind_dir_degrees) { tafLine.Wind = new WindDto() { Direction = ParserHelpers.ParseInt(fieldVal) }; continue; } if (fieldOrder[idx] == TAFCSVField.wind_gust_kt) { tafLine.Wind.Gust = ParserHelpers.ParseInt(fieldVal); continue; } if (fieldOrder[idx] == TAFCSVField.wind_speed_kt) { tafLine.Wind.Speed = ParserHelpers.ParseInt(fieldVal); continue; } if (fieldOrder[idx] == TAFCSVField.wx_string) { tafLine.Weather = fieldVal; continue; } if (fieldOrder[idx] == TAFCSVField.wind_shear_hgt_ft_agl) { tafLine.WindShear = new WindShearDto() { Height = ParserHelpers.ParseInt(fieldVal) }; continue; } if (fieldOrder[idx] == TAFCSVField.wind_shear_dir_degrees) { tafLine.WindShear.Direction = ParserHelpers.ParseInt(fieldVal); continue; } if (fieldOrder[idx] == TAFCSVField.wind_shear_speed_kt) { tafLine.WindShear.Speed = ParserHelpers.ParseInt(fieldVal); continue; } } catch (Exception ex) { Console.WriteLine($"Index: {idx}, Message: {ex.Message}"); } } // Add the most recently parsed line if it has valid data if (tafLine.ForecastTimeStart > ParserConstants.DefaultDateTime) { dto.TAF[0].TAFLine.Add(tafLine); } return(dto); }