在这里介绍的是用NetCDF(c++接口)读取NC文件信息,主要包括NC文件里的维度与变量,我用的VS2015+Qt+NetCDF进行读取的,至于这个环境的配置可以参考我的上一篇文章,在这里我就不介绍环境的配置了。不过你也可用VS+NetCDF进行读取,把结果展示在控制台窗口内,我用Qt的目的是为了将读取结果展示在一个表格控件内。
1.首先读取文件
QString filename = QFileDialog::getOpenFileName(this, "开打文件", ".", "*.nc");
if (filename.isEmpty())
{
QMessageBox::information(this, "提示", "文件指针为空"); return;
}
NcFile dataFile(filename.toStdString(), NcFile::read);
2.读取维度
//维度操作
multimap<string, NcDim> group_dim;
group_dim = dataFile.getDims();
multimap<string, NcDim>::iterator it_dim;
it_dim = group_dim.begin();
int row_dim = 0;
for (; it_dim != group_dim.end(); it_dim++)
{ //读取维度的ID
int id = it_dim->second.getId();
QString txt_id = QString::number(id);
tableV_dim->setItem(row_dim, 0, new QStandardItem(txt_id));
//读取维度的名称
string name = it_dim->second.getName();
QString txt_name = QString::fromStdString(name);
tableV_dim->setItem(row_dim, 1, new QStandardItem(txt_name));
//读取维度的长度
int size = it_dim->second.getSize();
QString txt_size = QString::number(size);
tableV_dim->setItem(row_dim, 2, new QStandardItem(txt_size));
row_dim++;
}
//设置表格的Model
ui.tableView_dim->setModel(tableV_dim);
上面的tableView_dim是我自定义的QStandardItemModel;
3.读取变量
//变量操作
std::multimap<string, NcVar> group_var;
group_var = dataFile.getVars();
std::multimap<string, NcVar>::iterator it_var;
it_var = group_var.begin();
int row = 0;
for (; it_var != group_var.end(); it_var++)
{ //读取变量ID
int id = it_var->second.getId();
QString txt_id = QString::number(id);
tableV_var->setItem(row, 0, new QStandardItem(txt_id));
//读取变量名称
string name = it_var->second.getName();
QString txt_name = QString::fromStdString(name);
tableV_var->setItem(row, 1, new QStandardItem(txt_name));
//读取变量类型
string type = it_var->second.getType().getName();
QString txt_type = QString::fromStdString(type);
tableV_var->setItem(row, 2, new QStandardItem(txt_type));
//读取变量所拥有的维度名称
int dim = it_var->second.getDimCount();
QString txt_dim = QString::number(dim);
tableV_var->setItem(row, 3, new QStandardItem(txt_dim));
string name_dim = "";
for (int i = 0; i < it_var->second.getDimCount(); i++)
{
NcDim dim = it_var->second.getDim(i);
if (i == 0)
{
name_dim = name_dim + dim.getName();
}
else
{
name_dim = name_dim + " , " + dim.getName();
}
}
QString txt_name_dim = QString::fromStdString(name_dim);
tableV_var->setItem(row, 4, new QStandardItem(txt_name_dim));
row++;
}
附上一个打开NC文件并读入一个表格内的完整函数
void NC_show::act_tool_open_triggered()
{
QString filename = QFileDialog::getOpenFileName(this, "开打文件", ".", "*.nc");
if (filename.isEmpty())
{
QMessageBox::information(this, "提示", "文件指针为空"); return;
}
NcFile dataFile(filename.toStdString(), NcFile::read);
//维度操作
multimap<string, NcDim> group_dim;
group_dim = dataFile.getDims();
multimap<string, NcDim>::iterator it_dim;
it_dim = group_dim.begin();
int row_dim = 0;
for (; it_dim != group_dim.end(); it_dim++)
{
int id = it_dim->second.getId();
QString txt_id = QString::number(id);
tableV_dim->setItem(row_dim, 0, new QStandardItem(txt_id));
string name = it_dim->second.getName();
QString txt_name = QString::fromStdString(name);
tableV_dim->setItem(row_dim, 1, new QStandardItem(txt_name));
int size = it_dim->second.getSize();
QString txt_size = QString::number(size);
tableV_dim->setItem(row_dim, 2, new QStandardItem(txt_size));
row_dim++;
}
ui.tableView_dim->setModel(tableV_dim);
//变量操作
std::multimap<string, NcVar> group_var;
group_var = dataFile.getVars();
std::multimap<string, NcVar>::iterator it_var;
it_var = group_var.begin();
int row = 0;
for (; it_var != group_var.end(); it_var++)
{ //读取变量ID
int id = it_var->second.getId();
QString txt_id = QString::number(id);
tableV_var->setItem(row, 0, new QStandardItem(txt_id));
//读取变量名称
string name = it_var->second.getName();
QString txt_name = QString::fromStdString(name);
tableV_var->setItem(row, 1, new QStandardItem(txt_name));
//读取变量类型
string type = it_var->second.getType().getName();
QString txt_type = QString::fromStdString(type);
tableV_var->setItem(row, 2, new QStandardItem(txt_type));
//读取变量所拥有的维度名称
int dim = it_var->second.getDimCount();
QString txt_dim = QString::number(dim);
tableV_var->setItem(row, 3, new QStandardItem(txt_dim));
string name_dim = "";
for (int i = 0; i < it_var->second.getDimCount(); i++)
{
NcDim dim = it_var->second.getDim(i);
if (i == 0)
{
name_dim = name_dim + dim.getName();
}
else
{
name_dim = name_dim + " , " + dim.getName();
}
}
QString txt_name_dim = QString::fromStdString(name_dim);
tableV_var->setItem(row, 4, new QStandardItem(txt_name_dim));
row++;
}
ui.tableView_var->setModel(tableV_var);
}
结果图:
来源:oschina
链接:https://my.oschina.net/u/4311839/blog/3300147