logviewer.C

Go to the documentation of this file.
00001 /* Gnome gxsm - Gnome X Scanning Microscopy
00002  * universal STM/AFM/SARLS/SPALEED/... controlling and
00003  * data analysis software
00004  *
00005  * Gxsm Tool Name: logviewer.C
00006  * ========================================
00007  * 
00008  * Copyright (C) 2004 The Free Software Foundation
00009  *
00010  * Authors: Thorsten Wagner <thorsten.wagner@uni-essen.de> 
00011  *
00012  * This program is free software; you can redistribute it and/or modify
00013  * it under the terms of the GNU General Public License as published by
00014  * the Free Software Foundation; either version 2 of the License, or
00015  * (at your option) any later version.
00016  *
00017  * This program is distributed in the hope that it will be useful,
00018  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00019  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020  * GNU General Public License for more details.
00021  *
00022  * You should have received a copy of the GNU General Public License
00023  * along with this program; if not, write to the Free Software
00024  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
00025  */
00026 
00027 #include "logviewer.h"
00028 
00030 // function definition: main         //
00032 int main(int argc, char *argv[])
00033 {
00034 
00035 /***************************************************************
00036 *** Define some variables 
00037 ***************************************************************/
00038                         gchar inputfile[WSXM_MAXCHARS];                 // input file name
00039         gchar outputfile[WSXM_MAXCHARS];                // prefix for output file
00040         gchar xpos[WSXM_MAXCHARS];                                      // used to buffer x position string
00041         gchar ypos[WSXM_MAXCHARS];                                      // used to buffer y position string
00042         guint rows, cols;                   // number of rows, cols within a log file
00043         guint buffer_cols = 0;                                                  // to check the log file integrity
00044         enum FileTypeEnum type = IV;          // default file type is single IV curve 
00045         guint xchannel = Umon;                                                  // defines xchannel (within SourceChannels)
00046         gdouble xscale = 1.0;                                                           // custom scaling for x-axis
00047         guint ychannel = ADC5;                                                  // defines ychannel (within SourceChannels)
00048         gdouble yscale = 1.0;                                                           // custom scaling for y-axis
00049         
00050         GPtrArray *ptrscans;                                                            // array that holds all data
00051                         ptrscans = g_ptr_array_new();
00052         glong points = 0;                                                                               // number of data points in log file
00053         glong source = 0;                                                                               // source channels used in log file
00054         gint x = 1;                                                                                                     // Number of x-Channel (Bias-Voltage) (within ptrscans)
00055                         gint y = 1;                                                                                                     // Number of y-Channel (Tunneling Current) (within ptrscans)
00056                         gdouble u_start = 0;                                                            // bias voltage to start with during STS
00057                         gdouble u_end = 0;                                                                      // bias voltage to end with during STS
00058 
00059                         // define some (global) variables
00060                         gchar buffer_string[WSXM_MAXCHARS];
00061 
00062 /***************************************************************
00063 *** Analyse arguments 
00064 ***************************************************************/
00065         if (argc == 2) {        // Test for --help option
00066                 if (strstr(argv[1],"--help")) {
00067                 std::cout << "Usage: logviewer [options] inputfile [prefix for outputfile]" << std::endl;
00068         std::cout << "Possible options are:" << std::endl;
00069         std::cout << "\t--help\tThis help message" << std::endl;
00070         std::cout << "\t-IV\tSave as IV file (*.iv.cur)" << std::endl;
00071         std::cout << "\t\tPossible file types: IV, IZ, ZV, FZ" << std::endl;
00072         std::cout << "\t-CITS\tnot implement yet" << std::endl;
00073         std::cout << "\t-x=[factor*]channel" << std::endl;
00074         std::cout << "\t-y=[factor*]channel" << std::endl;
00075         std::cout << "\t\tPossible channels: Zmon, Umon, ADC5, ADC0, ADC1, ADC2, \n\t\tADC3, ADC4, ADC6, ADC7, LockIn1st, LockIn2nd, LockIn0" << std::endl;
00076         return -1;      
00077         }
00078         }
00079         if (argc < 3) { // Need at least three arguments (programm name, option or input, input or output prefix
00080                 std::cerr << "Too few arguments. Specify spectra type and input file." << std::endl;
00081                 std::cerr << "Usage: logviewer [options] inputfile [prefix for outputfile]" << std::endl;
00082                 return -1;
00083         } else {                // Now check each argument if it is an option
00084                 for (int i = 1; i < argc; ++i) {
00085                         // std::cout << "Logviewer was called with: " << i << ". " << argv[i] << std::endl;
00086                 // Check if file type is given as an argument. If not, IV is default.
00087                 extern FileIdStruc FileId[];
00088                 FileIdStruc *FileIdPtr = FileId;
00089                 for (; FileIdPtr->name != 0; ++FileIdPtr) {
00090                                 if (strstr(FileIdPtr->option, argv[i])) {
00091                         std::cout << "Option " << FileIdPtr->name << " detected." << std::endl;
00092                         type = (enum FileTypeEnum) FileIdPtr->index;
00093                         xchannel = (ProbeChannelDefault[(guint)FileIdPtr->index]).xchannel;
00094                         ychannel = (ProbeChannelDefault[(guint)FileIdPtr->index]).ychannel;
00095                         if (strstr(argv[argc - 2],"-")){
00096                                 // only last argument can be file name, so make own prefix for output file
00097                                         if (strstr(argv[argc -1],"-")){
00098                                                 std::cerr << "No file name was specified. Add at least filename for input." << std::endl;
00099                                                 return -1;
00100                                         } else {
00101                                                 strncpy (inputfile, argv [argc -1], WSXM_MAXCHARS);
00102                                                 strncpy (outputfile, FileIdPtr->name, WSXM_MAXCHARS);
00103                                         }
00104                                 } else {
00105                                         strncpy (inputfile, argv [argc -2], WSXM_MAXCHARS);
00106                                         strncpy (outputfile, argv [argc -1], WSXM_MAXCHARS);    
00107                                 }
00108                         std::cout << "Prefix for output file is " << outputfile << ". The type is " << type <<"." << std::endl;
00109                         break;
00110                                 }
00111                         }
00112                 
00113         if (strstr(argv[i], "-x=")) {
00114             SourceChannelStruc *SourceChannelPtr = SourceChannels;
00115             for (;SourceChannelPtr->number < END; SourceChannelPtr++) {
00116                         if (strstr(argv[i],SourceChannelPtr->name)||strstr(argv[i],SourceChannelPtr->aliasname)) {
00117                                 xchannel = SourceChannelPtr->number;
00118                                 std::cout << "Customized x-channel: " << SourceChannelPtr->name << std::endl;
00119                         }
00120                 }
00121                 if (strstr(argv[i],"*")){
00122                                 // customized scaling of the x-axis
00123                                 xscale = atof(g_strsplit_set(argv[i],"=*",-1)[1]);
00124                                 std::cout << "x scaling factor is: " << xscale << std::endl;
00125                 }
00126                 }
00127           if (strstr(argv[i], "-y=")) {
00128                 SourceChannelStruc *SourceChannelPtr = SourceChannels;
00129                 for (;SourceChannelPtr->number < END; SourceChannelPtr++) {
00130                         if (strstr(argv[i],SourceChannelPtr->name)||strstr(argv[i],SourceChannelPtr->aliasname)) {
00131                                 ychannel = SourceChannelPtr->number;
00132                                 std::cout << "Customized y-channel: " << SourceChannelPtr->name << std::endl;
00133                         }
00134                 }
00135                 if (strstr(argv[i],"*")){
00136                         // customized scaling of the y-axis
00137                         yscale = atof(g_strsplit_set(argv[i],"=*",-1)[1]);
00138                         std::cout << "y scaling factor is: " << yscale << std::endl;
00139                 }
00140                 }
00141         }
00142 
00143         /*************************************************************
00144         *** Read data from file
00145         *************************************************************/
00146         if (argc >= 2) {
00147   fstream f;
00148   f.open(inputfile, ios::in);
00149         if (!f.good()) {
00150                 std::cerr << "Error: Not possible to open file for reading: " << inputfile << std::endl;
00151                 return -1;
00152         } else {
00153         std::cout << "Opened succesfull file: " << inputfile << std::endl;
00154                 
00155         while (f.getline(buffer_string, WSXM_MAXCHARS)) {
00156                 if (strstr(buffer_string, "# DSPControlClass->Points: ")) {
00157                         f.seekg((int)(-f.gcount() + strlen("# DSPControlClass->Points: ")), ios::cur);
00158                         f.getline(buffer_string, WSXM_MAXCHARS);
00159                         points = atoi(buffer_string);
00160                         std::cout << "DSPControlClass->Points: " << points << std::endl;
00161                 }
00162 
00163                 if (strstr(buffer_string, "# DSPControlClass->Source: ")) {
00164                         f.seekg((int) (-f.gcount() + strlen("# DSPControlClass->Source: ")), ios::cur);
00165                         f.getline(buffer_string, WSXM_MAXCHARS);
00166                         source = atoi(buffer_string);
00167                         std::cout << "DSPControlClass->Source: " << source << std::endl;
00168                         SourceChannelStruc *SourceChannelPtr = SourceChannels;
00169                         for (; SourceChannelPtr->bitcoding > 0; SourceChannelPtr++) {
00170                             if (source & SourceChannelPtr->bitcoding) {
00171                                                 scandata *scanptr = new scandata(0, SourceChannelPtr->number);
00172                                                 g_ptr_array_add(ptrscans, scanptr);
00173                                 }
00174                                 }
00175                                         for (guint i = 0; i < ptrscans->len; ++i) {
00176                                 if (strstr (getscan(ptrscans, i)->getchannel()->str, (SourceChannels[(enum SourceChannelEnum)ychannel]).name )) {
00177                                                         y = i;
00178                                                         std::cout << i << ". channel is " << getscan(ptrscans, i)->getchannel()->str << ". Use as Y." << std::endl;
00179                                 } else {
00180                                         if (strstr (getscan(ptrscans, i)->getchannel()->str, (SourceChannels[(enum SourceChannelEnum)xchannel]).name )) {
00181                                                                 x = i;
00182                                                                 std::cout << i << ". channel is " << getscan(ptrscans, i)->getchannel()->str << ". Use as X." << std::endl;
00183                                         } else {
00184                                                 std::cout << i << ". channel is " << getscan(ptrscans, i)->getchannel()->str << "." << std::endl;
00185                                                         }
00186                                 }
00187                                         }
00188                     }
00189                     // neuer C++ cast: static_cast<int>(integer);
00190                     if (strstr(buffer_string, "# DSPControlClass->U_start: ")) {
00191                                         f.seekg((int) (-f.gcount() + strlen("# DSPControlClass->U_start: ")),   ios::cur);
00192                                         f.getline(buffer_string, WSXM_MAXCHARS);
00193                                         u_start = atof(buffer_string);
00194                                         std::cout << "DSPControlClass->U_start: " << u_start << std::endl;
00195                     }
00196 
00197                     if (strstr(buffer_string, "# DSPControlClass->U_end: ")) {
00198                                         f.seekg((int) (-f.gcount() + strlen("# DSPControlClass->U_end: ")), ios::cur);
00199                                         f.getline(buffer_string, WSXM_MAXCHARS);
00200                                         u_end = atof(buffer_string);
00201                                         std::cout << "DSPControlClass->U_end: " << u_end << std::endl;
00202                     }
00203                     
00204                     if (strstr(buffer_string, "# x_scan: ")) {
00205                         f.seekg((int) (-f.gcount() + strlen("# x_scan: ")), ios::cur);
00206                         f.getline(xpos, WSXM_MAXCHARS);
00207                     }
00208                     
00209                     if (strstr(buffer_string, "# y_scan: ")) {
00210                         f.seekg((int) (-f.gcount() + strlen("# y_scan: ")), ios::cur);
00211                         f.getline(ypos, WSXM_MAXCHARS);
00212                     }
00213                     
00214                     if (!strstr(buffer_string, "# ") && ptrscans->len > 0 && strstr(buffer_string, "[")) {
00215                                         GArray *tempint;
00216                                         tempint = g_array_new(TRUE, FALSE, sizeof(int));
00217                                         tempint = getintsfromstring(buffer_string);
00218                                         for (guint i = 0; i < tempint->len; i++) {
00219                                 if (strstr(buffer_string, "[0]")) {
00220                                 // create a new spectra if begin of new spectra is dectected
00221                                 getscan(ptrscans, i)->addspectra();
00222                                 // store xpos and ypos
00223                                 strncpy (getscan(ptrscans, i)->getspectrum(-1)->xpos, xpos, WSXM_MAXCHARS);
00224                                 strncpy (getscan(ptrscans, i)->getspectrum(-1)->ypos, ypos, WSXM_MAXCHARS);
00225                                 // check for new row
00226                                 if (i == 0) {
00227                                                                         if (getscan(ptrscans, 0)->count() >  0){
00228                                                                         if (strncmp(getscan(ptrscans, 0)->getspectrum(-1)->ypos, ypos, WSXM_MAXCHARS)) {
00229                                                                         std::cout<<std::endl<<"y= "<<ypos<<": ";
00230                                                                         if (rows == 1) {
00231                                                                         cols = buffer_cols;
00232                                                                         } else {
00233                                                                         if (!(cols == buffer_cols)) {
00234                                                                                                         std::cerr<<"Log file corrupted. Not all spectra saved."<<std::endl;
00235                                                                         }
00236                                                                         }
00237                                                                         rows++;
00238                                                                         buffer_cols = 0;
00239                                                                         }
00240                                                                         } else { 
00241                                                                         std::cout<<"y= "<<ypos<<": ";
00242                                                                         rows = 1;
00243                                                                         }
00244                                                                         std::cout<<"|x= "<<xpos;
00245                                                                         buffer_cols++;
00246                                         }
00247                                         }
00248                                         getscan(ptrscans, i)->getspectrum(-1)->addpoint((glong) g_array_index (tempint, int, i));
00249                                                 }
00250                         }
00251                                 }
00252                         f.close();
00253                 
00254                         /*************************************************************
00255                 *** Write spectra to file
00256                 *************************************************************/
00257                 std::cout << std::endl << "The log file contains "<<cols<<" by "<<rows<<" spectra."<<std::endl;
00258                         writeIVs(outputfile, getscan(ptrscans, x),getscan(ptrscans, y), xscale, yscale);
00259                         }
00260                 } else {
00261                         std::cerr << "You have to specify a log file to handle!" <<std::endl;
00262                 }
00263   }
00264 }
00265 
00267 // function definition: getintsfromstring     //
00269 GArray *getintsfromstring(gchar * string)
00270 {
00271         gchar **ptr_buffer;
00272   gint nIndex = 1;
00273   glong lbuffer;
00274   ptr_buffer = g_strsplit(string, " ", 16);
00275   GArray *glong_buffer;
00276   glong_buffer = g_array_new(TRUE, TRUE, sizeof(glong));
00277   while (ptr_buffer[nIndex]) {
00278                 lbuffer = atol(ptr_buffer[nIndex]);
00279                 g_array_append_val(glong_buffer, lbuffer);
00280                 nIndex++;
00281   }
00282   return glong_buffer;
00283   g_strfreev(ptr_buffer);
00284   g_array_free(glong_buffer, FALSE);
00285 }
00286 
00288 // function definition: getscan               //
00290 scandata *getscan(GPtrArray * ptrarray, gint i)
00291 {
00292         if (i < 0) {
00293                 i = (gint) ptrarray->len-1;
00294   }
00295   if (i < (gint) ptrarray->len) {
00296         return (scandata *) g_ptr_array_index(ptrarray, i);
00297   } else {
00298                 std::cerr << "Warning: Scan index out of range! Call for scan " << i << std::endl;
00299                 return 0;
00300   }
00301 }
00302 
00304 // function definition: writeIVs              //
00306 int writeIVs(gchar * fileprefix, scandata * scanx, scandata * scany, gdouble xfactor, gdouble yfactor ){
00307         // File pointer for destination file
00308         FILE *pDestinationFile = NULL;
00309         // set destination for writing data 
00310         gchar sDestinationFileName[WSXM_MAXCHARS];
00311         for (gint index=0; index < scanx->count(); ++index) {
00312                 std::cout << "Processing spectra " << index << "\r";    
00313                 sprintf (sDestinationFileName, "%s-%i.iv.cur",fileprefix,index);
00314                 // Start creating header 
00315                 WSxM_HEADER header;
00316                 // the header is ASCII. So opening the header in text mode
00317                 pDestinationFile = fopen(sDestinationFileName, "wt");
00318                 if (pDestinationFile == NULL) {
00319                 std::cerr << "Error: Not possible to open file for writing." << std::endl;
00320                 return -1;
00321                 } else {
00322                         /***********************************************************************
00323                         *** Header initialization 
00324                         ***********************************************************************/
00325                         HeaderInit(&header);
00326                         // buffer variable
00327                         char szValue[WSXM_MAXCHARS];    // Needed to handle/convert different values
00328 
00329                         /***********************************************************************
00330       *** Header Sektion: General_Info
00331                 ***********************************************************************/
00332                         sprintf(szValue, "%i", 1);      // Only one curve is saved, could be two for forward and backward
00333                         HeaderAddValue(&header, IMAGE_HEADER_GENERAL_INFO, IMAGE_HEADER_GENERAL_INFO_LINES, szValue);
00334                         sprintf(szValue, "%i", scanx->getspectrum(index)->count());
00335                         HeaderAddValue(&header, IMAGE_HEADER_GENERAL_INFO, IMAGE_HEADER_GENERAL_INFO_POINTS, szValue);
00336                         // Set labels and units for the axis
00337                         HeaderAddValue(&header, IMAGE_HEADER_GENERAL_INFO, IMAGE_HEADER_GENERAL_INFO_X_TEXT, "V[#x]");
00338                         HeaderAddValue(&header, IMAGE_HEADER_GENERAL_INFO, IMAGE_HEADER_GENERAL_INFO_X_UNIT, "Volt");
00339                         HeaderAddValue(&header, IMAGE_HEADER_GENERAL_INFO, IMAGE_HEADER_GENERAL_INFO_Y_TEXT, "I[#y]");
00340                         HeaderAddValue(&header, IMAGE_HEADER_GENERAL_INFO, IMAGE_HEADER_GENERAL_INFO_Y_UNIT, "nA");
00341 
00342                         /***********************************************************************        
00343       *** Header Sektion: General_Misc
00344                 ***********************************************************************/
00345                         HeaderAddValue(&header, IMAGE_HEADER_MISC_INFO, IMAGE_HEADER_MISC_INFO_FIRST, "Yes");
00346                         HeaderAddValue(&header, IMAGE_HEADER_CONTROL, IMAGE_HEADER_CONTROL_X_OFFSET , scanx->getspectrum(index)->xpos);
00347                         HeaderAddValue(&header, IMAGE_HEADER_CONTROL, IMAGE_HEADER_CONTROL_Y_OFFSET , scanx->getspectrum(index)->ypos);
00348 
00349                         // Write header now
00350                         HeaderWrite(&header, pDestinationFile, "IV File");
00351 
00352                         /**********************************************************************
00353           *** Write data to destination file
00354                 **********************************************************************/
00355             for (gint i = 0; i < scanx->getspectrum(index)->count(); ++i) {
00356                                 fprintf(pDestinationFile, "%lf ", scanx->getspectrum(index)->getvalue(i)* xfactor);
00357                                 fprintf(pDestinationFile, "%lf ", scany->getspectrum(index)->getvalue(i)* yfactor);
00358                                 fprintf(pDestinationFile, " \n");               
00359                         }
00360                         fclose(pDestinationFile);
00361                 }
00362         }
00363         return 0;
00364 }
00366 // function definitions for probedata         //
00368 
00369 // constructor: probedata::probedata(glong datapoints=0)
00370 probedata::probedata(gdouble factor = 1.0, gint datapoints = 0)
00371 {
00372   if (datapoints <= 0) {
00373                 points = g_ptr_array_new();
00374   } else {
00375                 points = g_ptr_array_sized_new(datapoints);
00376   }
00377   m = datapoints;
00378   scalefactor = factor; 
00379 }
00380 
00381 // destructor: probedata::~probedata()
00382 probedata::~probedata()
00383 {
00384   g_ptr_array_free(points, TRUE);
00385 }
00386 
00387 // function: probedata::addpoint(glong point)
00388 void probedata::addpoint(glong point)
00389 {
00390         glong *temp = new glong;
00391   *temp = point;
00392   g_ptr_array_add(this->points, temp);
00393   this->m++;
00394 }
00395 
00396 // function: probedata::getpoint(gulong index)
00397 glong probedata::getpoint(glong index)
00398 {
00399   if (index < 0) {
00400                 index = count()-1;
00401   }
00402   if (index < count()) {
00403                 return *(glong *) g_ptr_array_index(points, index);
00404   } else {
00405                 std::cerr << "Warning: Point index out of range!" << std::endl;
00406                 return 0;
00407   }
00408 }
00409 
00410 // function: probedata::getvalue(gulong index)
00411 gdouble probedata::getvalue(glong index)
00412 {
00413         if (index < 0) {
00414                 index = count()-1;
00415   }
00416   if (index <= count()) {
00417         return scalefactor*(gdouble)*(glong *) g_ptr_array_index(points, index);
00418   } else {
00419                 std::cerr << "Warning: Point index out of range!" << std::endl;
00420                 return 0;
00421   }
00422 }
00423 
00424 // function: probedata::count()
00425 gint probedata::count()
00426 {
00427         return m;
00428 }
00429 
00431 // function definitions for scandata          //
00433 
00434 // constructor: scandata::scandata(glong spectra=0, GString *name="")
00435 scandata::scandata(guint spectras, enum SourceChannelEnum name)
00436 {
00437         if (spectras == 0) {
00438                 spectra = g_ptr_array_new();
00439         } else {
00440                 spectra = g_ptr_array_sized_new(spectras);
00441   }
00442   n = spectras;
00443   channel = name;
00444 }
00445 
00446 // destructor: scandata::~scandata()
00447 scandata::~scandata()
00448 {
00449   g_ptr_array_free(spectra, TRUE);
00450 }
00451 
00452 // function: scandata::addspectra(probedata spectrum=0)
00453 void scandata::addspectra()
00454 {
00455   gdouble tempscale=1.0;
00456   tempscale = (SourceChannels[(enum SourceChannelEnum) channel]).scaling;
00457   probedata *tempspectrum = new probedata(tempscale);
00458   g_ptr_array_add(this->spectra, tempspectrum);
00459   this->n++;
00460 }
00461 
00462 // function: scandata::count()
00463 gint scandata::count()
00464 {
00465   return n;
00466 }
00467 
00468 // function: scandata::getchannel()
00469 GString *scandata::getchannel()
00470 {
00471         return g_string_new((SourceChannels[(enum SourceChannelEnum)channel]).name);
00472 }
00473 
00474 // function: scandata::getspectrum(gint i);
00475 probedata *scandata::getspectrum(gint i)
00476 {
00477         if (i < 0) {
00478                 i = this->count()-1;
00479   }
00480   if (i < this->count()) {
00481                 return (probedata *) g_ptr_array_index((GPtrArray *) this->spectra, i);
00482   } else {
00483                 std::cerr << "Not possible to accese spectra " << i << " in channel " << this->getchannel()->str << "." << std::endl;
00484                 return 0;
00485   }
00486 }
00487 
00488 // function: scandata::getmax(gint layer)
00489 glong scandata::getmax(gint layer)
00490 {
00491   glong max;
00492   for (gint i = 0; i < count(); ++i) {
00493                 max = getspectrum(i)->getpoint(layer) > max ? getspectrum(i)->getpoint(layer) : max;
00494   }
00495   return max;
00496 }
00497 
00498 // function: scandata::getmin(gint layer)
00499 glong scandata::getmin(gint layer)
00500 {
00501         glong min;
00502   for (gint i = 0; i < count(); ++i) {
00503                 min = getspectrum(i)->getpoint(layer) < min ? getspectrum(i)->getpoint(layer) : min;
00504   }
00505   return min;
00506 }
00507 
00508 // function: scandata::getmaxvalue(gint layer)
00509 gdouble scandata::getmaxvalue (gint layer)
00510 {
00511   return getmax(layer)*(SourceChannels[(enum SourceChannelEnum)channel]).scaling;
00512 }
00513 
00514 // function: scandata::getminvalue(gint layer)
00515 gdouble scandata::getminvalue(gint layer)
00516 {
00517   return getmax(layer)*(SourceChannels[(enum SourceChannelEnum)channel]).scaling;
00518 }

Generated on Sat Apr 1 09:04:28 2006 for GXSM by  doxygen 1.4.6