public override Dictionary <string, string> GetDatasetAttributes(string datasetName) { using (Hdf5Operator hdf5 = new Hdf5Operator(_provider.fileName)) { return(hdf5.GetAttributes(datasetName)); } }
private void SetInputFileDataSet(string filename) { try { using (Hdf5Operator oper = new Hdf5Operator(filename)) { _dsNames = oper.GetDatasetNames; _dsAttributes.Clear(); _fileAttributes.Clear(); foreach (string dsName in _dsNames) { Dictionary <string, string> dsAttrs = oper.GetAttributes(dsName); _dsAttributes.Add(dsName, dsAttrs); } _fileAttributes = oper.GetAttributes(); } FillTreeView(filename); } catch (Exception ex) { ShowMsgBox(ex.Message); } }
private CoordEnvelope TrySetGeoInfo() { using (Hdf5Operator oper = new Hdf5Operator(_fileName)) { _fileAttributes = oper.GetAttributes(); } if (_fileAttributes == null) { return(null); } double minX = 0d, maxX = 0d, minY = 0d, maxY = 0d; if (_fileAttributes.ContainsKey("Left-Bottom Latitude")) { string minLat = _fileAttributes["Left-Bottom Latitude"]; if (!double.TryParse(minLat, out minY)) { return(null); } } if (_fileAttributes.ContainsKey("Left-Bottom Longitude")) { string minlon = _fileAttributes["Left-Bottom Longitude"]; if (!double.TryParse(minlon, out minX)) { return(null); } } if (_fileAttributes.ContainsKey("Left-Top Latitude")) { string maxLat = _fileAttributes["Left-Top Latitude"]; if (!double.TryParse(maxLat, out maxY)) { return(null); } } if (_fileAttributes.ContainsKey("Left-Top Longitude")) { string maxLon = _fileAttributes["Right-Top Longitude"]; if (!double.TryParse(maxLon, out maxX)) { return(null); } } return(new CoordEnvelope(minX, maxX, minY, maxY)); }
protected override bool IsCompatible(string fileName, byte[] header1024, params object[] args) { if (header1024 == null) { header1024 = new byte[1024]; using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read)) { fs.Read(header1024, 0, 1024); fs.Close(); } } if (!HDF5Helper.IsHdf5(header1024)) { return(false); } string geoprjstr = "Projection Type".ToUpper(); string geoprj1 = "Geographic Longitude/Latitude".ToUpper(); string geoprj2 = "Geographic Longitude/Latitute".ToUpper(); string geoprj3 = "GLL"; string prjType = null; Hdf5Operator hdfic = new Hdf5Operator(fileName); Dictionary <string, string> fileAttributes = hdfic.GetAttributes(); foreach (KeyValuePair <string, string> fileAttribute in fileAttributes) { if (fileAttribute.Key.ToUpper() == geoprjstr) { prjType = fileAttribute.Value.ToUpper(); break; } } if (prjType == null) { return(false); } if (prjType != null && (prjType != geoprj1 && prjType != geoprj2 && prjType != geoprj3)) { return(false); } return(true); }
public string ExtractFile(string filename, string dataset, PrjEnvelope env, string regionNam, string outdir) { string projectionType = null; string dstFileName = null; string[] dsNames = null; if (GeoDo.HDF5.HDF5Helper.IsHdf5(filename)) { Dictionary <string, string> _fileAttributes = new Dictionary <string, string>(); using (Hdf5Operator oper = new Hdf5Operator(filename)) { dsNames = oper.GetDatasetNames; int id = 0; foreach (string ds in dsNames) { if (ds == dataset) { id = 1; } } if (id == 0) { return(null); } _fileAttributes = oper.GetAttributes(); } double minX = 0d, maxX = 0d, minY = 0d, maxY = 0d, resolutionX = 0d, resolutionY = 0d; if (_fileAttributes.ContainsKey("Left-Bottom Latitude")) { string minLat = _fileAttributes["Left-Bottom Latitude"]; if (double.TryParse(minLat, out minY)) { } } if (_fileAttributes.ContainsKey("Left-Bottom Longitude")) { string minlon = _fileAttributes["Left-Bottom Longitude"]; if (double.TryParse(minlon, out minX)) { } } if (_fileAttributes.ContainsKey("Left-Top Latitude")) { string maxLat = _fileAttributes["Left-Top Latitude"]; if (double.TryParse(maxLat, out maxY)) { } } if (_fileAttributes.ContainsKey("Left-Top Longitude")) { string maxLon = _fileAttributes["Right-Top Longitude"]; if (double.TryParse(maxLon, out maxX)) { } } if (_fileAttributes.ContainsKey("Latitude Resolution")) { string latRegolution = _fileAttributes["Latitude Resolution"]; if (double.TryParse(latRegolution, out resolutionY)) { } } if (_fileAttributes.ContainsKey("Longitude Resolution")) { string lonRegolution = _fileAttributes["Longitude Resolution"]; if (double.TryParse(lonRegolution, out resolutionX)) { } } if (_fileAttributes.ContainsKey("Projection Type")) { projectionType = _fileAttributes["Projection Type"]; } string[] args = new string[1]; args[0] = "datasets = " + dataset; IRasterDataProvider prd = GeoDataDriver.Open(filename, args) as IRasterDataProvider; double dminx = env.MinX, dmax = env.MaxX, dminy = env.MinY, dmaxy = env.MaxY; int xoffset = (int)(Math.Round(dminx - minX) / resolutionX); int yoffset = (int)(Math.Round(maxY - dmaxy) / resolutionY); int width = (int)(Math.Round((dmax - dminx) / resolutionX)); int height = (int)(Math.Round((dmaxy - dminy) / resolutionY)); float tResolutionX; float tResolutionY; tResolutionX = Convert.ToSingle(resolutionX); tResolutionY = Convert.ToSingle(resolutionY); string[] optionString = new string[] { "INTERLEAVE=BSQ", "VERSION=LDF", "WITHHDR=TRUE", "SPATIALREF=" + projectionType, "MAPINFO={" + 1 + "," + 1 + "}:{" + dminx + "," + dmaxy + "}:{" + tResolutionX + "," + tResolutionY + "}" }; enumDataType dataType = enumDataType.Int16; if (!Directory.Exists(outdir)) { Directory.CreateDirectory(outdir); } dstFileName = outdir + "\\" + Path.GetFileNameWithoutExtension(filename).Replace("GBAL", regionNam) + ".ldf"; if (File.Exists(dstFileName)) { System.Windows.Forms.MessageBox.Show(dstFileName + "已存在"); return(null); } IRasterDataDriver dataDriver = GeoDataDriver.GetDriverByName("LDF") as IRasterDataDriver; IRasterDataProvider dstRaster = dataDriver.Create(dstFileName, width, height, 1, dataType, optionString); Int16[] buffer = new Int16[width * height]; unsafe { fixed(Int16 *ptr = buffer) { IntPtr bufferPtr = new IntPtr(ptr); prd.GetRasterBand(1).Read(xoffset, yoffset, width, height, bufferPtr, enumDataType.Int16, width, height); fixed(Int16 *wptr = buffer) { IntPtr newBuffer = new IntPtr(wptr); dstRaster.GetRasterBand(1).Write(0, 0, width, height, newBuffer, enumDataType.Int16, width, height); } } } } return(null); }
private CoordEnvelope TrySetGeoInfo() { using (Hdf5Operator oper = new Hdf5Operator(_fileName)) { _fileAttributes = oper.GetAttributes(); } if (_fileAttributes == null) { return(null); } double lulat = 0, lulon = 0, rdlat = 0, rdlon = 0; double ldlat = 0, ldlon = 0, rulat = 0, rulon = 0; double lulatorbit = 0, lulonorbit = 0, rdlatorbit = 0, rdlonorbit = 0, ldlatorbit = 0, ldlonorbit = 0, rulatorbit = 0, rulonorbit = 0; string lulatstr = "Left-Top Latitude", lulonstr = "Left-Top Longitude", rdlatstr = "Right-Bottom Latitude", rdlonstr = "Right-Bottom Longitude"; string ldlatstr = "Left-Bottom Latitude", ldlonstr = "Left-Bottom Longitude", rulatstr = "Right-Top Latitude", rulonstr = "Right-Top Longitude"; string lulatstrOrbit = "Left-Top X", lulonstrOrbit = "Left-Top Y", rdlatstrOrbit = "Right-Bottom X", rdlonstrOrbit = "Right-Bottom Y"; string ldlatstrOrbit = "Left-Bottom X", ldlonstrOrbit = "Left-Bottom Y", rulatstrOrbit = "Right-Top X", rulonstrOrbit = "Right-Top Y"; foreach (KeyValuePair <string, string> fileAttribute in _fileAttributes) { if (fileAttribute.Key == lulatstr) { if (!double.TryParse(fileAttribute.Value, out lulat)) { return(null); } } else if (fileAttribute.Key == lulonstr) { if (!double.TryParse(fileAttribute.Value, out lulon)) { return(null); } } else if (fileAttribute.Key == rdlatstr) { if (!double.TryParse(fileAttribute.Value, out rdlat)) { return(null); } } else if (fileAttribute.Key == rdlonstr) { if (!double.TryParse(fileAttribute.Value, out rdlon)) { return(null); } } else if (fileAttribute.Key == ldlonstr) { if (!double.TryParse(fileAttribute.Value, out ldlon)) { return(null); } } else if (fileAttribute.Key == ldlatstr) { if (!double.TryParse(fileAttribute.Value, out ldlat)) { return(null); } } else if (fileAttribute.Key == rulonstr) { if (!double.TryParse(fileAttribute.Value, out rulon)) { return(null); } } else if (fileAttribute.Key == rulatstr) { if (!double.TryParse(fileAttribute.Value, out rulat)) { return(null); } } else if (fileAttribute.Key == lulatstrOrbit) { if (!double.TryParse(fileAttribute.Value, out lulatorbit)) { return(null); } } else if (fileAttribute.Key == lulonstrOrbit) { if (!double.TryParse(fileAttribute.Value, out lulonorbit)) { return(null); } } else if (fileAttribute.Key == ldlatstrOrbit) { if (!double.TryParse(fileAttribute.Value, out ldlatorbit)) { return(null); } } else if (fileAttribute.Key == ldlonstrOrbit) { if (!double.TryParse(fileAttribute.Value, out ldlonorbit)) { return(null); } } else if (fileAttribute.Key == rdlatstrOrbit) { if (!double.TryParse(fileAttribute.Value, out rdlatorbit)) { return(null); } } else if (fileAttribute.Key == rdlonstrOrbit) { if (!double.TryParse(fileAttribute.Value, out rdlonorbit)) { return(null); } } else if (fileAttribute.Key == rulatstrOrbit) { if (!double.TryParse(fileAttribute.Value, out rulatorbit)) { return(null); } } else if (fileAttribute.Key == rulonstrOrbit) { if (!double.TryParse(fileAttribute.Value, out rulonorbit)) { return(null); } } } double minlon, maxlon, minlat, maxlat; bool lonreverse = false, latreverse = false; if (lulon != 0 || rdlon != 0 || rdlat != 0 || lulat != 0 || ldlat != 0 || ldlon != 0 || rulat != 0 || rulon != 0) { if (lulon != 0 && rdlon != 0 && lulon > rdlon) { lonreverse = true; } if (rdlat != 0 && lulat != 0 && rdlat > lulat) { latreverse = true; } if (!lonreverse) { lulon = Math.Min(lulon, ldlon); rdlon = Math.Max(rdlon, rulon); } else { lulon = Math.Max(lulon, ldlon); rdlon = Math.Min(rdlon, rulon); } if (!latreverse) { lulat = Math.Max(lulat, rulat); rdlat = Math.Min(ldlat, rdlat); } else { lulat = Math.Min(lulat, rulat); rdlat = Math.Max(rdlat, ldlat); } minlon = !lonreverse ? lulon : rdlon; maxlon = lonreverse ? lulon : rdlon; minlat = !latreverse ? rdlat : lulat; maxlat = latreverse ? rdlat : lulat; } else { lulon = lulonorbit < ldlonorbit ? lulonorbit : ldlonorbit; //左侧经度取小的 lulat = lulatorbit > rulatorbit ? lulatorbit : rulatorbit; //上侧纬度取大的 rdlon = rulonorbit > rdlonorbit ? rulonorbit : rdlonorbit; //右侧经度取大的 rdlat = ldlatorbit < rdlatorbit ? ldlatorbit : rdlatorbit; //下侧纬度取小的 minlon = lulon < rdlon ? lulon : rdlon; maxlon = lulon > rdlon ? lulon : rdlon; minlat = rdlat < lulat ? rdlat : lulat; maxlat = rdlat > lulat ? rdlat : lulat; } return(new CoordEnvelope(minlon, maxlon, minlat, maxlat)); }
public IExtractResult DoCalLAIfromHDF() { if (_progressTracker != null) { _progressTracker(1, "开始计算LAI..."); } Dictionary <string, string[]> arguments = _argumentProvider.GetArg("IOFilesArguments") as Dictionary <string, string[]>; if (arguments == null || !arguments.ContainsKey("INPUTFILES")) { return(null); } if (arguments["INPUTFILES"].Length != 3)// || arguments["OUTPARAS"].Length != 3) { return(null); } foreach (string infile in arguments["INPUTFILES"]) { if (!File.Exists(infile)) { return(null); } } string L2LSRFile = arguments["INPUTFILES"][0]; string L1GranulesFile = arguments["INPUTFILES"][1]; string landCoverFile = arguments["INPUTFILES"][2]; //string outpath = arguments["OUTPARAS"][0]; output = new UInt16[1][]; #region 计算LAI if (_progressTracker != null) { _progressTracker(5, "开始读取数据文件..."); } IRasterDataProvider lsrRaster = null; IRasterDataProvider angleRaster = null; IRasterDataProvider landcoverRaster = null; int outwidth, outheight; try { #region if (_progressTracker != null) { _progressTracker(7, "正在读取LSR数据文件..."); } #region 读取xml获取波段配置信息 RasterIdentify datid = new RasterIdentify(Path.GetFileName(L2LSRFile)); if (!ParseLAIConfigXml.ParseXML(datid.Satellite, datid.Sensor, true)) { return(null); } string lsrdataset = ParseLAIConfigXml.DataSet; int rno = ParseLAIConfigXml.RedNO; int irno = ParseLAIConfigXml.IRNO; int swirno = ParseLAIConfigXml.SWIRNO; string geodatastes = ParseLAIConfigXml.GeoDataSets; Hdf5Operator hdfic = new Hdf5Operator(L2LSRFile); string[] HDFdatasets = hdfic.GetDatasetNames; bool matched = false; for (int i = 0; i < HDFdatasets.Length; i++) { if (HDFdatasets[i].ToUpper() == lsrdataset.ToUpper()) { matched = true; break; } } if (!matched) { return(null); } Dictionary <string, string> dsAttrs = hdfic.GetAttributes(lsrdataset); if (!dsAttrs.ContainsKey("band_name")) { return(null); } string[] bandNO = dsAttrs["band_name"].Split(','); //if (!bandNO.Contains(rno.ToString()) || !bandNO.Contains(irno.ToString()) || !bandNO.Contains(swirno.ToString())) // return null; int ridx = 0, iridx = 0, swiridx = 0; for (int id = 0; id < bandNO.Length; id++) { if (bandNO[id] == rno.ToString()) { ridx = id + 1; } else if (bandNO[id] == irno.ToString()) { iridx = id + 1; } else if (bandNO[id] == swirno.ToString()) { swiridx = id + 1; } } if (ridx == 0 || iridx == 0 || swiridx == 0) { return(null); } if (dsAttrs.ContainsKey("Intercept")) { string[] bandIntstr = dsAttrs["Intercept"].Split(','); if (bandIntstr.Length == 1) { float bandintval = float.Parse(bandIntstr[0]); bandInt = new float[] { bandintval, bandintval, bandintval }; } else { bandInt[0] = float.Parse(bandIntstr[ridx - 1]); bandInt[1] = float.Parse(bandIntstr[iridx - 1]); bandInt[2] = float.Parse(bandIntstr[swiridx - 1]); } } if (dsAttrs.ContainsKey("Slope")) { string[] bandSlope = dsAttrs["Slope"].Split(','); if (bandSlope.Length == 1) { float bandslpval = float.Parse(bandSlope[0]); bandslope = new float[] { bandslpval, bandslpval, bandslpval }; } else { bandslope[0] = float.Parse(bandSlope[ridx - 1]); bandslope[1] = float.Parse(bandSlope[iridx - 1]); bandslope[2] = float.Parse(bandSlope[swiridx - 1]); } } #endregion string[] openArgs = new string[] { "datasets=" + lsrdataset }; lsrRaster = RasterDataDriver.Open(L2LSRFile, openArgs) as IRasterDataProvider; if (lsrRaster == null || lsrRaster.BandCount != bandNO.Length) { return(null); } outwidth = lsrRaster.Width; outheight = lsrRaster.Height; UInt16[] reddata = GetDataValue <UInt16>(lsrRaster.GetRasterBand(ridx), 0, 0, outwidth, outheight); UInt16[] IRdata = GetDataValue <UInt16>(lsrRaster.GetRasterBand(iridx), 0, 0, outwidth, outheight); UInt16[] swirdata = GetDataValue <UInt16>(lsrRaster.GetRasterBand(swiridx), 0, 0, outwidth, outheight); string[] locationArgs = new string[] { "datasets = " + geodatastes };//SolarZenith,SolarAzimuth,SensorZenith,SensorAzimuth,Longitude,Latitude if (_progressTracker != null) { _progressTracker(9, "正在读取L1数据文件..."); } angleRaster = RasterDataDriver.Open(L1GranulesFile, locationArgs) as IRasterDataProvider; if (angleRaster == null || angleRaster.BandCount != 6) { return(null); } Int16[] sza = GetDataValue <Int16>(angleRaster.GetRasterBand(1), 0, 0, outwidth, outheight); Int16[] saa = GetDataValue <Int16>(angleRaster.GetRasterBand(2), 0, 0, outwidth, outheight); Int16[] vza = GetDataValue <Int16>(angleRaster.GetRasterBand(3), 0, 0, outwidth, outheight); Int16[] vaa = GetDataValue <Int16>(angleRaster.GetRasterBand(4), 0, 0, outwidth, outheight); float[] lonM = GetDataValue <float>(angleRaster.GetRasterBand(5), 0, 0, outwidth, outheight); float[] latM = GetDataValue <float>(angleRaster.GetRasterBand(6), 0, 0, outwidth, outheight); if (_progressTracker != null) { _progressTracker(11, "正在读取LandCover数据文件..."); } landcoverRaster = GeoDataDriver.Open(landCoverFile) as IRasterDataProvider; if (landcoverRaster == null || landcoverRaster.BandCount < 1) { return(null); } #endregion output[0] = new ushort[reddata.Length]; //output[1]=new ushort[reddata.Length]; #region 分块并行处理 int parts = 7; int step = reddata.Length / parts; int[] istart = new int[parts]; int[] iend = new int[parts]; istart[0] = 0; iend[0] = 0 + step; for (int p = 1; p < parts; p++) { istart[p] = istart[p - 1] + step; iend[p] = iend[p - 1] + step; } iend[parts - 1] = reddata.Length; var tasks = new Action[] { () => CalLAIParts(istart[0], iend[0], outwidth, reddata, IRdata, swirdata, landcoverRaster, sza, saa, vza, vza, latM, lonM), () => CalLAIParts(istart[1], iend[1], outwidth, reddata, IRdata, swirdata, landcoverRaster, sza, saa, vza, vza, latM, lonM), () => CalLAIParts(istart[2], iend[2], outwidth, reddata, IRdata, swirdata, landcoverRaster, sza, saa, vza, vza, latM, lonM), () => CalLAIParts(istart[3], iend[3], outwidth, reddata, IRdata, swirdata, landcoverRaster, sza, saa, vza, vza, latM, lonM), () => CalLAIParts(istart[4], iend[4], outwidth, reddata, IRdata, swirdata, landcoverRaster, sza, saa, vza, vza, latM, lonM), () => CalLAIParts(istart[5], iend[5], outwidth, reddata, IRdata, swirdata, landcoverRaster, sza, saa, vza, vza, latM, lonM), () => CalLAIParts(istart[6], iend[6], outwidth, reddata, IRdata, swirdata, landcoverRaster, sza, saa, vza, vza, latM, lonM), }; if (_progressTracker != null) { _progressTracker(13, "正在逐点计算LAI,请稍候..."); } System.Threading.Tasks.Parallel.Invoke(tasks); #endregion #region 建立输出文件 if (_progressTracker != null) { _progressTracker(75, "正在投影LAI数据,请稍候..."); } return(OutputLAI(angleRaster, L2LSRFile)); #endregion } catch (System.Exception ex) { //_progressTracker(0, ex.Message); PrintInfo(ex.Message); return(null); } finally { if (lsrRaster != null) { lsrRaster.Dispose(); } if (angleRaster != null) { angleRaster.Dispose(); } if (landcoverRaster != null) { landcoverRaster.Dispose(); } if (output != null) { output = null; } } #endregion }