用Qt+NetCDF 读取NC文件

news/2024/12/22 20:05:55/

用Qt+NetCDF 读取NC文件_ivqtnc-CSDN博客

基于NetCDF-CXX4 封装,对NC文件进行读取,读取内存放到vector中。

ncBase.h

#ifndef  __NC_BASE_H__
#define __NC_BASE_H__#include <QVariant>
#include <vector>
#include <map>
using namespace std;typedef std::vector<float> _dataVector_f;   //存放一维数据
typedef std::vector<double> _dataVector_d;   //存放一维数据
typedef std::vector<QString> _dataVector_s;   //存放一维数据
typedef std::vector<_dataVector_d> _3DDataVector_v;   //存放三维数据
typedef QPair<float, float> _Pos;Q_DECLARE_METATYPE(_dataVector_d)
Q_DECLARE_METATYPE(_3DDataVector_v)#endif

ncReader.h

#pragma once
#ifndef _NC_READER_H_
#define _NC_READER_H_#include "qfileinfo.h"
#include "ncBase.h"
#include <QMap>
#include <QString>
#include <Qvector>
#include <QFileInfo>
#include <QDir>
#include <QTimer>
#include <netcdfcpp.h>
#include "../../Z_PlantInterface/plantinterface.h"using namespace  std;
using  namespace netCDF;class ncReader :QObject
{Q_OBJECT
private:QString curFilePath;CPlantInterface* m_pInterface;NcFile *m_pDataFile{};
public:ncReader(QString filePath);~ncReader();void SetPlantInterface(CPlantInterface* pInterface) { m_pInterface = pInterface; };bool ReadData_D(_dataVector_d& vet_data, QString strEle, long l0 = -1, long l1 = -1, long l2 = -1, long l3 = -1, long l4 = -1);bool ReadData_F(_dataVector_f& vet_data, QString strEle, long l0 = -1, long l1 = -1, long l2 = -1, long l3 = -1, long l4 = -1);long *GetEleEdge(QString strElement);NcVar* GetVar(QString strEle);QString getfilePath();int  GetVarNums();void GetVars(_dataVector_s &vet_Vars);
};#endif

ncReader.cpp

#include"ncReader.h"
#include<qdebug.h>
#include <QPixmap>
#include <QFile>
#include <QDataStream>
#include <QApplication>
#include <QSettings>
#include <iostream>
#include <QDateTime>
#include <string.h>ncReader::ncReader(QString filePath) :curFilePath(filePath)
{//中文字符串转换std::string pathstr = filePath.toLocal8Bit();const char* temp = pathstr.c_str();m_pDataFile = new NcFile(temp, NcFile::read);//只读文件中的数据
}ncReader::~ncReader()
{}bool ncReader::ReadData_D(_dataVector_d& vet_data, QString strEle, long l0 /*=-1*/, long l1/*=-1*/, long l2/*=-1*/, long l3 /*= -1*/, long l4 /*= -1*/)
{if (m_pDataFile->isNull() || !m_pDataFile)return false;if (l0 < -1 || l1 < -1 || l2 < -1 || l3 < -1 || l4 < -1)return false;long lg0 = 1, lg1 = 1, lg2 = 1, lg3 = 1, lg4 = 1, lg5 = 1;long *p = GetEleEdge(strEle);NcVar pVar = m_pDataFile->getVar(strEle.toStdString().c_str());if (pVar.isNull())return false;int nDims = pVar.getDimCount();std::vector<size_t> vet_Inx;//存放每个维度上索引(开始位置)std::vector<size_t> vet_Count;//存放索引(数量) value >=1vet_Inx.clear();switch (nDims){case 1://1维{float* data = new float[p[0]];pVar.getVar(data);_dataVector_d vet_tmp(data, data + p[0]);vet_data.swap(vet_tmp);delete[]data;}break;case 2://2维{long long nLen = 1;if (l0 == -1){nLen *= p[0];lg0 = p[0];vet_Inx.push_back(0);vet_Count.push_back(lg0);}else{vet_Inx.push_back(l0);vet_Count.push_back(1);}if (l1 == -1){nLen *= p[1];lg1 = p[1];vet_Count.push_back(lg1);vet_Inx.push_back(0);vet_Count.push_back(lg1);}else{vet_Inx.push_back(l1);vet_Count.push_back(1);}float* data = new float[nLen];pVar.getVar(vet_Inx, vet_Count, data);_dataVector_d vet_tmp(data, data + nLen);//转存,效率提升170倍vet_data.swap(vet_tmp);delete[]data;}break;case 3://3维{long nLen = 1;if (l0 == -1){nLen *= p[0];lg0 = p[0]; vet_Inx.push_back(0);vet_Count.push_back(lg0);}else{vet_Inx.push_back(l0);vet_Count.push_back(1);}if (l1 == -1){nLen *= p[1];lg1 = p[1]; vet_Inx.push_back(0);vet_Count.push_back(lg1);}else{vet_Inx.push_back(l1);vet_Count.push_back(1);}if (l2 == -1){nLen *= p[2];lg2 = p[2]; vet_Inx.push_back(0);vet_Count.push_back(lg2);}else{vet_Inx.push_back(l2);vet_Count.push_back(1);}float fscale = 1.0f;float foffset = 0.0f;float* data = new float[nLen];pVar.getVar(vet_Inx, vet_Count, data);_dataVector_d vet_tmp(data, data + nLen);//转存,效率提升170倍if (fscale != 1.0f || foffset != 0.0f){int n = 0;for (auto it : vet_tmp){vet_tmp[n] = it * fscale + foffset;n++;}}vet_data.swap(vet_tmp);delete[]data;}break;case 4://4维{long nLen = 1;if (l0 == -1){nLen *= p[0];lg0 = p[0];vet_Inx.push_back(0);vet_Count.push_back(lg0);}else{vet_Inx.push_back(l0);vet_Count.push_back(1);}if (l1 == -1){nLen *= p[1];lg1 = p[1];vet_Inx.push_back(0);vet_Count.push_back(lg1);}else{vet_Inx.push_back(l1);vet_Count.push_back(1);}if (l2 == -1){nLen *= p[2];lg2 = p[2];vet_Inx.push_back(0);vet_Count.push_back(lg2);}else{vet_Inx.push_back(l2);vet_Count.push_back(1);}if (l3 == -1){nLen *= p[3];lg3 = p[3];vet_Inx.push_back(0);vet_Count.push_back(lg3);}else{vet_Inx.push_back(l3);vet_Count.push_back(1);}float* data = new float[nLen];pVar.getVar(vet_Inx, vet_Count, data);_dataVector_d vet_tmp(data, data + nLen);//转存,效率提升170倍vet_data.swap(vet_tmp);delete[]data;}break;case 5://5维{long nLen = 1;if (l0 == -1){nLen *= p[0];lg0 = p[0];vet_Inx.push_back(0);vet_Count.push_back(lg0);}else{vet_Inx.push_back(l0);vet_Count.push_back(1);}if (l1 == -1){nLen *= p[1];lg1 = p[1];vet_Inx.push_back(0);vet_Count.push_back(lg1);}else{vet_Inx.push_back(l1);vet_Count.push_back(1);}if (l2 == -1){nLen *= p[2];lg2 = p[2];vet_Inx.push_back(0);vet_Count.push_back(lg2);}else{vet_Inx.push_back(l2);vet_Count.push_back(1);}if (l3 == -1){nLen *= p[3];lg3 = p[3];vet_Inx.push_back(0);vet_Count.push_back(lg3);}else{vet_Inx.push_back(l3);vet_Count.push_back(1);}if (l4 == -1){nLen *= p[4];lg4 = p[4];vet_Inx.push_back(0);vet_Count.push_back(lg4);}else{vet_Inx.push_back(l4);vet_Count.push_back(1);}float* data = new float[nLen];pVar.getVar(vet_Inx, vet_Count, data);_dataVector_d vet_tmp(data, data + nLen);//转存,效率提升170倍vet_data.swap(vet_tmp);delete[]data;}break;default:break;}
}bool ncReader::ReadData_F(_dataVector_f& vet_data, QString strEle, long l0 /*=-1*/, long l1/*=-1*/, long l2/*=-1*/, long l3 /*= -1*/, long l4 /*= -1*/)
{if (m_pDataFile->isNull() || !m_pDataFile)return false;if (l0 < -1 || l1 < -1 || l2 < -1 || l3 < -1 || l4 < -1)return false;long lg0 = 1, lg1 = 1, lg2 = 1, lg3 = 1, lg4 = 1, lg5 = 1;long *p = GetEleEdge(strEle);NcVar pVar = m_pDataFile->getVar(strEle.toStdString().c_str());if (pVar.isNull())return false;int nDims = pVar.getDimCount();std::map<std::string, NcVarAtt> map_atts = pVar.getAtts();//获取所有的属性,因为获取不存在的属性会崩溃std::vector<size_t> vet_Inx;//存放每个维度上索引(开始位置)std::vector<size_t> vet_Count;//存放索引(数量) value >=1vet_Inx.clear();switch (nDims){case 1://1维{float* data = new float[p[0]];pVar.getVar(data);_dataVector_f vet_tmp(data, data + p[0]);vet_data.swap(vet_tmp);delete []data;}break;case 2://2维{long long nLen = 1;if (l0 == -1){nLen *= p[0];lg0 = p[0];vet_Inx.push_back(0);vet_Count.push_back(lg0);}else{vet_Inx.push_back(l0);vet_Count.push_back(1);}if (l1 == -1){nLen *= p[1];lg1 = p[1];vet_Count.push_back(lg1);vet_Inx.push_back(0);vet_Count.push_back(lg1);}else{vet_Inx.push_back(l1);vet_Count.push_back(1);}float* data = new float[nLen];pVar.getVar(vet_Inx, vet_Count, data);_dataVector_f vet_tmp(data, data + nLen);//转存,效率提升170倍vet_data.swap(vet_tmp);delete[]data;}break;case 3://3维{long nLen = 1;if (l0 == -1){nLen *= p[0];lg0 = p[0];vet_Inx.push_back(0);vet_Count.push_back(lg0);}else{vet_Inx.push_back(l0);vet_Count.push_back(1);}if (l1 == -1){nLen *= p[1];lg1 = p[1];vet_Inx.push_back(0);vet_Count.push_back(lg1);}else{vet_Inx.push_back(l1);vet_Count.push_back(1);}if (l2 == -1){nLen *= p[2];lg2 = p[2];vet_Inx.push_back(0);vet_Count.push_back(lg2);}else{vet_Inx.push_back(l2);vet_Count.push_back(1);}//查看压缩值float fscale = 1.0f;float foffset = 0.0f;if (map_atts.find("scale_factor") != map_atts.end()){NcVarAtt AttT = pVar.getAtt("scale_factor");if (!AttT.isNull()){NcType TypeId = AttT.getType();if (TypeId == NC_FLOAT || TypeId == NC_DOUBLE)AttT.getValues(&fscale);}}if (map_atts.find("add_offset") != map_atts.end()){NcVarAtt AttS = pVar.getAtt("add_offset");if (!AttS.isNull()){NcType TypeId = AttS.getType();if (TypeId == NC_FLOAT || TypeId == NC_DOUBLE)AttS.getValues(&foffset);}}float* data = new float[nLen];pVar.getVar(vet_Inx, vet_Count, data);_dataVector_f vet_tmp(data, data + nLen);//转存if (fscale != 1.0f || foffset != 0.0f){int n = 0;for (auto it : vet_tmp){vet_tmp[n] = it * fscale + foffset;n++;}}vet_data.swap(vet_tmp);delete[]data;}break;case 4://4维{long nLen = 1;if (l0 == -1){nLen *= p[0];lg0 = p[0];vet_Inx.push_back(0);vet_Count.push_back(lg0);}else{vet_Inx.push_back(l0);vet_Count.push_back(1);}if (l1 == -1){nLen *= p[1];lg1 = p[1];vet_Inx.push_back(0);vet_Count.push_back(lg1);}else{vet_Inx.push_back(l1);vet_Count.push_back(1);}if (l2 == -1){nLen *= p[2];lg2 = p[2];vet_Inx.push_back(0);vet_Count.push_back(lg2);}else{vet_Inx.push_back(l2);vet_Count.push_back(1);}if (l3 == -1){nLen *= p[3];lg3 = p[3];vet_Inx.push_back(0);vet_Count.push_back(lg3);}else{vet_Inx.push_back(l3);vet_Count.push_back(1);}//查看压缩值float fscale = 1.0f;float foffset = 0.0f;if (map_atts.find("scale_factor") != map_atts.end()){NcVarAtt AttT = pVar.getAtt("scale_factor");if (!AttT.isNull()){NcType TypeId = AttT.getType();if (TypeId == NC_FLOAT || TypeId == NC_DOUBLE)AttT.getValues(&fscale);}}if (map_atts.find("add_offset") != map_atts.end()){NcVarAtt AttS = pVar.getAtt("add_offset");if (!AttS.isNull()){NcType TypeId = AttS.getType();if (TypeId == NC_FLOAT || TypeId == NC_DOUBLE)AttS.getValues(&foffset);}}//默认填充值float fFillVaue = 0.0f;if (map_atts.find("_FillValue") != map_atts.end()){NcVarAtt AttF = pVar.getAtt("_FillValue");if (!AttF.isNull()){NcType TypeId = AttF.getType();if (TypeId == NC_FLOAT || TypeId == NC_DOUBLE || TypeId == NC_SHORT)AttF.getValues(&fFillVaue);}}float* data = new float[nLen];pVar.getVar(vet_Inx, vet_Count, data);_dataVector_f vet_tmp(data, data + nLen);//转存int n = 0;for (auto it : vet_tmp){if (fabs(it - fFillVaue) < 0.000001){//not vaild valuevet_tmp[n] = 9999;}else if (fscale != 1.0f || foffset != 0.0f){vet_tmp[n] = it * fscale + foffset;}n++;}vet_data.swap(vet_tmp);delete[]data;}break;case 5://5维{	long nLen = 1;if (l0 == -1){nLen *= p[0];lg0 = p[0];vet_Inx.push_back(0);vet_Count.push_back(lg0);}else{vet_Inx.push_back(l0);vet_Count.push_back(1);}if (l1 == -1){nLen *= p[1];lg1 = p[1];vet_Inx.push_back(0);vet_Count.push_back(lg1);}else{vet_Inx.push_back(l1);vet_Count.push_back(1);}if (l2 == -1){nLen *= p[2];lg2 = p[2];vet_Inx.push_back(0);vet_Count.push_back(lg2);}else{vet_Inx.push_back(l2);vet_Count.push_back(1);}if (l3 == -1){nLen *= p[3];lg3 = p[3];vet_Inx.push_back(0);vet_Count.push_back(lg3);}else{vet_Inx.push_back(l3);vet_Count.push_back(1);}if (l4 == -1){nLen *= p[4];lg4 = p[4];vet_Inx.push_back(0);vet_Count.push_back(lg4);}else{vet_Inx.push_back(l4);vet_Count.push_back(1);}float* data = new float[nLen];pVar.getVar(vet_Inx, vet_Count, data);_dataVector_f vet_tmp(data, data + nLen);vet_data.swap(vet_tmp);delete[]data;}break;default:break;}return true;
}long* ncReader::GetEleEdge(QString strElement)
{long *p = NULL;if (m_pDataFile->isNull() ||!m_pDataFile)return p;NcVar pVar = m_pDataFile->getVar(strElement.toStdString().c_str());if (pVar.isNull())return p;int nLen = pVar.getDimCount();if (nLen <= 0)return p;p = new long[nLen];for (int n = 0; n < nLen; n++){int nSize = pVar.getDim(n).getSize();p[n] = nSize;}return p;
}NcVar* ncReader::GetVar(QString strEle)
{if (m_pDataFile)return &(m_pDataFile->getVar(strEle.toStdString().c_str()));return nullptr;
}QString ncReader::getfilePath()
{return curFilePath;
}int ncReader::GetVarNums()
{if (m_pDataFile)return m_pDataFile->getDimCount();return 0;
}void ncReader::GetVars(_dataVector_s &vet_Vars)
{if (!m_pDataFile)return;vet_Vars.clear();std::multimap<std::string, NcDim> dims = m_pDataFile->getDims();int nIndex = 0;for (auto it : dims){vet_Vars.push_back(QString::fromStdString(it.first));nIndex++;}
}


http://www.ppmy.cn/news/1429156.html

相关文章

【Linux】系统安全及应用

目录 一、账号安全基本措施 1.系统账号清理 2.密码安全控制 3.历史命令安全管理 4.限制su切换用户 1&#xff09;将信任的用户加入到wheel组中 2&#xff09;修改su的PAM认证配置文件 5.ssh远程登录输入三次密码错误则锁定用户 二、Linux中的PAM安全认证 1.su命令的…

mfc140.dll丢失如何修复,分享多种有效的修复方法

在日常操作和使用电脑的过程中&#xff0c;我们可能会遇到一种较为常见的问题&#xff0c;即在尝试启动或运行某个应用程序时&#xff0c;系统突然弹出一个错误提示窗口&#xff0c;明确指出“mfc140.dll文件丢失”。这个mfc140.dll实际上是一个动态链接库文件&#xff08;DLL&…

虚幻引擎源码版安装下载,点击GenerateProjectFiles.bat报错 error NU1101NuGet包问题解决参考方案

开发环境配置与源码安装使用 安装VS2022 按照官方文档安装需要的vs配置 虚幻引擎源代码下载 Epic里面下载的引擎与源代码引擎区别&#xff1a;Epic里面下载的引擎是已经编译过的它的源代码访问权限不完整&#xff0c;源代码版本提供比较完整引擎代码&#xff0c;并且可以修…

【iOS安全】iOS ARM汇编

mov指令 MOV X22, X0 将X0的值移到X22中 参数传递 参数1&#xff1a;寄存器X0传递 参数2&#xff1a;寄存器X1传递 参数3&#xff1a;寄存器X2传递 参数4&#xff1a;寄存器X3传递 &#xff08;这里的X0传递的就是第一个实际参数&#xff0c;而不是self、selector之类的&…

mac tcp实现客户端与服务端进行图像传输及处理

客户端发送图像到服务端&#xff0c;服务端对图像进行处理&#xff0c;在将处理后的图像发送到客户端&#xff0c;并且服务端持续监听客户端。 客户端 #include <iostream> #include <fstream> #include <vector> #include <unistd.h> #include <…

Jackson知识点记录

文章目录 一.Jackson模块说明 二.ObjectMapper基本功能使用ObjectMapper的一些核心方法&#xff1a;示例代码1. 序列化示例2. 反序列化示例3. JsonNode 处理示例 高级配置 三.各种Node1. ObjectNode2. ArrayNode3. ValueNode4. MissingNode示例 一.Jackson Jackson 库主要分为…

Cjson 库使用

1. JSON简介 JSON全称 JavaScript Object Notation&#xff0c;即 JS对象简谱&#xff0c;是一种轻量级的数据格式。 它采用完全独立于编程语言的文本格式来存储和表示数据&#xff0c;语法简洁、层次结构清晰&#xff0c;易于人阅读和编写&#xff0c;同时也易于机器解析和生成…

耀斑层-如何在Unity中实现耀斑亮光效果

在Unity中实现耀斑亮光效果可以通过以下步骤来实现&#xff1a; 创建一个空物体作为光源&#xff1a;在场景中创建一个空物体&#xff0c;并将其放置在需要发出耀斑亮光效果的位置上。 添加光源组件&#xff1a;选中空物体&#xff0c;在Inspector面板中点击"Add Compone…