1 # calibcant - tools for thermally calibrating AFM cantilevers
3 # Copyright (C) 2008-2011 W. Trevor King <wking@drexel.edu>
5 # This file is part of calibcant.
7 # calibcant is free software: you can redistribute it and/or
8 # modify it under the terms of the GNU Lesser General Public
9 # License as published by the Free Software Foundation, either
10 # version 3 of the License, or (at your option) any later version.
12 # calibcant is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Lesser General Public License for more details.
17 # You should have received a copy of the GNU Lesser General Public
18 # License along with calibcant. If not, see
19 # <http://www.gnu.org/licenses/>.
22 Separate the more general T_analyze() from the other T_*()
23 functions in calibcant. Also provide a command line interface
24 for analyzing data acquired through other workflows.
26 The relevant physical quantities are :
27 T Temperature at which thermal vibration measurements were aquired
33 from splittable_kwargs import splittableKwargsFunction, \
34 make_splittable_kwargs_function
41 "Convert Celsius -> Kelvin."
42 return celsius + 273.15
45 "Convert Kelvin -> Kelvin."
48 @splittableKwargsFunction()
49 def T_analyze(T, convert_to_K=C_to_K) :
51 Not much to do here, just convert to Kelvin.
52 Uses C_to_K (defined above) by default.
54 try : # if T is an array, convert element by element
55 for i in range(len(T)) :
56 T[i] = convert_to_K(T[i])
57 except TypeError : # otherwise, make an array from a single T
61 @splittableKwargsFunction()
62 def T_save(T, log_dir=None) :
64 Save either a single T (if you are crazy :p),
65 or an array of them (more reasonable)
67 T = numpy.array(T, dtype=numpy.float)
69 log = data_logger.data_log(log_dir, noclobber_logsubdir=False,
71 log.write_binary(T.tostring())
72 if config.LOG_DATA != None :
73 log = data_logger.data_log(config.LOG_DIR, noclobber_logsubdir=False,
75 log.write_binary(T.tostring())
77 def T_load(datafile=None) :
79 Load the saved T array (possibly of length 1), and return it. If
80 datafile == None, return an array of one config.DEFAULT_TEMP
84 return numpy.array([config.DEFAULT_TEMP], dtype=numpy.float)
86 dl = data_logger.data_load()
87 return dl.read_binary(datafile)
89 @splittableKwargsFunction()
90 def T_plot(T, plotVerbose=False) :
92 Umm, just print the temperature?
94 if plotVerbose or config.PYLAB_VERBOSE or config.TEXT_VERBOSE :
95 print "Temperature ", T
97 @splittableKwargsFunction((T_analyze, 'T', 'convert_to_K'),
99 def T_load_analyze_tweaked(tweak_file, convert_to_K=C_to_K, textVerboseFile=None, **kwargs) :
100 "Load all the T array files from a tweak file and return a single array"
101 T_analyze_kwargs,T_plot_kwargs = \
102 T_load_analyze_tweaked._splitargs(T_load_analyze_tweaked, kwargs)
104 for line in file(tweak_file, 'r') :
105 parsed = line.split()
106 path = parsed[0].strip()
107 if path[0] == '#': # a comment
109 if textVerboseFile != None :
110 print >> textVerboseFile, "Reading data from %s" % (path)
114 T_analyze(Ts, convert_to_K=convert_to_K)
115 return numpy.array(Ts, dtype=numpy.float)
117 # commandline interface functions
120 def read_data(ifile):
121 "ifile can be a filename string or open (seekable) file object"
122 if ifile == None : ifile = sys.stdin
123 unlabeled_data=scipy.io.read_array(ifile)
124 return unlabeled_data
126 if __name__ == '__main__' :
127 # command line interface
128 from optparse import OptionParser
130 usage_string = ('%prog <input-file>\n'
131 '2008, W. Trevor King.\n'
133 'There are two operation modes, one to analyze a single T (temperature) file,\n'
134 'and one to analyze tweak files.\n'
136 'Single file mode (the default) :\n'
137 'Reads in single column ASCII file of temperatures and... prints them back out.\n'
138 'No need to do this, but the option is available for consistency with the other\n'
139 'calibcant modules.\n'
142 'Runs the same analysis as in single file mode for each T file in\n'
143 'a tweak file. Each line in the tweak file specifies a single T file.\n'
144 'Blank lines and those beginning with a pound sign (#) are ignored.\n'
145 'A T file contains a sequence of 32 bit floats representing temperature in K.\n'
147 parser = OptionParser(usage=usage_string, version='%prog 0.1')
148 parser.add_option('-C', '--celsius', dest='celsius',
149 help='Use Celsius input temperatures instead of Kelvin (default %default)\n',
150 action='store_true', default=False)
151 parser.add_option('-o', '--output-file', dest='ofilename',
152 help='write output to FILE (default stdout)',
153 type='string', metavar='FILE')
154 parser.add_option('-c', '--comma-out', dest='comma_out', action='store_true',
155 help='Output comma-seperated values (default %default)',
157 parser.add_option('-t', '--tweak-mode', dest='tweakmode', action='store_true',
158 help='Run in tweak-file mode',
160 parser.add_option('-v', '--verbose', dest='verbose', action='store_true',
161 help='Print lots of debugging information',
164 options,args = parser.parse_args()
166 assert len(args) >= 1, "Need an input file"
170 if options.ofilename != None :
171 ofile = file(options.ofilename, 'w')
174 if options.verbose == True :
178 config.TEXT_VERBOSE = options.verbose
179 config.PYLAB_VERBOSE = False
180 config.GNUPLOT_VERBOSE = False
182 convert_to_K = C_to_K
184 convert_to_K = K_to_K
186 if options.tweakmode == False :
187 data = read_data(ifilename)
188 Ts = T_analyze(data, convert_to_K)
189 else : # tweak file mode
190 Ts = T_load_analyze_tweaked(ifilename, convert_to_K, textVerboseFile=vfile)
192 if options.comma_out :
196 common.write_array(ofile, Ts, sep)
198 if options.ofilename != None :