本文整理汇总了C++中NcFile::is_valid方法的典型用法代码示例。如果您正苦于以下问题:C++ NcFile::is_valid方法的具体用法?C++ NcFile::is_valid怎么用?C++ NcFile::is_valid使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类NcFile
的用法示例。
在下文中一共展示了NcFile::is_valid方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: open
bool ReadAstro::open(const char *path)
{
assert(!m_ncfile);
m_ncfile = new NcFile(path, NcFile::ReadOnly);
if (!m_ncfile->is_valid())
{
close();
sendError("failed to open NetCDF file %s", path);
return false;
}
if (m_ncfile->get_format() == NcFile::BadFormat)
{
close();
sendError("bad NetCDF file");
return false;
}
fprintf(stderr, "dims=%d, vars=%d, attrs=%d\n",
m_ncfile->num_dims(), m_ncfile->num_vars(), m_ncfile->num_atts());
for (int i = 0; i < m_ncfile->num_dims(); ++i)
{
fprintf(stderr, "%s: %ld\n",
m_ncfile->get_dim(i)->name(),
m_ncfile->get_dim(i)->size());
}
for (int i = 0; i < m_ncfile->num_vars(); ++i)
{
fprintf(stderr, "%s: dims=%d atts=%d vals=%ld type=%d\n",
m_ncfile->get_var(i)->name(),
m_ncfile->get_var(i)->num_dims(),
m_ncfile->get_var(i)->num_atts(),
m_ncfile->get_var(i)->num_vals(),
m_ncfile->get_var(i)->type());
//int dims = m_ncfile->get_var(i)->num_dims();
NcVar *var = m_ncfile->get_var(i);
for (int j = 0; j < var->num_dims(); ++j)
{
fprintf(stderr, " %s: %ld edge=%ld\n",
var->get_dim(j)->name(),
var->get_dim(j)->size(),
var->edges()[j]);
}
}
for (int i = 0; i < m_ncfile->num_atts(); ++i)
{
fprintf(stderr, "%s\n", m_ncfile->get_att(i)->name());
}
return true;
}
示例2: isValid
bool FileArome::isValid(std::string iFilename) {
bool status = false;
NcFile file = NcFile(iFilename.c_str(), NcFile::ReadOnly);
if(file.is_valid()) {
status = hasDim(file, "time") && hasDim(file, "x") && hasDim(file, "y") &&
!hasDim(file, "ensemble_member") &&
hasVar(file, "latitude") && hasVar(file, "longitude");
}
file.close();
return status;
}
示例3: output
void FlowManager::output(const string &fileName) const
{
NcFile *file;
struct stat statInfo;
int ret = stat(fileName.c_str(), &statInfo);
NcError ncError(NcError::silent_nonfatal);
if (ret != 0 || TimeManager::isFirstStep()) {
file = new NcFile(fileName.c_str(), NcFile::Replace);
} else {
file = new NcFile(fileName.c_str(), NcFile::Write);
}
if (!file->is_valid()) {
char message[100];
sprintf(message, "Failed to open file %s.", fileName.c_str());
REPORT_ERROR(message)
}
示例4: read
const bool read( const std::string filename )
{
if ( m_file ) delete m_file;
m_filename = filename;
m_file = new NcFile( filename.c_str(), NcFile::ReadOnly );
if ( !m_file ) return( false );
if ( !m_file->is_valid() ) return( false );
const int ndims = m_file->num_dims();
for ( int i = 0; i < ndims; i++ )
{
m_dims.push_back( new Dim( m_file->get_dim(i) ) );
}
const int nvars = m_file->num_vars();
for ( int i = 0; i < nvars; i++ )
{
m_vars.push_back( new Var( m_file->get_var(i) ) );
}
return( true );
}
示例5: CopyNcVar
void CopyNcVar(
NcFile & ncIn,
NcFile & ncOut,
const std::string & strVarName,
bool fCopyAttributes,
bool fCopyData
) {
if (!ncIn.is_valid()) {
_EXCEPTIONT("Invalid input file specified");
}
if (!ncOut.is_valid()) {
_EXCEPTIONT("Invalid output file specified");
}
NcVar * var = ncIn.get_var(strVarName.c_str());
if (var == NULL) {
_EXCEPTION1("NetCDF file does not contain variable \"%s\"",
strVarName.c_str());
}
NcVar * varOut;
std::vector<NcDim *> dimOut;
dimOut.resize(var->num_dims());
std::vector<long> counts;
counts.resize(var->num_dims());
long nDataSize = 1;
for (int d = 0; d < var->num_dims(); d++) {
NcDim * dimA = var->get_dim(d);
dimOut[d] = ncOut.get_dim(dimA->name());
if (dimOut[d] == NULL) {
if (dimA->is_unlimited()) {
dimOut[d] = ncOut.add_dim(dimA->name());
} else {
dimOut[d] = ncOut.add_dim(dimA->name(), dimA->size());
}
if (dimOut[d] == NULL) {
_EXCEPTION2("Failed to add dimension \"%s\" (%i) to file",
dimA->name(), dimA->size());
}
}
if (dimOut[d]->size() != dimA->size()) {
if (dimA->is_unlimited() && !dimOut[d]->is_unlimited()) {
_EXCEPTION2("Mismatch between input file dimension \"%s\" and "
"output file dimension (UNLIMITED / %i)",
dimA->name(), dimOut[d]->size());
} else if (!dimA->is_unlimited() && dimOut[d]->is_unlimited()) {
_EXCEPTION2("Mismatch between input file dimension \"%s\" and "
"output file dimension (%i / UNLIMITED)",
dimA->name(), dimA->size());
} else if (!dimA->is_unlimited() && !dimOut[d]->is_unlimited()) {
_EXCEPTION3("Mismatch between input file dimension \"%s\" and "
"output file dimension (%i / %i)",
dimA->name(), dimA->size(), dimOut[d]->size());
}
}
counts[d] = dimA->size();
nDataSize *= counts[d];
}
// ncByte / ncChar type
if ((var->type() == ncByte) || (var->type() == ncChar)) {
DataVector<char> data;
data.Initialize(nDataSize);
varOut =
ncOut.add_var(
var->name(), var->type(),
dimOut.size(), (const NcDim**)&(dimOut[0]));
if (varOut == NULL) {
_EXCEPTION1("Cannot create variable \"%s\"", var->name());
}
var->get(&(data[0]), &(counts[0]));
varOut->put(&(data[0]), &(counts[0]));
}
// ncShort type
if (var->type() == ncShort) {
DataVector<short> data;
data.Initialize(nDataSize);
varOut =
ncOut.add_var(
var->name(), var->type(),
dimOut.size(), (const NcDim**)&(dimOut[0]));
if (varOut == NULL) {
_EXCEPTION1("Cannot create variable \"%s\"", var->name());
}
if (fCopyData) {
var->get(&(data[0]), &(counts[0]));
//.........这里部分代码省略.........
示例6: initFromFile
//
// Reads variable data from dump file
//
bool DataVar::initFromFile(const string& filename, const_DomainChunk_ptr dom)
{
cleanup();
#if ESYS_HAVE_NETCDF
NcError ncerr(NcError::silent_nonfatal);
NcFile* input = new NcFile(filename.c_str());
if (!input->is_valid()) {
cerr << "Could not open input file " << filename << "." << endl;
delete input;
return false;
}
NcDim* dim;
NcAtt* att;
att = input->get_att("type_id");
int typeID = att->as_int(0);
if (typeID != 2) {
cerr << "WARNING: Only expanded data supported!" << endl;
delete input;
return false;
}
att = input->get_att("rank");
rank = att->as_int(0);
dim = input->get_dim("num_data_points_per_sample");
ptsPerSample = dim->size();
att = input->get_att("function_space_type");
funcSpace = att->as_int(0);
centering = dom->getCenteringForFunctionSpace(funcSpace);
dim = input->get_dim("num_samples");
numSamples = dim->size();
#ifdef _DEBUG
cout << varName << ":\t" << numSamples << " samples, "
<< ptsPerSample << " pts/s, rank: " << rank << endl;
#endif
domain = dom;
NodeData_ptr nodes = domain->getMeshForFunctionSpace(funcSpace);
if (nodes == NULL) {
delete input;
return false;
}
meshName = nodes->getName();
siloMeshName = nodes->getFullSiloName();
initialized = true;
size_t dimSize = 1;
vector<long> counts;
if (rank > 0) {
dim = input->get_dim("d0");
int d = dim->size();
shape.push_back(d);
counts.push_back(d);
dimSize *= d;
}
if (rank > 1) {
dim = input->get_dim("d1");
int d = dim->size();
shape.push_back(d);
counts.push_back(d);
dimSize *= d;
}
if (rank > 2) {
cerr << "WARNING: Rank " << rank << " data is not supported!\n";
initialized = false;
}
if (initialized && numSamples > 0) {
sampleID.insert(sampleID.end(), numSamples, 0);
NcVar* var = input->get_var("id");
var->get(&sampleID[0], numSamples);
size_t dataSize = dimSize*numSamples*ptsPerSample;
counts.push_back(ptsPerSample);
counts.push_back(numSamples);
float* tempData = new float[dataSize];
var = input->get_var("data");
var->get(tempData, &counts[0]);
const float* srcPtr = tempData;
for (size_t i=0; i < dimSize; i++, srcPtr++) {
float* c = averageData(srcPtr, dimSize);
dataArray.push_back(c);
}
delete[] tempData;
initialized = reorderSamples();
}
//.........这里部分代码省略.........
示例7: NetCDFProduct
//
// Creates NetCDF product
//
bool NetCDFProduct(MSG_header *PRO_head, MSG_data* PRO_data,
int totalsegs, int *segsindexes,
MSG_header *header, MSG_data *msgdat)
{
struct tm *tmtime;
char NcName[1024];
char reftime[64];
char projname[16];
int wd, hg;
int bpp;
int ncal;
float *cal;
NcVar *ivar;
NcVar *tvar;
NcDim *tdim;
NcDim *ldim;
NcDim *cdim;
NcDim *caldim;
int npix = header[0].image_structure->number_of_columns;
int nlin = header[0].image_structure->number_of_lines;
size_t npixperseg = npix*nlin;
size_t total_size = totalsegs*npixperseg;
MSG_SAMPLE *pixels = new MSG_SAMPLE[total_size];
memset(pixels, 0, total_size*sizeof(MSG_SAMPLE));
size_t pos = 0;
for (int i = 0; i < totalsegs; i ++)
{
if (segsindexes[i] >= 0)
memcpy(pixels+pos, msgdat[segsindexes[i]].image->data,
npixperseg*sizeof(MSG_SAMPLE));
pos += npixperseg;
}
nlin = nlin*totalsegs;
// Manage subarea
if (is_subarea)
{
if (AreaLinStart < 0 ||
AreaLinStart > nlin - AreaNlin ||
AreaNlin > nlin - AreaLinStart)
{
std::cerr << "Wrong Subarea in lines...." << std::endl;
throw;
}
if (AreaPixStart < 0 ||
AreaPixStart > npix - AreaNpix ||
AreaNpix > npix - AreaPixStart)
{
std::cerr << "Wrong Subarea in Pixels...." << std::endl;
throw;
}
size_t newsize = AreaNpix * AreaNlin;
MSG_SAMPLE *newpix = new MSG_SAMPLE[newsize];
memset(newpix, 0, newsize*sizeof(MSG_SAMPLE));
for (int i = 0; i < AreaNlin; i ++)
memcpy(newpix + i * AreaNpix,
pixels + (AreaLinStart + i) * npix + AreaPixStart,
AreaNpix * sizeof(MSG_SAMPLE));
delete [ ] pixels;
pixels = newpix;
total_size = newsize;
}
else
{
AreaNpix = npix;
AreaNlin = nlin;
}
tmtime = PRO_data->prologue->image_acquisition.PlannedAquisitionTime.TrueRepeatCycleStart.get_timestruct( );
t_enum_MSG_spacecraft spc = header[0].segment_id->spacecraft_id;
uint_1 chn = header[0].segment_id->spectral_channel_id;
float sublon = header[0].image_navigation->subsatellite_longitude;
int cfac = header[0].image_navigation->column_scaling_factor;
int lfac = header[0].image_navigation->line_scaling_factor;
int coff = header[0].image_navigation->column_offset;
int loff = header[0].image_navigation->line_offset;
float sh = header[0].image_navigation->satellite_h;
char *channelstring = strdup(MSG_channel_name(spc, chn).c_str( ));
char *channel = chname(channelstring, strlen(channelstring) + 1);
// Build up output NetCDF file name and open it
sprintf( NcName, "%s_%4d%02d%02d_%02d%02d.nc", channel,
tmtime->tm_year + 1900, tmtime->tm_mon + 1, tmtime->tm_mday,
tmtime->tm_hour, tmtime->tm_min );
NcFile ncf ( NcName , NcFile::Replace );
if (! ncf.is_valid()) return false;
// Fill arrays on creation
ncf.set_fill(NcFile::Fill);
// Add Global Attributes
if (! ncf.add_att("Satellite", MSG_spacecraft_name(spc).c_str()))
return false;
//.........这里部分代码省略.........
示例8: size_read
void size_read ( string filename, int *dim, int *vertices, int *edges,
int *triangles, int *quadrilaterals, int *tetrahedrons, int *hexahedrons )
//*****************************************************************************80
//
// Purpose:
//
// SIZE_READ reads ICE sizes from a NETCDF file.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 30 November 2010
//
// Author:
//
// John Burkardt
//
// Reference:
//
// Pascal Frey,
// MEDIT: An interactive mesh visualization software,
// Technical Report RT-0253,
// Institut National de Recherche en Informatique et en Automatique,
// 03 December 2001.
//
// Parameters:
//
// Input, string FILENAME, the name of the file to be read.
// Ordinarily, the name should include the extension ".nc".
//
// Output, int *DIM, the spatial dimension, which should be 2 or 3.
//
// Output, int *VERTICES, the number of vertices.
//
// Output, int *EDGES, the number of edges (may be 0).
//
// Output, int *TRIANGLES, the number of triangles (may be 0).
//
// Output, int *QUADRILATERALS, the number of quadrilaterals (may be 0).
//
// Output, int *TETRAHEDRONS, the number of tetrahedrons (may be 0).
//
// Output, int *HEXAHEDRONS, the number of hexahedrons (may be 0).
//
{
NcDim *dim_dimension;
NcDim *dim_edges;
NcDim *dim_eight;
NcDim *dim_four;
NcDim *dim_hexahedrons;
NcToken dim_name;
int dim_num;
NcDim *dim_quadrilaterals;
NcDim *dim_tetrahedrons;
NcDim *dim_three;
NcDim *dim_triangles;
NcDim *dim_two;
NcDim *dim_vertices;
NcDim *dim_pointer;
int i;
//
// Initialize everything to nothing.
//
*dim = 0;
*vertices = 0;
*edges = 0;
*triangles = 0;
*quadrilaterals = 0;
*tetrahedrons = 0;
*hexahedrons = 0;
//
// Open the file in "read only" mode.
//
NcFile dataFile ( filename.c_str ( ), NcFile::ReadOnly );
if ( !dataFile.is_valid ( ) )
{
cout << "\n";
cout << "SIZE_READ: Fatal error!\n";
cout << " Could not open file.\n";
exit ( 1 );
}
//
// Get the dimension information.
//
// I would much prefer to write "0" as the size of certain dimensions, but I am not
// allowed to, so I simply omit them from the file.
//
// Therefore, when I open the file and try to determine dimensions, some dimensions
// are "missing", which I would have presumed I could discover painlessly by asking
// for pointers to them, and getting NULLs back. But that doesn't seem to work either.
//
// So my bonehead backup is simply to read all the dimensions by index, retrieve
// their names, and see what I find.
//
dim_num = dataFile.num_dims ( );
//.........这里部分代码省略.........
示例9: data_read
void data_read ( string filename, int dim, int vertices, int edges,
int triangles, int quadrilaterals, int tetrahedrons, int hexahedrons,
double vertex_coordinate[], int vertex_label[], int edge_vertex[],
int edge_label[], int triangle_vertex[], int triangle_label[],
int quadrilateral_vertex[], int quadrilateral_label[],
int tetrahedron_vertex[], int tetrahedron_label[], int hexahedron_vertex[],
int hexahedron_label[] )
//****************************************************************************80
//
// Purpose:
//
// DATA_READ reads ICE data from a NETCDF file.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 30 November 2010
//
// Author:
//
// John Burkardt
//
// Reference:
//
// Pascal Frey,
// MEDIT: An interactive mesh visualization software,
// Technical Report RT-0253,
// Institut National de Recherche en Informatique et en Automatique,
// 03 December 2001.
//
// Russ Rew,
// The NetCDF C++ Interface Guide,
// Unidata Program Center, August 2008.
//
// Parameters:
//
// Input, string FILENAME, the name of the file to be read.
// Ordinarily, the name should include the extension ".nc".
//
// Input, int DIM, the spatial dimension, which should be 2 or 3.
//
// Input, int VERTICES, the number of vertices.
//
// Input, int EDGES, the number of edges (may be 0).
//
// Input, int TRIANGLES, the number of triangles (may be 0).
//
// Input, int QUADRILATERALS, the number of quadrilaterals (may be 0).
//
// Input, int TETRAHEDRONS, the number of tetrahedrons (may be 0).
//
// Input, int HEXAHEDRONS, the number of hexahedrons (may be 0).
//
// Output, double VERTEX_COORDINATE[DIM*VERTICES], the coordinates
// of each vertex.
//
// Output, int VERTEX_LABEL[VERTICES], a label for each vertex.
//
// Output, int EDGE_VERTEX[2*EDGES], the vertices that form each edge.
//
// Output, int EDGE_LABEL[EDGES], a label for each edge.
//
// Output, int TRIANGLE_VERTEX[3*TRIANGLES], the vertices that form
// each triangle.
//
// Output, int TRIANGLE_LABEL[TRIANGLES], a label for each triangle.
//
// Output, int QUADRILATERAL_VERTEX[4*QUADRILATERALS], the vertices that
// form each quadrilateral.
//
// Output, int QUADRILATERAL_LABEL[QUADRILATERALS], a label for
// each quadrilateral.
//
// Output, int TETRAHEDRON_VERTEX[4*TETRAHEDRONS], the vertices that
// form each tetrahedron.
//
// Output, int TETRAHEDRON_LABEL[TETRAHEDRONS], a label for
// each tetrahedron.
//
// Output, int HEXAHEDRON_VERTEX[8*HEXAHEDRONS], the vertices that form
// each hexahedron.
//
// Output, int HEXAHEDRON_LABEL[HEXAHEDRONS], a label for each hexahedron.
//
{
//
// Open the file in "read only" mode.
//
NcFile dataFile ( filename.c_str ( ), NcFile::ReadOnly );
if ( !dataFile.is_valid ( ) )
{
cout << "\n";
cout << "DATA_READ: Fatal error!\n";
cout << " Could not open file.\n";
exit ( 1 );
//.........这里部分代码省略.........
示例10: ice_write
//.........这里部分代码省略.........
// each hexahedron.
//
// Input, int HEXAHEDRON_LABEL[HEXAHEDRONS], a label for each hexahedron.
//
{
NcDim *dim_dimension;
NcDim *dim_edges;
NcDim *dim_eight;
NcDim *dim_four;
NcDim *dim_hexahedrons;
NcDim *dim_quadrilaterals;
NcDim *dim_tetrahedrons;
NcDim *dim_three;
NcDim *dim_triangles;
NcDim *dim_two;
NcDim *dim_vertices;
NcVar *var_edge_vertex;
NcVar *var_edge_label;
NcVar *var_hexahedron_vertex;
NcVar *var_hexahedron_label;
NcVar *var_quadrilateral_vertex;
NcVar *var_quadrilateral_label;
NcVar *var_tetrahedron_vertex;
NcVar *var_tetrahedron_label;
NcVar *var_triangle_vertex;
NcVar *var_triangle_label;
NcVar *var_vertex_coordinate;
NcVar *var_vertex_label;
//
// Create the file.
//
NcFile dataFile ( filename.c_str ( ), NcFile::Replace );
if ( !dataFile.is_valid ( ) )
{
cout << "\n";
cout << "ICE_WRITE - Fatal error!\n";
cout << " Could not open the file.\n";
exit ( 1 );
}
//
// Dimension information.
//
dim_dimension = dataFile.add_dim ( "Dimension", dim );
dim_vertices = dataFile.add_dim ( "Vertices", vertices );
if ( 0 < edges )
{
dim_edges = dataFile.add_dim ( "Edges", edges );
}
if ( 0 < triangles )
{
dim_triangles = dataFile.add_dim ( "Triangles", triangles );
}
if ( 0 < quadrilaterals )
{
dim_quadrilaterals = dataFile.add_dim ( "Quadrilaterals", quadrilaterals );
}
if ( 0 < tetrahedrons )
{
dim_tetrahedrons = dataFile.add_dim ( "Tetrahedrons", tetrahedrons );
}