/** * @brief Returns a value in the flat array of the variable and index requested. * * Use this method on variables that have a type of float * * @param variable The variable in the file * @param index The index in the variable's array in the file * * @return float of the value in the array. */ float CDFFileReader::getVariableAtIndex(const std::string& variable, long index) { //std::cout << "index " << index << std::endl; //get variable number long variableNum = CDFgetVarNum(current_file_id, (char *) variable.c_str()); long recStart = 0L; long recCount = 1L; long recInterval = 1L; long dimIndices[] = { index }; long dimIntervals[] = { 1 }; long count[1] = {1}; //get dim sizes //CDFgetzVarDimSizes(current_file_id, variableNum, dimSizes); float * buffer = new float[1]; CDFhyperGetzVarData(current_file_id, variableNum, recStart, recCount, recInterval, dimIndices, count, dimIntervals, buffer); float value = buffer[0]; delete[] buffer; //std::cout << "finished reading " << variable << std::endl; //std::cout << "size of variable: " << variableData.size() << std::endl; //std::cout << "dimSizes[0]: " << dimSizes[0] << std::endl; return value; }
/** * @param variable * @return */ long CDFFileReader::getNumberOfRecords(const std::string& variable) { //std::cout << "reading " << variable << std::endl; //get variable number long variableNum = CDFgetVarNum(current_file_id, (char *) variable.c_str()); return getNumberOfRecords(variableNum); }
/** * Returns a pointer to a std::vector<float> containing the values of the selected variable * in the range specified by the startIndex and count (the number of records to read) stored in the selected file. * This allocates a new std::vector<float> pointer. Make sure you * delete the contents when you done using it, or you will have a memory leak. * @param variable * @param startIndex * @param count * @return std::vector<float> containing the values of the selected variable. */ std::vector<float>* CDFFileReader::getVariable(const std::string& variable, long startIndex, long count) { //std::cout << "reading " << variable << std::endl; //get variable number std::vector<float>* variableData = new std::vector<float>(); if (this->doesVariableExist(variable)) { long variableNum = CDFgetVarNum(current_file_id, (char *) variable.c_str()); long recStart = 0L; long recCount = 1L; long recInterval = 1L; long dimIndices[] = { 0 }; long dimIntervals[] = { 1 }; long counts[1] = {count}; //get dim sizes //CDFgetzVarDimSizes(current_file_id, variableNum, counts); long dataType; CDFgetzVarDataType(current_file_id, variableNum, &dataType); if (dataType == CDF_DOUBLE) { double * buffer = new double[counts[0]]; CDFhyperGetzVarData(current_file_id, variableNum, recStart, recCount, recInterval, dimIndices, counts, dimIntervals, buffer); //add data to vector type, and delete original array variableData->reserve(counts[0]); for (int i = 0; i < counts[0]; i++) { variableData->push_back((float)buffer[i]); } delete[] buffer; } else if (dataType == CDF_FLOAT) { float * buffer = new float[counts[0]]; CDFhyperGetzVarData(current_file_id, variableNum, recStart, recCount, recInterval, dimIndices, counts, dimIntervals, buffer); //add data to vector type, and delete original array variableData->reserve(counts[0]); for (int i = 0; i < counts[0]; i++) { variableData->push_back(buffer[i]); } delete[] buffer; } //std::cout << "finished reading " << variable << std::endl; //std::cout << "size of variable: " << variableData.size() << std::endl; //std::cout << "dimSizes[0]: " << dimSizes[0] << std::endl; } return variableData; }
//===============================================================// QList<QVector<QVariant > > cdfDataReader::getZVariableAttribute(const QString Attribute, const QString Variable) { long attNum = CDFgetAttrNum(this->fileId, Attribute.toAscii().data()); long varNum = CDFgetVarNum(this->fileId, Variable.toAscii().data()); //convert the names to values, and call other version of the call return this->getZVariableAttribute(attNum, varNum); }
/** * This allocates a new std::vector<int> pointer. Make sure you * delete the contents when you done using it, or you will have a memory leak. * @param variable * @return vector<int> containing the integer values of the variable */ std::vector<int>* CDFFileReader::getVariableInt(const std::string& variable) { long counts[1]; std::vector<int>* variableData = new std::vector<int>(); if (this->doesVariableExist(variable)) { //std::cout << "reading " << variable << std::endl; //get variable number long variableNum = CDFgetVarNum(current_file_id, (char *) variable.c_str()); long recStart = 0L; long recCount = 1L; long recInterval = 1L; long dimIndices[] = { 0 }; long dimIntervals[] = { 1 }; //get dim sizes CDFgetzVarDimSizes(current_file_id, variableNum, counts); // std::cerr << "(" << ") count: " << (int)counts[0] << std::endl; // std::cerr << "(" << variable << ") variableNum: " << variableNum << std::endl; // std::cout << BOOST_CURRENT_FUNCTION << ": current_file_id: " << current_file_id << std::endl; if (counts[0] > 0) { try { int * buffer = new int[counts[0]]; CDFhyperGetzVarData(current_file_id, variableNum, recStart, recCount, recInterval, dimIndices, counts, dimIntervals, buffer); //add data to vector type, and delete original array variableData->reserve(counts[0]); for (int i = 0; i < counts[0]; i++) { variableData->push_back(buffer[i]); } delete[] buffer; } catch (std::bad_alloc& ba) { std::cerr << "bad_alloc caught: " << ba.what() << std::endl; } } } else { std::cout << "variable: " << variable << " does not exist!!!!" << std::endl; } //std::cout << "finished reading " << variable << std::endl; return variableData; }
//===============================================================// QMap<QString, QList<QVector< QVariant > > > cdfDataReader::getZVariableAttributes(QStringList Attributes, QString Variable) { QMap< QString, QList<QVector <QVariant > > > returnVal; QStringList::Iterator iter; long varN = CDFgetVarNum(this->fileId, Variable.toAscii().data()); //for each Attribute in the list, get the attributes for the given variable for(iter = Attributes.begin(); iter != Attributes.end(); ++iter) { long attN = CDFgetAttrNum(this->fileId, (*iter).toAscii().data()); returnVal[*iter] = this->getZVariableAttribute(attN, varN); } return returnVal; }
/** * @brief Returns a value in the flat array of the variable and index requested. * * Use this method on variables that have a type of int * * @param variable The variable in the file * @param index The index in the variable's array in the file * * @return int of the value in the array. */ int CDFFileReader::getVariableIntAtIndex(const std::string& variable, long index) { //std::cout << "reading " << variable << std::endl; //get variable number long variableNum = CDFgetVarNum(current_file_id, (char *) variable.c_str()); long recStart = 0L; long recCount = 1L; long recInterval = 1L; long dimIndices[] = { index }; long dimIntervals[] = { 1 }; long dimSizes[1] = {1}; //get dim sizes //CDFgetzVarDimSizes(current_file_id, variableNum, dimSizes); int * buffer = new int[1]; CDFgetzVarData(current_file_id, variableNum, recStart, dimIndices, buffer); //add data to vector type, and delete original array int value = buffer[0]; delete[] buffer; //std::cout << "finished reading " << variable << std::endl; return value; }
int main () { CDFid id; CDFstatus status; long varnum; long variance = { VARY }; long dimSizes[1] = {0}, dimVary[1] = {VARY}; long indices[1] = {0}, counts[1]= {1}, intervals[1] = {1}; long long tt2000[8], mm[8]; double year1=2008, month1=12, day1=31, hour1=23, minute1=59, second1=57, msec1=100, usec1=200, nsec1=300; double year=2008, month=12, day=31, hour=23, minute=59, second=57, msec=100, usec=200, nsec=300; double yearOut, monthOut, dayOut, hourOut, minuteOut, secondOut, msecOut, usecOut, nsecOut; int ix; char string[TT2000_3_STRING_LEN+1]; long long int8[3][2], int8o[3][2], nn; /****************************************************************************** * Display title. ******************************************************************************/ printf ("Testing Internal/C interface for CDF_TIME_TT2000 ...\n"); tt2000[0] = computeTT2000 (2000., 1., 1., 12., 0., 0., TT2000END); breakdownTT2000 (tt2000[0], &yearOut, &monthOut, &dayOut, &hourOut, &minuteOut, &secondOut, &msecOut, &usecOut, &nsecOut); printf ("Base: 2000-01-01T12:00.00.000 => %lld nanoseconds\n", tt2000[0]); encodeTT2000 (tt2000[0], string, 3); printf (" %lld => %s\n", tt2000[0], string); /****************************************************************************** * Create CDF. ******************************************************************************/ status = CDFlib (CREATE_, CDF_, "TEST", 0, dimSizes, &id, NULL_); if (status < CDF_OK) { if (status == CDF_EXISTS) { status = CDFlib (OPEN_, CDF_, "TEST", &id, NULL_); if (status < CDF_OK) QuitCDF ("1.0", status); status = CDFlib (DELETE_, CDF_, NULL_); if (status < CDF_OK) QuitCDF ("1.1", status); status = CDFlib (CREATE_, CDF_, "TEST", 0L, dimSizes, &id, NULL_); if (status < CDF_OK) QuitCDF ("6.0", status); } } /****************************************************************************** * Create variables. ******************************************************************************/ status = CDFlib (CREATE_, zVAR_, "myTT2000", CDF_TIME_TT2000, 1L, 0L, dimSizes, variance, dimVary, &varnum, NULL_); if (status < CDF_OK) QuitCDF ("7.0", status); dimSizes[0] = 2; status = CDFlib (CREATE_, zVAR_, "myINT8", CDF_INT8, 1L, 1L, dimSizes, variance, dimVary, &varnum, NULL_); if (status < CDF_OK) QuitCDF ("7.1", status); /****************************************************************************** * PUT to variables. ******************************************************************************/ tt2000[0] = computeTT2000 (year, month, day, hour, minute, second, msec, usec, nsec); if (tt2000[0] == ILLEGAL_TT2000_VALUE) QuitCDF ("9.0", TT2000_TIME_ERROR); for (ix = 1; ix < 8; ++ix) tt2000[ix] = tt2000[0] + (long long) ix*1000000000; varnum = CDFgetVarNum (id, "myTT2000"); status = CDFlib (SELECT_, zVAR_, varnum, zVAR_RECNUMBER_, 0L, zVAR_RECCOUNT_, 8L, zVAR_RECINTERVAL_, 1L, zVAR_DIMINDICES_, indices, zVAR_DIMCOUNTS_, counts, zVAR_DIMINTERVALS_, intervals, PUT_, zVAR_HYPERDATA_, tt2000, NULL_); if (status < CDF_OK) QuitCDF ("10.0", status); /****************************************************************************** * HyperGET from variables. ******************************************************************************/ status = CDFlib (GET_, zVAR_HYPERDATA_, mm, NULL_); if (status < CDF_OK) QuitCDF ("11.0", status); for (ix= 0; ix < 8; ix++) { breakdownTT2000 (mm[ix], &yearOut, &monthOut, &dayOut, &hourOut, &minuteOut, &secondOut, &msecOut, &usecOut, &nsecOut); if (year != yearOut) QuitCDF("11.1", status); if (month != monthOut) QuitCDF("11.2", status); if (day != dayOut) QuitCDF("11.3", status); if (hour != hourOut) QuitCDF("11.4", status); if (minute != minuteOut) QuitCDF("11.5", status); if (second != secondOut) QuitCDF("11.6", status); if (msec != msecOut) QuitCDF("11.7", status); if (usec != usecOut) QuitCDF("11.8", status); if (nsec != nsecOut) QuitCDF("11.9", status); ++second; if (second > 60) { year = 2009; month = 1; day = 1; hour = 0; minute = 0; second = 0; } } varnum = CDFgetVarNum (id, "myINT8"); nn = 88888888888LL; for (ix = 0; ix < 3; ++ix) { int8[ix][0] = nn; int8[ix][1] = -nn; ++nn; } counts[0] = 2; status = CDFlib (SELECT_, zVAR_, varnum, zVAR_RECNUMBER_, 0L, zVAR_RECCOUNT_, 3L, zVAR_RECINTERVAL_, 1L, zVAR_DIMINDICES_, indices, zVAR_DIMCOUNTS_, counts, zVAR_DIMINTERVALS_, intervals, PUT_, zVAR_HYPERDATA_, int8, NULL_); if (status < CDF_OK) QuitCDF ("12.0", status); status = CDFlib (GET_, zVAR_HYPERDATA_, int8o, NULL_); if (status < CDF_OK) QuitCDF ("13.0", status); nn = 88888888888LL; for (ix = 0; ix < 3; ++ix) { if (int8o[ix][0] != nn) QuitCDF ("14.0", status);; if (int8o[ix][1] != -nn) QuitCDF ("14.1", status);; ++nn; } /****************************************************************************** * Close CDF. ******************************************************************************/ status = CDFlib (CLOSE_, CDF_, NULL_); if (status < CDF_OK) QuitCDF ("26.2", status); /****************************************************************************** * Re-open the CDF. ******************************************************************************/ status = CDFlib (OPEN_, CDF_, "TEST", &id, NULL_); if (status < CDF_OK) QuitCDF ("30.1", status); varnum = CDFvarNum (id, "myTT2000"); if (varnum < CDF_OK) QuitCDF ("31.1", status); /****************************************************************************** * HyperGET from variables. ******************************************************************************/ status = CDFlib (SELECT_, zVAR_, varnum, zVAR_RECNUMBER_, 0L, zVAR_RECCOUNT_, 8L, zVAR_RECINTERVAL_, 1L, zVAR_DIMINDICES_, indices, zVAR_DIMCOUNTS_, counts, zVAR_DIMINTERVALS_, intervals, GET_, zVAR_HYPERDATA_, mm, NULL_); if (status < CDF_OK) QuitCDF ("32.1", status); for (ix= 0; ix < 8; ix++) { breakdownTT2000 (mm[ix], &yearOut, &monthOut, &dayOut, &hourOut, &minuteOut, &secondOut, &msecOut, &usecOut, &nsecOut); encodeTT2000 (mm[ix], string, 3); printf ("%s\n",string); if (year1 != yearOut) QuitCDF("33.1", status); if (month1 != monthOut) QuitCDF("33.2", status); if (day1 != dayOut) QuitCDF("33.3", status); if (hour1 != hourOut) QuitCDF("33.4", status); if (minute1 != minuteOut) QuitCDF("33.5", status); if (second1 != secondOut) QuitCDF("33.6", status); if (msec1 != msecOut) QuitCDF("33.7", status); if (usec1 != usecOut) QuitCDF("33.8", status); if (nsec1 != nsecOut) QuitCDF("33.9", status); ++second1; if (second1 > 60) { year1 = 2009; month1 = 1; day1 = 1; hour1 = 0; minute1 = 0; second1 = 0; } } /****************************************************************************** * Close CDF. ******************************************************************************/ status = CDFlib (CLOSE_, CDF_, NULL_); if (status < CDF_OK) QuitCDF ("26.2", status); /****************************************************************************** * Successful completion. ******************************************************************************/ return EXIT_SUCCESS_; }
/** * @param variable * @param vattribute * @return */ Attribute CDFFileReader::getVariableAttribute(const std::string& variable, const std::string& vattribute) { //first, check the vAttributes map boost::unordered_map<std::string, boost::unordered_map< std::string, Attribute> >::iterator iter = vAttributes.find(variable); if (iter != vAttributes.end()) { boost::unordered_map< std::string, Attribute>::iterator iter2 = vAttributes[variable].find(vattribute); if (iter2 != vAttributes[variable].end()) { return (*iter2).second; } } long variableNumber = CDFgetVarNum(current_file_id, (char *) variable.c_str()); long attributeNumber = CDFgetAttrNum(current_file_id, (char *) vattribute.c_str()); long dataType; CDFstatus status = CDFgetAttrzEntryDataType(current_file_id, attributeNumber, variableNumber, &dataType); Attribute attribute; // std::cout << "CDFFileReader::getVariableAttribute - datatype: " << dataType << " CDF_CHAR: " << CDF_CHAR << std::endl; if (dataType == CDF_CHAR) { char value[1024]; long status = CDFgetAttrzEntry(current_file_id, attributeNumber, variableNumber, value); long numElements; status = CDFgetAttrzEntryNumElements(current_file_id, attributeNumber, variableNumber, &numElements); value[numElements] = '\0'; //std::cout << "C: attributeValue (" << vattribute << "): " << value << std::endl; std::string valueString = value; attribute.setAttributeName(vattribute); attribute.setAttributeValue(valueString); } else if (dataType == CDF_INT4) { int value; long status = CDFgetAttrzEntry(current_file_id, attributeNumber, variableNumber, &value); //std::cout << "I: attributeValue (" << vattribute << "): " << value << std::endl; attribute.setAttributeName(vattribute); attribute.setAttributeValue(value); } else if (dataType == CDF_FLOAT) //CDF_FLOAT { float value; long status = CDFgetAttrzEntry(current_file_id, attributeNumber, variableNumber, &value); //std::cout << "F: attributeValue (" << vattribute << "): " << value << std::endl; attribute.setAttributeName(vattribute); attribute.setAttributeValue(value); } (vAttributes[variable])[vattribute] = attribute; return attribute; }
//===============================================================// cdfVarInfo cdfDataReader::getZVariableInformation(QString variable) { int64_t varNum = CDFgetVarNum(this->fileId, variable.toAscii().data()); return this->getZVariableInformation(varNum); }
//===============================================================// cdfDataSet cdfDataReader::getZVariableRecord(QString variable, int64_t record) { return this->getZVariableRecord(CDFgetVarNum(this->fileId, variable.toAscii().data()), record); }
//===============================================================// cdfDataSet cdfDataReader::getCorrectedZVariable(QString variable) { long varNum = CDFgetVarNum(this->fileId,variable.toAscii().data()); return this->getCorrectedZVariable(varNum); }
//===============================================================// cdfDataSet cdfDataReader::getCorrectedZVariableRecord(QString variable, int64_t record) { long varNum = CDFgetVarNum(this->fileId, variable.toAscii().data()); return this->getCorrectedZVariableRecord(varNum, record); }
//===============================================================// cdfDataSet cdfDataReader::getZVariable(QString variable) { // get the number for the name long varNum = CDFgetVarNum(this->fileId, variable.toAscii().data()); return this->getZVariable(varNum); }