// writing attribute to the file void WriteAttribute( RString& rFilename, RString& rPath, RVector<int> rIntdata, RVector<double> rDoubledata, RString rStrdata, vector<STLTypes::NameValuePair>& inAttributes, SEXP& results) { HDF5DA da(rFilename.Data(0),StringUtils::CompareNoCase(ExtractAttribute(inAttributes,NEWFILE),"true")); HDF5OutputConverter oc; string strPath(rPath.Data(0)); // read the compress level string compressStr = ExtractAttribute(inAttributes,COMPRESSLEVEL); int compressedLevel = 0; if ((compressStr.size()>0)) compressedLevel = atoi(compressStr.c_str()); // the dimension of the data to be read string strRow = ExtractAttribute(inAttributes,ROW); string strCol = ExtractAttribute(inAttributes,COL); size_t row=0; size_t col=0; if ((strRow.size()>0) && (strCol.size()>0)) { row = atoi(strRow.c_str()); col = atoi(strCol.c_str()); } string::size_type index = strPath.find_last_of('/'); if((index == string::npos )||(index==0)) throw RException("Attribute can only be directly under an existing dataset or group."); string parent = strPath.substr(0,index); string attr = strPath.substr(index+1,string::npos); // are we going to overwrite an existing attribute bool overwrite = StringUtils::CompareNoCase(ExtractAttribute(inAttributes,OVERWRITE),"true"); if(rIntdata.Size()>0) { // obtain the data from input rIntdata.Dimensions(row,col); Matrix<int> intdata; rIntdata.Extract(intdata); if(CheckDataset(da,parent)) { HDF5Dataset ds = da.OpenDatasetByFullPath(parent); if (overwrite) ds.DeleteAttribute(attr); ds.CreateAttribute(attr,intdata, compressedLevel); } else if(CheckGroup(da,parent)) { HDF5Group gp = da.OpenGroupByFullPath(parent); if (overwrite) gp.DeleteAttribute(attr); gp.CreateAttribute(attr,intdata, compressedLevel); } else throw RException("Attribute can only be directly under an existing dataset or group."); } else if(rDoubledata.Size()>0) { // obtain the data from input rDoubledata.Dimensions(row,col); Matrix<double> doubledata; rDoubledata.Extract(doubledata); if(CheckDataset(da,parent)) { HDF5Dataset ds = da.OpenDatasetByFullPath(parent); if (overwrite) ds.DeleteAttribute(attr); ds.CreateAttribute(attr,doubledata, compressedLevel); } else if(CheckGroup(da,parent)) { HDF5Group gp = da.OpenGroupByFullPath(parent); if (overwrite) gp.DeleteAttribute(attr); gp.CreateAttribute(attr,doubledata, compressedLevel); } else throw RException("Attribute can only be directly under an existing dataset or group."); } else if(rStrdata.Size()>0) { // obtain the data from input rStrdata.Dimensions(row,col); Matrix<string> strdata; rStrdata.Extract(strdata,row,col); if(CheckDataset(da,parent)) { HDF5Dataset ds = da.OpenDatasetByFullPath(parent); if (overwrite) ds.DeleteAttribute(attr); ds.CreateAttribute(attr,strdata, compressedLevel); } else if(CheckGroup(da,parent)) { HDF5Group gp = da.OpenGroupByFullPath(parent); if (overwrite) gp.DeleteAttribute(attr); gp.CreateAttribute(attr,strdata, compressedLevel); } else throw RException("Attribute can only be directly under an existing dataset or group."); } }
// write data to file void WriteData( RString& rFilename, RString& rPath, RVector<int> rIntdata, RVector<double> rDoubledata, RString rStrdata, RDataFrame rDataframedata, vector<STLTypes::NameValuePair>& inAttributes, SEXP& results) { HDF5DA da(rFilename.Data(0),StringUtils::CompareNoCase(ExtractAttribute(inAttributes,NEWFILE),"true")); HDF5OutputConverter oc; string strPath(rPath.Data(0)); string compressStr = ExtractAttribute(inAttributes,COMPRESSLEVEL); int compressedLevel = 0; if ((compressStr.size()>0)) compressedLevel = atoi(compressStr.c_str()); // the dimension of the data to be written string strRow = ExtractAttribute(inAttributes,ROW); string strCol = ExtractAttribute(inAttributes,COL); size_t row=0; size_t col=0; if ((strRow.size()>0) && (strCol.size()>0)) { row = atoi(strRow.c_str()); col = atoi(strCol.c_str()); } // if start index and nRows =0, it means user has not specified a subset to write, // therefore we will read the whole data set string startStr = ExtractAttribute(inAttributes,STARTINDEX); string nrowsStr = ExtractAttribute(inAttributes,NROWS); size_t start=0; size_t nRows=0; bool writeAll=true; if (rIntdata.Size()>0) nRows=row; if (rDoubledata.Size()>0) nRows=row; if (rStrdata.Size()>0) nRows=row; if ((startStr.size()>0)) { start = atoi(startStr.c_str()); writeAll=false; } if (nrowsStr.size()>0) { nRows = atoi(nrowsStr.c_str()); writeAll=false; } // delete the dataset first if it already exists string overwrite = ExtractAttribute(inAttributes,OVERWRITE); if(rIntdata.Size()>0) { // obtain the data from input rIntdata.Dimensions(row,col); Matrix<int> intdata; rIntdata.Extract(intdata); if(writeAll) { if(StringUtils::CompareNoCase(overwrite,"true")) { if(da.DatasetFullPathExist(strPath)) da.DeleteDataByFullPath(strPath); } da.WriteDataByFullPath(strPath, intdata, compressedLevel); } else da.WriteDataByFullPath(strPath, start, nRows, intdata, compressedLevel); } else if(rDoubledata.Size()>0) { // obtain the data from input rDoubledata.Dimensions(row,col); Matrix<double> doubledata; rDoubledata.Extract(doubledata); if(writeAll) { if(StringUtils::CompareNoCase(overwrite,"true")) { if(da.DatasetFullPathExist(strPath)) da.DeleteDataByFullPath(strPath); } da.WriteDataByFullPath(strPath, doubledata, compressedLevel); } else da.WriteDataByFullPath(strPath, start, nRows, doubledata, compressedLevel); } else if(rStrdata.Size()>0) { // obtain the data from input rStrdata.Dimensions(row,col); Matrix<string> strdata; rStrdata.Extract(strdata,row,col); if(writeAll) { if(StringUtils::CompareNoCase(overwrite,"true")) { if(da.DatasetFullPathExist(strPath)) da.DeleteDataByFullPath(strPath); } da.WriteDataByFullPath(strPath, strdata, compressedLevel); } else da.WriteDataByFullPath(strPath, start, nRows, strdata, compressedLevel); } else if(rDataframedata.Size()>0) { if(writeAll) { if(StringUtils::CompareNoCase(overwrite,"true")) { if(da.DatasetFullPathExist(strPath)) da.DeleteDataByFullPath(strPath); } da.WriteDataByFullPath(strPath, rDataframedata.CompoundType(), compressedLevel); } else da.WriteDataByFullPath(strPath, start, nRows, rDataframedata.CompoundType(), compressedLevel); } }