基于GDAL库,读取海洋风场数据(.nc格式)c++版

        经过这一段时间的对海洋数据的处理,接触了大量的与海洋相关的数据,例如海洋地形、海洋表面温度、盐度、湿度、云场、风场等数据,除了地形数据是grd格式外,其他的都是nc格式的数据。本文将以海洋风场数据为例,进行nc格式文件的读取。

         海洋风场数据(ccmp_wind)一般情况下会包含三个数据集:第一个数据集是uwnd(standard_name = "eastward_wind"),第二个数据集是vwnd(standard_name = "northward_wind"),第三个数据集是nobs或者wspd。前两个数据集是矢量数据,表示此处的风场方向最后一个数据集是标量数据,代表此处的风速。每个数据集中数据的存储又分为四个波段(也可以说是图层),一天的观测时间分为四个时间点,所以有四个图层。

          GDAL库可以提供对nc格式数据的读取,本次数据的读取是在qt+vs2017环境下配置gdal库和netcdf库,环境的配置可以在网上找到,GDAL库的配置可以根据《GDAL源码剖析和开发指南》书中的内容进行编译和配置,配置完成后就可以运行数据,读取nc文件。

           数据读取的代码如下:

头文件:

 1 #ifndef CCMPFILEREAD_H
 2 #define CCMPFILEREAD_H
 3 class ccmpFileRead
 4 {
 5 public:
 6     void ccmpFileRead::fileread(const char*ccmpFilename);
 7 };
 8 
 9 
10 
11 #endif // CCMPFILEREAD_H

源文件:

  1 #include "ccmpfileread.h"
  2 
  3 #include <gdal_priv.h>
  4 #include <vector>
  5 #include <QVector>
  6 
  7 #include <string>
  8 #include <QString>
  9 #include <QStringList>
 10 #include <QDebug>
 11 
 12 #include <fstream>
 13 
 14 using namespace std;
 15 
 16 void ccmpFileRead::fileread(const char *ccmpFilename)
 17 {
 18     vector <string>         vFileSets;
 19     vector <string>         pStrDesc;
 20     vector<vector<float>>   allSSTPixelNum1,allSSTPixelNum2,allSSTPixelNum3;
 21 
 22 
 23     GDALAllRegister();
 24     CPLSetConfigOption("GDAL_FILENAME_IS_UTF8","NO");//中文路径
 25     GDALDataset* fileDataset = (GDALDataset*) GDALOpen(ccmpFilename,GA_ReadOnly);//打开HDF数据集
 26     if (fileDataset == NULL)
 27     {
 28         return;
 29     }
 30 
 31     char** sublist = GDALGetMetadata((GDALDatasetH) fileDataset,"SUBDATASETS");//获得数据的字符串,可以打印出来看看自己需要的数据在那
 32 
 33     int iCount = CSLCount(sublist);
 34     if(iCount <= 0){
 35         qDebug() << "该文件没有子数据" << endl;
 36         GDALClose((GDALDriverH)fileDataset);
 37     }
 38 
 39     //存储数据集信息
 40     for(int i = 0; sublist[i] != NULL;i++)
 41     {
 42 
 43         qDebug() << sublist[i] << endl;
 44 
 45         if(i%2 != 0)
 46         {
 47             continue;
 48         }
 49 
 50         //三个数据集:uwnd vwnd wspd 只读取前两个数据集,第三个数据集是补充数据集
 51 
 52         string tmpstr = sublist[i];
 53         tmpstr = tmpstr.substr(tmpstr.find_first_of("=")+1);
 54         const char *tmpc_str = tmpstr.c_str();
 55 
 56         string tmpdsc = sublist[i+1];
 57         tmpdsc = tmpdsc.substr(tmpdsc.find_first_of("=")+1);
 58 
 59         GDALDataset* hTmpDt = (GDALDataset*)GDALOpen(tmpc_str,GA_ReadOnly);//打开该数据
 60 
 61         if (hTmpDt != NULL)
 62         {
 63             vFileSets.push_back(tmpc_str);
 64         }
 65         if(&pStrDesc != NULL){
 66             pStrDesc.push_back(tmpdsc);
 67         }
 68         GDALClose(hTmpDt);
 69     }
 70 
 71 
 72 //三个数据集分别读取
 73 
 74     qDebug() << "read uwnd ......" << endl;
 75 
 76     QString qtmpdsc1 = QString::fromStdString(pStrDesc[0]);//锁定某一个数据集
 77 
 78     qDebug()<<qtmpdsc1<<endl;
 79 
 80     float *lineData = NULL;
 81     if (qtmpdsc1!=NULL)
 82     {
 83         GDALDataset  *tempDt = (GDALDataset *)GDALOpen(vFileSets[0].data(), GA_ReadOnly);
 84                int BandNum = tempDt->GetRasterCount();
 85 
 86                int panBandmap[1] ={1};
 87                lineData = new float[1 * 200*200];
 88           tempDt->RasterIO(GF_Read,508,112,32,24,lineData,50,50,GDT_Float32,1,panBandmap,0,0,0);
 89 
 90 
 91            for (int iLine = 0; iLine <tempDt->GetRasterYSize(); iLine++)
 92             {
 93                  allSSTPixelNum1.resize(tempDt->GetRasterYSize());
 94             for (int iPixel = 0; iPixel < tempDt->GetRasterXSize(); iPixel++)
 95              {
 96                   allSSTPixelNum1[iLine].resize(tempDt->GetRasterXSize());
 97                   tempDt->RasterIO(GF_Read, 0, iLine, tempDt->GetRasterXSize(), 1,lineData, tempDt->GetRasterXSize(), 1, GDT_Float32, 1, panBandmap,0,0,0);
 98                   allSSTPixelNum1[iLine][iPixel] = lineData[iPixel];
 99                 }
100 
101            }
102            if(lineData)
103              {
104             delete[]lineData;
105             lineData = NULL;
106               }
107 
108            qDebug() << "uwnd read over!" << endl;
109 
110            qDebug() <<"uwnd="<<'\n'<<allSSTPixelNum1[200]<<'\n'<<endl;
111 
112         }
113 
114     //d读取vwnd数据集
115 
116     QString qtmpdsc2 = QString::fromStdString(pStrDesc[2]);
117 
118     if (qtmpdsc2!=NULL)
119     {
120         GDALDataset  *tempDt = (GDALDataset *)GDALOpen(vFileSets[0].data(), GA_ReadOnly);
121                int BandNum = tempDt->GetRasterCount();
122                qDebug()<<BandNum<<endl;
123                int panBandmap[1] ={1};
124                lineData = new float[1 * 200*200];
125           tempDt->RasterIO(GF_Read,508,112,32,24,lineData,50,50,GDT_Float32,1,panBandmap,0,0,0);
126 
127 
128            for (int iLine = 0; iLine <tempDt->GetRasterYSize(); iLine++)
129             {
130                  allSSTPixelNum2.resize(tempDt->GetRasterYSize());
131             for (int iPixel = 0; iPixel < tempDt->GetRasterXSize(); iPixel++)
132              {
133                   allSSTPixelNum2[iLine].resize(tempDt->GetRasterXSize());
134                   tempDt->RasterIO(GF_Read, 0, iLine, tempDt->GetRasterXSize(), 1,lineData, tempDt->GetRasterXSize(), 1, GDT_Float32, 1, panBandmap,0,0,0);
135                   allSSTPixelNum2[iLine][iPixel] = lineData[iPixel];
136                 }
137 
138            }
139            if(lineData)
140              {
141             delete[]lineData;
142             lineData = NULL;
143               }
144 
145            qDebug() << "vwnd read over!" << endl;
146 
147            qDebug() <<"vwnd="<<'\n'<<allSSTPixelNum2[200]<<'\n'<<endl;
148 
149         }
150 
151     //读取wspd数据
152 
153     QString qtmpdsc3 = QString::fromStdString(pStrDesc[2]);
154 
155     if (qtmpdsc3!=NULL)
156     {
157         GDALDataset  *tempDt = (GDALDataset *)GDALOpen(vFileSets[0].data(), GA_ReadOnly);
158                int BandNum = tempDt->GetRasterCount();
159                qDebug()<<BandNum<<endl;
160                int panBandmap[1] ={1};
161                lineData = new float[1 * 200*200];
162           tempDt->RasterIO(GF_Read,508,112,32,24,lineData,50,50,GDT_Float32,1,panBandmap,0,0,0);
163 
164 
165            for (int iLine = 0; iLine <tempDt->GetRasterYSize(); iLine++)
166             {
167                  allSSTPixelNum3.resize(tempDt->GetRasterYSize());
168             for (int iPixel = 0; iPixel < tempDt->GetRasterXSize(); iPixel++)
169              {
170                   allSSTPixelNum3[iLine].resize(tempDt->GetRasterXSize());
171                   tempDt->RasterIO(GF_Read, 0, iLine, tempDt->GetRasterXSize(), 1,lineData, tempDt->GetRasterXSize(), 1, GDT_Float32, 1, panBandmap,0,0,0);
172                   allSSTPixelNum3[iLine][iPixel] = lineData[iPixel];
173                 }
174 
175            }
176 
177            if(lineData)
178              {
179             delete[]lineData;
180             lineData = NULL;
181               }
182 
183            qDebug() << "wspd read over!" << endl;
184 
185            qDebug() <<"wspd="<<'\n'<<allSSTPixelNum3[200]<<'\n'<<endl;
186 
187            GDALClose((GDALDatasetH)tempDt);
188 
189         }
190 
191         GDALClose((GDALDriverH)fileDataset);
192 }

主函数调用:

1 #include <QCoreApplication>
2 #include <ccmpfileread.h>
3 int main(int argc, char *argv[])
4 {
5     QCoreApplication a(argc, argv);
6     ccmpFileRead a1;
7     a1.fileread("E:/odp_workplace/odp_data/testdata/CCMP_Wind_Analysis_198707_V02.0_L3.5_RSS.nc");
8     return a.exec();
9 }

输出结果:

如上图所示数据已经读取并显示成功。

 

转载于:https://www.cnblogs.com/KunZ586/p/10060424.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/278760.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

zune linux_快速提示:在出售Zune HD之前,先擦除所有内容

zune linuxBefore selling your Zune HD online or to another individual, you’ll probably want to erase all of its content. Here we show you how to erase all of the content through the Zune Desktop Player. 在在线上出售Zune HD或将其出售给其他人之前&#xff0c…

镜像VirtualBox 下安装 CentOS 7搭建python项目

一、下载和安装VirtualBox工具 CentOS 镜像 下载地址&#xff08;windows x86&#xff09;&#xff1a;百度网盘 提取码&#xff1a;z44g 安装说明&#xff1a;简书-XiTeacher 二、下载OS辅助工具——putty&#xff0c;mtputty&#xff0c;winscp 下载地址&#xff08;windo…

ios 取消交互_每日新闻摘要:Google披露了iOS“无交互”漏洞

ios 取消交互Google, through its Project Zero initiative, disclosed six vulnerabilities in iOS. In each case, a hacker could execute remote code on someone’s iPhone without any interaction by the user. Apple’s iOS 12.3 fixes five of the issues. 谷歌通过其…

Ubuntu 16.04使用timedatectl进行管理时间(UTC/CST)(服务器/桌面)

说明&#xff1a;16.04开始&#xff0c;systemd接管了系统之后就不再使用/etc/default/rcS和ntpdate、dpkg-reconfigure tzdata进行时间的管理&#xff0c;所以在这些地方设置是无效的&#xff0c;标准的写法是使用timedatectl进行管理。且经过测试hwclock操作硬件BIOS&#xf…

黑客攻防:从入门到入狱_每日新闻摘要:游戏服务黑客被判入狱27个月

黑客攻防:从入门到入狱On Christmas day, 2013, many delighted people opened up new Xbox and Playstation gifts. That excitement turned to disappointment when they were unable to log onto game services and play. Now the hacker responsible will spend 27 months …

如何下载手机的App Store中不再存在的应用程序

Smartphone app stores are well established at this point, and as much as we love to see new apps become available, that also means the inevitable: sometimes apps go away. Here’s what you can do if your favorites disappear. 在这一点上&#xff0c;智能手机应…

Q_learning简介与实例

1、算法思想 QLearning是强化学习算法中value-based的算法&#xff0c;Q即为在某一环境下&#xff0c;Q&#xff08;state,action&#xff09;在某一时刻的 s 状态下(s∈S)&#xff0c;采取 动作a (a∈A)动作能够获得收益的期望&#xff0c;环境会根据agent的动作反馈相应的回…

2-1 gradle安装

因为Gradle是基于JVM的&#xff0c;所以一定要确保本机已经安装了JDK&#xff0c;我们可以通过java -version来验证一下是否已经安装了JDK。 bin目录里面是两个可执行文件&#xff0c;一个是Windows下面的可执行文件&#xff0c;还有一个就是类Unix文件系统的可执行文件。所有的…

Django中session和cookie简单的使用

一、简单的理解 session和cookie是request下的两个对象&#xff0c;操作他们的值就是在操作字典&#xff0c;设置他们的属性就是调用方法。 会话&#xff08;Session&#xff09;跟踪是Web程序中常用的技术&#xff0c;用来跟踪用户的整个会话。Web应用程序是使用HTTP协议传输…

摄影中的曝光补偿是什么?

When you use your camera in some automatic modes like Program—or one of the semi-manual modes like Aperture Priority or Shutter Speed Priority—you don’t give up total control over everything: you can still control the exposure using exposure compensatio…

ajax回调打开新窗体防止浏览器拦截方法

2019独角兽企业重金招聘Python工程师标准>>> 问题剖析&#xff1a; function click_fun(){ window.open("www.baidu.com");//能打开 $.ajax({ url: ${pageContext.request.contextPath}/activity/savePrizes.htm, type: post, dataType: json, data: data…

多点认证wi-fi_准备使用Wi-Fi 6:认证将于2019年第三季度启动

多点认证wi-fiThe Wi-Fi Alliance already announced Wi-Fi 6 back in October. Today, it’s announcing the details of the Wi-Fi 6 certification process, which will launch in the third quarter of 2019. Expect many new Wi-Fi 6 devices later this year. Wi-Fi联盟已…

pdf文档遇到了共享冲突_如何将链接共享为PDF格式的Google文档链接

pdf文档遇到了共享冲突Using Google Docs is a great way to collaborate on and share documents. Sometimes, though, you want to provide somebody with a PDF instead of an editable document. Google Docs now lets you edit your sharing link to provide a PDF. Best …

Visual Studio 2019 preview中体验C# 8.0新语法

准备工作&#xff1a; Visual Studio 2019 Preview版本中并没有包含所有的C# 8.0的新功能&#xff0c;但目前也有一些可以试用了。在开始之前&#xff0c;需要进行入两项设置&#xff1a; 将Framework设置为.net core 3.0 将C#语法设置为8.0 也可以直接编辑.csproj文件&#x…

jQuery 对HTML的操作(二)

文章目录一、jQuery获取、设置HTML标签的内容和属性获得内容 - text()、html() 以及 val()获取属性 - attr()&#xff0c;prop()二、增删 HTML 的内容增加删除三、操作CSSaddClass 添加removeClass 删除toggleClass 切换css 获取与设置所有操作html、css方法参考 ☆四、操作元素…

roku能不能安装软件_如何在Roku中使用Google Assistant

roku能不能安装软件As more of our devices connect to each other, it’s always nice to know that different products from different companies work together. A Chromecast isn’t expensive, but being able to use your TV directly with Google Assistant is better.…

如何使用卡巴斯基急救盘清理感染的PC

When you’re dealing with a PC that is completely infected in viruses, sometimes the best thing to do is reboot into a rescue disk and run a full virus scan from there. Here’s how to use the Kaspersky Rescue Disk to clean an infected PC. 当您要处理一台完全…

jQuery杂项进阶(四)

文章目录一、$ 的替换二、使用JSONP实现跨域三、jQuery 杂项方法、实用工具、回调对象、延迟对象参考 ☆四、jQuery 自身属性参考 ☆五、jQuery 插件介绍和参考 ☆jQuery 树型菜单插件(Treeview)jQuery Validate表单验证&#xff0c;jQuery Password Validation&#xff08;密码…

什么是WLIDSVC.EXE和WLIDSVCM.EXE,它们为什么运行?

You’re no doubt reading this article because you’re wondering what those two processes are doing cluttering up Task Manager, and also wondering why they are in capital letters. You’ve come to the right place. 毫无疑问&#xff0c;您阅读本文是因为您想知道…

linux压缩和解压缩_Linux QuickTip:一步下载和解压缩

linux压缩和解压缩Most of the time, when I download something it’s a file archive of some kind – usually a tarball or a zip file. This could be some source code for an app that isn’t included in Gentoo’s Portage tree, some documentation for an internal …