Commit 82a7c08c54657fabe6ea7466813931c6c276133a

Authored by David Mayerich
1 parent 65c1aac0

added an append command for concatenating bands from multiple files

stim/envi/bil.h
... ... @@ -1336,6 +1336,27 @@ public:
1336 1336 }
1337 1337 }
1338 1338  
  1339 + ///Append two files together along the band dimension
  1340 + void append(std::string outfile, bil<T>* C, bool PROGRESS = false) {
  1341 + std::ofstream out(outfile.c_str(), std::ios::binary); //open the output file for writing
  1342 + file.seekg(0, std::ios::beg); //move to the beginning of both files
  1343 + C->file.seekg(0, std::ios::beg);
  1344 + size_t a_bytes = X() * Z() * sizeof(T); //calculate the number of bytes in a single plane of this file
  1345 + size_t b_bytes = C->X() * C->Z() * sizeof(T); //calculate the number of bytes in a single plane of the appending file
  1346 + T* a = (T*)malloc(a_bytes); //allocate space for a plane of the current file
  1347 + T* b = (T*)malloc(b_bytes); //allocate space for a plane of the appended file
  1348 + if (PROGRESS) progress = 0;
  1349 + for (size_t y = 0; y < Y(); y++) {
  1350 + read_plane_xz(a, y); //read a plane from the current file
  1351 + out.write((char*)a, a_bytes); //write the plane to disk
  1352 + C->read_plane_xz(b, y); //read a plane from the appending file
  1353 + out.write((char*)b, b_bytes);
  1354 + if (PROGRESS) progress = (double)(y + 1) / (double)(Y()) * 100;
  1355 + }
  1356 +
  1357 + out.close();
  1358 + }
  1359 +
1339 1360 /// Convolve the given band range with a kernel specified by a vector of coefficients.
1340 1361  
1341 1362 /// @param outfile is an already open stream to the output file
... ...
stim/envi/bip.h
... ... @@ -1562,6 +1562,26 @@ public:
1562 1562 }
1563 1563 }
1564 1564  
  1565 + ///Append two files together along the band dimension
  1566 + void append(std::string outfile, bip<T>* C, bool PROGRESS = false) {
  1567 + std::ofstream out(outfile.c_str(), std::ios::binary); //open the output file for writing
  1568 + file.seekg(0, std::ios::beg); //move to the beginning of both files
  1569 + C->file.seekg(0, std::ios::beg);
  1570 + size_t a_bytes = Z() * sizeof(T); //calculate the number of bytes in a single plane of this file
  1571 + size_t b_bytes = C->Z() * sizeof(T); //calculate the number of bytes in a single plane of the appending file
  1572 + T* a = (T*)malloc(a_bytes); //allocate space for a plane of the current file
  1573 + T* b = (T*)malloc(b_bytes); //allocate space for a plane of the appended file
  1574 + if (PROGRESS) progress = 0;
  1575 + for (size_t xy = 0; xy < X()*Y(); xy++) {
  1576 + spectrum(a, xy); //read a plane from the current file
  1577 + out.write((char*)a, a_bytes); //write the plane to disk
  1578 + C->spectrum(b, xy); //read a plane from the appending file
  1579 + out.write((char*)b, b_bytes);
  1580 + if (PROGRESS) progress = (double)(xy + 1) / (double)(X() * Y()) * 100;
  1581 + }
  1582 +
  1583 + out.close();
  1584 + }
1565 1585 /// Convolve the given band range with a kernel specified by a vector of coefficients.
1566 1586  
1567 1587 /// @param outfile is an already open stream to the output file
... ...
stim/envi/bsq.h
... ... @@ -1359,6 +1359,20 @@ public:
1359 1359 out.close();
1360 1360 }
1361 1361  
  1362 + /// Append an image to this one along the band dimension
  1363 + void append(std::string outfile, bsq<T>* C, bool PROGRESS = false) {
  1364 + std::ofstream out(outfile.c_str(), std::ios::binary); //open the output file for writing
  1365 + file.seekg(0, std::ios::beg); //move to the beginning of both files
  1366 + C->file.seekg(0, std::ios::beg);
  1367 +
  1368 + if (PROGRESS) progress = 0;
  1369 + out << file.rdbuf(); //copy the data from this ENVI file
  1370 + if (PROGRESS) progress = (double)(Z() + 1) / (double)(Z() + C->Z()) * 100;
  1371 + out << C->file.rdbuf(); //copy the data from the appending file
  1372 + if (PROGRESS) progress = 100;
  1373 + out.close();
  1374 + }
  1375 +
1362 1376 /// Convolve the given band range with a kernel specified by a vector of coefficients.
1363 1377  
1364 1378 /// @param outfile is an already open stream to the output file
... ...
stim/envi/envi.h
... ... @@ -1728,6 +1728,67 @@ public:
1728 1728 }
1729 1729 }
1730 1730  
  1731 + void append(std::string outfile, envi C, bool PROGRESS = false) {
  1732 + if (C.header.samples != header.samples || //verify that the images are the same size
  1733 + C.header.lines != header.lines) {
  1734 + std::cout << "ERROR - appended images must be the same size: input = [" << header.samples << " x " << header.lines << "], output = [" << C.header.samples << " x " << C.header.lines << "]" << std::endl;
  1735 + exit(1);
  1736 + }
  1737 + if (C.header.interleave != header.interleave) {
  1738 + std::cout << "ERROR - appended images must have the same interleave format" << std::endl;
  1739 + exit(1);
  1740 + }
  1741 +
  1742 + stim::envi_header new_header = header; //create a header for the output image
  1743 + new_header.bands = header.bands + C.header.bands; //calculate the number of bands in the new image
  1744 +
  1745 + if (header.wavelength.size() != 0 && C.header.wavelength.size() != 0) { //if both files contain wavelength information
  1746 + for (size_t b = 0; b < C.header.wavelength.size(); b++)
  1747 + new_header.wavelength.push_back(C.header.wavelength[b]); //append the wavelength labels to the new header array
  1748 + }
  1749 + else new_header.wavelength.clear();
  1750 +
  1751 + if (header.band_names.size() != 0 && C.header.band_names.size() != 0) { //if both files contain band name information
  1752 + for (size_t b = 0; b < C.header.band_names.size(); b++)
  1753 + new_header.band_names.push_back(C.header.band_names[b]); //append the wavelength labels to the new header array
  1754 + }
  1755 + else new_header.wavelength.clear();
  1756 +
  1757 + new_header.save(outfile + ".hdr"); //save the output
  1758 +
  1759 + if (header.interleave == envi_header::BSQ) {
  1760 + if (header.data_type == envi_header::float32)
  1761 + ((bsq<float>*)file)->append(outfile, (bsq<float>*)C.file, PROGRESS);
  1762 + else if (header.data_type == envi_header::float64)
  1763 + ((bsq<double>*)file)->append(outfile, (bsq<double>*)C.file, PROGRESS);
  1764 + else {
  1765 + std::cout << "ERROR: unidentified data type" << std::endl;
  1766 + exit(1);
  1767 + }
  1768 + }
  1769 + else if (header.interleave == envi_header::BIL) {
  1770 + if (header.data_type == envi_header::float32)
  1771 + ((bil<float>*)file)->append(outfile, (bil<float>*)C.file, PROGRESS);
  1772 + else if (header.data_type == envi_header::float64)
  1773 + ((bil<double>*)file)->append(outfile, (bil<double>*)C.file, PROGRESS);
  1774 + else {
  1775 + std::cout << "ERROR: unidentified data type" << std::endl;
  1776 + exit(1);
  1777 + }
  1778 + }
  1779 + else if (header.interleave == envi_header::BIP) {
  1780 + if (header.data_type == envi_header::float32)
  1781 + ((bip<float>*)file)->append(outfile, (bip<float>*)C.file, PROGRESS);
  1782 + else if (header.data_type == envi_header::float64)
  1783 + ((bip<double>*)file)->append(outfile, (bip<double>*)C.file, PROGRESS);
  1784 + else {
  1785 + std::cout << "ERROR: unidentified data type" << std::endl;
  1786 + exit(1);
  1787 + }
  1788 + }
  1789 +
  1790 + }
  1791 +
1731 1792 /// Convolve the given band range with a kernel specified by a vector of coefficients.
1732 1793  
1733 1794 /// @param outfile is the combined file to be output
... ...