コード例 #1
0
ファイル: netcdf_mpas.cpp プロジェクト: denyslf/jburkardt-cpp
int netcdf_mpas_read_nvertices ( string filename )

//****************************************************************************80
//
//  Purpose:
//
//    NETCDF_MPAS_READ_NVERTICES gets the number of vertices.
//
//  Licensing:
//
//    This code is distributed under the GNU LGPL license.
//
//  Modified:
//
//    30 December 2010
//
//  Author:
//
//    John Burkardt
//
//  Reference:
//
//    Russ Rew, Glenn Davis, Steve Emmerson, Harvey Davies, Ed Hartne,
//    The NETCDF User's Guide,
//    Unidata Program Center, March 2009.
//
//  Parameters:
//
//    Input, string NC_FILENAME, the name of the NETCDF file to examine.
//
//    Output, int NETCDF_MPAS_READ_NVERTICES, the value of NVERTICES.
//
{
  int nvertices;
  long int nvertices_size;
  NcDim *nvertices_id;
//
//  Open the file.
//
  NcFile ncid ( filename.c_str ( ), NcFile::ReadOnly );
//
//  Get NCELLS, which is a NETCDF dimension.
//
  nvertices_id = ncid.get_dim ( "nVertices" );

  nvertices_size = (*nvertices_id).size ( );
//
//  Close the file.
//
  ncid.close ( );

  nvertices = ( int ) nvertices_size;

  return nvertices;
}
コード例 #2
0
ファイル: netcdf_mpas.cpp プロジェクト: denyslf/jburkardt-cpp
int netcdf_mpas_read_maxedges ( string filename )

//****************************************************************************80
//
//  Purpose:
//
//    NETCDF_MPAS_READ_MAXEDGES gets MAXEDGES.
//
//  Licensing:
//
//    This code is distributed under the GNU LGPL license.
//
//  Modified:
//
//    01 January 2011
//
//  Author:
//
//    John Burkardt
//
//  Reference:
//
//    Russ Rew, Glenn Davis, Steve Emmerson, Harvey Davies, Ed Hartne,
//    The NETCDF User's Guide,
//    Unidata Program Center, March 2009.
//
//  Parameters:
//
//    Input, string NC_FILENAME, the name of the NETCDF file to examine.
//
//    Output, int NETCDF_MPAS_READ_MAXEDGES, the value of MAXEDGES.
//
{
  int maxedges;
  long int maxedges_size;
  NcDim *maxedges_id;
//
//  Open the file.
//
  NcFile ncid ( filename.c_str ( ), NcFile::ReadOnly );
//
//  Get MAXEDGES, which is a NETCDF dimension.
//
  maxedges_id = ncid.get_dim ( "maxEdges" );

  maxedges_size = (*maxedges_id).size ( );
//
//  Close the file.
//
  ncid.close ( );

  maxedges = ( int ) maxedges_size;

  return maxedges;
}
コード例 #3
0
ファイル: ReadMeteoCosmo.cpp プロジェクト: nixz/covise
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;
}
コード例 #4
0
ファイル: NetcdfDataset.hpp プロジェクト: tpajenka/RNNLIB
static int load_nc_dim(const NcFile& ncf, const string& name, bool required = true)
{
	NcDim* d = 0;
	try
	{
		d = ncf.get_dim(name.c_str());
	}
	catch(char* str)
	{
		check(!required, string(str) + "\ndimension " + name + " not found in netcdf file");
	}
	int size = d ? d->size() : 0;
	return size;
}
コード例 #5
0
ファイル: main.cpp プロジェクト: zkbreeze/OceanVis
    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 );
    }
コード例 #6
0
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]));
			varOut->put(&(data[0]), &(counts[0]));
		}
	}

	// ncInt type
	if (var->type() == ncInt) {
		DataVector<int> 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]));
			varOut->put(&(data[0]), &(counts[0]));
		}
	}

	// ncFloat type
	if (var->type() == ncFloat) {
		DataVector<float> 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]));
			varOut->put(&(data[0]), &(counts[0]));
		}
	}

	// ncDouble type
	if (var->type() == ncDouble) {
		DataVector<double> 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]));
			varOut->put(&(data[0]), &(counts[0]));
		}
	}

	// ncInt64 type
	if (var->type() == ncInt64) {
		DataVector<ncint64> 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]));
			varOut->put(&(data[0]), &(counts[0]));
		}
	}

	// Check output variable exists
	if (varOut == NULL) {
		_EXCEPTION1("Unable to create output variable \"%s\"",
			var->name());
	}

	// Copy attributes
	if (fCopyAttributes) {
		CopyNcVarAttributes(var, varOut);
	}
}
コード例 #7
0
ファイル: DataVar.cpp プロジェクト: svn2github/Escript
//
// 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();
    }

    delete input;
#endif // ESYS_HAVE_NETCDF

    return initialized;
}
コード例 #8
0
ファイル: netcdf_mpas.cpp プロジェクト: denyslf/jburkardt-cpp
void netcdf_mpas_report ( string filename )

//****************************************************************************80
//
//  Purpose:
//
//    NETCDF_MPAS_REPORT reads an MPAS NETCDF grid file and reports.
//
//  Discussion:
//
//    In this example, we want to extract all the information from a file
//    of unknown type.
//
//    Here, we are pretending we have no idea what's in the file, so we
//    have to go step by step, making inquiries to NETCDF.
//
//    As we go, we print out what we have discovered.  We don't attempt
//    to return any of the data.
//
//  Licensing:
//
//    This code is distributed under the GNU LGPL license.
//
//  Modified:
//
//    29 December 2010
//
//  Author:
//
//    John Burkardt
//
//  Reference:
//
//    Russ Rew, Glenn Davis, Steve Emmerson, Harvey Davies, Ed Hartne,
//    The NETCDF User's Guide,
//    Unidata Program Center, March 2009.
//
//  Parameters:
//
//    Input, string FILENAME, the name of the NETCDF file to examine.
//
{
  NcAtt *att;
  NcDim *dim;
  int i;
  int j;
  long int len;

  NcToken name;
  int num_atts;
  int num_dims;
  int num_vars;
  NcType type;
  NcDim *unlimdimid;
  NcVar *var;

  cout << "\n";
  cout << "NETCDF_MPAS_REPORT:\n";
  cout << "  Report the information stored in a NETCDF\n";
  cout << "  file.  Although we wish to examine a file containing\n";
  cout << "  grid data generated by MPAS, we will assume we do not\n";
  cout << "  have any idea of what is in the file.  So we just read,\n";
  cout << "  inquire, and print out.\n";

  cout << "\n";
  cout << "  The name of the file is \"" << filename << "\"\n";
//
//  Open the file.
//
  NcFile ncid ( filename.c_str ( ), NcFile::ReadOnly );
//
//  Return information about the NETCDF file.
//
  num_dims = ncid.num_dims ( );
  num_vars = ncid.num_vars ( );
  num_atts = ncid.num_atts ( );
  unlimdimid = ncid.rec_dim ( );

  cout << "\n";
  cout << "PRIMARY PARAMETERS:\n";
  cout << "\n";
  cout << "  The number of dimensions         NUM_DIMS   = " << num_dims << "\n";
  cout << "  The number of variables          NUM_VARS   = " << num_vars << "\n";
  cout << "  The number of global attributes  NUM_ATTS   = " << num_atts << "\n";
  cout << "  The unlimited dimension (if any) UNLIMDIMID = \"" << (*unlimdimid).name ( ) << "\"\n";
//
//  Retrieve global attributes.
//  First, we must evaluate the constant "NC_GLOBAL".
//
//  nc_global = netcdf.getConstant ( 'NC_GLOBAL' );

  cout << "\n";
  cout << "GLOBAL ATTRIBUTES:\n";
  cout << " Att  --------Name--------  Type   Len\n";
  cout << "\n";
  for ( i = 0; i < num_atts; i++ )
  {
    att = ncid.get_att ( i );
    name = (*att).name ( );
    type = (*att).type ( );
    len = (*att).num_vals ( );
    cout << "  " << setw(2) << i
         << "  \"" << setw(18) << name << "\""
         << "  " << setw(4) << type
         << "  " << setw(4) << len << "\n";
  }
//
//  Determine names and extents of dimensions.
//  Since each NAME is a char array, we make a cell array to hold them.
//
  cout << "\n";
  cout << "DIMENSIONS:\n";
  cout << " Dim  --------Name--------  Extent\n";
  cout << "\n";

  for ( i = 0; i < num_dims; i++ )
  {
    dim = ncid.get_dim ( i );
    name = (*dim).name ( );
    len = (*dim).size ( );
    cout << "  " << setw(2) << i
         << "  \"" << setw(18) << name << "\""
         << "  " << setw(6) << len << "\n";
  }
//
//  Get variable names, types, dimensions, number of attributes.
//
  cout << "\n";
  cout << "VARIABLES:\n";
  cout << " Var  --------Name--------  Type Natts Ndims  Dims\n";
  cout << "\n";

  for ( i = 0; i < num_vars; i++ )
  {
    var = ncid.get_var ( i );
    name = (*var).name ( );
    type = (*var).type ( );
    num_dims = (*var).num_dims ( );
    num_atts = (*var).num_atts ( );
    cout << "  " << setw(2) << i
         << "  \"" << setw(18) << name << "\""
         << "  " << setw(4) << type
         << "  " << setw(4) << num_atts
         << "  " << setw(4) << num_dims
         << "  ";
    for ( j = 0; j < num_dims; j++ )
    {
      dim = (*var).get_dim ( j );
      if ( j == 0 )
      {
        cout << "[";
      }
      cout << (*dim).name ( );
      if ( j < num_dims - 1 )
      {
        cout <<",";
      }
      else
      {
        cout << "]";
      }
    }
    cout << "\n";
  }
//
//  Close the file.
//
  ncid.close ( );

  return;
}
コード例 #9
0
ファイル: ice_to_mesh.cpp プロジェクト: denyslf/jburkardt-cpp
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 ( );

  for ( i = 0; i < dim_num; i++ )
  {
    dim_pointer = dataFile.get_dim ( i );
    dim_name = dim_pointer->name ( );

    if ( !strcmp ( dim_name, "Dimension" ) )
    {
      *dim = dim_pointer->size ( );
    }
    else if ( !strcmp ( dim_name, "Vertices" ) )
    {
      *vertices = dim_pointer->size ( );
    }
    else if ( !strcmp ( dim_name, "Edges" ) )
    {
      *edges = dim_pointer->size ( );
    }
    else if ( !strcmp ( dim_name, "Triangles" ) )
    {
      *triangles = dim_pointer->size ( );
    }
    else if ( !strcmp ( dim_name, "Quadrilaterals" ) )
    {
      *quadrilaterals = dim_pointer->size ( );
    }
    else if ( !strcmp ( dim_name, "Tetrahedrons" ) )
    {
      *tetrahedrons = dim_pointer->size ( );
    }
    else if ( !strcmp ( dim_name, "Hexahedrons" ) )
    {
      *hexahedrons = dim_pointer->size ( );
    }
    else
    {
      cout << "  Ignoring information about dimension \"" << dim_name << "\".\n";
    }
  }
//
//  Close the file.
//
  dataFile.close ( );

  return;
}