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

wrap.cc

Go to the documentation of this file.
00001 // wrap.c
00002 // geos1::aCC wrap.c -o wrap
00003 // usage: wrap file [a b [ofile]]
00004 // wrap file to interval [a b)
00005 // file: binary float pixel interleaved
00006 // default interval [-pi pi)
00007 // default output file: ifile.wrap
00008 //
00009 // exit levels:
00010 //  0: successful exit;
00011 // -1: wrong input;
00012 //  1: input file cannot be opened;
00013 //  2: output file cannot be opened;
00014 
00015 // idea to read in diff. formats (float int etc.)
00016 // and output the values in format char (1B per pixel)
00017 //
00018 //  RCS: // // // // // // // // // // // // // // // // // // // // //
00019 // $Header: /users/kampes/DEVELOP/DORIS/SARtools/RCS/wrap.cc,v 3.6 2003/06/24 08:37:12 kampes Exp $ //
00020 // // // // // // // // // // // // // // // // // // // // // // // //
00021 
00022 
00023 using namespace std;
00024 #include <iostream>                             // cout
00025 #include <fstream>                              // file
00026 #include <cstdlib>                              // exit
00027 #include <cmath>                                // atan etc.
00028 #include <cstring>      // strcat etc.
00029 
00030 
00031 void usage(char *programname)
00032   {
00033   cerr << "\nProgram: " << programname 
00034        << " wraps float binary file to interval [a,b)\n"
00035        << "\n  USAGE:\n\t" << programname
00036        << " infile [a b [ofile]]\n\n"
00037        << "  EXAMPLE:\n\t" << programname
00038        << " interferogram.raw -4pi 4pi interf4.raw\n"
00039        << "\ndefault output file    == infile.wrap"
00040        << "\ndefault interval [a b) == [-pi pi)\n\n";
00041   exit(-1);
00042   }
00043 
00044 int main(int argc, char* argv[])
00045   {
00046   //char ident[] = "@(#)Doris software, kampes@geo.tudelft.nl";
00047   char ident[] = "@(#)wrap: Doris software, $Revision: 3.6 $, $Author: kampes $";
00048   cerr << ident << endl;
00049   const int ONE27   = 127;
00050   const float PI    = 4.*atan(1.);              // pi
00051   char ifile[ONE27];                            // input file name
00052   char ofile[ONE27] = " ";                      // output filename == "ifile.ml"
00053   char dummy[ONE27];                            // dummy string
00054   //char dummy2[ONE27];                         // dummy string
00055   float a = -999.;                              // start interval
00056   float b = -999.;                              // stop interval
00057   register int i;                               // general counter
00058   const int sizeofelement = sizeof(float);      // data in float file
00059 
00060 // ====== Handle input ======
00061   switch (argc) // wrap infile a b outfile
00062     {
00063     case 3:
00064       usage(argv[0]);                           // 3 arg. not allowed.
00065       break;                                    // not required
00066 
00067     case 5:
00068       strcpy(ofile,argv[4]);                    // output filename arg4
00069       //--- fall through ---//
00070 
00071     case 4:
00072       strcpy(dummy,argv[3]);                    // interval: b
00073       i = strlen(dummy);
00074       if (dummy[i-1] == 'i')                    // likely to be pi
00075         {
00076         if (!strcmp(dummy,"pi"))
00077           b = PI;
00078         else if (!strcmp(dummy,"-pi"))
00079           b = -PI;
00080         else
00081           {
00082           dummy[i-2]='\0';
00083           b = PI*atof(dummy);
00084           }
00085         }
00086       else
00087         {
00088         b = atof(dummy);
00089         }
00090 
00091       strcpy(dummy,argv[2]);                    // interval: a
00092       i = strlen(dummy);
00093       if (dummy[i-1] == 'i')                    // likely to be pi
00094         {
00095         if (!strcmp(dummy,"pi"))
00096           a = PI;
00097         else if (!strcmp(dummy,"-pi"))
00098           a = -PI;
00099         else
00100           {
00101           dummy[i-2]='\0';
00102           a = PI*atof(dummy);
00103           }
00104         }
00105       else
00106         {
00107         a = atof(dummy);
00108         }
00109       //--- fall through ---//
00110 
00111     case 2:
00112       strcpy(ifile,argv[1]);                    // input filename arg1
00113       break; // ---      ---//
00114 
00115     default:
00116       usage(argv[0]);
00117     } // switch input
00118 
00119 // ______ Set defaults if required _____
00120   if (abs(a+999.) < 1e-10)
00121     a = -PI;
00122   if (abs(b+999.) < 1e-10)
00123     b =  PI;
00124   if (!strcmp(ofile," "))                       // nothing specified
00125     {
00126     strcpy(ofile,ifile);
00127     strcat(ofile,".wrap");
00128     }
00129 
00130 // ______ Check input ______
00131   if (a>=b)
00132     {
00133     cerr << "interval (a,b) = (" << a << "," << b 
00134          << "): a should be smaller than b.\n";
00135     usage(argv[0]);
00136     }
00137 
00138   if (abs(a+b)>1e-10 && abs(a)>1e-10)
00139     {
00140     cerr << "interval (a,b): should be either: a=-b or a=0\n";
00141     usage(argv[0]);
00142     }
00143   if (!strcmp(ofile,ifile))
00144     {
00145     cerr << "input file name same as output file name: "
00146          << ifile << " = " << ofile << endl;
00147     usage(argv[0]);
00148     }
00149 
00150   cerr << "Program parameters:\n\t" << argv[0] << " " 
00151        << ifile << " " << a << " " << b << " " << ofile << endl;
00152 
00153 
00154 // ====== Start wrapping ======
00155 //#if __GNUC__ >= 3
00156 //  ifstream image(ifile, ios::in | ios::nocreate | ios::binary);
00157 //#else
00158   ifstream image(ifile, ios::in | ios::binary);
00159 //#endif
00160 
00161   image.seekg(0,ios::end);                    // filepointer at end
00162 
00163   if (!image) cerr << "Problem opening file: " << ifile << endl, exit(1);
00164   const int totalbytes = image.tellg();
00165   const int numberofpixels = totalbytes/sizeofelement;
00166   image.seekg(0,ios::beg);                      // start of file
00167   ofstream wrapped(ofile, ios::out | ios::binary | ios::trunc);
00168   if (!wrapped) cerr << "Problem opening file: " << ofile << endl, exit(2);
00169 
00170   register float phase;
00171   const float bereik = b-a;
00172   const float normal = (.5*bereik)/PI;
00173   bool ais0 = false;
00174   if (abs(a)<1e-10)
00175     ais0 = true;
00176 
00177   for (i=0; i<numberofpixels; ++i)
00178     {
00179     image.read((char*)&phase,sizeofelement);
00180 
00181 // ______ c = cos(p)+ i*sin(p); p=atan2(im,re); ______
00182     phase /= normal;
00183     phase  = atan2(sin(phase),cos(phase));      // [-pi,pi]
00184     phase *= normal;                            // [a,-a]
00185     if (ais0)
00186       if (phase < 0)
00187         phase += bereik;                        // [0,b]
00188 
00189     wrapped.write((char*)&phase,sizeofelement);
00190     } // loop over all pixels
00191 
00192 // ______ Tidy up ______
00193   image.close();
00194   wrapped.close();
00195 
00196   cout << "\n\nAll done!\n";
00197   return 0;
00198   } // END

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