unit.h

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 #ifndef __UNIT_H
00029 #define __UNIT_H
00030 
00031 #include <stdio.h>
00032 #include <stdlib.h>
00033 #include <math.h>
00034 #include <string.h>
00035 
00036 #include <libgnome/libgnome.h>
00037 #include <gdk/gdk.h>
00038 
00039 class UnitObj;
00040 
00041 // Object UnitObj:
00042 typedef enum { UNIT_SM_NORMAL, UNIT_SM_PS } UNIT_MODES;
00043 
00044 class UnitObj{
00045 public:
00046   UnitObj(UnitObj &usrc){
00047     sym   = g_strdup(usrc.sym); 
00048     pssym = g_strdup(usrc.pssym); 
00049     prec  = g_strdup(usrc.prec);
00050     label = g_strdup(usrc.label);
00051     if (usrc.alias)
00052             alias = g_strdup(usrc.alias);
00053     else
00054             alias = NULL;
00055   };
00056   UnitObj(const gchar *s, const gchar *pss){ 
00057     sym   = g_strdup(s); 
00058     pssym = g_strdup(pss); 
00059     prec  = g_strdup("g");
00060     label = g_strdup(" ");
00061     alias = NULL;
00062   };
00063   UnitObj(const gchar *s, const gchar *pss, const gchar *precc){ 
00064     sym   = g_strdup(s); 
00065     pssym = g_strdup(pss); 
00066     prec  = g_strdup(precc);
00067     label = g_strdup(" ");
00068     alias = NULL;
00069   };
00070   UnitObj(const gchar *s, const gchar *pss, const gchar *precc, const gchar *lab){ 
00071     sym   = g_strdup(s); 
00072     pssym = g_strdup(pss); 
00073     prec  = g_strdup(precc);
00074     label = g_strdup(lab);
00075     alias = NULL;
00076   };
00077   virtual ~UnitObj(){ 
00078           g_free(sym); g_free(pssym); g_free(prec); g_free(label); 
00079           if(alias) g_free(alias);
00080   };
00081 
00082   virtual UnitObj* Copy(){ return new UnitObj(*this); };
00083 
00084   void SetAlias(const gchar *a){ 
00085           if(alias == NULL)
00086                   alias = g_strdup(a);
00087   };
00088 
00089   void ChangeSym(gchar *s, gchar *pss){ 
00090     g_free(sym); g_free(pssym); 
00091     sym=g_strdup(s); pssym=g_strdup(pss); 
00092   };
00093 
00094   void ChangePrec(gchar *precc){ 
00095     g_free(prec);
00096     prec = g_strdup(precc);
00097   };
00098 
00099   void SetLabel(gchar *lab){ 
00100     g_free(label);
00101     label = g_strdup(lab);
00102   };
00103 
00104   /* Unit Symbol */
00105   /* do not g_free this return string !!! */
00106   gchar *Symbol(UNIT_MODES um=UNIT_SM_NORMAL){
00107     switch(um){
00108     case UNIT_SM_NORMAL: return sym; 
00109     case UNIT_SM_PS: return pssym; 
00110     }
00111     return sym; 
00112   };     
00113   gchar *psSymbol(){ return pssym; }; /* Symbol for Postscript use */
00114   gchar *Label(){ return label; }; /* Name/Label for this Unit */
00115   gchar *Alias(){ return alias; }; /* Units Aliasname */
00116   gchar *MakeLongLabel( UNIT_MODES um=UNIT_SM_NORMAL ){ /* free this later !! */
00117     return g_strconcat(label, " [", Symbol(um), "]", NULL);
00118   };
00119 
00120   virtual double Usr2Base(double u){ return u; }; /* Usr -> Base */
00121   virtual double Base2Usr(double b){ return b; }; /* Usr <- Base */
00122   virtual void Change(double x, int n=0){};
00123 
00124   /* do g_free this returned string !!! */
00125   gchar *UsrString(double b, UNIT_MODES um=UNIT_SM_NORMAL){
00126     gchar *fmt = g_strconcat("%",prec," ",Symbol(um),NULL);
00127     gchar *txt;
00128     txt = g_strdup_printf(fmt, Base2Usr(b));
00129     g_free(fmt);
00130     return txt;
00131   };
00132   gchar *UsrStringSqr(double b, UNIT_MODES um=UNIT_SM_NORMAL){
00133 //    gchar *fmt = g_strconcat("%",prec," ",Symbol(um),"^2",NULL);
00134     gchar *fmt = g_strconcat("%",prec," ",Symbol(um),"\302\262",NULL);
00135     gchar *txt;
00136     txt = g_strdup_printf(fmt, Base2Usr(Base2Usr(b)));
00137     g_free(fmt);
00138     return txt;
00139   };
00140   virtual void setval(gchar *name, double x){};
00141  protected:
00142   gchar *sym, *pssym, *prec;
00143   gchar *label;
00144   gchar *alias;
00145  private:
00146 };
00147 
00148 class LinUnit;
00149 
00150 class LinUnit : public UnitObj{
00151 public:
00152   LinUnit(LinUnit &usrc)
00153     :UnitObj(usrc){ fac=usrc.fac; off=usrc.off; };
00154   LinUnit(const gchar *s, const gchar *pss, double f=1., double o=0.)
00155     :UnitObj(s, pss){ fac=f; off=o; };
00156   LinUnit(const gchar *s, const gchar *pss, const gchar *lab, double f=1., double o=0.)
00157     :UnitObj(s, pss, "g", lab){ fac=f; off=o; };
00158 
00159   virtual UnitObj* Copy(){ return new LinUnit(*this); };
00160   virtual void setval(const gchar *name, double x){
00161     switch(*name){
00162     case 'f': fac=x; break;
00163     case 'o': off=x; break;
00164     }
00165   };
00166 
00167   virtual double Usr2Base(double u){ return (u*fac+off); };   /* Usr -> Base */
00168   virtual double Base2Usr(double b){ return ((b-off)/fac); }; /* Usr <- Base */
00169 
00170 private:
00171   double fac, off;
00172 };
00173 
00174 // % BZ (Brilluin Zone) auf der Basis von Volt (Oktopol Ablenspannung)
00175 class BZUnit;
00176 
00177 class BZUnit : public UnitObj{
00178 public:
00179   BZUnit(BZUnit &usrc):UnitObj(usrc){ En=usrc.En; Sens=usrc.Sens; };
00180   BZUnit(const gchar *s, const gchar *pss, double sensi=50., double energy=100.)
00181     :UnitObj(s, pss, "g", "k"){ Sens=sensi; En=energy; };
00182   BZUnit(const gchar *s, const gchar *pss, const gchar *lab, double sensi=50., double energy=100.)
00183     :UnitObj(s, pss, "g", lab){ Sens=sensi; En=energy; };
00184 
00185   virtual UnitObj* Copy(){ return new BZUnit(*this); };
00186 
00187   virtual double Usr2Base(double bz){ return (bz/Sens*sqrt(En)); }; /* Usr (BZ) -> Base (V) */
00188   virtual double Base2Usr(double  u){ return ( u*Sens/sqrt(En)); }; /* Usr (BZ) <- Base (V) */
00189 
00190   double GetE() { return En; };
00191   void SetE(double E) { En=E; };
00192   double GetSens() { return Sens; };
00193   void CalcSens(double volt, double bz){
00194     Sens = bz/volt*sqrt(En);
00195   };
00196 private:
00197   double En, Sens;
00198 };
00199 
00200 
00201 // Phase S auf der Basis von Energy [eV], d0 Lagenabstand
00202 class SUnit;
00203 
00204 class SUnit : public UnitObj{
00205 public:
00206   SUnit(SUnit &usrc):UnitObj(usrc){ d0=usrc.d0; cth=usrc.cth; };
00207   SUnit(const gchar *s, const gchar *pss, double dd0=3.141, double theta=5.)
00208     :UnitObj(s, pss, "g", "Phase"){ 
00209     d0=dd0; cth=cos(theta*M_PI/180.);
00210   };
00211   SUnit(const gchar *s, const gchar *pss, const gchar *lab, double dd0=3.141, double theta=5.)
00212     :UnitObj(s, pss, "g", lab){ 
00213     d0=dd0; cth=cos(theta*M_PI/180.);
00214   };
00215 
00216   virtual UnitObj* Copy(){ return new SUnit(*this); };
00217 
00218   virtual double Usr2Base(double  S){  /* Usr (S) -> Base (eV) */
00219     double sdc=S/d0/cth;
00220     return (37.6*sdc*sdc);
00221   };
00222   virtual double Base2Usr(double En){  /* Usr (S) <- Base (eV) */
00223     return (d0*cth*sqrt(En/37.6));
00224   };
00225 
00226 private:
00227   double d0, cth;
00228 };
00229 
00230 // % CPSCNTUnit (Counts Per Second) needs Gatetime in ms
00231 class CPSCNTUnit;
00232 
00233 class CPSCNTUnit : public UnitObj{
00234 public:
00235   CPSCNTUnit(CPSCNTUnit &usrc):UnitObj(usrc){ gt=usrc.gt; };
00236   CPSCNTUnit(const gchar *s, const gchar *pss, double Gt=1.)
00237     :UnitObj(s, pss, "g", "CPS"){ gt=Gt; };
00238   CPSCNTUnit(const gchar *s, const gchar *pss, const gchar *lab, double Gt=1.)
00239     :UnitObj(s, pss, "g", lab){ gt=Gt; };
00240 
00241   virtual UnitObj* Copy(){ return new CPSCNTUnit(*this); };
00242 
00243   virtual double Usr2Base(double cps){ return (cps*gt) ; }; /* Usr (CPS) -> Base (CNT) */
00244   virtual double Base2Usr(double cnt){ return (cnt/gt) ; }; /* Usr (CPS) <- Base (CNT) */
00245   virtual void Change(double x, int n=0){ SetGatetime(x); };
00246 
00247   double GetGatetime() { return gt; };
00248   void SetGatetime(double Gt) { gt=Gt; };
00249 private:
00250   double gt;
00251 };
00252 
00253 class LogUnit : public UnitObj{
00254 public:
00255   LogUnit(LogUnit &usrc)
00256     :UnitObj(usrc){ fac=usrc.fac; off=usrc.off; };
00257   LogUnit(const gchar *s, const gchar *pss, double f=1., double o=1.)
00258     :UnitObj(s, pss){ fac=f; off=o; };
00259   LogUnit(const gchar *s, const gchar *pss, const gchar *lab, double f=1., double o=1.)
00260     :UnitObj(s, pss, "g", lab){ fac=f; off=o; };
00261 
00262   virtual UnitObj* Copy(){ return new LogUnit(*this); };
00263   virtual void setval(const gchar *name, double x){
00264     switch(*name){
00265     case 'f': fac=x; break;
00266     case 'o': off=x; break;
00267     }
00268   };
00269   virtual double Usr2Base(double u){ return (off * log10 (u/fac) ); };   /* Usr -> Base */
00270   virtual double Base2Usr(double b){ return (fac * pow (10., b/off)); }; /* Usr <- Base */
00271 
00272 private:
00273   double fac, off;
00274 };
00275 
00276 class InvUnit : public UnitObj{
00277 public:
00278   InvUnit(InvUnit &usrc)
00279     :UnitObj(usrc){ fac=usrc.fac; off=usrc.off; };
00280   InvUnit(const gchar *s, const gchar *pss, double f=1., double o=0.)
00281     :UnitObj(s, pss){ fac=f; off=o; };
00282   InvUnit(const gchar *s, const gchar *pss, const gchar *lab, double f=1., double o=0.)
00283     :UnitObj(s, pss, "g", lab){ fac=f; off=o; };
00284 
00285   virtual UnitObj* Copy(){ return new InvUnit(*this); };
00286   virtual void setval(const gchar *name, double x){
00287     switch(*name){
00288     case 'f': fac=x; break;
00289     case 'o': off=x; break;
00290     }
00291   };
00292   virtual double Usr2Base(double u){ return (fac/u+off); };   /* Usr -> Base */
00293   virtual double Base2Usr(double b){ return (fac/(b-off)); }; /* Usr <- Base */
00294 
00295 private:
00296   double fac, off;
00297 };
00298 
00299 
00300 
00301 #endif
00302 
00303 
00304 
00305 

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