lineprofile.C

Go to the documentation of this file.
00001 /* Gxsm - Gnome X Scanning Microscopy
00002  * universal STM/AFM/SARLS/SPALEED/... controlling and
00003  * data analysis software
00004  * 
00005  * Copyright (C) 1999,2000,2001,2002,2003 Percy Zahl
00006  *
00007  * Authors: Percy Zahl <zahl@users.sf.net>
00008  * additional features: Andreas Klust <klust@users.sf.net>
00009  * WWW Home: http://gxsm.sf.net
00010  *
00011  * This program is free software; you can redistribute it and/or modify
00012  * it under the terms of the GNU General Public License as published by
00013  * the Free Software Foundation; either version 2 of the License, or
00014  * (at your option) any later version.
00015  *
00016  * This program is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  * GNU General Public License for more details.
00020  *
00021  * You should have received a copy of the GNU General Public License
00022  * along with this program; if not, write to the Free Software
00023  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
00024  */
00025 
00026 /* -*- Mode: C++; indent-tabs-mode: nil; c-basic-offset: 8 c-style: "K&R" -*- */
00027 
00028 #include "lineprofile.h"
00029 #include "mem2d.h"
00030 #include "glbvars.h"
00031 
00032 LineProfile1D::LineProfile1D(){
00033         masterscan=NULL;
00034         scan1d=NULL;
00035         private_scan=TRUE;
00036 }
00037 
00038 LineProfile1D::LineProfile1D(int n, UnitObj *ux, UnitObj *uy, double xmin, double xmax){
00039         masterscan=NULL;
00040         scan1d=NULL;
00041         private_scan=TRUE;
00042         scan1d = new Scan(n, 0, -1, NULL, ZD_DOUBLE);
00043         
00044         scan1d->data.s.ny=1;
00045         scan1d->data.s.nx=n;
00046 
00047         scan1d->data.s.rx=xmax-xmin;
00048         scan1d->data.s.ry=0.;
00049         scan1d->data.s.rz=0.;
00050         scan1d->data.s.dl=scan1d->data.s.rx;
00051 
00052         scan1d->data.s.dx=scan1d->data.s.rx/(n-1);
00053         scan1d->data.s.dy=1.;
00054         scan1d->data.s.dz=1.;
00055 
00056         scan1d->data.s.x0=xmin;
00057         scan1d->data.s.y0=0.;
00058         scan1d->data.s.alpha=0.;
00059 
00060         UnitObj Unity(" "," ","g","arb. units");
00061         scan1d->data.SetXUnit (ux);
00062         scan1d->data.SetYUnit (&Unity);
00063         scan1d->data.SetZUnit (uy);
00064 
00065         scan1d->create();
00066         scan1d->mem2d->data->MkXLookup(xmin, xmax);
00067 }
00068 
00069 LineProfile1D::~LineProfile1D(){
00070   //  XSMDEBUG("LineProfile1D::~LineProfile1D");
00071   if(scan1d && private_scan)
00072     delete scan1d;
00073 }
00074 
00075 // Lesen und schreiben von spa4 .d1d Daten
00076 
00077 int LineProfile1D::load(gchar *fname){
00078         XSM_DEBUG (DBG_L3, "LineProfile1D::load - file to new scan: " << fname);
00079         std::ifstream f;
00080 
00081         if (fname == NULL) return 1;
00082         
00083         f.open(fname, std::ios::in);
00084         if(!f.good()){
00085                 //    fl_show_alert(ERR_SORRY, ERR_FILEREAD,fname,1);
00086                 return 1;
00087         }
00088 
00089         XSM_DEBUG (DBG_L3, "LineProfile1D::load - file check OK");
00090 
00091         private_scan=TRUE;
00092 
00093         XSM_DEBUG (DBG_L3, "LineProfile1D::load - new Scan");
00094         scan1d = new Scan(0, 0, -1, NULL, ZD_DOUBLE);
00095         
00096         scan1d->data.s.ny=1;
00097         scan1d->data.s.nx=0;
00098 
00099         scan1d->data.s.rx=1.;
00100         scan1d->data.s.ry=0.;
00101         scan1d->data.s.rz=0.;
00102         scan1d->data.s.dl=scan1d->data.s.rx;
00103 
00104         scan1d->data.s.dx=1.;
00105         scan1d->data.s.dy=1.;
00106         scan1d->data.s.dz=1.;
00107 
00108         scan1d->data.s.x0=0.;
00109         scan1d->data.s.y0=0.;
00110         scan1d->data.s.alpha=0.;
00111 
00112         UnitObj Unity(" "," ","g","arb. units");
00113         scan1d->data.SetXUnit (&Unity);
00114         scan1d->data.SetYUnit (&Unity);
00115         scan1d->data.SetZUnit (&Unity);
00116 
00117         XSM_DEBUG (DBG_L3, "LineProfile1D::load - reading asc Header");
00118         gchar l[1024];
00119         while (f.good()){
00120                 f.getline(l, 1024);
00121                 XSM_DEBUG(DBG_L2, l );
00122                 if (strncmp (l, "# Len   = ", 10) == 0){
00123                         scan1d->data.s.rx=atof(l+10); 
00124                         scan1d->data.s.dl=scan1d->data.s.rx;
00125                         continue;
00126                 }
00127                 if (strncmp (l, "# dX    = ", 10) == 0){
00128                         scan1d->data.s.dx=atof(l+10); 
00129                         continue;
00130                 }
00131                 if (strncmp (l, "# dY    = ", 10) == 0){
00132                         scan1d->data.s.dy=atof(l+10); 
00133                         continue;
00134                 }
00135                 if (strncmp (l, "# dZ    = ", 10) == 0){
00136                         scan1d->data.s.dz=1.; // reading world data now!!
00137                         continue;
00138                 }
00139                 if (strncmp (l, "# X0    = ", 10) == 0){
00140                         scan1d->data.s.x0=atof(l+10); 
00141                         continue;
00142                 }
00143                 if (strncmp (l, "# Y0    = ", 10) == 0){
00144                         scan1d->data.s.y0=atof(l+10); 
00145                         continue;
00146                 }
00147                 if (strncmp (l, "# Anz   = ", 10) == 0){
00148                         scan1d->data.s.nx=atoi(l+10); 
00149                         continue;
00150                 }
00151                 if (strncmp (l, "# NSets = ", 10) == 0){
00152                         scan1d->data.s.ny=atoi(l+10); 
00153                         continue;
00154                 }
00155                 if (strncmp (l, "# Xunit = ", 10) == 0){
00156                         gchar *u = g_strdup(l+10);
00157                         gchar *ua=NULL;
00158                         gchar *ul=NULL;
00159                         f.getline(l, 1024); // expecting label in next line!
00160                         if (strncmp (l, "# Xalias= ", 10) == 0){
00161                                 ua = g_strdup(l+10);
00162                                 f.getline(l, 1024); // expecting label in next line!
00163                                 if (strncmp (l, "# Xlabel= ", 10) == 0)
00164                                         ul = g_strdup(l+10);
00165                                 UnitObj *uobj=NULL;
00166                                 if (ua)
00167                                         uobj = gapp->xsm->MakeUnit (ua, ul?ul:ua);
00168                                 else
00169                                         uobj = gapp->xsm->MakeUnit (u, ul?ul:u);
00170 
00171                                 scan1d->data.SetXUnit (uobj);
00172                                 delete uobj;
00173                                         
00174                                 if (u) g_free(u);
00175                                 if (ua) g_free(ua);
00176                                 if (ul) g_free(ul);
00177                                 continue;
00178                         }
00179                 }
00180                 if (strncmp (l, "# Yunit = ", 10) == 0){
00181                         gchar *u = g_strdup(l+10);
00182                         gchar *ua=NULL;
00183                         gchar *ul=NULL;
00184                         f.getline(l, 1024); // expecting label in next line!
00185                         if (strncmp (l, "# Yalias= ", 10) == 0){
00186                                 ua = g_strdup(l+10);
00187                                 f.getline(l, 1024); // expecting label in next line!
00188                                 if (strncmp (l, "# Ylabel= ", 10) == 0)
00189                                         ul = g_strdup(l+10);
00190                                 UnitObj *uobj=NULL;
00191                                 if (ua)
00192                                         uobj = gapp->xsm->MakeUnit (ua, ul?ul:ua);
00193                                 else
00194                                         uobj = gapp->xsm->MakeUnit (u, ul?ul:u);
00195 
00196                                 scan1d->data.SetZUnit (uobj);
00197                                 delete uobj;
00198                                         
00199                                 if (u) g_free(u);
00200                                 if (ua) g_free(ua);
00201                                 if (ul) g_free(ul);
00202                                 continue;
00203                         }
00204                 }
00205                 if (strncmp (l, "# Title = ", 10) == 0){
00206                         scan1d->data.ui.SetTitle(l+10); 
00207                         continue;
00208                 }
00209                 if (strncmp (l, "# Format= ", 10) == 0){
00210                         break;
00211                 }
00212                 if (strncmp (l, "# Fromat= ", 10) == 0){ // bugfix
00213                         break;
00214                 }
00215         }
00216         XSM_DEBUG (DBG_L3, "LineProfile1D::load - new Scan->create()");
00217         XSM_DEBUG (DBG_L3, "LineProfile1D::load NX: "<<scan1d->data.s.nx); 
00218         XSM_DEBUG (DBG_L3, "LineProfile1D::load NY: "<<scan1d->data.s.ny); 
00219         scan1d->create();
00220 
00221         XSM_DEBUG (DBG_L3, "LineProfile1D::load - reading data");
00222         for(int i=0; i<scan1d->data.s.nx && f.good(); ++i){
00223                 double x=0., y=0.;
00224                 f.getline (l, 1024);
00225                 gchar **record = g_strsplit(l, " ", 100);
00226 
00227                 gchar **token = record;
00228                 for (int j=-1; *token; ++token){
00229                         if (! strcmp (*token, "InUnits:")){
00230                                 ++j;
00231                                 continue;
00232                         }
00233                         if (j==0){
00234                                 x = atof (*token);
00235                                 scan1d->mem2d->data->SetXLookup (i, x);
00236                                 XSM_DEBUG(DBG_L2, i << " " << x );
00237                                 ++j;
00238                                 continue;
00239                         }
00240                         if (j>0){
00241                                 y = atof (*token);
00242                                 if (j <= scan1d->mem2d->GetNy ())
00243                                         scan1d->mem2d->PutDataPkt (y, i, j-1);
00244                                 ++j;
00245                                 continue;
00246                         }
00247                 }
00248                 g_strfreev (record);
00249         }
00250     
00251         f.close();
00252         XSM_DEBUG (DBG_L3, "LineProfile1D::load - Done.");
00253         return 0;
00254 }
00255 
00256 int LineProfile1D::save(gchar *fname){
00257   std::ofstream f;
00258   int   i,j;
00259   time_t t;
00260   time(&t); 
00261   f.open(fname, std::ios::out | std::ios::trunc);
00262   if(!f.good()){
00263     //    fl_show_alert(ERR_SORRY, ERR_FILEWRITE,fname,1);
00264     return 1;
00265   }
00266   f << "# ASC LineProfile Data" << std::endl
00267     << "# Date: " << ctime(&t)
00268     << "# FileName: " << fname << std::endl
00269     << "# Len   = " << scan1d->data.s.rx << "A" << std::endl
00270     << "# tStart= " << scan1d->data.s.tStart << "s" << std::endl
00271     << "# tEnd  = " << scan1d->data.s.tEnd << "s" << std::endl
00272     << "# phi   = " << scan1d->data.s.alpha << "degree" << std::endl
00273     << "# dX    = " << scan1d->data.s.dx << "A" << std::endl
00274     << "# dY    = " << scan1d->data.s.dy << "A" << std::endl
00275     << "# dZ    = " << scan1d->data.s.dz << "A" << std::endl
00276     << "# Xo    = " << scan1d->data.s.x0 << "A" << std::endl
00277     << "# Yo    = " << scan1d->data.s.y0 << "A" << std::endl
00278     << "# Anz   = " << scan1d->data.s.nx << std::endl
00279     << "# NSets = " << scan1d->data.s.ny << std::endl
00280     << "# Xunit = " << (scan1d->data.Xunit->Symbol()? scan1d->data.Xunit->Symbol():"N/A") << std::endl
00281     << "# Xalias= " << (scan1d->data.Xunit->Alias()? scan1d->data.Xunit->Alias():"N/A") << std::endl
00282     << "# Xlabel= " << scan1d->data.Xunit->Label() << std::endl
00283     << "# Yunit = " << (scan1d->data.Zunit->Symbol()? scan1d->data.Zunit->Symbol():"N/A")  << std::endl
00284     << "# Yalias= " << (scan1d->data.Zunit->Alias()? scan1d->data.Zunit->Alias():"N/A") << std::endl
00285     << "# Ylabel= " << scan1d->data.Zunit -> Label() << std::endl
00286     << "# Title = " << scan1d->data.ui.title << std::endl
00287     << "# Format= X[index*LineLen/Nx] Z[DA]... InUnit: X[Xunit] Z[Zunit]..." << std::endl
00288     ;
00289 
00290   for(i=0; i<scan1d->mem2d->GetNx(); i++){
00291     f << i*scan1d->data.s.dl/(double)scan1d->mem2d->GetNx();
00292     for(j=0; j<scan1d->mem2d->GetNy(); j++)
00293       f << " " << (double)scan1d->mem2d->GetDataPkt(i,j);
00294 
00295     f << " InUnits: " 
00296       << ( scan1d->data.Xunit ? scan1d->data.Xunit : scan1d->data.Xunit) -> Base2Usr(scan1d->mem2d->data->GetXLookup(i));
00297     for(j=0; j<scan1d->mem2d->GetNy(); j++)
00298       f << " " << scan1d->data.Zunit->Base2Usr(scan1d->mem2d->GetDataPkt(i,j)*scan1d->data.s.dz);
00299 
00300     f << std::endl;
00301   }
00302   
00303   f.close();
00304 
00305   return 0;
00306 }
00307 
00308 int LineProfile1D::SetData(Scan *sc, int line){
00309   //  XSMDEBUG("LineProfile1D::SetData");
00310 
00311   if(line == -2){
00312     scan1d = sc;
00313     private_scan=FALSE;
00314     return 0;
00315   }
00316 
00317   if(! scan1d){
00318     if(scan1d && private_scan)
00319         delete scan1d;
00320     private_scan=TRUE;
00321     scan1d = new Scan(sc);
00322   }
00323 
00324   scan1d->CpyDataSet (sc->data);
00325   scan1d->data.CpUnits (sc->data);
00326   scan1d->data.s.ny=1;
00327   scan1d->create();
00328   if(line < 0){
00329           if( sc->RedLineActive ){
00330                   scan1d->mem2d->GetDataLineFrom(&sc->Pkt2dScanLine[0], &sc->Pkt2dScanLine[1], 
00331                                                  sc->mem2d, &scan1d->data, 
00332                                                  (GETLINEORGMODE)xsmres.LineProfileOrgMode);
00333           }
00334           else{
00335                   switch (sc->get_data ()){
00336                   case MPOINT:
00337                           if (sc->PktVal == 1)
00338                                   scan1d->mem2d->GetLayerDataLineFrom (&sc->Pkt2d[0],
00339                                                                        sc->mem2d, &scan1d->data);
00340                           break;
00341                   case MCIRC:
00342                           if (sc->PktVal == 2)
00343                                   scan1d->mem2d->GetArcDataLineFrom (&sc->Pkt2d[0], &sc->Pkt2d[1],
00344                                                                   sc->mem2d, &scan1d->data);
00345                           break;
00346                   default: 
00347                           if (sc->PktVal == 2)
00348                                   scan1d->mem2d->GetDataLineFrom(&sc->Pkt2d[0], &sc->Pkt2d[1], 
00349                                                                  sc->mem2d, &scan1d->data, 
00350                                                                  (GETLINEORGMODE)xsmres.LineProfileOrgMode);
00351                           break;
00352                   }
00353           }
00354   }
00355   else
00356     scan1d->mem2d->CopyFrom(sc->mem2d, 0,line, 0,0, scan1d->mem2d->GetNx()); 
00357 
00358   return 0;
00359 }
00360 
00361 
00362 double LineProfile1D::GetPoint(int n) {
00363         if (scan1d) return scan1d->mem2d->GetDataPkt (n,0); 
00364         else return 0.; 
00365 }
00366 void LineProfile1D::SetPoint(int n, double y) { if (scan1d) scan1d->mem2d->PutDataPkt (y, n,0); }
00367 void LineProfile1D::SetPoint(int n, double x, double y) { 
00368         if (scan1d) { 
00369                 scan1d->mem2d->PutDataPkt (y, n,0);
00370                 scan1d->mem2d->data->SetXLookup (n, x);   
00371         } 
00372 }
00373 void LineProfile1D::AddPoint(int n, double a) { if (scan1d) scan1d->mem2d->data->Zadd (a, n,0); }
00374 void LineProfile1D::MulPoint(int n, double f) { if (scan1d) scan1d->mem2d->data->Zmul (f, n,0); }
00375 
00376 
00377 // END

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