USGS

Isis 3.0 Application Source Code Reference

Home

ascii2isis.cpp

Go to the documentation of this file.
00001 #include "Isis.h"
00002 
00003 #include <iostream>
00004 #include <fstream>
00005 
00006 #include "ProcessBySpectra.h"
00007 #include "ProcessByLine.h"
00008 #include "SpecialPixel.h"
00009 
00010 using namespace std; 
00011 using namespace Isis;
00012 
00013 // Global declarations
00014 double TestSpecial(const double pixel);
00015 void ascii2isis (Buffer &out);
00016 ifstream fin;
00017 string order;
00018 //Initialize values to make special pixels invalid
00019 double null_min = DBL_MAX;
00020 double null_max = DBL_MIN;
00021 double hrs_min = DBL_MAX;
00022 double hrs_max = DBL_MIN;
00023 double lrs_min = DBL_MAX;
00024 double lrs_max = DBL_MIN;
00025 
00026 void IsisMain() {
00027 
00028   //  Open input text file
00029   UserInterface &ui = Application::GetUserInterface();
00030   string from = ui.GetFilename("FROM");
00031   // Get storage order of data
00032   order = ui.GetString("ORDER");
00033 
00034   // Get the size of the cube
00035   int ns = ui.GetInteger("SAMPLES");
00036   int nl = ui.GetInteger("LINES");
00037   int nb = ui.GetInteger("BANDS");
00038   int skip = ui.GetInteger("SKIP");
00039 
00040   //  Setup output cube
00041   CubeAttributeOutput &att = ui.GetOutputAttribute("TO");
00042 
00043   // Set special pixel ranges
00044   if (ui.GetBoolean("SETNULLRANGE")) {
00045     null_min = ui.GetDouble("NULLMIN");
00046     null_max = ui.GetDouble("NULLMAX");
00047   }
00048   if (ui.GetBoolean("SETHRSRANGE")) {
00049     hrs_min = ui.GetDouble("HRSMIN");
00050     hrs_max = ui.GetDouble("HRSMAX");
00051   }
00052   if (ui.GetBoolean("SETLRSRANGE")) {
00053     lrs_min = ui.GetDouble("LRSMIN");
00054     lrs_max = ui.GetDouble("LRSMAX");
00055   }
00056 
00057   fin.open (from.c_str(),std::ios::in);
00058   if (!fin.is_open()) {
00059     string msg = "Cannot open input file [" + from + "]";
00060     throw Isis::iException::Message(Isis::iException::Io,msg,_FILEINFO_);
00061   }
00062 
00063   //  Skip header information if it exists
00064   fin.seekg(skip,std::ios::beg);
00065 
00066   //  Set up process depending on order
00067   if (order == "BSQ") {
00068     ProcessByLine p;
00069 
00070     p.SetOutputCube (ui.GetFilename("TO"),att,ns,nl,nb);
00071     p.StartProcess(ascii2isis);
00072     p.EndProcess();
00073   }
00074   if (order == "BIL") {
00075     ProcessBySpectra p(Isis::ProcessBySpectra::ByLine);
00076 
00077     // Set Special Pixel ranges
00078     p.SetOutputCube (ui.GetFilename("TO"),att,ns,nl,nb);
00079     p.StartProcess(ascii2isis);
00080     p.EndProcess();
00081   }
00082   if (order == "BIP") {
00083     ProcessBySpectra p(Isis::ProcessBySpectra::PerPixel);
00084 
00085     p.SetOutputCube (ui.GetFilename("TO"),att,ns,nl,nb);
00086     p.StartProcess(ascii2isis);
00087     p.EndProcess();
00088   }
00089 
00090   fin.close ();
00091 }
00092 
00093 void ascii2isis (Buffer &out) {
00094   //Define all legal characters for the beginning of a number
00095   const string legal = ".0123456789+-";
00096   for (int i=0; i<out.size(); i++) {
00097     fin >> out[i];
00098     out[i] = TestSpecial(out[i]);
00099     //Discard all nonlegal characters
00100     while ((legal.find(fin.peek())== string::npos) && !fin.eof()) {
00101       fin.ignore();
00102     }
00103   }
00104 }
00105 
00106 /** 
00107   * Tests the pixel. If it is valid it will return the dn value,
00108   * otherwise it will return the Isis special pixel value that
00109   * corresponds to it
00110   * 
00111   * @param pixel The double precision value that represents a
00112   *              pixel.
00113   * @return double  The double precision value representing the
00114   *         pixel will return as a valid dn or changed to an isis
00115   *         special pixel.
00116   */
00117   double TestSpecial(const double pixel){
00118     if (pixel <= null_max && pixel >= null_min){
00119       return Isis::NULL8;
00120     } else if (pixel <= hrs_max && pixel >= hrs_min){
00121       return Isis::HIGH_REPR_SAT8;
00122     } else if (pixel <= lrs_max && pixel >= lrs_min){
00123       return Isis::LOW_REPR_SAT8;
00124     } else {
00125       return pixel;
00126     }
00127   }