Thread: Python code

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Location
    Delhi
    Posts
    35
    Rep Power
    2

    Question Python code


    how can i plot the graph for this data.
    I am attaching excel file for actual data

    import csv
    import os
    import time
    import sys
    import string
    import math
    from operator import mul
    from test import test1
    from test import time1
    from test import fopen


    def main():
    pass

    if __name__ == '__main__':
    main()
    COUNT=0
    fopen()
    while COUNT<=5:

    time.sleep(3)
    str=("MODE STATUS:Track\r\ncalc dir: FWD\r\nActuator On/OFF:0\r\nMode:1\r\nLocal Date & Time:16/3/2013:.2.30.10\r\nTracker Desired Angle:-29.99\r\nTracker Actual Pos:-27.63\r\nRTC reset count is:0\r\n------------\r\n")

    val=str.split(":")
    #print "value is:\n",val
    lines=str.split("\r\n")
    # print "line statement are :\n",lines
    COUNT=COUNT+1
    print COUNT

    f=open("test.csv","a+");
    test1()

    result = csv.writer(f,delimiter=',', dialect='excel-tab')
    wst=[]
    for line in lines[:-1]:
    parts=line.split(":")
    for p in parts[1:]:
    wst.append(p)
    result.writerow(wst)
    f.close()



    f1=open("time.csv","a+");
    time1()
    result = csv.writer(f1,delimiter=',')
    str1=wst[5]
    #print "str1 is :\n",str1
    sp=str1.split(".")
    del sp[0]
    # print "sp is :\n",sp
    y0=int(sp[0])
    y1=int(sp[1])
    y2=int(sp[2])
    y=[y0,y1,y2]
    x=[3600,60,1]
    z =map(mul,x,y)
    y.insert(3,sum(z))
    result.writerow(y)
    f1.close()




    print "wst:\n",wst
    print "value of y is :\n",y



    f.close()


    test.py


    import csv


    def test1():
    inputCSV = open(r'test.csv', 'rb')
    outputCSV = open(r'test1.csv', 'wb+')
    appendCSV = open(r'test1.csv', 'ab+')
    cr = csv.reader(inputCSV, dialect = 'excel')
    cw = csv.writer(outputCSV, dialect = 'excel')
    ca = csv.writer(appendCSV, dialect = 'excel')
    for row in cr:
    if row or any(row) or any(field.strip() for field in row):
    ca.writerow(row)
    inputCSV.close()
    outputCSV.close()
    appendCSV.close()


    def time1():
    inputCSV = open(r'time.csv', 'rb')
    outputCSV = open(r'time1.csv', 'wb+')
    appendCSV = open(r'time1.csv', 'ab+')
    cr = csv.reader(inputCSV, dialect = 'excel')
    cw = csv.writer(outputCSV, dialect = 'excel')
    ca = csv.writer(appendCSV, dialect = 'excel')
    for row in cr:
    if row or any(row) or any(field.strip() for field in row):
    ca.writerow(row)
    inputCSV.close()
    outputCSV.close()
    appendCSV.close()

    def fopen():
    f=open("test.csv","w+");
    f1=open("time.csv","w+")
    result = csv.writer(f1,delimiter=',', dialect='excel-tab')
    result_statement1=("HOUR","MIN","SEC","Total_Sec")
    result.writerow(result_statement1)
    f1.close()

    #result = csv.writer(f,delimiter=',')
    result = csv.writer(f,delimiter=',', dialect='excel-tab')
    result_statement=("MODE_STATUS","Dir","ACTUATOR_ON_OFF","MODE","DATE","TIME"," TRACKER DESIRED ANGLE"," TRACKER ACTUAL ANGLE","RESET_FLAG")
    result.writerow(result_statement)
    f.close()
    Last edited by ajit.nayak87; March 18th, 2013 at 11:42 PM.
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,855
    Rep Power
    481
    And for some reason you ignored the solution I posted in the other thread.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Location
    Delhi
    Posts
    35
    Rep Power
    2
    Please check this code and run it on python. You can see output format.whenever it is writing in to csv file it leaving single row blank.
    How to over come this:

    2) i have time in 7th column and i can separate it then how i should calculate TS formula when they are in list format

    3)For plotting graph how shull i download GNUplot library. i tried to download it is not working'

    4) i need shutil library to caary out file operation . how can use it to copy data from one file to another . since i cant able to download it or paste it from net
    5)In my additional option in forum there is no option called manage . to attach file.



    Originally Posted by b49P23TIvg
    And for some reason you ignored the solution I posted in the other thread.
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,855
    Rep Power
    481
    list.__imul__ lengthens the list
    Code:
    [1,2,3]*=4>>> L = list('--#')
    >>> L
    ['-', '-', '#']
    >>> L *= 8
    >>> L
    ['-', '-', '#', '-', '-', '#', '-', '-', '#', '-', '-', '#', '-', '-', '#', '-', '-', '#', '-', '-', '#', '-', '-', '#']
    >>>

    In first post you said "plot the graph for total time for all selected data." What plot program did you want to use? I use gnuplot.


    I showed conversion from hours, minutes, seconds to seconds using
    (H,M,S,) = map(int,TIME.split('.'))
    SECONDS.append(S+60*(M+(60*(H))))


    Good luck.
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Location
    Delhi
    Posts
    35
    Rep Power
    2
    1)i don't have any library for plotting the graph.Where i can download library
    2) Have you compiled the above program . where you can find data getting gather separate by row empty.
    3) In above code i will split time again and then i wanted to added element (hh.mm.sec) to wst [list]
    and then perform operation for plotting graph

    Originally Posted by b49P23TIvg
    list.__imul__ lengthens the list
    Code:
    [1,2,3]*=4>>> L = list('--#')
    >>> L
    ['-', '-', '#']
    >>> L *= 8
    >>> L
    ['-', '-', '#', '-', '-', '#', '-', '-', '#', '-', '-', '#', '-', '-', '#', '-', '-', '#', '-', '-', '#', '-', '-', '#']
    >>>

    In first post you said "plot the graph for total time for all selected data." What plot program did you want to use? I use gnuplot.


    I showed conversion from hours, minutes, seconds to seconds using
    (H,M,S,) = map(int,TIME.split('.'))
    SECONDS.append(S+60*(M+(60*(H))))


    Good luck.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Location
    Delhi
    Posts
    35
    Rep Power
    2
    Here i am attaching format for plotting graph data.

    How can do this using python . i am having matplot lib for putting graph
  12. #7
  13. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,855
    Rep Power
    481
    Example data file graph_plot.txt . It contains some blank lines and some lines with white space.
    Code:
    
    Dir	state	MODE	DATE	TIME	v1	v2
    				
    FWD	0	1	13/3/2013	18.12.11	-39.85	-37.25
    				
    
    FWD	0	1	13/3/2013	18.13.11	-39.96	-37.25
    FWD	0	1	13/3/2013	18.14.12	-40.06	-37.25
    FWD	0	1	13/3/2013	18.15.13	-40.17	-37.25
    				
    
    FWD	0	1	13/3/2013	18.16.14	-40.28	-38.23
    FWD	0	1	13/3/2013	18.17.15	-40.39	-38.23
    FWD	0	1	13/3/2013	18.18.16	-40.49	-38.23
    FWD	0	1	13/3/2013	18.19.16	-40.6 	-38.23

    Here is a program to load the data and prepare it for plotting.
    Code:
    import pprint
    
    
    FILENAME = '/tmp/graph_plot.txt'
    
    REQUIRED_HEADINGS = 'TIME v1 v2'.split()
    
    with open(FILENAME, 'r') as inf:
    
        # read the headings as the first non-blank line from the input
        HEAD = inf.readline()
        while not HEAD.strip():
            HEAD = inf.readline()
    
        # Make a dictionary keyed by the headings.
        # The corresponding value is a list of the data in that column
        # Unlike DictReader this code will not support blank fields.  Does, however, bypass lines with only white space.
        KEYS = HEAD.split()
    
        # verify the file, following the principle "fail as soon as possible".
        # the graphs depend on TIME, v1, and v2
        assert all(KEY in KEYS for KEY in REQUIRED_HEADINGS)
    
        DATA_DICTIONARY = {KEY: [] for KEY in KEYS}
    
        # append the non-blank rows as data into the dictionary, per key
        for ROW in inf:
            SPLIT_ROW = ROW.strip().split()
            for (KEY, VALUE,) in zip(KEYS, SPLIT_ROW,):
                DATA_DICTIONARY[KEY].append(VALUE)
    
    # next make a graphable dictionary.
    GRAPH_ME = dict()
    for KEY in 'v1 v2'.split():
        GRAPH_ME[KEY] = list(map(float, DATA_DICTIONARY[KEY]))
    
    L = GRAPH_ME['seconds'] = []
    I = GRAPH_ME['index'] = []
    for (INDEX, TIME,) in enumerate(DATA_DICTIONARY['TIME']):
        (H, M, S,) = map(int, TIME.split('.'))
        L.append(S + 60 * (M + 60 * (H)))
        I.append(INDEX)
    
    print('graphing the data in GRAPH_ME is your problem')
    print('keys of GRAPH_ME: {}'.format(GRAPH_ME.keys()))
    print('information in GRAPH_ME')
    pprint.pprint(GRAPH_ME, indent = 4)
    And here is the output produced by the program that read the data that I stole.
    Code:
    $ python3 q.py
    python3 q.py
    graphing the data in GRAPH_ME is your problem
    keys of GRAPH_ME: dict_keys(['seconds', 'v1', 'v2', 'index'])
    information in GRAPH_ME
    {   'index': [0, 1, 2, 3, 4, 5, 6, 7],
        'seconds': [65531, 65591, 65652, 65713, 65774, 65835, 65896, 65956],
        'v1': [-39.85, -39.96, -40.06, -40.17, -40.28, -40.39, -40.49, -40.6],
        'v2': [-37.25, -37.25, -37.25, -37.25, -38.23, -38.23, -38.23, -38.23]}
    Last edited by b49P23TIvg; March 18th, 2013 at 10:42 AM.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo