Monday, March 18, 2013

Convert a CPM .txt file to a new DPM .txt file

This little function will take a file full of CPM values and convert it to DPM values in a new file.  This is very useful for wipe tests.

example data:
wipe test 3/15/13 results in cpm
number 24 is blank
   
    1 = 29
    2 = 34
    3 = 31
    4 = 24
    5 = 34
    6 = 27
    7 = 26
    8 = 35
    9 = 25
    10 = 31
    11 = 31
    12 = 25
    13 = 23
    14 = 34
    15 = 37
    16 = 32
    17 = 28
    18 = 35
    19 = 29
    20 = 29
    21 = 35
    22 = 27
    23 = 31
    24 = 32

Example output data:
converted DPM values

1 = 0
2 = 2.86
3 = 0
4 = 0
5 = 2.86
6 = 0
7 = 0
8 = 4.29
9 = 0
10 = 0
11 = 0
12 = 0
13 = 0
14 = 2.86
15 = 7.15
16 = 0
17 = 0
18 = 4.29
19 = 0
20 = 0
21 = 4.29
22 = 0
23 = 0
24 = 0


def CPM_to_DPM(CPM_file, DPM_file):
    # open the files
    CPM_file = open(CPM_file, 'r') # read CPM_file
    DPM_file = open(DPM_file, 'w') # write DPM_file

    # Skip over header
    line = CPM_file.readline()
    while line != '\n':
        line = CPM_file.readline()
  
    # slice out  '1 = ' and record the rest as an int in cpm_data variable
    line = CPM_file.readline()
    cpm_data = [] # accumaltor variable
    while line != "":

        cpm = line[line.rfind(' ') + 1:]
        cpm_data.append(int(cpm))
        line = CPM_file.readline()

    #set last int in array to background
    background = cpm_data[-1]
   
    #go over list if bigger than background perform cpm to dpm equation. if smaller value = 0
    dpm_value = []
    for i in cpm_data:
        if i > background:
            dpm_values = (i - background)*1.43
            dpm_values = round(dpm_values, 2)
            dpm_value.append(dpm_values)
        else:
            dpm_value.append(0)
   
     # write a header on DPM file
    DPM_file.write('converted DPM values')
    DPM_file.write('\n')
    DPM_file.write('\n')
   
    #write dpm_value to DPM file with increasing number for each line
    sample_num = 0
   
    for i in dpm_value:
        sample_num = sample_num + 1
        DPM_file.write(str(sample_num) + ' = ' + str(i))
        DPM_file.write('\n')
       
    # close files
    CPM_file.close()
    DPM_file.close()


    #print done so the user knows to go to look at their dpm file
    print ('done')