基于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…

SSM框架——使用MyBatis Generator自动创建代码

SSM框架——使用MyBatis Generator自动创建代码 这是通过命令行, 不用ide插件. 若在IDEA中通过插件generator, 还可以参考另一篇: IDEA搭建SpringSpringMVCmybatis框架教程转载于:https://www.cnblogs.com/yadongliang/p/8097449.html

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

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

Python基础七(函数)

函数概述 函数&#xff1a;组织好的、可重复使用的。杉树能提高应用的模块性和代码的重复利用性。Python提供了很多的内置函数&#xff0c;比如len()等等&#xff0c;可以自行定义函数。 函数的定义 def 函数名&#xff08;参数列表&#xff09;&#xff1a; #函数定义 函数体…

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…

让你的div可拖动(手机端)

电脑端引入 jQuery UI 可以实现。而手机并没有 mousemove 等事件&#xff0c;所以这里采用手机事件&#xff1a;touchstart 和 touchmove 实现拖拽。 一、引入&#xff1a; 只要引入 jQuery.js 和 dragger.js&#xff08;如下&#xff09;即可 注&#xff1a;实现拖拽部分转…

5-4 全局变量

1、函数内部使用全局变量时&#xff0c;需要申明global 1 name 小明 # 定义一个全局变量name,并给它赋值小明2 stus [] # 定义一个空list3 # list、字典、集合4 5 def a():6 # 字符串、int、float、元组 需要声明global7 global name # 函数内部使用局部变量时&a…

黑客攻防:从入门到入狱_每日新闻摘要:游戏服务黑客被判入狱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 …

Self Introduction

名流时尚服饰 dior 夏季 男装 男士t恤衬衫卫衣休闲裤牛仔裤英伦 socool 搜酷女包◆任选两款正价包包邮◆5周年店庆◆5折疯抢 紫紫 超人气包邮特价创意家居收纳压缩袋饰品服饰配件包包 socool 搜酷女包◆任选两款正价包包邮◆5周年店庆◆5折疯抢 dior 风格 CF Homme 男装 男士t恤…

爬虫notes

‘’’ 爬取思路&#xff1a; 1、requests&#xff08;url&#xff09; 2、requests json 3、requests XPath 4、requests BeautifulSoup 5、selenium 6、scrapy框架 7、scrapy-redis 及分布式 OS&#xff1a; import os os.system(“C: && p.txt”) os.system(“p…

Android 电量优化

Android系统上App的电量消耗主要由cpu、wakelock、数据传输&#xff08;流量和wifi&#xff09;、wifi运行、gps、other senior组成&#xff0c;而耗电异常也是由于这几个模块的使用不当。 BroaddcastReceiver 为了减少应用损耗的电量,代码中需要尽量避免无用的操作代码的执行 …

如何下载手机的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的动作反馈相应的回…

吴颖二:12.27 午评 地缘政治一波未平一波又起,千三可到?

前言&#xff1a;圣诞节后首个交易日&#xff0c;金银油均走出了大行情。美国因导弹项目制裁朝鲜两名官员&#xff0c;地缘局势再升温黄金本周能否突破1300关口&#xff1f;油价刷新两年半高位后&#xff0c;一个重要指标却已暗示短期内或面临风险…… 朝鲜局势进一步恶化的同时…

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…

ES6学习--对象属性的遍历

ES6一共有5种方法可以遍历对象的属性。 &#xff08;1&#xff09;for...in for...in循环遍历对象自身的和继承的可枚举属性&#xff08;不含Symbol属性&#xff09;。 &#xff08;2&#xff09;Object.keys(obj) Object.keys返回一个数组&#xff0c;包括对象自身的&#xff…