Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members

productinfo.cc

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 1999-2003 Bert Kampes
00003  * Copyright (c) 1999-2003 Delft University of Technology, The Netherlands
00004  *
00005  * This file is part of Doris, the Delft o-o radar interferometric software.
00006  *
00007  * Doris program is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation; either version 2 of the License, or
00010  * (at your option) any later version.
00011  *
00012  * Doris is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020  *
00021  * Publications that contain results produced by the Doris software should
00022  * contain an acknowledgment. (For example: The interferometric processing
00023  * was performed using the freely available Doris software package developed
00024  * by the Delft Institute for Earth-Oriented Space Research (DEOS), Delft
00025  * University of Technology, or include a reference to: Bert Kampes and
00026  * Stefania Usai. \"Doris: The Delft Object-oriented Radar Interferometric
00027  * software.\" In: proceedings 2nd ITC ORS symposium, August 1999. (cdrom)).
00028  *
00029  */
00030 /****************************************************************
00031  * $Source: /users/kampes/DEVELOP/DORIS/doris/src/RCS/productinfo.cc,v $        *
00032  * $Revision: 3.12 $                                            *
00033  * $Date: 2005/04/11 13:47:45 $                                 *
00034  * $Author: kampes $                                            *
00035  *                                                              *
00036  * implementation of product info class.                        *
00037  * - data filling/updating.                                     * 
00038  * - reading in matrices.                                       *
00039  * - etc.                                                       *
00040  #%// BK 01-Sep-2000
00041  ****************************************************************/
00042 
00043 
00044 #include "constants.hh"                 // DEBUG
00045 #include "productinfo.hh"               // declarations, constants.h
00046 #include "ioroutines.hh"                // printcpu
00047 #include "exceptions.hh"                 // my exceptions class
00048 
00049 
00050 
00051 /****************************************************************
00052  *    productinfo::fillproductinfo                              *
00053  * Fills data of productinfo object, read from resultfile after *
00054  * steps that produced a product, identified by 'iden'.         *
00055  * 13 lines are checked for info, first occurence counts.       *
00056  * exit if 8 are found, or 15 lines are checked.                *
00057  *                                                              *
00058  *  "Data_output_file:"                                         *
00059  *  "Data_output_format:"                                       *
00060  *  "Multilookfactor_azimuth_direction:"                        *
00061  *  "Multilookfactor_range_direction:"                          *
00062  *  "First_line"  (w.r.t. original):                            *
00063  *  "Last_line"  (w.r.t. original):                             *
00064  *  "First_pixel"  (w.r.t. original):                           *
00065  *  "Last_pixel"  (w.r.t. original):                            *
00066  *                                                              *
00067  * input:                                                       *
00068  *  - resultfilename                                            *
00069  *  - identifier                                                *
00070  * output:                                                      *
00071  *  - (updated) productinfo                                     *
00072  *                                                              *
00073  *    Bert Kampes, 22-Dec-1998                                  *
00074  *  Dont know why found1 is used more then ones?                *
00075  *  BK 13-april-2000                                            *
00076  #%// BK 01-Sep-2000 class implementation                       *
00077  * added check for filesize                                     *
00078  #%// BK 08-Mar-2001                                            *
00079  ****************************************************************/
00080 void productinfo::fillproductinfo(
00081         const char* resultfile,
00082         const char* iden)
00083   {
00084   TRACE_FUNCTION("fillproductinfo (BK 01-Sep-2000)")
00085   char                  word[ONE27]=" ";
00086   char                  dummyline[ONE27];
00087 
00088 // ______Open file______
00089   //ifstream resfile(resultfile, ios::in | ios::nocreate);
00090   ifstream resfile(resultfile, ios::in);
00091   bk_assert(resfile,resultfile,__FILE__,__LINE__);
00092 
00093   bool foundiden = false;
00094   while(resfile)
00095     {
00096     if (strcmp(word,iden))                              // Lookfor identifier
00097       {
00098       resfile.getline(dummyline,ONE27,'\n');            // next line
00099       resfile >> word;                                  // read word
00100       }
00101     else
00102       {
00103       foundiden = true;
00104       break;
00105       }
00106     }
00107 
00108 // ______Check if section has been found______
00109   if (!foundiden)
00110     {
00111     ERROR << "(fillproductinfo). identifier: \"" << iden
00112          << "\" not found in file: "            << resultfile;
00113     PRINT_ERROR(ERROR.get_str())
00114     throw(file_error);
00115     }
00116 
00117 // ======Extract info (file name/format and window size)======
00118   int32 numlineschecked = 0;                    // number of lines checked after iden
00119   bool foundfilename    = false;
00120   bool foundfileformat  = false;
00121   bool foundfirstline   = false;
00122   bool foundlastline    = false;
00123   bool foundfirstpixel  = false;
00124   bool foundlastpixel   = false;
00125   bool foundmlL         = false;
00126   bool foundmlP         = false;
00127 
00128   while(resfile)                                 // i.e. rest of file
00129     {
00130     numlineschecked++;
00131     resfile.getline(dummyline,ONE27,'\n');       // next line
00132     resfile >> word;                             // read word
00133 
00134     if (!strcmp(word,"Data_output_file:"))
00135       {
00136       if (!foundfilename)
00137         {
00138         foundfilename = true;
00139         resfile >> file;
00140         DEBUG << "String: \"Data_output_file:\", \t\t\tvalue: " << file;
00141         DEBUG.print();
00142         }
00143       else
00144         WARNING.print("String: \"Data_output_file:\" found again (ignored).");
00145       }
00146     else if (!strcmp(word,"Data_output_format:"))
00147       {
00148       if (!foundfileformat)
00149         {
00150         foundfileformat = true;
00151         resfile >> word;
00152         if (!strcmp(word,"complex_short"))
00153           formatflag = FORMATCI2;
00154         else if (!strcmp(word,"complex_real4"))
00155           formatflag = FORMATCR4;
00156         else if (!strcmp(word,"real4"))
00157           formatflag = FORMATR4;
00158         else if (!strcmp(word,"hgt"))
00159           formatflag = FORMATHGT;
00160         else
00161           {
00162           PRINT_ERROR("wrong format specifier (impossible?)")
00163           throw(unhandled_case_error);
00164           }
00165         DEBUG << "String: \"Data_output_format:\", \t\tvalue: " << formatflag;
00166         DEBUG.print();
00167         }
00168       else
00169         WARNING.print("String: \"Data_output_format:\" found again (ignored).");
00170       }
00171 
00172     else if (!strcmp(word,"Multilookfactor_azimuth_direction:"))
00173       {
00174       if (!foundmlL)
00175         {
00176         foundmlL = true;
00177         resfile >> multilookL;
00178         DEBUG << "String: \"Multilookfactor_azimuth_direction:\", value: "
00179              << multilookL;
00180         DEBUG.print();
00181         }
00182       else
00183         WARNING.print("String: \"Multilookfactor_azimuth_direction:\" found again (ignored).");
00184       }
00185     else if (!strcmp(word,"Multilookfactor_range_direction:"))
00186       {
00187       if (!foundmlP)
00188         {
00189         foundmlP = true;
00190         resfile >> multilookP;
00191         DEBUG << "String: \"Multilookfactor_range_direction:\", \tvalue: "
00192              << multilookP;
00193         DEBUG.print();
00194         }
00195       else
00196         WARNING.print("String: \"Multilookfactor_range_direction:\" found again (ignored).");
00197       }
00198 
00199     else if (!strcmp(word,"First_line"))                // (w.r.t. original):
00200       {
00201       if (!foundfirstline)
00202         {
00203         foundfirstline = true;
00204         resfile >> word >> word >> win.linelo ;
00205         DEBUG << "String: \"First_line:\", \t\t\tvalue: " << win.linelo;
00206         DEBUG.print();
00207         }
00208       else
00209         WARNING.print("String: \"First_line:\" found again (ignored).");
00210       }
00211 
00212     else if (!strcmp(word,"Last_line"))                 // (w.r.t. original):
00213       {
00214       if  (!foundlastline)
00215         {
00216         foundlastline = true;
00217         resfile >> word >> word >> win.linehi ;
00218         DEBUG << "String: \"Last_line:\", \t\t\tvalue: " << win.linehi;
00219         DEBUG.print();
00220         }
00221       else
00222         WARNING.print("String: \"Last_line:\" found again (ignored).");
00223       }
00224 
00225     else if (!strcmp(word,"First_pixel"))               // (w.r.t. original):
00226       {
00227       if (!foundfirstpixel)
00228         {
00229         foundfirstpixel = true;
00230         resfile >> word >> word >> win.pixlo ;
00231         DEBUG << "String: \"First_pixel:\", \t\t\tvalue: " << win.pixlo;
00232         DEBUG.print();
00233         }
00234       else
00235         WARNING.print("String: \"First_pixel:\" found again (ignored).");
00236       }
00237 
00238     else if (!strcmp(word,"Last_pixel"))                // (w.r.t. original):
00239       {
00240       if (!foundlastpixel)
00241         {
00242         foundlastpixel = true;
00243         resfile >> word >> word >> win.pixhi ;
00244         DEBUG << "String: \"Last_pixel:\", \t\t\tvalue: " << win.pixhi;
00245         DEBUG.print();
00246         }
00247       else
00248         WARNING.print("String: \"Last_pixel:\" found again (ignored).");
00249       }
00250 
00251     // ______ Check if everything has been found, or too much lines read ______
00252     if (foundfilename  && foundfileformat &&
00253         foundfirstline && foundlastline && foundfirstpixel && foundlastpixel &&
00254         foundmlL       && foundmlP) break; 
00255     //if (numlineschecked == 13) break;
00256     // report that 13 was too little for some output sections
00257     // so increased it to 15, BK 07-05-2002
00258     if (numlineschecked == 15) break;
00259     } // while resultfile
00260 
00261 
00262   // ______ Check filesize with format/dimensions (BK 08-Mar-2001) ______
00263   // BK 08-Mar-2001
00264   //ifstream tmpfile(file, ios::in | ios::nocreate);
00265   ifstream tmpfile(file, ios::in);
00266   if (tmpfile)
00267     {
00268     tmpfile.seekg(0,ios::end); // internal filesize, normal one exists if not exists
00269     uint filesizetrue  = tmpfile.tellg();
00270     int32 bytesperelem = 4;
00271     if (formatflag==FORMATCI2) bytesperelem=4;
00272     if (formatflag==FORMATCR4) bytesperelem=8;
00273     if (formatflag==FORMATR4) bytesperelem=4;
00274     if (formatflag==FORMATI2) bytesperelem=2;
00275     if (formatflag==FORMATI2_BIGENDIAN) bytesperelem=2;
00276     if (formatflag==FORMATR8) bytesperelem=8;
00277     if (formatflag==FORMATHGT) bytesperelem=8;
00278     int32 filesizecomp = int32(win.lines()/multilookL) *
00279                          int32(win.pixels()/multilookP) *
00280                          bytesperelem;
00281     DEBUG << "Checking format/dimensions file=" << file;
00282     DEBUG.print();
00283     if (filesizecomp != filesizetrue)
00284       {
00285       WARNING << "Fileformat or dimensions seem wrong"
00286            << "; bytesperpix=" << bytesperelem
00287            << "; #l=" << win.lines()
00288            << "; #p=" << win.pixels()
00289            << "; size=" << filesizetrue
00290            << "B; size computed=" << filesizecomp << "B";
00291       WARNING.print();
00292       }
00293     else
00294       {
00295       DEBUG.print("Fileformat and dimensions ok.");
00296       }
00297     tmpfile.close();
00298     } // stream ok
00299   else
00300     {
00301     WARNING << "File: " << file
00302          << " does not seem to exist (may not be a problem).";
00303     WARNING.print();
00304     }
00305 
00306 
00307 
00308   // ______Tidy up______
00309   resfile.close();
00310 
00311 #ifdef __DEBUG
00312   DEBUG.print("finished fillproductinfo");
00313   DEBUG.print("content of struct:");
00314   showdata();
00315 #endif
00316   } // END fillproductinfo
00317 
00318 
00319 /****************************************************************
00320  * productinfo::readphase                                       *
00321  *  read data from file in a real4 matrix                       *
00322  *  phase is extracted from hgt, not unwrapped set to NaN       *
00323  *  real4 is read 'as is'.                                      *
00324  *  complex real4: angle is taken.                              *
00325  *  hgt: phase is read, set to NaN if amplitude==0              *
00326  * cutout window in own system starting at line 1,              *
00327  * (no offset win)                                              *
00328  * This function is useful for reading products: complex interf.*
00329  * unwrapped interf. and to get phase for s2h etc.              *
00330  #%// BK 22-Sep-2000                                            *
00331  ****************************************************************/
00332 matrix<real4> productinfo::readphase(
00333         window cutout) const               // window to be read
00334   {
00335   TRACE_FUNCTION("productinfo::readphase (BK 22-Sep-2000)")
00336   // ______ Check if cutout < #lines/pixels ______
00337   const int32 linesondisk  = int32(win.lines()/multilookL);
00338   const int32 pixelsondisk = int32(win.pixels()/multilookP);
00339 
00340   #ifdef __DEBUGMAT2
00341     if (cutout.linelo < 1)
00342       {
00343       PRINT_ERROR("readphase: cutout window larger than what's on disk!")
00344       throw(input_error);
00345       }
00346     if (cutout.linehi>linesondisk)
00347       {
00348       PRINT_ERROR("readphase: cutout window larger than what's on disk!")
00349       throw(input_error);
00350       }
00351     if (cutout.pixlo  < 1)
00352       {
00353       PRINT_ERROR("readphase: cutout window larger than what's on disk!")
00354       throw(input_error);
00355       }
00356     if (cutout.pixhi>pixelsondisk)
00357       {
00358       PRINT_ERROR("readphase: cutout window larger than what's on disk!")
00359       throw(input_error);
00360       }
00361   #endif
00362 
00363   // ______ Open file ______
00364 //#ifdef __NO_IOS_BINARY__
00365 //  ifstream ifile(file, ios::in);
00366 //#else
00367 //  ifstream ifile(file, ios::in | ios::binary);
00368 //#endif
00369   ifstream ifile;
00370   openfstream(ifile,file);
00371   bk_assert(ifile,file,__FILE__,__LINE__);
00372 
00373   matrix<real4> Result(cutout.lines(),cutout.pixels());
00374 
00375   // ====== Actual read data ======
00376   switch (formatflag)
00377     {
00378     case FORMATR4:
00379       {
00380       // why not use readfile function?
00381       DEBUG.print("reading real4 from file");
00382       matrix<real4> LINE(1,cutout.pixels());            // phase
00383       for (int32 line=cutout.linelo; line<=cutout.linehi; ++line)
00384         {
00385         int32 start = (cutout.pixlo-1 + pixelsondisk*(line-1))
00386                       * sizeof(real4);
00387         ifile.seekg(start,ios::beg);
00388         ifile >> LINE;
00389         Result.setrow(line-cutout.linelo,LINE);         // not the fastest way...
00390                                                         // but cannot write directly
00391                                                         // to private data ... (bk)
00392         }
00393       break;
00394       }
00395 
00396     case FORMATCR4:
00397       {
00398       DEBUG.print("reading complex real4 from file (get phase)");
00399       const window dummyoffset(1,99999,1,99999);        // only 1's used -> no offset
00400       matrix<complr4> TMP(cutout.lines(),cutout.pixels());
00401       readfile(TMP,file,linesondisk,cutout,dummyoffset);
00402       Result = angle(TMP);
00403       break;
00404       }
00405 
00406     // ______ hgt: first amplitude band, then phase, then next line ______
00407     case FORMATHGT:
00408       {
00409       DEBUG.print("reading hgt (band interleaved) from file (get phase)");
00410       // ______ Read in one line, set to NaN, fill result ______
00411       matrix<real4> LINE(1,win.pixels()*2);     // disk phase/ampl band interleaved
00412       for (int32 line=cutout.linelo; line<=cutout.linehi; ++line)
00413         {
00414         const int32 start = (line-1) * pixelsondisk * 2 * sizeof(real4);
00415         ifile.seekg(start,ios::beg);            // file pointer to 1st pix of line
00416         ifile >> LINE;
00417         for (int32 pix=cutout.pixlo; pix<=cutout.pixhi; ++pix)
00418           {
00419           // ______ Check amplitude defined as 0 if not unwrapped ok. ______
00420           if (LINE(0,pix-1) == 0.)              // ampl. band
00421             Result(line-cutout.linelo,pix-cutout.pixlo) = NaN;
00422           else                                                  // phase band
00423             Result(line-cutout.linelo,pix-cutout.pixlo) = LINE(0,pix-1+pixelsondisk);
00424           }
00425         }
00426       break;
00427       }
00428 
00429     default:
00430       PRINT_ERROR("readphase::not correct format on file.")
00431       throw(unhandled_case_error);
00432     }
00433 
00434   ifile.close();
00435   return Result;
00436   } // END readphase
00437 
00438 

Generated on Fri Apr 22 15:57:57 2005 for Doris by doxygen 1.3.6