用Qt+NetCDF 读取NC文件

2024-04-17 16:20
文章标签 读取 qt nc netcdf

本文主要是介绍用Qt+NetCDF 读取NC文件,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

用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++;}
}

这篇关于用Qt+NetCDF 读取NC文件的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/912283

相关文章

C#读取本地网络配置信息全攻略分享

《C#读取本地网络配置信息全攻略分享》在当今数字化时代,网络已深度融入我们生活与工作的方方面面,对于软件开发而言,掌握本地计算机的网络配置信息显得尤为关键,而在C#编程的世界里,我们又该如何巧妙地读取... 目录一、引言二、C# 读取本地网络配置信息的基础准备2.1 引入关键命名空间2.2 理解核心类与方法

基于Qt开发一个简单的OFD阅读器

《基于Qt开发一个简单的OFD阅读器》这篇文章主要为大家详细介绍了如何使用Qt框架开发一个功能强大且性能优异的OFD阅读器,文中的示例代码讲解详细,有需要的小伙伴可以参考一下... 目录摘要引言一、OFD文件格式解析二、文档结构解析三、页面渲染四、用户交互五、性能优化六、示例代码七、未来发展方向八、结论摘要

SpringBoot使用Apache POI库读取Excel文件的操作详解

《SpringBoot使用ApachePOI库读取Excel文件的操作详解》在日常开发中,我们经常需要处理Excel文件中的数据,无论是从数据库导入数据、处理数据报表,还是批量生成数据,都可能会遇到... 目录项目背景依赖导入读取Excel模板的实现代码实现代码解析ExcelDemoInfoDTO 数据传输

Python读取TIF文件的两种方法实现

《Python读取TIF文件的两种方法实现》本文主要介绍了Python读取TIF文件的两种方法实现,包括使用tifffile库和Pillow库逐帧读取TIFF文件,具有一定的参考价值,感兴趣的可以了解... 目录方法 1:使用 tifffile 逐帧读取安装 tifffile:逐帧读取代码:方法 2:使用

python与QT联合的详细步骤记录

《python与QT联合的详细步骤记录》:本文主要介绍python与QT联合的详细步骤,文章还展示了如何在Python中调用QT的.ui文件来实现GUI界面,并介绍了多窗口的应用,文中通过代码介绍... 目录一、文章简介二、安装pyqt5三、GUI页面设计四、python的使用python文件创建pytho

QT实现TCP客户端自动连接

《QT实现TCP客户端自动连接》这篇文章主要为大家详细介绍了QT中一个TCP客户端自动连接的测试模型,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录版本 1:没有取消按钮 测试效果测试代码版本 2:有取消按钮测试效果测试代码版本 1:没有取消按钮 测试效果缺陷:无法手动停

基于Qt实现系统主题感知功能

《基于Qt实现系统主题感知功能》在现代桌面应用程序开发中,系统主题感知是一项重要的功能,它使得应用程序能够根据用户的系统主题设置(如深色模式或浅色模式)自动调整其外观,Qt作为一个跨平台的C++图形用... 目录【正文开始】一、使用效果二、系统主题感知助手类(SystemThemeHelper)三、实现细节

Qt实现文件的压缩和解压缩操作

《Qt实现文件的压缩和解压缩操作》这篇文章主要为大家详细介绍了如何使用Qt库中的QZipReader和QZipWriter实现文件的压缩和解压缩功能,文中的示例代码简洁易懂,需要的可以参考一下... 目录一、实现方式二、具体步骤1、在.pro文件中添加模块gui-private2、通过QObject方式创建

Qt QWidget实现图片旋转动画

《QtQWidget实现图片旋转动画》这篇文章主要为大家详细介绍了如何使用了Qt和QWidget实现图片旋转动画效果,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 一、效果展示二、源码分享本例程通过QGraphicsView实现svg格式图片旋转。.hpjavascript

嵌入式QT开发:构建高效智能的嵌入式系统

摘要: 本文深入探讨了嵌入式 QT 相关的各个方面。从 QT 框架的基础架构和核心概念出发,详细阐述了其在嵌入式环境中的优势与特点。文中分析了嵌入式 QT 的开发环境搭建过程,包括交叉编译工具链的配置等关键步骤。进一步探讨了嵌入式 QT 的界面设计与开发,涵盖了从基本控件的使用到复杂界面布局的构建。同时也深入研究了信号与槽机制在嵌入式系统中的应用,以及嵌入式 QT 与硬件设备的交互,包括输入输出设