public TimeRegion(ITimeRegion r) { Years = r.Years; Days = r.Days; Hours = r.Hours; IsIntervalsGridDays = r.IsIntervalsGridDays; IsIntervalsGridHours = r.IsIntervalsGridHours; IsIntervalsGridYears = r.IsIntervalsGridYears; }
public FetchDomain(double[] lats, double[] lons, double[] lats2, double[] lons2, ITimeRegion timeRegion, SpatialRegionSpecification spatType, Array mask) { if (mask != null && mask.GetType().GetElementType() != typeof(bool)) { throw new ArgumentException("Mask array must have bool type"); } this.mask = mask; this.lats = lats; this.lons = lons; this.lats2 = lats2; this.lons2 = lons2; this.timeRegion = timeRegion; this.spatialType = spatType; }
public static IEnumerable <Tuple <int, int> > GetHoursSegments(this ITimeRegion timeRegion) { int len = timeRegion.HoursAxisLength; if (timeRegion.IsIntervalsGridHours) { for (int i = 0; i < len; i++) { yield return(Tuple.Create(timeRegion.Hours[i], timeRegion.Hours[i + 1])); } } else { for (int i = 0; i < len; i++) { yield return(Tuple.Create(timeRegion.Hours[i], timeRegion.Hours[i])); } } }
/// <summary> /// Fills the dataset with yearly timeseries definition (iterating years) /// </summary> /// <param name="job"></param> /// <param name="firstYear"></param> /// <param name="lastYear"></param> /// <param name="firstDay"></param> /// <param name="lastDay"></param> /// <param name="startHour"></param> /// <param name="stopHour"></param> /// <param name="stepLen">1..n</param> public static ITimeRegion GetYearlyTimeseries(this ITimeRegion r, int firstYear = 1961, int lastYear = 1990, int stepLen = 1, bool isIntervalTimeseries = true) { var region = new TimeRegion(r); if (isIntervalTimeseries) { lastYear++; } List <int> firstYearsList = new List <int>(); int year = firstYear; while (year <= lastYear) { firstYearsList.Add(year); year += stepLen; } region.Years = firstYearsList.ToArray(); region.IsIntervalsGridYears = isIntervalTimeseries; return(region); }
private static IEnumerable <TIndex> EnumerateHours(ITimeRegion tr) { bool isAxisReduced = tr.HoursAxisLength == 1; var hours = tr.Hours; if (tr.IsIntervalsGridHours) { for (var i = 0; i < hours.Length - 1; i++) { yield return(new TIndex(isAxisReduced ? -1 : i, hours[i], hours[i + 1])); } } else { for (var i = 0; i < hours.Length; i++) { yield return(new TIndex(isAxisReduced ? -1 : i, hours[i], hours[i])); } } yield break; }
/// <summary> /// Fills the dataset with seasonly timeseries definition (iterating days withing each year) /// </summary> /// <param name="job"></param> /// <param name="firstYear"></param> /// <param name="lastYear"></param> /// <param name="firstDay"></param> /// <param name="lastDay"></param> /// <param name="startHour"></param> /// <param name="stopHour"></param> /// <param name="stepLen">1..n</param> public static ITimeRegion GetSeasonlyTimeseries(this ITimeRegion r, int firstDay = 1, int lastDay = -1, int stepLen = 1, bool isIntervalTimeseries = true) { TimeRegion region = new TimeRegion(r); int firstYear = region.Years[0]; bool isOneYear = ((region.Years.Length == 1 && !region.IsIntervalsGridYears) || (region.Years.Length == 2 && (region.Years[0] == region.Years[region.Years.Length - 1] - 1))); if (isIntervalTimeseries && lastDay != -1) { lastDay++; } if (lastDay == -1) { lastDay = (isOneYear && DateTime.IsLeapYear(firstYear)) ? 366 : 365; } if (isIntervalTimeseries) { lastDay++; } List <int> firstDaysList = new List <int>(); int day = firstDay; bool overlap = firstDay > lastDay; //crossing new year if (overlap) { lastDay += 365; } while (day <= lastDay) { firstDaysList.Add(overlap ? ((day - 1) % 365) + 1 : day); day += stepLen; } region.Days = firstDaysList.ToArray(); region.IsIntervalsGridDays = isIntervalTimeseries; return(region); }
private static IEnumerable <TIndex> EnumerateYears(ITimeRegion tr) { var years = tr.Years; if (tr.IsIntervalsGridYears) { bool isAxisReduced = years.Length == 2; for (var i = 0; i < years.Length - 1; i++) { yield return(new TIndex(isAxisReduced ? -1 : i, years[i], years[i + 1] - 1)); } } else { bool isAxisReduced = years.Length == 1; for (var i = 0; i < years.Length; i++) { yield return(new TIndex(isAxisReduced ? -1 : i, years[i], years[i])); } } yield break; }
/// <summary> /// Fills the dataset with monthly timeseries definition /// </summary> /// <param name="job"></param> /// <param name="firstYear"></param> /// <param name="lastYear"></param> /// <param name="firstMonth">1..12</param> /// <param name="lastMonth">1..12</param> /// <param name="startHour"></param> /// <param name="stopHour"></param> public static ITimeRegion GetMonthlyTimeseries(this ITimeRegion r, int firstMonth = 1, int lastMonth = 12) { var region = new TimeRegion(r); Debug.Assert(firstMonth <= lastMonth); bool isOneYear = ((region.Years.Length == 1 && !region.IsIntervalsGridYears) || (region.Years.Length == 2 && (region.Years[0] == region.Years[region.Years.Length - 1] - 1))); bool isLeap = isOneYear && DateTime.IsLeapYear(region.Years[0]); int monthsCount = lastMonth - firstMonth + 1; int[] firstDays = new int[monthsCount + 1]; int[] effectiveFirstDays = isLeap ? DaysOfYearConversions.MonthFirstDayLY : DaysOfYearConversions.MonthFirstDay; int[] effectiveLastDays = isLeap ? DaysOfYearConversions.MonthLastDayLY : DaysOfYearConversions.MonthLastDay; for (int i = 0; i < monthsCount; i++) { firstDays[i] = effectiveFirstDays[i + firstMonth - 1]; } firstDays[monthsCount] = effectiveLastDays[(monthsCount - 1) + firstMonth - 1] + 1; region.Days = firstDays; return(region); }
/// <summary> /// Fills the dataset with daily timeseries definition (iterating hours within each day) /// </summary> /// <param name="job"></param> /// <param name="firstYear"></param> /// <param name="lastYear"></param> /// <param name="firstDay"></param> /// <param name="lastDay"></param> /// <param name="startHour"></param> /// <param name="stopHour"></param> /// <param name="stepLen">0..n</param> public static ITimeRegion GetHourlyTimeseries(this ITimeRegion r, int startHour = 0, int stopHour = -1, int stepLen = 1, bool isIntervalTimeseries = false) { var region = new TimeRegion(r); List <int> startHoursList = new List <int>(); if (stopHour == -1) { stopHour = isIntervalTimeseries ? 24 : 23; } int hour = startHour; while (hour <= stopHour) { startHoursList.Add(hour); hour += stepLen; } region.Hours = startHoursList.ToArray(); region.IsIntervalsGridHours = isIntervalTimeseries; return(region); }
/// <summary> /// Gets the sequence of TimeSegments that are defined by TimeRegion /// </summary> /// <returns></returns> public static IEnumerable <ITimeSegment> GetSegments(this ITimeRegion timeRegion) { foreach (var years in timeRegion.GetYearsSegments()) { foreach (var days in timeRegion.GetDaysSegments()) { Tuple <int, int> effectiveDays; if (years.Item1 == years.Item2 && days.Item2 == 365 && DateTime.IsLeapYear(years.Item1)) { effectiveDays = Tuple.Create(days.Item1, 366); } else { effectiveDays = days; } foreach (var hours in timeRegion.GetHoursSegments()) { yield return(new TimeSegment(years.Item1, years.Item2, effectiveDays.Item1, effectiveDays.Item2, hours.Item1, hours.Item2)); } } } }
private static IEnumerable <TIndex> EnumerateDays(ITimeRegion tr, bool isLeapYear) { bool isAxisReduced = tr.DaysAxisLength == 1; var days = (int[])tr.Days; if (tr.IsIntervalsGridDays) { if (isLeapYear && days.Length == 13 && // Adjust monthly bounds for leap year days[0] == 1 && days[1] == 32 && days[2] == 60 && days[3] == 91 && days[4] == 121 && days[5] == 152 && days[6] == 182 && days[7] == 213 && days[8] == 244 && days[9] == 274 && days[10] == 305 && days[11] == 335 && days[12] == 366) { days = (int[])days.Clone(); for (var i = 2; i < 13; i++) { days[i]++; } } else if (isLeapYear && days.Length == 2 && days[0] == 1 && days[1] == 366) { // Adjust end of year for entire year days = (int[])days.Clone(); days[1] = 367; } for (var i = 0; i < days.Length - 1; i++) { yield return(new TIndex(isAxisReduced ? -1 : i, days[i], days[i + 1] - 1)); } } else { for (var i = 0; i < days.Length; i++) { yield return(new TIndex(isAxisReduced ? -1 : i, days[i], days[i])); } } yield break; }
/// <summary> /// Stretches the cell set definition (with optional timseries) into the GeoCellTuple sequence /// </summary> /// <param name="latmin"></param> /// <param name="latmax"></param> /// <param name="lonmin"></param> /// <param name="lonmax"></param> /// <param name="time"></param> /// <param name="mask"></param> /// <returns></returns> public static IEnumerable <IGeoCell> StretchCells(double[] latmin, double[] latmax, double[] lonmin, double[] lonmax, ITimeRegion time, Array mask = null) { int cellsLen = latmin.Length; int timeLen = time.SegmentsCount; ITimeSegment[] segments = time.GetSegments().ToArray(); if (mask == null) { for (int i = 0; i < cellsLen; i++) { double lonminV = lonmin[i]; double lonmaxV = lonmax[i]; double latminV = latmin[i]; double latmaxV = latmax[i]; for (int t = 0; t < timeLen; t++) { yield return new GeoCell { LatMin = latminV, LonMin = lonminV, LatMax = latmaxV, LonMax = lonmaxV, Time = segments[t] } } ; } } else { GCHandle?maskHandle = GCHandle.Alloc(mask, GCHandleType.Pinned); IntPtr maskPtr = maskHandle.Value.AddrOfPinnedObject(); try { for (int i = 0; i < cellsLen; i++) { double lonminV = lonmin[i]; double lonmaxV = lonmax[i]; double latminV = latmin[i]; double latmaxV = latmax[i]; for (int t = 0; t < timeLen; t++) { if (checkMdBoolArray(maskPtr, t + timeLen * i)) { yield return new GeoCell { LatMin = latminV, LonMin = lonminV, LatMax = latmaxV, LonMax = lonmaxV, Time = segments[t] } } } ; } } finally { maskHandle.Value.Free(); } } }
public RequestFormModel(ExtendedConfiguration config, NameValueCollection form, bool fromPost) : this(config) { if (fromPost) { // Get selected variables from form foreach (string key in form) { if (key.StartsWith("variable_")) { int idx = key.LastIndexOf("_"); var vname = key.Substring(9, idx - 9); var dsID = Int32.Parse(key.Substring(idx + 1)); List <string> dsList; if (!variables.TryGetValue(vname, out dsList)) { dsList = new List <string>(); variables.Add(vname, dsList); } dsList.Add(config.DataSources.Where(ds => ds.ID == dsID).First().Name); } } if (variables.Count == 0) { variableErrors = "No variables selected"; } try { ParseRegion(RegionsText = form["regionText"]); } catch (Exception exc) { regionErrors = exc.Message; } YearCellStart = (string)form["yearCellStart"]; YearCellEnd = (string)form["yearCellEnd"]; YearCellSize = (string)form["yearCellSize"]; IndividualYearStart = (string)form["indYearStart"]; IndividualYearEnd = (string)form["indYearEnd"]; IndividualYearStep = (string)form["indYearStep"]; try { switch ((string)form["yearsMode"]) { case "cells": tr = tr.GetYearlyTimeseries(Int32.Parse(YearCellStart), Int32.Parse(YearCellEnd), Int32.Parse(YearCellSize), true); YearsMode = Models.YearsMode.Cells; break; case "years": tr = tr.GetYearlyTimeseries(Int32.Parse(IndividualYearStart), Int32.Parse(IndividualYearEnd), Int32.Parse(IndividualYearStep), true); YearsMode = Models.YearsMode.Points; break; } } catch (Exception exc) { intervalErrors = "Year axis specification has errors: " + exc.Message; } DayCellStart = (string)form["dayCellStart"]; DayCellEnd = (string)form["dayCellEnd"]; DayCellSize = (string)form["dayCellSize"]; IndividualDayStart = (string)form["inDayStart"]; IndividualDayEnd = (string)form["inDayEnd"]; IndividualDayStep = (string)form["inDayStep"]; try { switch ((string)form["daysMode"]) { case "cells": tr = tr.GetSeasonlyTimeseries(Int32.Parse(DayCellStart), Int32.Parse(DayCellEnd), Int32.Parse(DayCellSize), true); break; case "days": tr = tr.GetSeasonlyTimeseries(Int32.Parse(IndividualDayStart), Int32.Parse(IndividualDayEnd), Int32.Parse(IndividualDayEnd), true); break; case "monthly": tr = tr.GetMonthlyTimeseries(); break; } } catch (Exception exc) { if (!String.IsNullOrEmpty(intervalErrors)) { intervalErrors += "\n"; } intervalErrors += "Days axis specification has errors: " + exc.Message; } HourCellStart = (string)form["hourCellStart"]; HourCellEnd = (string)form["hourCellEnd"]; HourCellSize = (string)form["hourCellSize"]; IndividualHourStart = (string)form["indHourStart"]; IndividualHourEnd = (string)form["indHourEnd"]; IndividualHourStep = (string)form["indHourStep"]; try { switch ((string)form["hoursMode"]) { case "cells": tr = tr.GetHourlyTimeseries(Int32.Parse(HourCellStart), Int32.Parse(HourCellEnd), Int32.Parse(HourCellSize), true); break; case "hours": tr = tr.GetHourlyTimeseries(Int32.Parse(IndividualHourStart), Int32.Parse(IndividualHourEnd), Int32.Parse(IndividualHourStep), true); break; } } catch (Exception exc) { if (!String.IsNullOrEmpty(intervalErrors)) { intervalErrors += "\n"; } intervalErrors += "Hours axis specification has errors: " + exc.Message; } } else // From get { string v = form["v"]; if (!String.IsNullOrEmpty(v)) { var names = Uri.UnescapeDataString(v).Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); foreach (var n in names) { if (config.EnvironmentalVariables.Any(vd => vd.Name == n)) { variables.Add(n, config.DataSources.Where(d => d.ProvidedVariables.Contains(n)).Select(d => d.Name).ToList()); } } } string p = form["p"]; if (!String.IsNullOrEmpty(p)) { var latLons = Uri.UnescapeDataString(p).Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < latLons.Length - 1; i += 2) { double lat, lon; if (Double.TryParse(latLons[i], out lat) && Double.TryParse(latLons[i + 1], out lon)) { points.Add(new GeoPoint(lat, lon)); } } } string g = form["g"]; if (!String.IsNullOrEmpty(g)) { var gp = Uri.UnescapeDataString(g).Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < gp.Length; i += 6) { double latmin, latmax, lonmin, lonmax; int latcount, loncount; if (Double.TryParse(gp[0], out latmin) && Double.TryParse(gp[1], out latmax) && Int32.TryParse(gp[2], out latcount) && Double.TryParse(gp[3], out lonmin) && Double.TryParse(gp[4], out lonmax) && Int32.TryParse(gp[5], out loncount) && latmin < latmax && lonmin < lonmax && latcount > 1 && loncount > 1) { grids.Add(new GeoGrid(latmin, latmax, latcount, lonmin, lonmax, loncount)); } } } } }
/// <summary> /// Produces a domain that depicts the grid composed of rectangular cells /// </summary> /// <param name="lat">Latitude axis of the grid</param> /// <param name="lon">Logitude axis of the grid</param> /// <param name="times">Temporal specification of the domain</param> /// <param name="mask">optional mask (calculation of points corrisponding to false mask is not nessesery, this points can have any value). /// The dimenstions of mask: lon,lat, time /// The length of lat and lon dimentions of mask array is 1 less than original lat and lon axis dimentions</param> /// <returns></returns> public static IFetchDomain CreateCellGrid(double[] lat, double[] lon, ITimeRegion times, Array mask = null) { return(new FetchDomain(lat, lon, null, null, times, SpatialRegionSpecification.CellGrid, mask)); }
/// <summary> /// Produces a domain that depicts the set of arbitrary placed rectangular cells /// </summary> /// <param name="latmin">left latitude bounds of the cells</param> /// <param name="latmax">right latitude bounds of the cells</param> /// <param name="lonmin">bottom latitude bounds of the cells</param> /// <param name="lonmax">upper latitude bounds of the cells</param> /// <param name="times">Temporal specification of the domain</param> /// <param name="mask">optional mask (calculation of points corrisponding to false mask is not nessesery, this points can have any value). /// The dimenstions of mask: points, time</param> /// <returns></returns> public static IFetchDomain CreateCells(double[] latmin, double[] lonmin, double[] latmax, double[] lonmax, ITimeRegion times, bool[,] mask = null) { return(new FetchDomain(latmin, lonmin, latmax, lonmax, times, SpatialRegionSpecification.Cells, mask)); }
/// <summary> /// Stretches the 3D cell cube into the sequence of GeoCellTuple /// </summary> /// <param name="lat"></param> /// <param name="lon"></param> /// <param name="region"></param> /// <param name="mask"></param> /// <returns></returns> public static IEnumerable <IGeoCell> StretchCellGrid(double[] lat, double[] lon, ITimeRegion region, Array mask = null) { int latLen = lat.Length; int lonLen = lon.Length; int timeLen = region.SegmentsCount; ITimeSegment[] segments = region.GetSegments().ToArray(); if (mask == null) { for (int i = 0; i < lonLen - 1; i++) { double lonminV = lon[i]; double lonmaxV = lon[i + 1]; for (int j = 0; j < latLen - 1; j++) { double latminV = lat[j]; double latmaxV = lat[j + 1]; for (int t = 0; t < timeLen; t++) { yield return new GeoCell { LatMin = latminV, LatMax = latmaxV, LonMin = lonminV, LonMax = lonmaxV, Time = segments[t] } } ; } } } else { GCHandle?maskHandle = GCHandle.Alloc(mask, GCHandleType.Pinned); IntPtr maskPtr = maskHandle.Value.AddrOfPinnedObject(); try { int firstDimLen = lonLen - 1; int secondDimLen = latLen - 1; int lastTwoDimsLen = timeLen * secondDimLen; for (int i = 0; i < firstDimLen; i++) { double lonminV = lon[i]; double lonmaxV = lon[i + 1]; for (int j = 0; j < secondDimLen; j++) { double latminV = lat[j]; double latmaxV = lat[j + 1]; for (int t = 0; t < timeLen; t++) { if (checkMdBoolArray(maskPtr, i * lastTwoDimsLen + j * timeLen + t)) { yield return new GeoCell { LatMin = latminV, LatMax = latmaxV, LonMin = lonminV, LonMax = lonmaxV, Time = segments[t] } } } ; } } } finally { maskHandle.Value.Free(); } } }
/// <summary> /// Stretches the point grid into the sequence of points /// </summary> /// <param name="lat"></param> /// <param name="lon"></param> /// <param name="region"></param> /// <param name="mask"></param> /// <returns></returns> public static IEnumerable <IGeoCell> StretchGrid(double[] lat, double[] lon, ITimeRegion region, Array mask = null) { //stretching to lon,lat,t int latLen = lat.Length; int lonLen = lon.Length; int times = region.SegmentsCount; ITimeSegment[] segments = region.GetSegments().ToArray(); if (mask == null) { for (int i = 0; i < lonLen; i++) { double lonV = lon[i]; for (int j = 0; j < latLen; j++) { double latV = lat[j]; for (int t = 0; t < times; t++) { yield return new GeoCell() { LatMin = latV, LatMax = latV, LonMin = lonV, LonMax = lonV, Time = segments[t] } } ; } } } else { GCHandle?maskHandle = GCHandle.Alloc(mask, GCHandleType.Pinned); IntPtr maskPtr = maskHandle.Value.AddrOfPinnedObject(); try { int lastTwoDimsLen = times * latLen; for (int i = 0; i < lonLen; i++) { double lonV = lon[i]; for (int j = 0; j < latLen; j++) { double latV = lat[j]; for (int t = 0; t < times; t++) { if (checkMdBoolArray(maskPtr, t + j * times + i * lastTwoDimsLen)) { yield return new GeoCell { LatMin = latV, LatMax = latV, LonMin = lonV, LonMax = lonV, Time = segments[t] } } } ; } } } finally { maskHandle.Value.Free(); } } }
public TimeRegion(CustomTimeRegionProvider provider, string id, ITimeRegion systemTimeRegion) : this(provider, id) { _systemTimeRegion = systemTimeRegion; }