USGS

Isis 3.0 Application Source Code Reference

Home

cnetadd.cpp

Go to the documentation of this file.
00001 #include "Isis.h"
00002 
00003 #include "UserInterface.h"
00004 #include "FileList.h"
00005 #include "Filename.h"
00006 #include "Pvl.h"
00007 #include "SerialNumber.h"
00008 #include "SerialNumberList.h"
00009 #include "CameraFactory.h"
00010 #include "ControlNet.h"
00011 #include "ControlPoint.h"
00012 #include "ControlMeasure.h"
00013 #include "iException.h"
00014 #include "CubeManager.h"
00015 #include "iTime.h"
00016 
00017 using namespace std;
00018 using namespace Isis;
00019 
00020 void SetControlPointLatLon( const std::string &incubes, const std::string &cnet );
00021 
00022 std::map< std::string, std::pair<double,double> > p_pointLatLon;
00023 
00024 void IsisMain() {
00025 
00026   UserInterface &ui = Application::GetUserInterface();
00027 
00028   FileList list2 (ui.GetFilename("ADDLIST"));
00029 
00030   bool log = false;
00031   Filename logFile;
00032   if (ui.WasEntered("LOG")) {
00033     log = true;
00034     logFile = ui.GetFilename("LOG");
00035   }
00036   Pvl results;
00037   results.SetName("cnetadd_Results");
00038   PvlKeyword added ("FilesAdded");
00039   PvlKeyword omitted ("FilesOmitted");
00040   PvlKeyword pointsModified ("PointsModified");
00041 
00042   string retrievalOpt = ui.GetString("RETRIEVAL");
00043   if (retrievalOpt == "REFERENCE") {
00044     FileList list1 (ui.GetFilename("FROMLIST"));
00045     SerialNumberList addSerials (ui.GetFilename("ADDLIST"));
00046     SerialNumberList fromSerials (ui.GetFilename("FROMLIST"));
00047 
00048     //Check for duplicate files in the lists by serial number
00049     vector<string> duplicates;
00050     for (int i = 0; i < addSerials.Size(); i++) {
00051 
00052       // Check for duplicate SNs accross the lists
00053       if (fromSerials.HasSerialNumber(addSerials.SerialNumber(i))) {
00054         duplicates.push_back(addSerials.Filename(i));
00055       }
00056 
00057       // Check for duplicate SNs within the addlist
00058       for (int j = i+1; j < addSerials.Size(); j++) {
00059         if (addSerials.SerialNumber(i) == addSerials.SerialNumber(j)) {
00060           string msg = "Add list files [" + addSerials.Filename(i) + "] and [";
00061           msg += addSerials.Filename(j) + "] share the same serial number.";
00062           throw Isis::iException::Message(Isis::iException::User,msg,_FILEINFO_);
00063         }
00064       }
00065     }
00066   
00067     // If duplicates throw error
00068     if (duplicates.size() > 0) {
00069       string msg = "The following files share serial numbers accross lists: [" + duplicates[0];
00070       for (unsigned int k = 1; k < duplicates.size(); k++) {
00071         msg += "," + duplicates[k];
00072       }
00073       msg += "]";
00074       throw Isis::iException::Message(Isis::iException::User,msg,_FILEINFO_);
00075     }
00076   
00077     SetControlPointLatLon( ui.GetFilename("FROMLIST"), ui.GetFilename("CNET") );
00078 
00079   }
00080 
00081   Filename outNet(ui.GetFilename("TO"));
00082   
00083   ControlNet inNet = ControlNet(ui.GetFilename("CNET"));
00084   inNet.SetUserName( Isis::Application::UserName() );
00085   //inNet.SetCreatedDate( Isis::Application::DateTime() );    //This should be done in ControlNet's Write fn
00086   inNet.SetModifiedDate( Isis::iTime::CurrentLocalTime() );
00087   
00088   Progress progress;
00089   progress.SetText("Adding Images");
00090   progress.SetMaximumSteps(list2.size());
00091   progress.CheckStatus();
00092   
00093   // loop through all the images
00094   vector<int> modPoints;
00095   for (unsigned int img = 0; img < list2.size(); img++) {
00096     Pvl cubepvl;
00097     bool imageAdded = false;
00098     cubepvl.Read(list2[img]);
00099     Camera *cam = CameraFactory::Create(cubepvl);
00100     
00101     //loop through all the control points
00102     for (int cp = 0; cp < inNet.Size(); cp++) {
00103       ControlPoint point( inNet[cp] );
00104       
00105       double latitude;
00106       double longitude;
00107       if (retrievalOpt == "REFERENCE") {
00108         // Get the lat/long coords from the existing reference measure
00109         latitude = p_pointLatLon[point.Id()].first;
00110         longitude = p_pointLatLon[point.Id()].second;
00111       }
00112       else {
00113         // Get the lat/long coords from the current control point
00114         latitude = point.UniversalLatitude();
00115         longitude = point.UniversalLongitude();
00116       }
00117 
00118       if (cam->SetUniversalGround(latitude, longitude)) {
00119       
00120         // Make sure the samp & line are inside the image
00121         if (cam->InCube()) {
00122 
00123           ControlMeasure newCm;
00124           newCm.SetCoordinate(cam->Sample(),cam->Line(),ControlMeasure::Estimated);
00125           newCm.SetCubeSerialNumber(SerialNumber::Compose(cubepvl));
00126           newCm.SetDateTime();
00127           newCm.SetChooserName("Application cnetadd");
00128           inNet[cp].Add(newCm);
00129       
00130           if (retrievalOpt == "POINT" && inNet[cp].Size() == 1) {
00131             inNet[cp].SetIgnore(false);
00132           }
00133 
00134           if (log) {
00135 
00136             // If we can't find this control point in the list of control points
00137             // that have already been modified, then add it to the list
00138             bool doesntContainPoint = true;
00139             for (unsigned int i = 0; i < modPoints.size() && doesntContainPoint; i++) {
00140               if (modPoints[i] == cp) doesntContainPoint = false;
00141             }
00142             if (doesntContainPoint) {
00143               modPoints.push_back(cp);
00144             }
00145 
00146             imageAdded = true;
00147           }
00148         }
00149       }
00150     }
00151 
00152     delete cam;
00153     cam = NULL;
00154 
00155     if (log) {
00156       if (imageAdded) added.AddValue(Isis::Filename(list2[img]).Basename());
00157       else omitted.AddValue(Isis::Filename(list2[img]).Basename());
00158     }
00159 
00160     progress.CheckStatus();
00161   }
00162 
00163   if (log) {
00164 
00165     // Shell sort the list of modified control points
00166     int increments[] = { 1391376, 463792, 198768, 86961, 33936, 13776, 4592, 1968,
00167                          861, 336, 112, 48, 21, 7, 3, 1 };
00168     for (unsigned int k = 0; k < 16; k++) {
00169       int inc = increments[k];
00170       for (unsigned int i = inc; i < modPoints.size(); i++) {
00171         int temp = modPoints[i];
00172         int j = i;
00173         while (j >= inc && modPoints[j - inc] > temp) {
00174           modPoints[j] = modPoints[j - inc];
00175           j -= inc;
00176         }
00177         modPoints[j] = temp;
00178       }
00179     }
00180   
00181     // Add the list of modified points to the output log file
00182     for (unsigned int i = 0; i < modPoints.size(); i++) {
00183       pointsModified += inNet[modPoints[i]].Id();
00184     }
00185    
00186     results.AddKeyword(added);
00187     results.AddKeyword(omitted);
00188     results.AddKeyword(pointsModified);
00189   
00190     results.Write(logFile.Expanded());
00191   }
00192 
00193   inNet.Write(outNet.Expanded());
00194 }
00195 
00196 
00197 /**
00198  * Calculates the lat/lon of the ControlNet.
00199  * 
00200  * @param incubes The filename of the list of cubes in the ControlNet
00201  * @param cnet    The filename of the ControlNet
00202  */
00203 void SetControlPointLatLon( const std::string &incubes, const std::string &cnet ) {
00204   SerialNumberList snl( incubes );
00205   ControlNet net( cnet );
00206 
00207   CubeManager manager;
00208   manager.SetNumOpenCubes(50); //Should keep memory usage to around 1GB
00209 
00210   Progress progress;
00211   progress.SetText("Calculating Lat/Lon");
00212   progress.SetMaximumSteps(net.Size());
00213   progress.CheckStatus();
00214 
00215   for (int cp = 0; cp < net.Size(); cp++) {
00216     ControlPoint point( net[cp] );
00217     ControlMeasure cm( point[ net[cp].ReferenceIndex() ] );
00218 
00219     Cube *cube = manager.OpenCube( snl.Filename( cm.CubeSerialNumber() ) );
00220     try {
00221       cube->Camera()->SetImage( cm.Sample(), cm.Line() );
00222       p_pointLatLon[point.Id()].first = cube->Camera()->UniversalLatitude();
00223       p_pointLatLon[point.Id()].second = cube->Camera()->UniversalLongitude();
00224     } catch (Isis::iException &e) {
00225       std::string msg = "Unable to create camera for cube file [";
00226       msg += snl.Filename( cm.CubeSerialNumber() ) + "]";
00227       throw Isis::iException::Message(Isis::iException::System,msg,_FILEINFO_);
00228     }
00229     cube = NULL; //Do not delete, manager still has ownership
00230 
00231     progress.CheckStatus();
00232   }
00233 
00234   manager.CleanCubes();
00235 }