#! /usr/bin/env python # The above line alows this script to be run as an executable # To make this script executable, use the chmod command: # chmod +x Flip510.py # # To run the script, just type: # ./Flip510.py [width] [infile].[ext] # # Output file is: [infile]_out.[ext] # Flip's Python Script for Cornell's Physics 510 Lab # I hate this class and will leave this university # if the department continues to treat its grad students # like delinquent children. # WHAT THIS SCRIPT DOES # Run the script in the folder where you have your preformatted data file. # (see below for instructions on pre-formatting) # The script then reads your data file and outputs a nicely formatted data file # that can be inserted nicely into GNUPlot, TopDrawer, or whatever # The default output format is just a list of numbers separated by line breaks # You can tweak this by modifying the starred (*) line in the code, # see the note at the bottom of the page # HOW TO PREFORMAT YOUR DATA FILE # 1. Chop off all the header information # 2. Chop off all the leading numbers (1: ..., 6:, ...) # I suggest using TextMate (vert select using option key) # 3. Chop off second page header in the middle of the file # 4. Chop off any non-full lines (e.g. last two channels) # 5. Filename should have a three-letter extension # The file should just be a box of numbers. # GRAB DATA FROM ARGUMENTS import sys # Module to take in arguments from terminal # Default values, for reference w = 54 datafile = 'F3ANG.DAT' outputfile = 'F3ANG_F.DAT' w = int(sys.argv[1]) # sys.argv[0] is .\Flip510.py datafile = sys.argv[2] dlength = len(datafile) first = datafile[:dlength-4] last = datafile[dlength-4:] outputfile = first + '_out' + last print w print datafile print outputfile # PROGRAM STARTS BELOW f = open(datafile,'r') # Open data file ou = open(outputfile,'w') # Open output file, erases existing s = f.readline() # Read the first line of the file i = 1 # Initialize data point counter while s != '': # While we're not at the end of file k=0 # Start from the first character t='' # Empty temp varible t while k <= w: # While k hasn't passed the end of line if s[k] != ' ': # If char not whitespace (then its a number) t=t+s[k] # Then append it to t else: # Otherwise if t != '': # If t is not empty # print t # Then print t ou.write(t+'\n') # Write t to file with line break (*) i=i+1 # Increment data point t='' # Empty out t k=k+1 # Go to next character in line s = f.readline() # Read the next line f.close() # Close data file ou.close() # Close output file # NOTE: (*) You can alternately have a different output format. # Here's one that I find useful sometimes:# # ou.write(str(i)+'\t'+t+'\n') # Just replaced the (*) line with this to get a two column output file # in the format (channel), (value). # Note: INDENTATION IS IMPORTANT, make sure it has the same justification # as the current line. # # ... by the way, this is why I have the 'i' counter in the code above, # even though the default output doesn't say anything.