netcdf库读取nc格式文件中的字符串类型的数据

一、背景

这两天解析数据需要解析nc格式的文件,时间是字符串类型的,最开始还以为这个数据有问题呢,使用panoply打不开该时间格式的文件,于是就确定应该是panoply的支持不好,后来使用HDFView打开了。(正是:踏破铁鞋无觅处,得来全不费工夫!)

二、工具使用

数据使用panoply和HDFView打开的截图如下:
在这里插入图片描述

在这里插入图片描述

三、测试代码

测试代码如下:

/*****************************************ReadNCString.h********************************************************/
/***************************************************
*Copyright(c) 2018 ISmileLi All Rights Reserved,
* Only for personal exchange,
* Don't use for commerical purpose,
* offenders responsibility conceited.
*文 件 名:	ReadNCString.h
*说    明:	analyse nc file
*创建日期:	2018-12-28
*作    者:	ISmileLi
*版    本:	1.0
*****************************************************/

#ifndef READNCSTRING_H
#define READNCSTRING_H
#include <vector>
#include <string>
#include <iostream>
#include "../include/netcdf.h"

using namespace std;
class ReadNCString
{
public:
    ReadNCString();
    ~ReadNCString(){}
    /// 打开文件
    int openFile(const char *path);
    /// 关闭文件
    int closeFile();
    // get  var length
    int getVarDataLen(const char *varName, size_t *&length,int &lengthCount);
    /// 根据varname获得其对应的1维数组string类型数据:例如经纬度、时间
    int getVarDataArray1D(const char *varName, vector<char*> &timeVector);

private:
    int m_ncid;
};

#endif // READNCSTRING_H

/*****************************************ReadNCString.cpp********************************************************/
#include "ReadNCString.h"
#define ERR(e) {printf("Error:%s\n",nc_strerror(e));return 2;}

ReadNCString::ReadNCString()
{

}

//************************************
// 函数名称: openFile
// 函数说明:打开文件
// 返 回 值: int
// 参    数: const char * path
// 作    者:ISmileLi
// 作成日期:2018/12/28
// 修改记录:
//************************************
int ReadNCString::openFile(const char *path)
{
    int ret = 0;
    ret = nc_open(path, NC_WRITE, &m_ncid);
    ERR(ret);
}

//************************************
// 函数名称: closeFile
// 函数说明:关闭文件
// 返 回 值: int
// 作    者:ISmileLi
// 作成日期:2018/12/28
// 修改记录:
//************************************
int ReadNCString::closeFile()
{
    int ret = 0;
    ret = nc_close(m_ncid);
    ERR(ret);
}

//************************************
// 函数名称: getGroupVarDataLen
// 函数说明:get group attribute length
// 返 回 值: int
// 参    数:const char *varName
// 参    数:size_t *&length
// 参    数:int &lengthCount
// 作    者:ISmileLi
// 作成日期:2018/12/28
// 修改记录:
//************************************
int ReadNCString::getVarDataLen(const char *varName, size_t *&length, int &lengthCount)
{
    int ret;
    int dimId;
    if ((ret = nc_inq_varid(m_ncid, varName, &dimId)))
        ERR(ret);

    //获取对应变量的维度
    int dimsCount = 0;
    if (ret = nc_inq_varndims(m_ncid, dimId, &dimsCount))
        ERR(ret);

    lengthCount = dimsCount;

    //获取每一维的长度
    int *dimidsp = new int[dimsCount];
    if(ret = nc_inq_vardimid(m_ncid, dimId, dimidsp))
        ERR(ret);

    length = new size_t[dimsCount];
    for(int i = 0; i < dimsCount; ++i)
    {
        if(ret = nc_inq_dimlen(m_ncid, dimidsp[i], &length[i]))
            ERR(ret);
    }
    ERR(NC_NOERR);
}

//************************************
// 函数名称: getVarDataArray1D
// 函数说明:获得一维数据
// 返 回 值: int
// 参    数: const char *varName
// 参    数: vector<string> &timeVector
// 作    者:ISmileLi
// 作成日期:2018/12/28
// 修改记录:
//************************************
int ReadNCString::getVarDataArray1D(const char *varName, vector<char *> &timeVector)
{
    size_t *length;
    int lengthCount = 0;
    getVarDataLen(varName,length,lengthCount);
    int dataLen = length[0];
    timeVector.resize(dataLen);

    int ret = 0;
    int varid = 0;
    if ((ret = nc_inq_varid(m_ncid, varName, &varid)))
        ERR(ret);

    if ((ret = nc_get_var_string(m_ncid, varid, &timeVector[0])))
        ERR(ret);
    ERR(NC_NOERR);
}

/*****************************************main.cpp********************************************************/

#include <iostream>
#include "ReadNCString.h"
#include <memory>

using namespace std;

int main(int argc, char *argv[])
{
    unique_ptr<ReadNCString> pReadNCFile(new ReadNCString);
    const char *filePath = "/work/CFO_SCA.nc";
    pReadNCFile->openFile(filePath);
    const char *varName = "wvc_row_time";
    vector<char*> timeVector;
    pReadNCFile->getVarDataArray1D(varName,timeVector);
    pReadNCFile->closeFile();
    if(timeVector.empty())
    {
        return -1;
    }

    for(auto iter:timeVector)
    {
        string tmpTime = string(iter);
        cout << "tmpTime: " << tmpTime << endl;
    }
    cout << "welcome to ISmileLi's world!" << endl;
    return 0;
}

四、测试结果

测试结果截图:

在这里插入图片描述

posted @ 2018-12-28 17:48  ISmileLi  阅读(195)  评论(0)    收藏  举报