26 #include <QtCore/QFile>
49 size_t size_of_type=1;
54 size_of_type =
sizeof(char);
57 size_of_type =
sizeof(short);
60 size_of_type =
sizeof(int);
63 size_of_type =
sizeof(float);
66 size_of_type =
sizeof(double);
83 "ncdfaux_data_allocate: the destination is not clean");
93 dest =
new short[num];
99 dest =
new float[num];
102 dest =
new double[num];
110 memset(dest, 0, size_of_type*num);
121 "ncdfaux_data_allocate_n_copy: the destination is not clean");
128 dest =
new char[num];
131 dest =
new short[num];
137 dest =
new float[num];
140 dest =
new double[num];
144 "ncdfaux_data_allocate_n_copy: unexpected data type");
147 memcpy(dest, src, size_of_type*num);
164 delete[] (
char*)dest;
167 delete[] (
short*)dest;
173 delete[] (
float*)dest;
176 delete[] (
double*)dest;
233 return "SgNcdfDimension";
241 std::cout <<
" dim #" <<
id_ <<
", \"" << qPrintable(
name_) <<
"\" = "
271 return "SgNcdfAttribute";
289 size_t numOfElements,
void *data)
302 : name_(attr.getName())
340 rc = nc_get_att_schar(ncid, varid, qPrintable(
name_), (
signed char*)
data_);
343 rc = nc_get_att_text(ncid, varid, qPrintable(
name_), (
char*)
data_);
346 rc = nc_get_att_short(ncid, varid, qPrintable(
name_), (
short*)
data_);
349 rc = nc_get_att_int(ncid, varid, qPrintable(
name_), (
int*)
data_);
352 rc = nc_get_att_float(ncid, varid, qPrintable(
name_), (
float*)
data_);
355 rc = nc_get_att_double(ncid, varid, qPrintable(
name_), (
double*)
data_);
360 "::readAttr(): unexpected data type");
375 "::nc_put_attr(): the data is NULL");
394 "::nc_put_attr(): unexpected data type");
421 return "SgNcdfVariable";
464 return (
const char*)
data_;
486 return (
const short*)
data_;
497 return (
short*)
data_;
508 return (
const int*)
data_;
530 return (
const double*)
data_;
541 return (
double*)
data_;
574 "::delDimension(): the dimension " + name +
" is not in the map");
587 "::delDimension(): cannot find [" + name +
"] in the list");
606 "::delDimension(): the dimension " + name +
" was set to zero");
617 "::addAttribute(): the attribute " + attr.
getName() +
" is already in the list");
632 "::addAttribute(): the attribute " + a->
getName() +
" is already in the list");
646 "::delAttribute(): the attribute " + name +
" is not in the list");
680 rc = nc_get_var_schar(ncid,
id_, (
signed char*)
data_);
683 rc = nc_get_var_text(ncid,
id_, (
char*)
data_);
686 rc = nc_get_var_short(ncid,
id_, (
short*)
data_);
689 rc = nc_get_var_int(ncid,
id_, (
int*)
data_);
692 rc = nc_get_var_float(ncid,
id_, (
float*)
data_);
695 rc = nc_get_var_double(ncid,
id_, (
double*)
data_);
700 "::nc_get_var(): unexpected data type");
715 "::nc_put_var(): the data is NULL");
721 rc = nc_put_var_schar(ncid,
id_, (
const signed char*)
data_);
724 rc = nc_put_var_text(ncid,
id_, (
const char*)
data_);
727 rc = nc_put_var_short(ncid,
id_, (
const short*)
data_);
730 rc = nc_put_var_int(ncid,
id_, (
const int*)
data_);
733 rc = nc_put_var_float(ncid,
id_, (
const float*)
data_);
736 rc = nc_put_var_double(ncid,
id_, (
const double*)
data_);
741 "::nc_put_var(): unexpected data type");
756 for (
int i=0; i<mult; i++)
760 "::multiplyData(): the data of \"" +
name_ +
"\" were multiplied " +
761 QString(
"").setNum(mult) +
" times");
787 "::check4multiplication(): the variable \"" +
name_ +
"\" already has an attribute \"REPEAT\"");
794 bool canBeConvolved=
true;
796 size_t size_of_newData;
798 int newNumOfElements=1;
803 size_of_newData = size_of_type*newNumOfElements;
804 memcpy((
unsigned char*)newData,
data_, size_of_newData);
805 for (
int i=0; i<cDim && canBeConvolved; i++)
806 if (memcmp((
unsigned char*)
data_ + size_of_newData*i, newData, size_of_newData) != 0)
807 canBeConvolved =
false;
827 "::check4multiplication(): the variable \"" +
name_ +
828 "\" has been convolved: dimension \"" + cDimName +
"\", multiplier=" +
829 QString(
"").setNum(cDim));
842 "::allocateData(): the data of \"" +
name_ +
"\" were already allocated");
849 "::allocateData(): impossible to allocate the data of \"" +
name_ +
"\": the size is zero");
866 std::cout <<
"Variable # " <<
id_ <<
" \"" << qPrintable(
name_) <<
"\" of type ";
869 std::cout <<
" Dimensions:\n";
875 std::cout <<
" Variable's attributes:\n";
877 for (
int i=0; i<atts.size(); i++)
881 std::cout <<
" Attr \"" << qPrintable(a->
getName()) <<
"\" of type ";
889 str += QString(
"").sprintf(
"%d,", ((
signed char*)a->
getData())[k]);
896 str += QString(
"").sprintf(
"%d,", ((
short*)a->
getData())[k]);
900 str += QString(
"").sprintf(
"%d,", ((
int*)a->
getData())[k]);
904 str += QString(
"").sprintf(
"%g,", ((
float*)a->
getData())[k]);
908 str += QString(
"").sprintf(
"%g,", ((
double*)a->
getData())[k]);
914 std::cout <<
"; value : [" << qPrintable(str) <<
"]\n";
918 std::cout <<
" Value: ";
923 std::cout << qPrintable(QString(
"").sprintf(
"%d,", ((
signed char*)
data_)[k]));
932 std::cout << qPrintable(QString(
"").sprintf(
"%d,", ((
short*)
data_)[k]));
937 std::cout << qPrintable(QString(
"").sprintf(
"%d,", ((
int*)
data_)[k]));
942 std::cout << qPrintable(QString(
"").sprintf(
"%g,", ((
float*)
data_)[k]));
947 std::cout << qPrintable(QString(
"").sprintf(
"%g,", ((
double*)
data_)[k]));
951 std::cout <<
"NC_NAT";
998 const QString& sessionId,
const QString& stationId,
const QString& bandId) :
1004 fmtVerId_(fmtVerId),
1005 sessionId_(sessionId),
1006 stationId_(stationId),
1024 delete it_d.value();
1035 delete it_d.value();
1040 QMap<QString, SgNcdfVariable*>::iterator it;
1061 "::getData(): the file name is not specified");
1064 if ((rc=nc_open(qPrintable(
fileName_), 0, &ncid)) != NC_NOERR)
1066 printf(
"%s: %s\n", qPrintable(
"Error opening " +
fileName_), nc_strerror(rc));
1068 "::getData(): cannot open the file " +
fileName_);
1079 QMap<int, SgNcdfDimension*> dimById;
1081 char buff[NC_MAX_NAME + 1];
1082 if ((rc=nc_inq_ndims(ncid, &numOfDims)) != NC_NOERR)
1085 qPrintable(
"Error inquiring number of dimensions from " +
fileName_), nc_strerror(rc));
1087 "::getData(): inquire of total number of dimensions has failed");
1091 for (
int i=0; i<numOfDims; i++)
1093 if ((rc=nc_inq_dim(ncid, i, buff, &len)) != NC_NOERR)
1096 qPrintable(
"Error inquiring dimension name and length from " +
fileName_), nc_strerror(rc));
1098 "::getData(): inquire of dimension name and length for dim#" + QString(
"").setNum(i) +
1108 QMap<int, SgNcdfVariable*> varById;
1110 if ((rc=nc_inq_nvars(ncid, &numOfVars)) != NC_NOERR)
1113 qPrintable(
"Error inquiring number of dimensions from " +
fileName_), nc_strerror(rc));
1115 "::getData(): inquire of total number of variables has failed");
1119 for (
int i=0; i<numOfVars; i++)
1121 int dimids[NC_MAX_VAR_DIMS];
1124 if ((rc=nc_inq_var(ncid, i, buff, &xtype, &ndims, dimids, &natts)) != NC_NOERR)
1127 qPrintable(
"Error inquiring variable from " +
fileName_), nc_strerror(rc));
1129 "::getData(): inquire of variable for var#" + QString(
"").setNum(i) +
1139 for (
int j=0; j<ndims; j++)
1145 for (
int j=0; j<natts; j++)
1148 if ((rc=nc_inq_attname(ncid, i, j, buff)) != NC_NOERR)
1151 qPrintable(
"Error inquiring attribute name from " +
fileName_), nc_strerror(rc));
1153 "::getData(): inquire of name of attribute# "+ QString(
"").setNum(j) +
" for variable #" +
1154 QString(
"").setNum(i) +
" has failed");
1159 if ((nc_inq_att(ncid, i, buff, &xtype, &len)) != NC_NOERR)
1162 qPrintable(
"Error inquiring attribute from " +
fileName_), nc_strerror(rc));
1164 "::getData(): inquire of attribute# "+ QString(
"").setNum(j) +
" for variable #" +
1165 QString(
"").setNum(i) +
" has failed");
1174 qPrintable(
"Error inquiring attribute name from " +
fileName_), nc_strerror(rc));
1176 "::getData(): inquire of name of attribute# "+ QString(
"").setNum(j) +
" for variable #" +
1177 QString(
"").setNum(i) +
" has failed");
1187 qPrintable(
"Error reading variable from " +
fileName_), nc_strerror(rc));
1189 "::getData(): reading of data of variable# "+ QString(
"").setNum(i) +
" has failed");
1199 int mult=*(
int*)att->
getData();
1204 "::getData(): reading of data of variable# "+ QString(
"").setNum(i) +
1205 ": has wrong attribute REPEAT");
1207 varById.insert(i, v);
1212 QMap<int, SgNcdfDimension*>::iterator it_d=dimById.begin();
1213 for(; it_d!=dimById.end(); ++it_d)
1218 const QString srvVarNames(
"Stub:CreateTime:CreatedBy:Program:Subroutine:DataOrigin:TimeTag:"
1219 "TimeTagFile:Session:Station:REPEAT:vgosDB_Version");
1220 QMap<int, SgNcdfVariable*>::iterator it_v=varById.begin();
1221 for(; it_v!=varById.end(); ++it_v)
1224 if (srvVarNames.contains(v->
getName()))
1232 "::getData(): read " + QString(
"").setNum(
dimensionByName_.size()) +
" dimensions, " +
1233 QString(
"").setNum(
serviceVars_.size()) +
" service and " +
1234 QString(
"").setNum(
contentVars_.size()) +
" content variables");
1248 int len=varValue.size();
1252 str.sprintf(
"Char_x_%d", len);
1255 strncpy(var->
data2char(), qPrintable(varValue), len);
1264 const QString& timeTagFile)
1270 std::cout <<
" no stub! \n";
1295 if (dataOrigin.size())
1301 if (timeTagFile.size())
1323 "::registerVariable(): the variable is NULL");
1329 "::registerVariable(): the variable \"" + var->
getName() +
"\" has no dimension");
1333 if (vars.contains(var->
getName().toUpper()))
1335 "::registerVariable(): the variable \"" + var->
getName() +
"\" already in the " +
1336 (is4Service?
"service":
"content") +
" map");
1338 vars.insert(var->
getName().toUpper(), var);
1340 for (
int i=0; i<var->
dimensions().size(); i++)
1348 "::registerVariable(): the dimension \"" + d->
getName() +
"\" has different meanings, " +
1350 QString(
"").setNum(d->
getN()));
1367 "::putData(): the file name is not specified");
1370 if ((rc=nc_create(qPrintable(
fileName_), 0, &ncid)) != NC_NOERR)
1372 printf(
"NetCDF: %s\n", nc_strerror(rc));
1374 "::putData(): cannot create the file " +
fileName_ +
": " + QString(nc_strerror(rc)));
1380 it.value()->check4multiplication();
1388 if ((rc=nc_def_dim(ncid, qPrintable(dim->
getName()), dim->
getN(), &dimId)) != NC_NOERR)
1390 printf(
"%s\n", nc_strerror(rc));
1392 "::putData(): defining of a new dimension " + dim->
getName() +
" has been failed");
1401 QMap<int, SgNcdfVariable*> varByOrder;
1405 varByOrder.insert(it.value()->getId(), it.value());
1406 variables = varByOrder.values();
1410 varByOrder.insertMulti(it.value()->sizeOfData(), it.value());
1413 variables << varByOrder.values();
1415 for (
int i=0; i<variables.size(); i++)
1419 int *dimids=
new int[nDims];
1421 for (
int j=0; j<nDims; j++)
1423 if ((rc=nc_def_var(ncid, qPrintable(var->
getName()), var->
getTypeOfData(), nDims, dimids, &varId))
1426 printf(
"%s\n", nc_strerror(rc));
1428 "::putData(): defining of a new variable " + var->
getName() +
" has been failed");
1435 for (QMap<QString, SgNcdfAttribute*>::const_iterator it=var->
attributeByName().begin();
1442 qPrintable(
"Error specifying an attribute " + att->
getName() +
" for " + var->
getName()),
1445 "::putData(): defining the attribute \"" + att->
getName() +
"\" for variable " +
1446 var->
getName() +
" has been failed");
1457 for (
int i=0; i<variables.size(); i++)
1462 printf(
"%s: %s\n", qPrintable(
"Error writting variable to " +
fileName_), nc_strerror(rc));
1464 "::putData(): writting the variable " + qPrintable(var->
getName()) +
" has failed");
1474 QFile::Permissions perm=f.permissions();
1475 if (!f.setPermissions(perm | QFile::WriteGroup))
1477 "::putData(): cannot adjust the permissions of the file \"" + f.fileName() +
"\"");
1487 "::putData(): the file name is not specified");
1491 std::cout <<
"DRY RUN: The netCDF file will be created: \""
static const int dimensionUnityValue
size_t ncdfaux_type_2_size(nc_type type)
void ncdfaux_data_allocate(void *&dest, nc_type type, size_t num)
QString ncdfaux_type_2_string(nc_type xtype)
void ncdfaux_data_free(void *&dest, nc_type type)
static const QString dimensionUnityName("DimUnity")
void ncdfaux_data_allocate_n_copy(void *&dest, const void *src, nc_type type, size_t num)
const SgNcdfDimension dUnity(dimensionUnityName, dimensionUnityValue, -1)
SgVersion libraryVersion("SgLib", 0, 8, 2, "Compton Peak (rc2)", SgMJD(2023, 4, 3, 10, 59))
const QString & getExecBinaryName() const
const QString & getMachineMachineName() const
const QString & getMachineRelease() const
const QString & getMachineNodeName() const
const QString & getAcFullName() const
const SgVersion & getDriverVersion() const
const QString & getExecDir() const
const QString & getUserName() const
const QString & getMachineSysName() const
virtual void write(LogLevel, quint32, const QString &, bool=false)
@ F_Simple
Digits: 2010/04/02 17:02:43.6.
static SgMJD currentMJD()
static const QString className()
nc_type getTypeOfData() const
void setNumOfElements(size_t num)
void setData(const void *, size_t)
void setTypeOfData(nc_type type)
size_t getNumOfElements() const
int nc_put_attr(int ncid, int varid)
void setName(const QString &str)
const void * getData() const
int nc_get_attr(int ncid, int varid)
const QString & getName() const
const QString & getName() const
static const QString className()
void delAttribute(const QString &)
QMap< QString, SgNcdfAttribute * > attributeByName_
nc_type getTypeOfData() const
QMap< QString, SgNcdfDimension * > dimensionByName_
const char * data2char() const
void setName(const QString &name)
void addAttribute(const SgNcdfAttribute &)
void setTypeOfData(nc_type type)
const int * data2int() const
const short * data2short() const
const QList< SgNcdfDimension * > & dimensions() const
const double * data2double() const
void addDimension(const SgNcdfDimension &d, int idx=-1)
const QString & getName() const
void delDimension(const QString &)
QList< SgNcdfDimension * > dimensions_
void check4multiplication()
static const QString className()
const QMap< QString, SgNcdfAttribute * > & attributeByName() const
bool setServiceVar(const QString &varName, const QString &varValue)
static const QString svcSubroutine
void setServiceVars(const QString &stub, const QString &dataOrigin, const QString &timeTag, const QString &timeTagFile)
static const QString svcCreatedBy
static const QString svcDataOrigin
QMap< QString, SgNcdfDimension * > dimensionByName_
static const QString svcProgram
static const QString svcTimeTag
static const QString svcStation
const SgIdentities * identities_
void registerVariable(SgNcdfVariable *, bool is4Service=false)
static const QString svcVgosDbVersion
QMap< QString, SgNcdfVariable * > serviceVars_
static const QString svcSession
QMap< QString, SgNcdfVariable * > contentVars_
static const QString svcCreateTime
static const QString className()
OperationMode operationMode_
static const QString svcTimeTagFile
static const QString svcBand
static const QString svcStub
const QString & getSoftwareName() const
QString name(NameFormat fmt=NF_Human) const