コード例 #1
0
ファイル: netcdf_mpas.cpp プロジェクト: denyslf/jburkardt-cpp
void netcdf_mpas_read_xyzcell ( string filename, int ncells, double xcell[],
  double ycell[], double zcell[] )

//****************************************************************************80
//
//  Purpose:
//
//    NETCDF_MPAS_READ_XYZCELL reads xCell, yCell, zCell.
//
//  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 NC_FILENAME, the name of the NETCDF file to examine.
//
//    Input, int NCELLS, the number of nodes.
//
//    Output, double XCELL[NCELLS], YCELL[NCELLS], ZCELL[NCELLS], the
//    coordinates of the nodes.
//
{
  NcVar *var_id;
//
//  Open the file.
//
  NcFile ncid ( filename.c_str ( ), NcFile::ReadOnly );
//
//  Get the variable values.
//
  var_id = ncid.get_var ( "xCell" );
  (*var_id).get ( &xcell[0], ncells );
  var_id = ncid.get_var ( "yCell" );
  (*var_id).get ( &ycell[0], ncells );
  var_id = ncid.get_var ( "zCell" );
  (*var_id).get ( &zcell[0], ncells );
//
//  Close the file.
//
  ncid.close ( );

  return;
}
コード例 #2
0
ファイル: netcdf_mpas.cpp プロジェクト: denyslf/jburkardt-cpp
void netcdf_mpas_read_xyzvertex ( string filename, int nvertices, 
  double xvertex[], double yvertex[], double zvertex[] )

//****************************************************************************80
//
//  Purpose:
//
//    NETCDF_MPAS_READ_CELLS gets the cell center coordinates.
//
//  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.
//
//    Input, int NVERTICES, the number of vertices.
//
//    Output, double XVERTEX[NVERTICES], YVERTEXL[NVERTICES], 
//    ZVERTEX[NVERTICES], the coordinates of the nodes.
//
{
  NcVar *var_id;
//
//  Open the file.
//
  NcFile ncid ( filename.c_str ( ), NcFile::ReadOnly );
//
//  Get the variable values.
//
  var_id = ncid.get_var ( "xVertex" );
  (*var_id).get ( &xvertex[0], nvertices );
  var_id = ncid.get_var ( "yVertex" );
  (*var_id).get ( &yvertex[0], nvertices );
  var_id = ncid.get_var ( "zVertex" );
  (*var_id).get ( &zvertex[0], nvertices );
//
//  Close the file.
//
  ncid.close ( );

  return;
}
コード例 #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
ファイル: ncutil.hpp プロジェクト: seifer08ms/icebin
std::vector<T> read_vector(NcFile &nc, std::string const &var_name)
{
	// Read points vector
	NcVar *vpoints = nc.get_var(var_name.c_str());
	long npoints = vpoints->get_dim(0)->size();
	std::vector<T> points(npoints);
	vpoints->get(&points[0], npoints);
	return points;
}
コード例 #5
0
ファイル: netcdf_mpas.cpp プロジェクト: denyslf/jburkardt-cpp
void netcdf_mpas_read_verticesoncell ( string filename, int maxedges,
  int ncells, int verticesoncell[] )

//****************************************************************************80
//
//  Purpose:
//
//    NETCDF_MPAS_READ_VERTICESONCELLS gets verticesOnCells.
//
//  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.
//
//    Input, int MAXEDGES, the maximum number of edges for a cell.
//
//    Input, int NCELLS, the number of cells.
//
//    Output, int VERTICESONCELLS[MAXEDGES*NCELLS];
//
{
  NcVar *var_id;
//
//  Open the file.
//
  NcFile ncid ( filename.c_str ( ), NcFile::ReadOnly );
//
//  Get the variable values.
//
  var_id = ncid.get_var ( "verticesOnCell" );
  (*var_id).get ( &verticesoncell[0], ncells, maxedges );
//
//  Close the file.
//
  ncid.close ( );

  return;
}
コード例 #6
0
ファイル: netcdf_mpas.cpp プロジェクト: denyslf/jburkardt-cpp
void netcdf_mpas_read_cellsonvertex ( string filename, int nvertices,
  int cellsonvertex[] )

//****************************************************************************80
//
//  Purpose:
//
//    NETCDF_MPAS_READ_CELLSONVERTEX gets the cellsOnVertex information.
//
//  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.
//
//    Input, int NEDGES, the number of edges.
//
//    Output, int CELLSONVERTEX[3*NVERTICES];
//
{
  NcVar *var_id;
//
//  Open the file.
//
  NcFile ncid ( filename.c_str ( ), NcFile::ReadOnly );
//
//  Get the variable values.
//
  var_id = ncid.get_var ( "cellsOnVertex" );
  (*var_id).get ( &cellsonvertex[0], nvertices, 3 );
//
//  Close the file.
//
  ncid.close ( );

  return;
}
コード例 #7
0
ファイル: Grid_LonLat.cpp プロジェクト: ckhroulev/glint2
void Grid_LonLat::read_from_netcdf(NcFile &nc, std::string const &vname)
{
	Grid::read_from_netcdf(nc, vname);

	NcVar *info_var = nc.get_var((vname + ".info").c_str());
	north_pole = (giss::get_att(info_var, "north_pole_cap")->as_int(0) != 0);
	south_pole = (giss::get_att(info_var, "south_pole_cap")->as_int(0) != 0);
	points_in_side = giss::get_att(info_var, "points_in_side")->as_int(0);
//	_nlon = giss::get_att(info_var, "nlon")->as_int(0);
//	_nlat = giss::get_att(info_var, "nlat")->as_int(0);

	lonb = giss::read_double_vector(nc, vname + ".lon_boundaries");
	latb = giss::read_double_vector(nc, vname + ".lat_boundaries");
}
コード例 #8
0
void CopyNcVarIfExists(
	NcFile & ncIn,
	NcFile & ncOut,
	const std::string & strVarName,
	bool fCopyAttributes,
	bool fCopyData
) {
	// Turn off fatal errors in NetCDF
	NcError error(NcError::silent_nonfatal);

	NcVar * var = ncIn.get_var(strVarName.c_str());
	if (var != NULL) {
		CopyNcVar(ncIn, ncOut, strVarName, fCopyAttributes, fCopyData);
	}
}
コード例 #9
0
ファイル: ncutil.hpp プロジェクト: seifer08ms/icebin
boost::function<void ()> netcdf_define(
	NcFile &nc,
	std::string const &vname_prefix,	// vname = vname_prefix + vmeta.name
	VarMetaData const &vmeta,
	blitz::Array<T,rank> const &val,
	std::vector<NcDim *> const &ddims = {})
{
	std::string vname = vname_prefix + vmeta.get_name();
	auto ret(netcdf_define(nc, vname, val, ddims));
	NcVar *nc_var = nc.get_var(vname.c_str());

	std::string const &description(vmeta.get_description());
	if (description != "") nc_var->add_att("long_name", description.c_str());

	std::string const &units(vmeta.get_units());
	if (units != "") nc_var->add_att("units", units.c_str());

	return ret;
}
コード例 #10
0
void TrajectoryForecaster::readTime(const NcFile &data )
{

  float *year = read1DimVar(data, "year");
  float *month = read1DimVar(data, "month");
  float *day = read1DimVar(data, "day");
  float *hour = read1DimVar(data, "hour");

  int n = data.get_var("year")->num_vals();

  for ( int i=0; i<n; i++ )
  {
    time.push_back( ptime( boost::gregorian::date(year[i],month[i],day[i]), boost::posix_time::hours(hour[i])) );
  }

  delete [] year;
  delete [] month;
  delete [] day;
  delete [] hour;
}
コード例 #11
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 );
    }
コード例 #12
0
ファイル: Grid_LonLat.cpp プロジェクト: ckhroulev/glint2
boost::function<void ()> Grid_LonLat::netcdf_define(NcFile &nc, std::string const &vname) const
{
	auto parent = Grid::netcdf_define(nc, vname);

	NcDim *lonbDim = nc.add_dim((vname + ".lon_boundaries.length").c_str(),
		this->lonb.size());
	NcVar *lonb_var = nc.add_var((vname + ".lon_boundaries").c_str(),
		ncDouble, lonbDim);

	NcDim *latbDim = nc.add_dim((vname + ".lat_boundaries.length").c_str(),
		this->latb.size());
	NcVar *latb_var = nc.add_var((vname + ".lat_boundaries").c_str(),
		ncDouble, latbDim);

	NcVar *info_var = nc.get_var((vname + ".info").c_str());
	info_var->add_att("north_pole_cap", north_pole ? 1 : 0);
	info_var->add_att("south_pole_cap", south_pole ? 1 : 0);
	info_var->add_att("points_in_side", points_in_side);
	info_var->add_att("nlon", nlon());
	info_var->add_att("nlat", nlat());

	return boost::bind(&Grid_LonLat_netcdf_write, parent, &nc, this, vname);
}
コード例 #13
0
ファイル: ncutil.hpp プロジェクト: seifer08ms/icebin
blitz::Array<T,rank> read_blitz(NcFile &nc, std::string const &var_name)
{
	// Read points vector
	NcVar *vpoints = nc.get_var(var_name.c_str());
	int ndims = vpoints->num_dims();
	if (ndims != rank) {
		fprintf(stderr, "NetCDF variable %s has rank %d, expected rank %d\n",
			var_name.c_str(), ndims, rank);
		throw std::exception();
	}

	blitz::TinyVector<int,rank> shape(0);
	long counts[rank];
	for (int i=0; i<rank; ++i) {
		shape[i] = vpoints->get_dim(i)->size();
printf("read_blitz: shape[%d] = %d\n", i, shape[i]);
		counts[i] = shape[i];
	}

	blitz::Array<T,rank> ret(shape);
for (int i=0; i<rank; ++i) printf("read_blitz: ret.extent(%d) = %d\n", i, ret.extent(i));
	vpoints->get(ret.data(), counts);
	return ret;
}
コード例 #14
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;
}
コード例 #15
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);
	}
}
コード例 #16
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;
}
コード例 #17
0
ファイル: ice_to_mesh.cpp プロジェクト: denyslf/jburkardt-cpp
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 );
  }
//
//  Vertices.
//
  NcVar *var_vertex_coordinate = dataFile.get_var ( "Vertex_Coordinate" );
  var_vertex_coordinate->get ( &vertex_coordinate[0], dim, vertices );

  NcVar *var_vertex_label = dataFile.get_var ( "Vertex_Label" );
  var_vertex_label->get ( &vertex_label[0], vertices );
//
//  Edges.
//
  if ( 0 < edges )
  {
    NcVar *var_edge_vertex = dataFile.get_var ( "Edge_Vertex" );
    var_edge_vertex->get ( &edge_vertex[0], 2, edges );

    NcVar *var_edge_label = dataFile.get_var ( "Edge_Label" );
    var_edge_label->get ( &edge_label[0], edges );
  }
//
//  Triangles.
//
  if ( 0 < triangles )
  {
    NcVar *var_triangle_vertex = dataFile.get_var ( "Triangle_Vertex" );
    var_triangle_vertex->get ( &triangle_vertex[0], 3, triangles );

    NcVar *var_triangle_label = dataFile.get_var ( "Triangle_Label" );
    var_triangle_label->get ( &triangle_label[0], triangles );
  }
//
//  Quadrilaterals.
//
  if ( 0 < quadrilaterals )
  {
    NcVar *var_quadrilateral_vertex = dataFile.get_var ( "Quadrilateral_Vertex" );
    var_quadrilateral_vertex->get ( &quadrilateral_vertex[0], 4, quadrilaterals );

    NcVar *var_quadrilateral_label = dataFile.get_var ( "Quadrilateral_Label" );
    var_quadrilateral_label->get ( &quadrilateral_label[0], quadrilaterals );
  }
//
//  Tetrahedrons.
//
  if ( 0 < tetrahedrons )
  {
    NcVar *var_tetrahedron_vertex = dataFile.get_var ( "Tetrahedron_Vertex" );
    var_tetrahedron_vertex->get ( &tetrahedron_vertex[0], 4, tetrahedrons );

    NcVar *var_tetrahedron_label = dataFile.get_var ( "Tetrahedron_Label" );
    var_tetrahedron_label->get ( &tetrahedron_label[0], tetrahedrons );
  }
//
//  Hexahedrons.
//
  if ( 0 < hexahedrons )
  {
    NcVar *var_hexahedron_vertex = dataFile.get_var ( "Hexahedron_Vertex" );
    var_hexahedron_vertex->get ( &hexahedron_vertex[0], 8, hexahedrons );

    NcVar *var_hexahedron_label = dataFile.get_var ( "Hexahedron_Label" );
    var_hexahedron_label->get ( &hexahedron_label[0], hexahedrons );
  }
//
//  Close the file.
//
  dataFile.close ( );

  return;
}