이 내 첫 번째 질문입니다 읽는 방법 : I합니다 (C++ 레거시 API를 사용) netCDF의 파일에서 "전역 속성"을 읽으려고하고C에서 netCDF의 "전역 속성"++
합니다. "글로벌 속성"이란 NcVar가 아닌 NcFile에 추가 된 속성을 의미합니다.
대부분의 경우 "Example netCDF programs"이 유용하지만 "전역 속성"에 대한 예는 없습니다. friend class NcFile;
: NcAtt* get_att(NcToken) const;
- NcFile이 멤버 함수를 가지고 다음은 "netcdfcpp.h"나는 몇 가지를 찾을 컨설팅
- NcAtt에는 전용 생성자가 있습니다.
NcAtt(NcFile*, NcToken);
- NcAtt에는 공개 멤버 함수가 있습니다.
NcValues* values(void) const;
- NcValues는
내 코딩 기술은 내가 NcFile 내 NcAtt 클래스에서 다시 NcValue로 저장 문자열/INT/플로트에서 얻을 방법을 이해하기에 충분하다 ncvalues.h
헤더를 통해 정의 된 API가 있습니다.
"LoadNetCDF"기능의 구현에서 중요한 부분이 누락 된 "NetCDF_test.cpp"문제의 예제 코드가 첨부되어 있습니다. (편집 : 또한, "TestFile.nc는"제대로 만든)
g++ -c NetCDF_test.cpp -o NetCDF_test.o
이
g++ -o NCTEST NetCDF_test.o -lnetcdf_c++ -lnetcdf
예제 코드 :
#include <iostream> // provides screen output (i.e. std::cout<<)
#include <netcdfcpp.h>
struct MyStructure {
std::string MyString;
int MyInt;
float MyFloat;
MyStructure(); // default constructor
int SaveNetCDF(std::string); // Save the struct content to "global attributes" in NetCDF
int LoadNetCDF(std::string); // Load the struct content from "global attributes" in NetCDF
};
MyStructure::MyStructure(void)
{
MyString = "TestString";
MyInt = 123;
MyFloat = 1.23;
}
int MyStructure::SaveNetCDF(std::string OUTPUT_FILENAME)
{
NcError err(NcError::silent_nonfatal);
static const int NC_ERR = 2;
NcFile NetCDF_File(OUTPUT_FILENAME.c_str(), NcFile::Replace);
if(!NetCDF_File.is_valid()) {return NC_ERR;}
if(!(NetCDF_File.add_att("MyString",MyString.c_str()))) {return NC_ERR;}
if(!(NetCDF_File.add_att("MyInt",MyInt))) {return NC_ERR;}
if(!(NetCDF_File.add_att("MyFloat",MyFloat))) {return NC_ERR;}
return 0;
}
int MyStructure::LoadNetCDF(std::string INPUT_FILENAME)
{
NcError err(NcError::silent_nonfatal);
static const int NC_ERR = 2;
NcFile NetCDF_File(INPUT_FILENAME.c_str(), NcFile::ReadOnly);
if(!NetCDF_File.is_valid()) {return NC_ERR;}
// ???? This is where I am stuck.
// How do I read the global attribute from the NetCDF_File ??
return 0;
}
int main()
{
std::cout<< "START OF TEST.\n";
MyStructure StructureInstance; // datamembers initialized by constructor
StructureInstance.SaveNetCDF("TestFile.nc");
StructureInstance.MyString = "Change string for sake of testing";
StructureInstance.MyInt = -987;
StructureInstance.MyFloat = -9.87;
StructureInstance.LoadNetCDF("TestFile.nc"); // data members are supposed to be read from file
std::cout<< "Now the data members of StructureInstance should be TestString, 123, and 1.23\n";
std::cout<< StructureInstance.MyString << " ; " << StructureInstance.MyInt << " ; " << StructureInstance.MyFloat <<"\n";
std::cout<< "END OF TEST.\n";
}
'(* NetCDF_File) .as_int (0)'을'NetCDF_File-> as_int (0)'으로 대체해서는 안됩니까? – DopplerShift
이것은 NcVar와 관련된 속성을 읽는 방법입니다.하지만 연산자가 분명히 구현되지 않았으므로 "전역 속성"에서는 작동하지 않습니다. "->"연산자를 사용하여 컴파일 할 때 다음 오류가 발생합니다 :'error : '->'의 기본 피연산자가 포인터 타입이 아닌 'NcFile'' – Mathis
Ooops, 그 :'NetCDF_File.get_att ("MyInt") -> as_int (0)'... 아니면 실패합니까? – DopplerShift