GLAMERDOC++
Gravitational Lensing Code Library
Loading...
Searching...
No Matches
Utilities::IO Namespace Reference

namespace for input/output utilities More...

Functions

bool file_exists (const std::string &name)
 
template<class T >
void read1columnfile (std::string filename, std::vector< T > &x, int skiplines=0, bool verbose=false)
 
template<class T1 , class T2 >
void read2columnfile (std::string filename, std::vector< T1 > &x, std::vector< T2 > &y, std::string delineator=" ", int skiplines=0, bool verbose=false)
 Read in data from an ASCII file with two columns.
 
template<class T1 , class T2 , class T3 >
void read3columnfile (std::string filename, std::vector< T1 > &x, std::vector< T2 > &y, std::vector< T3 > &z, std::string delineator=" ", bool verbose=false)
 Read in data from an ASCII file with three columns.
 
int NumberOfEntries (const std::string &string, char deliniator)
 
int CountColumns (std::string filename, char comment_char='#', char deliniator=' ')
 Count the number of columns in a ASCII data file.
 
void ReadFileNames (std::string dir, const std::string filespec, std::vector< std::string > &filenames, bool verbose)
 Reads the file names in a directory that contain a specific sub string.
 
bool check_directory (std::string dir)
 returns true if director exists and false if it doesn't
 
bool make_directories (const std::string &root_dir)
 make a directory if it doesn't exist
 
template<typename T >
void ReadASCII (std::vector< T > &data, std::string filename, int &columns, int &rows, char comment_char='#', int skiplines=0, size_t MaxNrows=std::numeric_limits< size_t >::max(), bool verbose=true)
 This function will read in all the numbers from a multi-column ,space seporated ASCII data file.
 
template<typename T >
int ReadCSVnumerical1 (std::string filename, std::vector< std::vector< T > > &data, std::vector< std::string > &column_names, size_t MaxNumber=100000000, char comment_char='#', char deliniator=',', std::string replace="\\N", std::function< bool(std::vector< T > &)> accept=[](std::vector< T > &v){return true;})
 Read numerical data from a csv file with a header.
 
template<typename T >
size_t ReadCSVrange (std::string filename, std::vector< std::vector< T > > &ranges, std::vector< std::string > &column_names, size_t MaxNumber=100000000, char comment_char='#', char deliniator=',', std::string replace="\\N", std::function< bool(std::vector< T > &)> accept=[](std::vector< T > &v){return true;})
 
template<typename T >
int ReadCSVnumerical2 (std::string filename, std::vector< std::vector< T > > &data, std::vector< std::string > &column_names, size_t Nmax=1000000, char comment_char='#', char deliniator=',', bool header=true, std::string reject="")
 Read numerical data from a csv file with a header.
 
template<typename T >
void writeCSV (const std::string filename, const std::vector< std::string > header, std::vector< T * > &data)
 write a CSV data file for some data vectors
 

Detailed Description

namespace for input/output utilities

Function Documentation

◆ CountColumns()

int Utilities::IO::CountColumns ( std::string filename,
char comment_char = '#',
char deliniator = ' ' )

Count the number of columns in a ASCII data file.

Parameters
filenamename of file
comment_charcomment charactor
deliniatordeliniator between columns

◆ read1columnfile()

template<class T >
void Utilities::IO::read1columnfile ( std::string filename,
std::vector< T > & x,
int skiplines = 0,
bool verbose = false )
Parameters
filenameinput file name
xvector that will contain the first column

◆ read2columnfile()

template<class T1 , class T2 >
void Utilities::IO::read2columnfile ( std::string filename,
std::vector< T1 > & x,
std::vector< T2 > & y,
std::string delineator = " ",
int skiplines = 0,
bool verbose = false )

Read in data from an ASCII file with two columns.

Parameters
filenameinput file name
xvector that will contain the first column
yvector that will contain the second column
delineatorspecific string the seporates columns, ex. ",", "|", etc.

◆ read3columnfile()

template<class T1 , class T2 , class T3 >
void Utilities::IO::read3columnfile ( std::string filename,
std::vector< T1 > & x,
std::vector< T2 > & y,
std::vector< T3 > & z,
std::string delineator = " ",
bool verbose = false )

Read in data from an ASCII file with three columns.

Parameters
filenameinput file name
xvector that will contain the first column
yvector that will contain the second column
zvector that will contain the third column
delineatorspecific string the seporates columns, ex. ",", "|", etc.

◆ ReadASCII()

template<typename T >
void Utilities::IO::ReadASCII ( std::vector< T > & data,
std::string filename,
int & columns,
int & rows,
char comment_char = '#',
int skiplines = 0,
size_t MaxNrows = std::numeric_limits<size_t>::max(),
bool verbose = true )

This function will read in all the numbers from a multi-column ,space seporated ASCII data file.

It will skip the comment lines if they are at the head of the file. The number of columns and rows are returned. The entry at row r and column c will be stored at data[c + column*r].

This function is not particularly fast for large amounts of data. If the number of roaws is large it would be best to use data.reserve() to set the capacity of data large enough that no rellocation of memory occurs.

◆ ReadCSVnumerical1()

template<typename T >
int Utilities::IO::ReadCSVnumerical1 ( std::string filename,
std::vector< std::vector< T > > & data,
std::vector< std::string > & column_names,
size_t MaxNumber = 100000000,
char comment_char = '#',
char deliniator = ',',
std::string replace = "\\N",
std::function< bool(std::vector< T > &)> accept = [](std::vector<T> &v){return true;} )

Read numerical data from a csv file with a header.

It will skip the comment lines if they are at the head of the file. The number of columns and rows are returned. The entries will be stored at data[column][row].

Comments must only be before the data. There must be a line with the column names after the comments and before the data.

This function is not particularly fast for large amounts of data. If the number of rows is large it would be best to use data.reserve() to set the capacity of data large enough that no rellocation of memory occurs.

The accept function can be used to limit the amount of data added. If there is an object, a, used to make this selection this can be done like [&a](str::vector<T> &v}{return a.itsok(v[3],v[4]);} where v corresponds to a row in the data file in order.

return to first data line

clean blank spaces

Parameters
filenamefile name to be read
dataoutput data
column_nameslist of column names
MaxNumbermaximum number of entries read
comment_charcomment charactor for header
deliniatordeliniator between values
replacereplace this string with zero
acceptfunction that determines if a row should be accepted

◆ ReadCSVnumerical2()

template<typename T >
int Utilities::IO::ReadCSVnumerical2 ( std::string filename,
std::vector< std::vector< T > > & data,
std::vector< std::string > & column_names,
size_t Nmax = 1000000,
char comment_char = '#',
char deliniator = ',',
bool header = true,
std::string reject = "" )

Read numerical data from a csv file with a header.

Same as ReadCSVnumerical1 except the order of the data storage is reversed data[row][column].

It will skip the comment lines if they are at the head of the file. The number of columns and rows are returned.

Comments must only be before the data. Then if header==true there should be a line of column names. If header!=true there are non column names.

This function is not particularly fast for large amounts of data. If the number of rows is large it would be best to use data.reserve() to set the capacity of data large enough that no rellocation of memory occurs.

clean blank spaces

Parameters
filenamefile name to be read
dataoutput data
column_nameslist of column names
comment_charcomment charactor for header
deliniatordeliniator between values
headerfalse if there are no column names
rejectreject lines with this entry after striping black space

◆ ReadCSVrange()

template<typename T >
size_t Utilities::IO::ReadCSVrange ( std::string filename,
std::vector< std::vector< T > > & ranges,
std::vector< std::string > & column_names,
size_t MaxNumber = 100000000,
char comment_char = '#',
char deliniator = ',',
std::string replace = "\\N",
std::function< bool(std::vector< T > &)> accept = [](std::vector<T> &v){return true;} )

Finds ranges

return to first data line

clean blank spaces

Parameters
filenamefile name to be read
rangesoutput data
column_nameslist of column names
MaxNumbermaximum number of entries read
comment_charcomment charactor for header
deliniatordeliniator between values
replacereplace this string with zero
acceptfunction that determines if a row should be accepted

◆ ReadFileNames()

void Utilities::IO::ReadFileNames ( std::string dir,
const std::string filespec,
std::vector< std::string > & filenames,
bool verbose )

Reads the file names in a directory that contain a specific sub string.

Parameters
dirpath to directory containing fits files
filespecstring of charactors in file name that are matched. It can be an empty string.
filenamesoutput vector of PixelMaps

◆ writeCSV()

template<typename T >
void Utilities::IO::writeCSV ( const std::string filename,
const std::vector< std::string > header,
std::vector< T * > & data )

write a CSV data file for some data vectors

example:

std::vector<std::string> header = {"alpha","kappa","gamma"};

std::vector<double> v1 = {1,2,3}; std::vector<double> v2 = {1.1,2.1,3.1}; std::vector<double> v3 = {3,4,5};

std::vector<std::vector<double> *> data; data.push_back(&v1); data.push_back(&v2); data.push_back(&v3); writeCSV(filename,header,data);

Parameters
filenameoutput file path/name
headercolumn labels
dataobjects must have operator []