9 #include <boost/bind.hpp>
10 #include <boost/function.hpp>
11 #include "../Global/Util.h"
17 static const std::string MeasPosXName =
"MeasPosX";
19 static const std::string MeasPosYName =
"MeasPosY";
21 static const std::string MeasPosZName =
"MeasPosZ";
23 static const std::string StationNumberName =
"StationNumber";
26 template<
class VectorType>
27 void ReadVec(NcFile &NetCDFFile,
const std::string &DataName,
28 const std::string &DimName, VectorType &Position)
31 NcDim *Dim = NetCDFFile.get_dim(DimName.c_str());
33 const size_t nvalues = Dim->size();
36 Position.resize(nvalues);
38 NcVar *SizeVar = NetCDFFile.get_var(DataName.c_str());
40 SizeVar->get(&Position[0], nvalues);
44 template<
class VectorType>
45 void WriteVec(NcFile &NetCDFFile,
const std::string &MeasPosName,
46 const VectorType &Position, NcDim *Dimension,
const std::string unit)
48 const size_t nmeas = Position.size();
49 NcVar *PosVar = NetCDFFile.add_var(MeasPosName.c_str(), ncDouble,
51 PosVar->add_att(
"units", unit.c_str());
52 PosVar->put(&Position[0], nmeas);
59 NcDim *FreqDim,
const gplib::rvec &Impedances,
60 const std::string &CompName,
const size_t compindex)
62 NcVar *CompVar = NetCDFFile.add_var(CompName.c_str(), ncDouble,
64 gplib::rvec Component(FreqDim->size() * StatNumDim->size());
65 for (
size_t i = 0; i < Component.size(); ++i)
67 Component(i) = Impedances(i * 8 + compindex);
69 CompVar->put(&Component[0], FreqDim->size(), StatNumDim->size());
74 const std::string &CompName,
const size_t compindex)
76 NcVar *SizeVar = NetCDFFile.get_var(CompName.c_str());
77 const size_t nvalues = Impedances.size() / 8;
78 gplib::rvec Temp(nvalues);
79 SizeVar->get(&Temp[0], SizeVar->edges()[0], SizeVar->edges()[1]);
80 for (
size_t i = 0; i < nvalues; ++i)
82 Impedances(i * 8 + compindex) = Temp(i);
88 const std::vector<double> &Frequencies,
89 const std::vector<double> &StatXCoord,
90 const std::vector<double> &StatYCoord,
91 const std::vector<double> &StatZCoord,
const gplib::rvec &Impedances)
93 const size_t nstats = StatXCoord.size();
94 const size_t nfreqs = Frequencies.size();
95 assert(nstats == StatYCoord.size());
96 assert(nstats == StatYCoord.size());
97 assert(Impedances.size() == nstats * nfreqs * 8);
99 NcFile DataFile(filename.c_str(), NcFile::Replace);
102 NcDim *StatNumDim = DataFile.add_dim(StationNumberName.c_str(), nstats);
103 std::vector<int> StationNumber;
104 std::generate_n(back_inserter(StationNumber), nstats, IntSequence(0));
105 NcVar *StatNumVar = DataFile.add_var(StationNumberName.c_str(), ncInt,
107 StatNumVar->put(&StationNumber[0], nstats);
109 WriteVec(DataFile, MeasPosXName, StatXCoord, StatNumDim,
"m");
110 WriteVec(DataFile, MeasPosYName, StatYCoord, StatNumDim,
"m");
111 WriteVec(DataFile, MeasPosZName, StatZCoord, StatNumDim,
"m");
113 NcDim *FreqDim = DataFile.add_dim(
FreqDimName.c_str(),
115 NcVar *FreqVar = DataFile.add_var(
FreqDimName.c_str(), ncDouble,
117 FreqVar->put(&Frequencies[0], nfreqs);
138 double> &Frequencies, std::vector<double> &StatXCoord, std::vector<
139 double> &StatYCoord, std::vector<double> &StatZCoord,
140 gplib::rvec &Impedances)
142 NcFile DataFile(filename.c_str(), NcFile::ReadOnly);
143 ReadVec(DataFile, MeasPosXName, StationNumberName, StatXCoord);
144 ReadVec(DataFile, MeasPosYName, StationNumberName, StatYCoord);
145 ReadVec(DataFile, MeasPosZName, StationNumberName, StatZCoord);
147 Impedances.resize(Frequencies.size() * StatXCoord.size() * 8);
void ReadVec(NcFile &NetCDFFile, const std::string &DataName, const std::string &DimName, VectorType &Position)
Read a vector from a netcdf file.
void WriteVec(NcFile &NetCDFFile, const std::string &MeasPosName, const VectorType &Position, NcDim *Dimension, const std::string unit)
Write a vectorial quantity to a netcdf file.
void WriteImpedanceComp(NcFile &NetCDFFile, NcDim *StatNumDim, NcDim *FreqDim, const gplib::rvec &Impedances, const std::string &CompName, const size_t compindex)
void ReadImpedanceComp(NcFile &NetCDFFile, gplib::rvec &Impedances, const std::string &CompName, const size_t compindex)
const std::string FreqDimName
void ReadImpedancesFromNetCDF(const std::string &filename, std::vector< double > &Frequencies, std::vector< double > &StatXCoord, std::vector< double > &StatYCoord, std::vector< double > &StatZCoord, gplib::rvec &Impedances)
Read magnetotelluric impedances from a netcdf file.
void WriteImpedancesToNetCDF(const std::string &filename, const std::vector< double > &Frequencies, const std::vector< double > &StatXCoord, const std::vector< double > &StatYCoord, const std::vector< double > &StatZCoord, const gplib::rvec &Impedances)
Write magnetotelluric impedances to a netcdf file.