internal VariableSchema(int changeSetId, int id, Type dataType, ReadOnlyDimensionList dimensions, string[] cs, MetadataDictionary metadata) { if (dimensions == null) throw new ArgumentNullException("dimensions"); if (metadata == null) throw new ArgumentNullException("metadata"); this.changeSetId = changeSetId; this.id = id; this.dimensions = dimensions; this.cs = cs; this.metadata = metadata; this.dataType = dataType; }
/// <summary> /// Gets specified version of the schema for the variable describing its structure. /// </summary> public override VariableSchema GetSchema(SchemaVersion version) { if (HasChanges) { if (version == SchemaVersion.Committed) { return(changes.InitialSchema); } // Making a schema for proposed version of the variable string name = changes.Name == null ? this.name : changes.Name; ReadOnlyDimensionList roDims = new ReadOnlyDimensionList(changes.Dimensions == null ? dims : changes.Dimensions); VariableMetadata metadata = this.metadata.Clone(); CoordinateSystemCollection cs = changes.Cs == null ? csystems : changes.Cs; CoordinateSystemSchema[] csSchemas = new CoordinateSystemSchema[cs.Count]; for (int i = 0; i < csSchemas.Length; i++) { csSchemas[i] = cs[i].GetSchema(); } VariableSchema schema = new VariableSchema(name, TypeOfData, roDims, csSchemas, metadata); return(schema); } else { if (version == SchemaVersion.Proposed) { throw new Exception("Variable is commited and has no changes."); } CoordinateSystemSchema[] csSchemas = new CoordinateSystemSchema[csystems.Count]; for (int i = 0; i < csSchemas.Length; i++) { csSchemas[i] = csystems[i].GetSchema(); } VariableSchema schema = new VariableSchema( name, TypeOfData, new ReadOnlyDimensionList(dimensions), csSchemas, metadata.Clone()); return(schema); } }
//Extract data from NetCDF files public List <Dictionary <string, string> > ExtractNetCDF(Extract Datasetinfo) { logger.Log(LogLevel.Info, "Entered AVISTED ExtractNetCDF()"); //Get selected parameters string[] paramlist = Datasetinfo.parameters.Split(','); //Get latitude and longitude int latmin = 0, latmax = 10, lonmin = 0, lonmax = 10; //Get start and end date DateTime start = Convert.ToDateTime(Datasetinfo.startDate); DateTime end = Convert.ToDateTime(Datasetinfo.endDate); //Get statistics string stat = Datasetinfo.stat; //initialize variables Dictionary <string, string> dict = new Dictionary <string, string>(); List <Dictionary <string, string> > list = new List <Dictionary <string, string> >(); float value = 0; try { //Get the Dataset Path String logDirectory = Datasetinfo.path; DirectoryInfo dir1 = new DirectoryInfo(logDirectory); FileInfo[] DispatchFiles = dir1.GetFiles(); //finding the closest point to the user selection string filePathlatlong = Path.Combine(dir1.FullName, "hourly_output_d01_lat.lon.elev.land.nc"); DataSet ds2 = DataSet.Open(filePathlatlong); ReadOnlyDimensionList dimension = ds2.Dimensions; float[,] latitude = new float[dimension[0].Length, dimension[1].Length]; float[,] longitude = new float[dimension[0].Length, dimension[1].Length]; foreach (Variable v in ds2.Variables) { if (v.Name.Equals("latitude")) { latitude = (float[, ])v.GetData(); } if (v.Name.Equals("longitude")) { longitude = (float[, ])v.GetData(); } } //getting the latitude longitude into the array GeoCoordinate[] coord = new GeoCoordinate[dimension[0].Length * dimension[1].Length]; int n = 0; for (int l = 0; l < dimension[0].Length; l++) { for (int m = 0; m < dimension[1].Length; m++) { coord[n] = new GeoCoordinate(latitude[l, m], longitude[l, m]); n++; } } //closest point to the user selection var coords = new GeoCoordinate(Datasetinfo.latmin, Datasetinfo.lonmin); var nearest = coord.Select(x => new GeoCoordinate(x.Latitude, x.Longitude)) .OrderBy(x => x.GetDistanceTo(coords)) .First(); int found = 0; for (int l = 0; l < dimension[0].Length; l++) { for (int m = 0; m < dimension[1].Length; m++) { if (latitude[l, m] == nearest.Latitude && longitude[l, m] == nearest.Longitude) { found = 1; latmin = l; lonmin = m; break; } } if (found == 1) { break; } } //get Data from NetCDF files if (DispatchFiles.Length > 0) { foreach (FileInfo aFile in DispatchFiles) { //Getting date from file name string dtime = aFile.Name.Substring(18, 10); DateTime datetime = DateTime.Now; if (DateTime.TryParseExact(dtime, "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.None, out datetime)) { int begin = DateTime.Compare(start.Date, datetime); int last = DateTime.Compare(end.Date, datetime); //check if the date is within the user requested range if (begin <= 0 && last >= 0) { string filePath = Path.Combine(dir1.FullName, aFile.Name); dict.Add("date", dtime.ToString()); //open dataset using (DataSet ds1 = DataSet.Open(filePath)) { //for each variable if selected by the user get the values foreach (Variable v in ds1.Variables) { if (Datasetinfo.parameters.Contains(v.Name)) { float[,] subset = (float[, ])v.GetData(new int[] { latmin, lonmin }, new int[] { latmax, lonmax }); List <float> lst = subset.Cast <float>().ToList(); List <double> doubleList = lst.ConvertAll(x => (double)x); //Apply the selected statistics to the list of values var statistics = new DescriptiveStatistics(doubleList); // Order Statistics if (string.Compare(stat, "max") == 0) { var largestElement = statistics.Maximum; value = System.Convert.ToSingle(largestElement); } else if (string.Compare(stat, "min") == 0) { var smallestElement = statistics.Minimum; value = System.Convert.ToSingle(smallestElement); } else if (string.Compare(stat, "med") == 0) { var median = statistics.Mean; value = System.Convert.ToSingle(median); } else if (string.Compare(stat, "mean") == 0) {//Central Tendency var mean = statistics.Mean; value = System.Convert.ToSingle(mean); } else if (string.Compare(stat, "var") == 0) {// Dispersion var variance = statistics.Variance; value = System.Convert.ToSingle(variance); } else if (string.Compare(stat, "stD") == 0) { var stdDev = statistics.StandardDeviation; value = System.Convert.ToSingle(stdDev); } dict.Add(v.Name, value.ToString()); } } list.Add(new Dictionary <string, string>(dict)); } dict.Clear(); } } } } } catch (Exception ex) { logger.Error("ExtractNetCDF:Failed with exception {0}", ex.Message); } return(list); }