基于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.pswp.cn/news/278760.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/278760.shtml
英文地址,請注明出處:http://en.pswp.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…