C#读取风云卫星(HDF5格式)遥感数据的部分代码
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HDF5DotNet;
using System.Collections;
namespace ReadHDF5
{
/*
* 2011.2.14 xyj
* 数据集操作:主要有:在指定群组中获取所有的数据集名称 , 获取指定名称的数据集的数据
*
*/
//得到hdf5数据集的数据,主要为int数组,float、double数组保存
class GetHdf5DataSet
{
#region 获取数据集数据,主要包含int,float,double
private Array _dataSet;
public Array get_Hdf5DataSet
{
get
{
return _dataSet;
}
}
///
/// 得到int类型的数据集
///
/// 文件名称,包含路径
/// 数据集所在的群组
/// 数据集的名称
/// 输出的结果数据集
public void hdf5_getDataSet(string fileName, string GroupName, string DsName, int[, ,] dataSet)
{
H5FileId fileId = H5F.open(fileName, H5F.OpenMode.ACC_RDONLY);
H5GroupId groupId = H5G.open(fileId, GroupName);
H5DataSetId dataSetId = H5D.open(groupId, DsName/*"EV_Emissive"*/);
H5DataTypeId tid0 = H5D.getType(dataSetId);
H5DataTypeId tid1 = H5T.enumCreate(tid0);//= new H5DataTypeId(H5T.H5Type.NATIVE_INT);//数据类型
// Read the array back
H5D.read(dataSetId, tid1,
new H5Array
H5D.close(dataSetId);
H5G.close(groupId);
H5F.close(fileId);
_dataSet = dataSet;
}
///
/// 得到float类型的数据集
///
/// 文件名称,包含路径
/// 数据集所在的群组
/// 数据集的名称
/// 输出的float[]结果数据集
public void hdf5_getDataSet(string fileName, string GroupName, string DsName, float[, ,] dataSet)
{
H5FileId fileId = H5F.open(fileName, H5F.OpenMode.ACC_RDONLY);
H5GroupId groupId = H5G.open(fileId, GroupName);
H5DataSetId dataSetId = H5D.open(groupId, DsName/*"EV_Emissive"*/);
H5DataTypeId tid1 = new H5DataTypeId(H5T.H5Type.NATIVE_FLOAT);
// Read the array back
H5D.read(dataSetId, tid1,
new H5Array
H5D.close(dataSetId);
H5G.close(groupId);
H5F.close(fileId);
}
///
/// 得到double类型的数据集
///
/// 文件名称,包含路径
/// 数据集所在的群组
/// 数据集的名称
/// 输出的结果数据集double[]
public void hdf5_getDataSet(string fileName, string GroupName, string DsName, double[, ,] dataSet)
{
H5FileId fileId = H5F.open(fileName, H5F.OpenMode.ACC_RDONLY);
H5GroupId groupId = H5G.open(fileId, GroupName);
H5DataSetId dataSetId = H5D.open(groupId, DsName/*"EV_Emissive"*/);
H5DataTypeId tid1 = new H5DataTypeId(H5T.H5Type.NATIVE_DOUBLE);
// Read the array back
H5D.read(dataSetId, tid1,
new H5Array
H5D.close(dataSetId);
H5G.close(groupId);
H5F.close(fileId);
}
///
/// 得到int数据集
///
/// 文件id
/// 群组id
/// 数据集id
/// int类型的数据集
public void hdf5_getDataSet(H5FileId fileId, H5GroupId groupId, H5DataSetId dataSetId, int[, ,] dataSet)
{
H5DataTypeId tid1 = new H5DataTypeId(H5T.H5Type.NATIVE_INT);//数据类型
// Read the array back
H5D.read(dataSetId, tid1,
new H5Array
_dataSet = dataSet;
}
///
/// 得到float数据集
///
/// 文件id
/// 群组id
/// 数据集id
/// float类型的数据集
public void hdf5_getDataSet(H5FileId fileId, H5GroupId groupId, H5DataSetId dataSetId, float[, ,] dataSet)
{
H5DataTypeId tid1 = new H5DataTypeId(H5T.H5Type.NATIVE_FLOAT);//数据类型
// Read the array back
H5D.read(dataSetId, tid1,
new H5Array
_dataSet = dataSet;
}
///
/// 得到double数据集
///
/// 文件id
/// 群组id
/// 数据集id
/// double类型的数据集
public void hdf5_getDataSet(H5FileId fileId, H5GroupId groupId, H5DataSetId dataSetId, double[,
,] dataSet)
{
H5DataTypeId tid1 = new H5DataTypeId(H5T.H5Type.NATIVE_DOUBLE);//数据类型
// Read the array back
H5D.read(dataSetId, tid1,
new H5Array
_dataSet = dataSet;
}
//---------------------------------------------------------------------------------------------------
#endregion
///
/// 获取群组中所有的数据集名称
///
/// 文件名
/// 群组名
///
public string[] getDatasetNames(string filename, string GroupName)
{
// filename = @"D:\FY_data\FY3A_VIRRX_GBAL_L1_20090503_0240_1000M_MS.HDF";
H5FileId fileId = H5F.open(filename, H5F.OpenMode.ACC_RDONLY);
H5GroupId groupId = H5G.open(fileId, "/");
ulong num_objs = H5G.getNumObjects(groupId);
int num = Convert.ToInt32(num_objs);
string[] dataNames = new string[num];
for (int i = 0; i < num; i++)
{
string name = H5G.getObjectNameByIndex(groupId, i);
dataNames[i] = name;
}
H5G.close(groupId);
H5F.close(fileId);
return dataNames;
}
}
/*
* 2011.2.14 xyj
* 功能:获取群组中所有的属性名 获取属性名的属性值
*
* */
class Hdf5AttributeData
{
Array _array;
#region //----- 获取群组中所有的属性名称attributeNames--------
///
/// 得到群组中所有属性字段名,Function used with H5A.iterate
///
///
///
///
///
///
static H5IterationResult MyH5AFunction(
H5AttributeId attributeId,
String attributeName,
H5AttributeInfo info,
Object attributeNames)
{
// Console.WriteLine("Iteration attribute is {0}", attributeName);
ArrayList nameList = (ArrayList)attributeNames;
nameList.Add(attributeName);
// Returning SUCCESS means that iteration should continue to the
// next attribute (if one exists).
return H5IterationResult.SUCCESS;
}
///
/// 返回群组中所有的属性名称
///
/// 群组id号
///
public ArrayList getAttributeNames(H5ObjectWithAttributes dataSetId)
{
H5AIterateDelegat
e attributeDelegate;
attributeDelegate = MyH5AFunction;
ArrayList attributeNames = new ArrayList();
ulong position = 0;
H5A.iterate(dataSetId, H5IndexType.CRT_ORDER,
H5IterationOrder.INCREASING,
ref position, attributeDelegate,
(object)attributeNames);
return attributeNames;
}
//--------------------------------------------------
#endregion
#region 获取属性值
///
/// 得到属性值,不足,不能得到rank为0的值,返回值也不好获取
///
/// 可以为groupID,dataSetId
///
public Array getAttributeValue(H5ObjectWithAttributes dataSetId, string atrrName)
{
H5AttributeId attributeId = H5A.openName(dataSetId, atrrName); //根据属性名称得到属性Id
H5DataTypeId attributeType = H5A.getType(attributeId); //得到属性数据类型
H5T.H5TClass tcls = H5T.getClass(attributeType); //类型名称
uint size = H5T.getSize(attributeType);
H5T.H5Type H5type;
string typeName = tcls.ToString();
H5DataSpaceId spaceid2 = H5A.getSpace(attributeId);
ulong[] dimsA = H5S.getSimpleExtentDims(spaceid2);
int rank = H5S.getSimpleExtentNDims(spaceid2);
// object[] readBackRamp = new object[dimsA[0]];
switch (typeName)
{
case "FLOAT":
if (size > 4)
{
H5type = H5T.H5Type.NATIVE_DOUBLE;
if (rank == 1)
{
double[] tmp = new Double[dimsA[0]];
H5A.read(attributeId, new H5DataTypeId(H5type), //读取属性值
new H5Array
_array = tmp;
}
else if (rank == 2)
{
double[,] tmp = new Double[dimsA[0], dimsA[1]];
H5A.read(attributeId, new H5DataTypeId(H5type), //读取属性值
new H5Array
_array = tmp;
}
}
else
{
H5type = H5T.H5Type.NATIVE_FLOAT;
if (rank == 1)
{
float[] tmp = new float[dimsA[0]];
H5A.read(attributeId, new H5DataTypeId(H5type), //读取属性值
new H5Array
_array = tmp;
}
else if (rank == 2)
{
float[,] tmp = new float[dimsA[0], dimsA[1]];
H5A.read(attributeId, new H5DataTypeId(H5type), //读取属性值
new H5Array
_array = tmp;
}
}
break;
case "STRING":
H5type = H5T.H5Type.C_S1;
if (rank == 1)
{
H5type = H5T.H5Type.C_S1;
string[] tmp = new string[dimsA[0]];
H5A.read(attributeId, new H5DataTypeId(H5type), //读取属性值
new H5Array
_array = tmp;
}
else if (rank == 2)
{
string[,] tmp = new string[dimsA[0], dimsA[1]];
H5A.read(attributeId, new H5DataTypeId(H5type), //读取属性值
new H5Array
_array = tmp;
}
break;
case "INTEGER":
H5type = H5T.H5Type.NATIVE_INT;
if (rank == 1)
{
int[] tmp = new int[dimsA[0]];
H5A.read(attributeId, new H5DataTypeId(H5type), //读取属性值
new H5Array
_array = tmp;
}
else if (rank == 2)
{
int[,] tmp = new int[dimsA[0], dimsA[1]];
H5A.read(attributeId, new H5DataTypeId(H5type), //读取属性值
new H5Array
_array = tmp;
}
break;
default:
H5type = H5T.H5Type.C_S1;
if (rank == 1)
{
string[] tmp = new string[dimsA[0]];
H5A.read(attributeId, new H5DataTypeId(H5type), //读取属性值
new H5Array
_array = tmp;
}
else if (rank == 2)
{
string[,] tmp = new string[dimsA[0], dimsA[1]];
H5A.read(attributeId, new H5DataTypeId(H5type), //读取属性值
new H5Array
_array = tmp;
}
break;
}
H5A.close(attributeId);
return _array;
}
///
/// 获取属性值
///
/// 群组id或数据集id
/// 属性id
///
public Array getAttributeValue(H5ObjectWithAttributes dataSetId, H5AttributeId attributeId)
{
H5DataTypeId attributeType = H5A.getType(attributeId); //得到属性数据类型
H5T.H5TClass tcls = H5T.getClass(attributeType); //类型名称
uint size = H5T.getSize(attributeType);
H5T.H5Type H5type;
string typeName = tcls.ToString();
H5DataSpaceId spaceid2 = H5A.getSpace(attributeId);
ulong[] dimsA = H5S.getSimpleExtentDims(spaceid2);
int rank = H5S.getSimpleExtentNDims(spaceid2);
// object[] readBackRamp = new object[dimsA[0]];
switch (typeName)
{
case "FLOAT":
if (size > 4)
{
H5type = H5T.H5Type.NATIVE_DOUBLE;
if (rank == 1)
{
double[] tmp = new Double[dimsA[0]];
H5A.read(attributeId, new H5DataTypeId(H5type), //读取属性值
new H5Array
_array = tmp;
}
else if (rank == 2)
{
double[,] tmp = new Double[dimsA[0], dimsA[1]];
H5A.read(attributeId, new H5DataTypeId(H5type), //读取属性值
new H5Array
_array = tmp;
}
}
else
{
H5type = H5T.H5Type.NATIVE_FLOAT;
if (rank == 1)
{
float[] tmp = new float[dimsA[0]];
H5A.read(attributeId, new H5DataTypeId(H5type), //读取属性值
new H5Array
_array = tmp;
}
else if (rank == 2)
{
float[,] tmp = new float[dimsA[0], dimsA[1]];
H5A.read(attributeId, new H5DataTypeId(H5type), //读取属性值
new H5Array
_array = tmp;
}
}
break;
case "STRING":
H5type = H5T.H5Type.C_S1;
if (rank == 1)
{
H5type = H5T.H5Type.C_S1;
string[] tmp = new string[dimsA[0]];
H5A.read(attributeId, new H5D
ataTypeId(H5type), //读取属性值
new H5Array
_array = tmp;
}
else if (rank == 2)
{
string[,] tmp = new string[dimsA[0], dimsA[1]];
H5A.read(attributeId, new H5DataTypeId(H5type), //读取属性值
new H5Array
_array = tmp;
}
break;
case "INTEGER":
H5type = H5T.H5Type.NATIVE_INT;
if (rank == 1)
{
int[] tmp = new int[dimsA[0]];
H5A.read(attributeId, new H5DataTypeId(H5type), //读取属性值
new H5Array
_array = tmp;
}
else if (rank == 2)
{
int[,] tmp = new int[dimsA[0], dimsA[1]];
H5A.read(attributeId, new H5DataTypeId(H5type), //读取属性值
new H5Array
_array = tmp;
}
break;
default:
H5type = H5T.H5Type.C_S1;
if (rank == 1)
{
string[] tmp = new string[dimsA[0]];
H5A.read(attributeId, new H5DataTypeId(H5type), //读取属性值
new H5Array
_array = tmp;
}
else if (rank == 2)
{
string[,] tmp = new string[dimsA[0], dimsA[1]];
H5A.read(attributeId, new H5DataTypeId(H5type), //读取属性值
new H5Array
_array = tmp;
}
break;
}
return _array;
}
#endregion
}
/*
* 2011.2.14 xyj
* 功能:获取数据集的信息,包含维数,波段、行列数据、数据集的类型,如int、float、double、long等。
*
*/
///
/// 获取数据集的信息,如数据集的维数,波段数,行列数据
///
class GetHdf5DataSetInfo
{
int _rank;
int _row;
int _col;
int _bands;
string _type;
///
/// 数据集维数
///
public int h5fDataRank
{
get
{
return this._rank;
}
}
///
/// 数据集的宽即列数据
///
public int h5fDataWeight
{
get
{
return this._col;
}
}
///
/// 数据矩阵的高即行数
///
public int h5fDataHeight
{
get
{
return this._row;
}
}
///
/// 数据的类型,如Int类型,float类型,double类型等
///
public string h5fDataType
{
get
{
return this._type;
}
}
///
/// 数据的波段数
///
public int h5fDataSetBands
{
get
{
return this._bands;
}
}
//构造函数
public GetHdf5DataSetInfo(string fileName, string GroupName, string DsName)
{
getH5FDataSetInfo(fileName, GroupName, DsName);
}
public GetHdf5DataSetInfo(H5FileId fileId, H5GroupId groupId, H5DataSetId dataSetId)
{
getH5FDataSetInfo( fileId, groupId, dataSetId);
}
///
/// 得到数据集数据的信息,包含数据集的维数,类型,行列数
///
///
///
///
public void getH5FDataSetInfo(string fileName, string GroupName, string DsName)
{
H5FileId fileId = H5F.open(fileName, H5F.OpenMode.ACC_RDONLY);
H5GroupId groupId = H5G.open(fileId, GroupName);
H5DataSetId dataSetId = H5D.open(groupId, DsName);
// ulong storeSize = H5D.getStorageSize(dataSetId); //得到数据数组存储大小
H5DataSpaceId spaceid = H5D.getSpace(dataSetId);
ulong[] dims = H5S.getSimpleExtentDims(spaceid);//得到数据数组的大小,比如[3,1800,2048]
int rank = H5S.getSimpleExtentNDims(spaceid);//得到数据数组的维数,比如3
_rank = rank;
int ll = dims.Length;
if (ll == 2)
{
_bands = 1;
_col = Convert.ToInt32(dims[1]);//宽
_row = Convert.ToInt32(dims[0]);
}
else if (ll == 3)
{
_bands = Convert.ToInt32(dims[0]);//波段数
_col = Convert.ToInt32(dims[2]); //宽
_row = Convert.ToInt32(dims[1]); //高
}
else if (ll == 1)
{
_bands = 1;
_col = 1;
_row = Convert.ToInt32(dims[0]);//高
}
else
{
_bands = 1;
_col = 1;
_row = 1;//高
}
H5DataTypeId dtype = H5D.getType(dataSetId);
H5T.H5TClass dtcls
= H5T.getClass(dtype);//得到数据集的类型
string typeName = dtcls.ToString();
uint size = H5T.getSize(dtype);
switch (typeName)
{
case "FLOAT":
if (size > 4)
{
_type = "DOUBLE";
}
else
{
_type = "FLOAT";
}
break;
case "INTEGER":
if (size > 4)
{
_type = "INT64";
}
else
{
_type = "INT";
}
break;
default:
_type = "INT";
break;
}
H5D.close(dataSetId);
H5G.close(groupId);
H5F.close(fileId);
}
///
/// 得到数据集数据的信息,包含数据集的维数,类型,行列数,波段数
///
/// 文件id
/// 群组id
/// 数据集id
public void getH5FDataSetInfo(H5FileId fileId, H5GroupId groupId, H5DataSetId dataSetId)
{
H5DataSpaceId spaceid = H5D.getSpace(dataSetId);
ulong[] dims = H5S.getSimpleExtentDims(spaceid);//得到数据数组的大小,比如[3,1800,2048]
int rank = H5S.getSimpleExtentNDims(spaceid);//得到数据数组的维数,比如3
_rank = rank;
int ll = dims.Length;
if (ll == 2)
{
_bands = 1;
_col =Convert.ToInt32( dims[0]);
_row = Convert.ToInt32(dims[1]);
}
else if (ll == 3)
{
_bands = Convert.ToInt32(dims[0]);
_col =Convert.ToInt32( dims[1]);
_row = Convert.ToInt32(dims[2]);
}
else if (ll == 1)
{
_bands = 1;
_col = 1;
_row = Convert.ToInt32(dims[0]);//高
}
else
{
_bands = 1;
_col = 1;
_row = 1;
}
H5DataTypeId dtype = H5D.getType(dataSetId);
H5T.H5TClass dtcls = H5T.getClass(dtype);//得到数据集的类型
string typeName = dtcls.ToString();
uint size = H5T.getSize(dtype);
switch (typeName)
{
case "FLOAT":
if (size > 4)
{
_type = "DOUBLE";
}
else
{
_type = "
FLOAT";
}
break;
case "INTEGER":
if (size > 4)
{
_type = "INT64";
}
else
{
_type = "INT";
}
break;
default:
_type = "INT";
break;
}
}
}
}