590d2b7bf5272b6d3b597795fec862406279ade2
[calibcant.git] / calibcant / common.py
1 # calibcant - tools for thermally calibrating AFM cantilevers
2 #
3 # Copyright (C) 2007-2009 William Trevor King
4 #
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License as
7 # published by the Free Software Foundation; either version 3 of the
8 # License, or (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 # See the GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
18 # 02111-1307, USA.
19 #
20 # The author may be contacted at <wking@drexel.edu> on the Internet, or
21 # write to Trevor King, Drexel University, Physics Dept., 3141 Chestnut St.,
22 # Philadelphia PA 19104, USA.
23
24 import config
25
26 VERSION="0.4"
27
28 # handle extra verbose input modules, only imported if we need them
29 _flush_plot = None
30 _final_flush_plot = None
31 _pylab = None
32 def _dummy_fn(): pass
33
34 def _import_pylab() :  # TODO: auto detect no DISPLAY and abort
35     """Import pylab plotting functions for when we need to plot.  This
36     function can be called multiple times, and ensures that the pylab
37     setup is only imported once.  It defines the functions
38       _flush_plot() to be called after incremental changes,
39       _final_flush_plot(), to be called at the end of any graphical processing, and
40       _pylab, a reference to the pylab module."""
41     global _pylab
42     global _flush_plot
43     global _final_flush_plot
44     if _pylab == None :
45         import pylab as _pylab
46     if _flush_plot == None :
47         if config.PYLAB_INTERACTIVE :
48             _flush_plot = _pylab.draw
49         else :
50             _flush_plot = _pylab.show #_dummy_fn
51     if _final_flush_plot == None :
52         if config.PYLAB_INTERACTIVE :
53             _final_flush_plot = _pylab.draw
54         else :
55             _final_flush_plot = _dummy_fn # _pylab.show
56
57 def write_array(ofile, array, seperator):
58     """Erite an iterable array seperated by seperator.
59     Terminate the output with an endline."""
60     strings = [repr(x) for x in array]
61     ofile.write(seperator.join(strings))
62     ofile.write('\n')