Commit 8157c3921287b34544ef9e5d823a635e818bf4dd

Authored by David Mayerich
1 parent 81e0d221

added parser and filename processing

envi/binary.h
... ... @@ -112,21 +112,21 @@ public:
112 112 }
113 113  
114 114 //save one band from the memory to the file
115   - bool writeZ( T * p, unsigned int page){
  115 + bool write_page( T * p, unsigned int page){
116 116  
117 117 if(p == NULL){
118 118 std::cout<<"ERROR: unable to write into file, empty pointer"<<std::endl;
119 119 exit(1);
120 120 }
121 121  
122   - file.seekg(R[1] * R[0] * page * sizeof(T), std::ios::beg); //write into memory from the binary file
123   - file.write((char *)p, R[0] * R[1] * sizeof(T));
  122 + file.seekg(R[1] * R[0] * page * sizeof(T), std::ios::beg); //seek to the desired location on disk
  123 + file.write((char *)p, R[0] * R[1] * sizeof(T)); //write binary data
124 124  
125 125 return true;
126 126 }
127 127  
128 128 //save one band of the file into the memory, and return the pointer
129   - bool saveZ( T * p, unsigned int page){
  129 + bool read_page( T * p, unsigned int page){
130 130  
131 131 if (page >= R[2]){ //make sure the bank number is right
132 132 std::cout<<"ERROR: page out of range"<<std::endl;
... ... @@ -140,7 +140,7 @@ public:
140 140 }
141 141  
142 142 //saves a hyperplane orthogonal to dimension d at intersection n
143   - bool getSlice(T * dest, unsigned int d, unsigned int n){
  143 + bool read_plane(T * dest, unsigned int d, unsigned int n){
144 144  
145 145 //reset the file pointer back to the beginning of the file
146 146 file.seekg(0, std::ios::beg);
... ... @@ -167,7 +167,7 @@ public:
167 167 }
168 168  
169 169 //save one pixel of the file into the memory, and return the pointer
170   - bool saveXY(T * p, unsigned x, unsigned y){
  170 + bool read_spectrum(T * p, unsigned x, unsigned y){
171 171  
172 172 unsigned int i;
173 173  
... ... @@ -186,17 +186,6 @@ public:
186 186 return true;
187 187 }
188 188  
189   - /*bool open(std::string filename, unsigned int X, unsigned int h = 0){
190   - return open(filename, vec<unsigned int, 1>(X), h);
191   - }
192   -
193   - bool open(std::string filename, unsigned int X, unsigned int Y, unsigned int h = 0){
194   - return open(filename, vec<unsigned int, 2>(X, Y), h);
195   - }
196   -
197   - bool open(std::string filename, unsigned int X, unsigned int Y, unsigned int Z, unsigned int h = 0){
198   - return open(filename, vec<unsigned int, 3>(X, Y, Z), h);
199   - }*/
200 189  
201 190 };
202 191  
... ...
grids/grid_data.cuh renamed to grids/grid_data.h
... ... @@ -8,6 +8,7 @@
8 8 #include "../cuda/threads.h"
9 9 #include "../cuda/error.h"
10 10 #include "../cuda/devices.h"
  11 +#include "../math/vector.h"
11 12  
12 13  
13 14 namespace stim{
... ... @@ -21,9 +22,50 @@ class grid_data{
21 22  
22 23 protected:
23 24  
24   - unsigned long R[D]; //elements in each dimension
25   - T* ptr; //pointer to the data (on the GPU or CPU)
26   - bool gpu; //true if the data is on the GPU
  25 + stim::vector<unsigned long, D> R; //elements in each dimension
  26 + T* ptr; //pointer to the data (on the GPU or CPU)
  27 + bool gpu; //true if the data is on the GPU
  28 +
  29 + //return the total number of values in the binary file
  30 + unsigned long samples(){
  31 +
  32 + unsigned long s = 1;
  33 + for(unsigned int d = 0; d < D; d++)
  34 + s *= R[d];
  35 +
  36 + return s;
  37 +
  38 + }
  39 +
  40 +public:
  41 +
  42 + //write data to disk
  43 + void write(std::string filename){
  44 +
  45 + //open the file as binary for reading
  46 + file.open(filename.c_str(), std::ios::out | std::ios::binary);
  47 +
  48 + //write file to disk
  49 + file.write((char *)p, samples() * sizeof(T));
  50 + }
  51 +
  52 + //load a binary file from disk
  53 + // header size is in bytes
  54 + void read(std::string filename, stim::vector<unsigned long, D> S, unsigned long header = 0){
  55 +
  56 + R = S; //set the sample resolution
  57 +
  58 + //open the file as binary for writing
  59 + file.open(filename.c_str(), std::ios::in | std::ios::binary);
  60 +
  61 + //seek past the header
  62 + file.seekg(header, std::ios::beg);
  63 +
  64 + //read the data
  65 + file.read((char *)p, samples() * sizeof(T));
  66 + }
  67 +
  68 +
27 69  
28 70 };
29 71  
... ...
grids/image_stack.h
... ... @@ -8,6 +8,14 @@ namespace stim{
8 8 template<typename T>
9 9 class image_stack : public virtual grid_data<T, 3>{
10 10  
  11 +public:
  12 +
  13 + void load(std::string wild_name){
  14 +
  15 + }
  16 +
  17 +
  18 +
11 19 };
12 20  
13 21  
... ...
ui/arguments.h renamed to parser/arguments.h
parser/filename.h 0 → 100644
  1 +#ifndef STIM_FILENAME_H
  2 +#define STIM_FILENAME_H
  3 +
  4 +#include <stdio.h> /* defines FILENAME_MAX */
  5 +#ifdef WINDOWS
  6 + #include <direct.h>
  7 + #define GetCurrentDir _getcwd
  8 + #define STIM_FILENAME_DIV '\\'
  9 +#else
  10 + #include <unistd.h>
  11 + #define GetCurrentDir getcwd
  12 + #define STIM_FILENAME_DIV '/'
  13 + #endif
  14 +
  15 +#include <string>
  16 +#include <sstream>
  17 +#include <vector>
  18 +#include <stack>
  19 +#include <algorithm>
  20 +
  21 +#include "../parser/parser.h"
  22 +
  23 +#include <boost/filesystem.hpp>
  24 +
  25 +namespace stim{
  26 +
  27 +//filename class designed to work with both Windows and Unix
  28 +
  29 +class filename{
  30 +
  31 +private:
  32 + void init(){
  33 +
  34 +
  35 + }
  36 +
  37 +protected:
  38 +
  39 + std::string drive; //drive letter (for Windows)
  40 + std::vector<std::string> path; //directory hierarchy
  41 + std::string prefix; //file prefix (without extension)
  42 + std::string ext; //file extension
  43 +
  44 + //replace any incorrect dividers with the appropriate version for the OS
  45 + std::string parse_div(std::string s) {
  46 + #ifdef _WIN32
  47 + std::replace( s.begin(), s.end(), '/', '\\');
  48 + #else
  49 + std::replace( s.begin(), s.end(), '\\', '/');
  50 + #endif
  51 +
  52 + return s;
  53 + }
  54 +
  55 + //parse the file name (prefix and extension)
  56 + void parse_name(std::string fname){
  57 +
  58 + //find the extension
  59 + size_t xpos = fname.find_last_of('.'); //find the position of the extension period (if there is one)
  60 + if(xpos != std::string::npos){ //split the prefix and extension
  61 + prefix = fname.substr(0, xpos);
  62 + ext = fname.substr(xpos + 1);
  63 + }
  64 + else
  65 + prefix = fname;
  66 + }
  67 +
  68 + //parse a file locator string
  69 + void parse(std::string loc){
  70 +
  71 + //determine the drive (if Windows)
  72 + drive = "";
  73 + #ifdef _WIN32
  74 + //if the second character is a colon, the character right before it is the drive
  75 + if(loc[1] == ':'){
  76 + drive = loc[0]; //copy the drive letter
  77 + loc = loc.substr(2); //remove the drive information from the locator string
  78 + }
  79 + #endif
  80 +
  81 + //remove the initial divider
  82 + if(loc[0] == STIM_FILENAME_DIV)
  83 + loc = loc.substr(1);
  84 +
  85 + //determine the file name
  86 + std::string fname = loc.substr( loc.find_last_of(STIM_FILENAME_DIV) + 1 ); //find the file name (including extension)
  87 +
  88 + //parse the file name
  89 + parse_name(fname);
  90 +
  91 + //find the directory hierarchy
  92 + std::string dir = loc.substr(0, loc.find_last_of(STIM_FILENAME_DIV) + 1 );
  93 + path = stim::parser::split(dir, STIM_FILENAME_DIV);
  94 + }
  95 +
  96 +public:
  97 +
  98 + filename(){
  99 + init();
  100 + }
  101 +
  102 + filename(std::string loc){
  103 + init();
  104 + parse(loc);
  105 + }
  106 +
  107 +
  108 +
  109 + std::string get_name(){
  110 + if(prefix == "" && ext == "")
  111 + return "";
  112 + else
  113 + return prefix + "." + ext;
  114 + }
  115 +
  116 + std::string get_extension(){
  117 + return ext;
  118 + }
  119 +
  120 + std::string get_prefix(){
  121 + return prefix;
  122 + }
  123 +
  124 + std::string dir(){
  125 + std::stringstream ss;
  126 +
  127 + //output the drive letter if in Windows
  128 + #ifdef _WIN32
  129 + ss<<drive<<":";
  130 + #endif
  131 + ss<<STIM_FILENAME_DIV;
  132 +
  133 + //output the directory
  134 + for(unsigned int d = 0; d < path.size(); d++)
  135 + ss<<path[d]<<STIM_FILENAME_DIV;
  136 +
  137 + return ss.str();
  138 +
  139 + }
  140 +
  141 + std::string str(){
  142 +
  143 + std::stringstream ss;
  144 +
  145 + ss<<dir()<<get_name();
  146 +
  147 + return ss.str();
  148 + }
  149 +
  150 + //get a list of files matching the current tamplate
  151 + std::vector<stim::filename> get_list(){
  152 +
  153 + boost::filesystem::path p(dir()); //create a path from the current filename
  154 +
  155 + std::vector<stim::filename> file_list;
  156 +
  157 + if(boost::filesystem::exists(p)){
  158 + if(boost::filesystem::is_directory(p)){
  159 +
  160 + typedef std::vector<boost::filesystem::path> vec; // store paths,
  161 + vec v; // so we can sort them later
  162 +
  163 + std::copy(boost::filesystem::directory_iterator(p), boost::filesystem::directory_iterator(), back_inserter(v));
  164 +
  165 + std::sort(v.begin(), v.end()); // sort, since directory iteration
  166 + // is not ordered on some file systems
  167 +
  168 + //compare file names to the current template (look for wild cards)
  169 + for (vec::const_iterator it(v.begin()), it_end(v.end()); it != it_end; ++it)
  170 + {
  171 +
  172 + //if the filename is a wild card *or* it matches the read file name
  173 + if( prefix == "*" || prefix == (*it).filename().stem().string()){
  174 +
  175 + //if the extension is a wild card *or* it matches the read file extension
  176 + if( ext == "*" || "." + ext == (*it).filename().extension().string()){
  177 +
  178 + file_list.push_back((*it).string()); //include it in the final list
  179 + }
  180 + }
  181 +
  182 + }
  183 + }
  184 + }
  185 +
  186 + return file_list;
  187 + }
  188 +
  189 + //gets the current working directory
  190 + static stim::filename cwd(){
  191 +
  192 +
  193 + char cCurrentPath[FILENAME_MAX];
  194 +
  195 + if (!GetCurrentDir(cCurrentPath, sizeof(cCurrentPath))){
  196 + std::cout<<"ERROR getting current working directory."<<std::endl;
  197 + exit(1);
  198 + }
  199 +
  200 + return stim::filename(std::string(cCurrentPath) + STIM_FILENAME_DIV);
  201 + }
  202 +
  203 + void set_name(std::string fname){
  204 + parse_name(fname);
  205 + }
  206 +
  207 + //get a path relative to the current one
  208 + stim::filename get_relative(std::string rel){
  209 +
  210 + std::vector<std::string> rel_path = stim::parser::split(rel, STIM_FILENAME_DIV);
  211 +
  212 + //if the relative path doesn't contain a file, add a blank string to be used as the filename
  213 + if(rel[rel.size()-1] == STIM_FILENAME_DIV)
  214 + rel_path.push_back("");
  215 +
  216 + //create a stack representing the current absolute path
  217 + std::stack<std::string, std::vector<std::string> > s(path);
  218 +
  219 + //for each token in the relative path
  220 + for(int d=0; d<rel_path.size() - 1; d++){
  221 +
  222 + //if the token is a double-dot, pop a directory off of the stack
  223 + if(rel_path[d] == "..")
  224 + s.pop();
  225 + else
  226 + s.push(rel_path[d]);
  227 + }
  228 +
  229 + std::string* end = &s.top() + 1;
  230 + std::string* begin = end - s.size();
  231 + std::vector<std::string> result_path(begin, end);
  232 +
  233 + //create a new path to be returned
  234 + stim::filename result;
  235 + result.path = result_path;
  236 + result.set_name(rel_path.back());
  237 +
  238 + return result;
  239 + }
  240 +
  241 +
  242 +
  243 +
  244 +
  245 +
  246 +
  247 +
  248 +};
  249 +
  250 +
  251 +} //end namespace stim
  252 +
  253 +#endif
... ...
parser/parser.h 0 → 100644
  1 +#ifndef STIM_PARSER_H
  2 +#define STIM_PARSER_H
  3 +
  4 +namespace stim{
  5 +
  6 +class parser{
  7 +
  8 +public:
  9 +
  10 + static std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) {
  11 + std::stringstream ss(s);
  12 + std::string item;
  13 + while (std::getline(ss, item, delim)) {
  14 + elems.push_back(item);
  15 + }
  16 + return elems;
  17 + }
  18 +
  19 + static std::vector<std::string> split(const std::string &s, char delim) {
  20 + std::vector<std::string> elems;
  21 + split(s, delim, elems);
  22 + return elems;
  23 + }
  24 +
  25 +};
  26 +
  27 +} //end namespace stim
  28 +
  29 +#endif
0 30 \ No newline at end of file
... ...
parser/wildcards.h 0 → 100644
  1 +#ifndef STIM_WILDCARDS_H
  2 +#define STIM_WILDCARDS_H
  3 +
  4 +#include <sstream>
  5 +#include <iomanip>
  6 +
  7 +
  8 +//#include <boost/regex.hpp>
  9 +
  10 +namespace stim{
  11 +
  12 +class wildcards{
  13 +public:
  14 + //generate a sequence of strings where '?' is replaced by integer increments
  15 + static std::vector<std::string> increment(std::string input,
  16 + unsigned int start,
  17 + unsigned int end,
  18 + unsigned int step){
  19 +
  20 + size_t a = input.find_first_of('?'); //find the first ?
  21 + size_t b = input.find_last_of('?'); //find the last ?
  22 + size_t n = b - a + 1; //calculate the number of ?
  23 +
  24 + std::string str_a = input.substr(0, a); //split the strings along the wildcard
  25 + std::string str_b = input.substr(b + 1, std::string::npos);
  26 +
  27 + //create a vector to hold the output strings
  28 + std::vector<std::string> out;
  29 +
  30 + //create a stringstream to handle the padding
  31 + for (unsigned int i = start; i <= end; i += step){
  32 +
  33 + std::stringstream ss;
  34 + ss << str_a
  35 + << std::setfill('0')
  36 + << std::setw(n)
  37 + << i
  38 + << str_b;
  39 +
  40 + out.push_back(ss.str());
  41 + }
  42 +
  43 + return out;
  44 +
  45 +
  46 + }
  47 +/*
  48 + //returns a list of files that match the specified wildcards in 'd'
  49 + static std::vector<std::string> get_file_list(std::string s){
  50 +
  51 + stim::filename f(s);
  52 + std::string target_path(f.dir());
  53 + boost::regex filter(f.name());
  54 +
  55 + std::vector< std::string > all_matching_files;
  56 +
  57 + boost::filesystem::directory_iterator end_itr; // Default ctor yields past-the-end
  58 + for( boost::filesystem::directory_iterator i( target_path ); i != end_itr; ++i )
  59 + {
  60 + // Skip if not a file
  61 + if( !boost::filesystem::is_regular_file( i->status() ) ) continue;
  62 +
  63 + boost::smatch what;
  64 +
  65 + // Skip if no match
  66 + if( !boost::regex_match( i->path().filename().string(), what, filter ) ) continue;
  67 +
  68 + // File matches, store it
  69 + all_matching_files.push_back( i->path().filename().string() );
  70 + }
  71 +
  72 + return all_matching_files;
  73 +
  74 + }
  75 +*/
  76 +
  77 +};
  78 +
  79 +
  80 +} //end namespace stim
  81 +
  82 +#endif
... ...