public static string EnumToString <T>(T value) { if (Namings.ContainsKey(typeof(T))) { var naming = (IEnumNaming <T>)Namings[typeof(T)]; return(naming.Name(value)); } return(value.ToString()); }
public static void FillDataSetWithRequest(DataSet ds, ClimateParameter parameter, double[] latmin, double[] latmax, double[] lonmin, double[] lonmax, int[] starthour, int[] stophour, int[] startday, int[] stopday, int[] startyear, int[] stopyear, FetchingOptions options) { ds.IsAutocommitEnabled = false; ds.Metadata[Namings.metadataNameParameter] = Namings.GetParameterName(parameter); ds.Metadata[Namings.metadataNameCoverage] = Namings.GetCoverageName(parameter); ds.Metadata[Namings.metadataNameProvenanceHint] = options.FetchClimateProvenanceControlStr; if (!ds.Variables.Contains(Namings.VarNameLatMax)) { ds.AddVariable <double>(Namings.VarNameLatMax, Namings.dimNameCells); } if (!ds.Variables.Contains(Namings.VarNameLatMin)) { ds.AddVariable <double>(Namings.VarNameLatMin, Namings.dimNameCells); } if (!ds.Variables.Contains(Namings.VarNameLonMax)) { ds.AddVariable <double>(Namings.VarNameLonMax, Namings.dimNameCells); } if (!ds.Variables.Contains(Namings.VarNameLonMin)) { ds.AddVariable <double>(Namings.VarNameLonMin, Namings.dimNameCells); } if (!ds.Variables.Contains(Namings.VarNameDayMax)) { ds.AddVariable <int>(Namings.VarNameDayMax, Namings.dimNameCells); } if (!ds.Variables.Contains(Namings.VarNameDayMin)) { ds.AddVariable <int>(Namings.VarNameDayMin, Namings.dimNameCells); } if (!ds.Variables.Contains(Namings.VarNameHourMax)) { ds.AddVariable <int>(Namings.VarNameHourMax, Namings.dimNameCells); } if (!ds.Variables.Contains(Namings.VarNameHourMin)) { ds.AddVariable <int>(Namings.VarNameHourMin, Namings.dimNameCells); } if (!ds.Variables.Contains(Namings.VarNameYearMax)) { ds.AddVariable <int>(Namings.VarNameYearMax, Namings.dimNameCells); } if (!ds.Variables.Contains(Namings.VarNameYearMin)) { ds.AddVariable <int>(Namings.VarNameYearMin, Namings.dimNameCells); } ds.Variables[Namings.VarNameDayMax].Append(stopday); ds.Variables[Namings.VarNameDayMin].Append(startday); ds.Variables[Namings.VarNameHourMax].Append(stophour); ds.Variables[Namings.VarNameHourMin].Append(starthour); ds.Variables[Namings.VarNameYearMax].Append(stopyear); ds.Variables[Namings.VarNameYearMin].Append(startyear); ds.Variables[Namings.VarNameLonMax].Append(lonmax); ds.Variables[Namings.VarNameLonMin].Append(lonmin); ds.Variables[Namings.VarNameLatMax].Append(latmax); ds.Variables[Namings.VarNameLatMin].Append(latmin); ds.Commit(); }
public static FetchClimateBatchResponce BuildBatchResult(DataSet ds) { if (IsProcessingSuccessful(ds)) // success { FetchClimateBatchResponce res = new FetchClimateBatchResponce(); if (ds.Metadata.ContainsKey(Namings.metadataNameServiceVersion)) { res.ServiceVersion = ds.Metadata[Namings.metadataNameServiceVersion].ToString(); } var activeParameter = Namings.GetParameterByName((string)ds.Metadata[Namings.metadataNameParameter]); //var activeCoverage = Namings.GetCoverageByName((string)ds.Metadata[Namings.metadataNameCoverage]); res.Values = null; double[] result = (double[])ds.Variables[Namings.VarNameResult].GetData(); double[] uncertatinty = null; string[] provenance = null; if (ds.Variables.Contains(Namings.VarNameUncertainty)) { uncertatinty = (double[])ds.Variables[Namings.VarNameUncertainty].GetData(); } else { uncertatinty = new double[result.Length]; } provenance = new string[result.Length]; ushort[] ids = null; short[] provArr = null; string[] descs = null; if (ds.Variables.Contains(Namings.VarNameResultProvenance) && ds.Variables.Contains(Namings.VarNameConfigProcDesc) && ds.Variables.Contains(Namings.VarNameConfigProcId)) { ids = (ushort[])ds.Variables[Namings.VarNameConfigProcId].GetData(); descs = (string[])ds.Variables[Namings.VarNameConfigProcDesc].GetData(); provArr = (short[])ds.Variables[Namings.VarNameResultProvenance].GetData(); for (int i = 0; i < provArr.Length; i++) { provenance[i] = descs[ids[provArr[i]]]; } } else { provArr = Enumerable.Repeat <short>(0, provenance.Length).ToArray(); } Func <double, double, ClimateParameterValue> ClimateValueConstructor = null; switch (activeParameter) { case Service.ClimateParameter.FC_TEMPERATURE: ClimateValueConstructor = (r, u) => new TemperatureValue(r, u); break; case Service.ClimateParameter.FC_PRECIPITATION: ClimateValueConstructor = (r, u) => new PrecipitationValue(r, u); break; case Service.ClimateParameter.FC_SOIL_MOISTURE: ClimateValueConstructor = (r, u) => new SoilMoistureValue(r, u); break; case Service.ClimateParameter.FC_RELATIVE_HUMIDITY: ClimateValueConstructor = (r, u) => new RelativeHumidityValue(r, u); break; case Service.ClimateParameter.FC_ELEVATION: ClimateValueConstructor = (r, u) => new ElevationValue(r, u); break; case Service.ClimateParameter.FC_DIURNAL_TEMPERATURE_RANGE: ClimateValueConstructor = (r, u) => new DiurnalTemperatureRangeValue(r, u); break; case Service.ClimateParameter.FC_FROST_DAY_FREQUENCY: ClimateValueConstructor = (r, u) => new FrostDayFrequencyValue(r, u); break; case Service.ClimateParameter.FC_WET_DAY_FREQUENCY: ClimateValueConstructor = (r, u) => new WetDayFrequencyValue(r, u); break; case Service.ClimateParameter.FC_WIND_SPEED: ClimateValueConstructor = (r, u) => new WindSpeedValue(r, u); break; case Service.ClimateParameter.FC_SUN_PERCENTAGE: ClimateValueConstructor = (r, u) => new SunPercentageValue(r, u); break; default: throw new NotSupportedException(); } res.Values = result .Zip(uncertatinty, ClimateValueConstructor) .Zip(provArr, (val, id) => { val.ProvenanceId = id; return(val); }) .Select(val => { val.ProcDesc = descs; val.ProcIds = ids; return(val); }) .Zip(provenance, (val, prov) => { val.Provenance = prov; return(val); }) .ToArray(); res.ProvenanceUsed = ds.Metadata.ContainsKey(Namings.metadataNameProvenanceUsed) && (ds.Metadata[Namings.metadataNameProvenanceUsed] != null) ? ds.Metadata[Namings.metadataNameProvenanceUsed].ToString() : "Unknown"; return(res); } else // failure { StringBuilder sb = new StringBuilder("Computation failed"); string[] mess = ds.GetData <string[]>(Namings.VarNameLogMessage); string[] types = ds.GetData <string[]>(Namings.VarNameLogMessageType); DateTime[] times = ds.GetData <DateTime[]>(Namings.VarNameLogMessageTime); for (int i = 0; i < mess.Length; i++) { sb.AppendLine().Append(String.Format("{0}({1}):{2}", times[i], types[i], mess[i])); } throw new Exception(sb.ToString()); } }
public static void RegisterNaming <T>(IEnumNaming <T> naming) { Namings.Add(typeof(T), naming); }