- Timestamp:
- Jan 30, 2008, 11:34:29 AM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
anuga_core/source/anuga/abstract_2d_finite_volumes/util.py
r4949 r4982 955 955 # * moving the reference direction from [1,0] to North 956 956 # * changing from counter clockwise to clocwise. 957 958 957 959 958 angle = degrees(atan(vh/(uh+1.e-15))) 960 959 if (0 < angle < 90.0): … … 963 962 if vh < 0: 964 963 bearing = 270.0 - abs(angle) 964 965 965 if (-90 < angle < 0): 966 966 if vh < 0: 967 bearing = 90.0 - abs(angle)967 bearing = 90.0 - (angle) 968 968 if vh > 0: 969 bearing = 270.0 - abs(angle)969 bearing = 270.0 - (angle) 970 970 if angle == 0: bearing = 0.0 971 971 972 972 return bearing 973 973 … … 1528 1528 1529 1529 1530 def OLD_csv2timeseries_graphs(directories_dic={},1531 output_dir='',1532 base_name=None,1533 plot_numbers='0',1534 quantities=['stage'],1535 extra_plot_name='',1536 assess_all_csv_files=True,1537 create_latex=False,1538 verbose=True):1539 1540 """WARNING!! NO UNIT TESTS... could make some as to test filename..but have1541 spend ages on this already...1542 AND NOTE Create_latex is NOT implemented yet.1543 1544 1545 Read in csv files that have the right header information and1546 plot time series such as Stage, Speed, etc. Will also plot several1547 time series on one plot. Filenames must follow this convention,1548 <base_name><plot_number>.csv eg gauge_timeseries3.csv1549 1550 Each file represents a location and within each file there are1551 time, quantity columns.1552 1553 For example:1554 if "directories_dic" defines 4 directories and in each directories1555 there is a csv files corresponding to the right "plot_numbers",1556 this will create a plot with 4 lines one for each directory AND1557 one plot for each "quantities".1558 1559 Inputs:1560 directories_dic: dictionary of directory with values (plot1561 legend name for directory), (start time of1562 the time series) and the (value to add to1563 stage if needed). For example1564 {dir1:['Anuga_ons',5000, 0],1565 dir2:['b_emoth',5000,1.5],1566 dir3:['b_ons',5000,1.5]}1567 1568 output_dir: directory for the plot outputs1569 1570 base_name: common name to all the csv files to be read. if1571 using plot_numbers must be the maximum common string1572 eg. gauges0.csv, gauges1.csv and gauges2.csv1573 base_name must be 'gauges' not 'gau'1574 1575 plot_numbers: a String list of numbers to plot. For example1576 [0-4,10,15-17] will read and attempt to plot1577 the follow 0,1,2,3,4,10,15,16,171578 quantities: Currently limited to "stage", "speed", and1579 "momentum", should be changed to incorporate1580 any quantity read from CSV file....1581 NOTE: ALL QUANTITY NAMES MUST BE lower case!1582 1583 extra_plot_name: A string that is appended to the end of the1584 output filename.1585 1586 assess_all_csv_files: if true it will read ALL csv file with1587 "base_name", regardless of 'plot_numbers'1588 and determine a uniform set of axes for1589 Stage, Speed and Momentum. IF FALSE it1590 will only read the csv file within the1591 'plot_numbers'1592 1593 create_latex: NOT IMPLEMENTED YET!! sorry Jane....1594 1595 OUTPUTS: None, it saves the plots to1596 <output_dir><base_name><plot_number><extra_plot_name>.png1597 1598 Usage: csv2timeseries_graphs({'J:'+sep+'anuga_validation'+sep:['new',20,-.1],1599 'J:'+sep+'conical_island'+sep:['test',0,0]},1600 output_dir='',1601 plot_numbers=['1','3'],1602 quantities=['stage','depth','bearing'],1603 base_name='gauge_b',1604 assess_all_csv_files=True,1605 verbose=True)1606 This will produce one plot for each quantity (therefore 3) in the current directory,1607 each plot will have 2 lines on them. The first plot named 'new' will have the time1608 offseted by 20secs and the stage height adjusted by -0.1m1609 1610 """1611 # import pylab1612 try:1613 import pylab1614 except ImportError:1615 msg='csv2timeseries_graphs needs pylab to be installed correctly'1616 raise msg1617 #ANUGA don't need pylab to work so the system doesn't1618 #rely on pylab being installed1619 return1620 from os import sep1621 from anuga.shallow_water.data_manager import \1622 get_all_files_with_extension, csv2dict1623 #find all the files that meet the specs1624 #FIXME remove all references to word that such as Stage1625 #(with a Capital letter) could use the header info from1626 #the dictionary that is returned from the csv2dict this could1627 #be very good and very flexable.... but this works for now!1628 1629 #FIXME MAKE NICER at the end i have started a new version...1630 # it almost completely works...1631 1632 seconds_in_hour = 36001633 time_label = 'time (hour)'1634 stage_label = 'wave height (m)'1635 speed_label = 'speed (m/s)'1636 momentum_label = 'momentum (m^2/sec)'1637 xmomentum_label = 'momentum (m^2/sec)'1638 ymomentum_label = 'momentum (m^2/sec)'1639 depth_label = 'water depth (m)'1640 bearing_label = 'degrees (o)'1641 1642 if extra_plot_name != '':1643 extra_plot_name='_'+extra_plot_name1644 1645 #finds all the files that fit the specs and return a list of them1646 #so to help find a uniform max and min for the plots...1647 list_filenames=[]1648 #print'oaeuaoe',verbose1649 if verbose: print 'Determining files to access for axes ranges \n'1650 #print 'heaoeu'1651 for i,directory in enumerate(directories_dic.keys()):1652 #print 'base',basename,directory1653 list_filenames.append(get_all_files_with_extension(directory,1654 base_name,'.csv'))1655 # print 'list_filenames',list_filenames1656 1657 #use all the files to get the values for the plot axis1658 max_xmom=min_xmom=max_ymom=min_ymom=max_st=max_sp=max_mom=min_st=min_sp=min_mom=\1659 max_depth=min_depth=max_bearing=min_bearing=max_t=min_t=0.1660 max_start_time= 0.1661 min_start_time = 1000001662 1663 print'hello'1664 new_plot_numbers=[]1665 #change plot_numbers to list, eg ['0-4','10']1666 #to ['0','1','2','3','4','10']1667 for i, num_string in enumerate(plot_numbers):1668 if '-' in num_string:1669 start = int(num_string[:num_string.rfind('-')])1670 end = int(num_string[num_string.rfind('-')+1:])+11671 for x in range(start, end):1672 new_plot_numbers.append(str(x))1673 else:1674 new_plot_numbers.append(num_string)1675 # print 'new_plot_numbers',new_plot_numbers1676 1677 if verbose: print 'Determining uniform axes \n'1678 #this entire loop is to determine the min and max range for the1679 #axes of the plot1680 for i, directory in enumerate(directories_dic.keys()):1681 1682 if assess_all_csv_files==False:1683 which_csv_to_assess = new_plot_numbers1684 else:1685 which_csv_to_assess = list_filenames[i]1686 1687 for j, filename in enumerate(which_csv_to_assess):1688 if assess_all_csv_files==False:1689 # dir_filename=directory+sep+base_name+filename1690 dir_filename=join(directory,base_name+filename)1691 else:1692 dir_filename=join(directory,filename)1693 print'dir_filename',dir_filename1694 attribute_dic, title_index_dic = csv2dict(dir_filename+1695 '.csv')1696 1697 directory_start_time = directories_dic[directory][1]1698 directory_add_tide = directories_dic[directory][2]1699 1700 time = [float(x) for x in attribute_dic["time"]]1701 min_t, max_t = get_min_max_values(time,min_t,max_t)1702 1703 stage = [float(x) for x in attribute_dic["stage"]]1704 stage =array(stage)+directory_add_tide1705 min_st, max_st = get_min_max_values(stage,min_st,max_st)1706 1707 if "speed" in quantities:1708 speed = [float(x) for x in attribute_dic["speed"]]1709 min_sp, max_sp = get_min_max_values(speed,min_sp,max_sp)1710 1711 if "momentum" in quantities:1712 momentum = [float(x) for x in attribute_dic["momentum"]]1713 min_mom, max_mom = get_min_max_values(momentum,1714 min_mom,1715 max_mom)1716 if "xmomentum" in quantities:1717 xmomentum = [float(x) for x in attribute_dic["xmomentum"]]1718 min_xmom, max_xmom = get_min_max_values(xmomentum,1719 min_xmom,1720 max_xmom)1721 if "ymomentum" in quantities:1722 ymomentum = [float(x) for x in attribute_dic["ymomentum"]]1723 min_ymom, max_ymom = get_min_max_values(ymomentum,1724 min_ymom,1725 max_ymom)1726 if "depth" in quantities:1727 depth = [float(x) for x in attribute_dic["depth"]]1728 min_depth, max_depth = get_min_max_values(depth,1729 min_depth,1730 max_depth)1731 if "bearing" in quantities:1732 bearing = [float(x) for x in attribute_dic["bearing"]]1733 min_bearing, max_bearing = get_min_max_values(bearing,1734 min_bearing,1735 max_bearing)1736 1737 # print 'min_sp, max_sp',min_sp, max_sp,1738 print directory_start_time1739 if min_start_time > directory_start_time:1740 min_start_time = directory_start_time1741 if max_start_time < directory_start_time:1742 max_start_time = directory_start_time1743 # print 'start_time' , max_start_time, min_start_time1744 1745 stage_axis = (min_start_time/seconds_in_hour,1746 (max_t+max_start_time)/seconds_in_hour,1747 min_st, max_st)1748 speed_axis = (min_start_time/seconds_in_hour,1749 (max_t+max_start_time)/seconds_in_hour,1750 min_sp, max_sp)1751 xmomentum_axis = (min_start_time/seconds_in_hour,1752 (max_t+max_start_time)/seconds_in_hour,1753 min_xmom, max_xmom)1754 ymomentum_axis = (min_start_time/seconds_in_hour,1755 (max_t+max_start_time)/seconds_in_hour,1756 min_ymom, max_ymom)1757 momentum_axis = (min_start_time/seconds_in_hour,1758 (max_t+max_start_time)/seconds_in_hour,1759 min_mom, max_mom)1760 depth_axis = (min_start_time/seconds_in_hour,1761 (max_t+max_start_time)/seconds_in_hour,1762 min_depth, max_depth)1763 bearing_axis = (min_start_time/seconds_in_hour,1764 (max_t+max_start_time)/seconds_in_hour,1765 min_bearing, max_bearing)1766 1767 cstr = ['b', 'r', 'g', 'c', 'm', 'y', 'k']1768 1769 # if verbose: print 'Now start to plot \n'1770 1771 i_max = len(directories_dic.keys())1772 legend_list_dic =[]1773 legend_list =[]1774 for i, directory in enumerate(directories_dic.keys()):1775 if verbose: print'Plotting in %s' %(directory)1776 print new_plot_numbers,base_name1777 1778 if assess_all_csv_files==False:1779 which_csv_to_assess = new_plot_numbers1780 else:1781 which_csv_to_assess = list_filenames[i]1782 1783 for j, number in enumerate(which_csv_to_assess):1784 1785 # for j, number in enumerate(new_plot_numbers):1786 print 'number',number1787 if verbose: print'Starting %s%s'%(base_name,number)1788 directory_name = directories_dic[directory][0]1789 directory_start_time = directories_dic[directory][1]1790 directory_add_tide = directories_dic[directory][2]1791 1792 #create an if about the start time and tide hieght1793 #if they don't exist1794 file=directory+number1795 #print 'i %s,j %s, number %s, file %s' %(i,j,number,file)1796 attribute_dic, title_index_dic = csv2dict(file+'.csv')1797 #get data from dict in to list1798 t = [float(x) for x in attribute_dic["time"]]1799 1800 #do maths to list by changing to array1801 t=(array(t)+directory_start_time)/seconds_in_hour1802 stage = [float(x) for x in attribute_dic["stage"]]1803 if "speed" in quantities:1804 speed = [float(x) for x in attribute_dic["speed"]]1805 if "xmomentum" in quantities:1806 xmomentum = [float(x) for x in attribute_dic["xmomentum"]]1807 if "ymomentum" in quantities:1808 ymomentum = [float(x) for x in attribute_dic["ymomentum"]]1809 if "momentum" in quantities:1810 momentum = [float(x) for x in attribute_dic["momentum"]]1811 if "depth" in quantities:1812 depth = [float(x) for x in attribute_dic["depth"]]1813 if "bearing" in quantities:1814 bearing = [float(x) for x in attribute_dic["bearing"]]1815 1816 #Can add tide so plots are all on the same tide height1817 stage =array(stage)+directory_add_tide1818 1819 #finds the maximum elevation1820 max_ele=-1000001821 min_ele=1000001822 # legend_list=[]1823 if "elevation" in quantities:1824 elevation = [float(x) for x in attribute_dic["elevation"]]1825 1826 min_ele, max_ele = get_min_max_values(elevation,1827 min_ele,1828 max_ele)1829 if min_ele != max_ele:1830 print "Note! Elevation changes in %s" %dir_filename1831 # print 'min_ele, max_ele',min_ele, max_ele1832 1833 1834 #populates the legend_list_dic with dir_name and the elevation1835 if i==0:1836 legend_list_dic.append({directory_name:max_ele})1837 else:1838 legend_list_dic[j][directory_name]=max_ele1839 1840 # creates a list for the legend after at "legend_dic" has been fully populated1841 # only runs on the last iteration for all the gauges(csv) files1842 # empties the list before creating it1843 1844 if i==i_max-1:1845 legend_list=[]1846 print 'legend',legend_list_dic, j1847 for k, l in legend_list_dic[j].iteritems():1848 # if "elevation" in quantities:1849 # legend_list.append('%s (elevation = %sm)'%(k,l))1850 # else:1851 legend_list.append('%s '%k)1852 #print k,l, legend_list_dic[j]1853 1854 if "stage" in quantities:1855 pylab.figure(100+j)1856 pylab.plot(t, stage, c = cstr[i], linewidth=1)1857 pylab.xlabel(time_label)1858 pylab.ylabel(stage_label)1859 pylab.axis(stage_axis)1860 pylab.legend(legend_list,loc='upper right')1861 if output_dir =='':1862 figname = '%sstage_%s%s.png' %(directory+sep,1863 base_name+number,1864 extra_plot_name)1865 else:1866 figname = '%sstage_%s%s.png' %(output_dir+sep,1867 base_name+number,1868 extra_plot_name)1869 if verbose: print 'saving figure here %s' %figname1870 pylab.savefig(figname)1871 1872 if "speed" in quantities:1873 pylab.figure(200+j)1874 pylab.plot(t, speed, c = cstr[i], linewidth=1)1875 pylab.xlabel(time_label)1876 pylab.ylabel(speed_label)1877 pylab.axis(speed_axis)1878 pylab.legend(legend_list,loc='upper right')1879 if output_dir =='':1880 figname = '%sspeed_%s%s.png' %(directory+sep,1881 base_name+number,1882 extra_plot_name)1883 else:1884 figname = '%sspeed_%s%s.png' %(output_dir+sep,1885 base_name+number,1886 extra_plot_name)1887 if verbose: print 'saving figure here %s' %figname1888 pylab.savefig(figname)1889 if "xmomentum" in quantities:1890 pylab.figure(300+j)1891 pylab.plot(t, xmomentum, c = cstr[i], linewidth=1)1892 pylab.xlabel(time_label)1893 pylab.ylabel(xmomentum_label)1894 pylab.axis(xmomentum_axis)1895 pylab.legend(legend_list,loc='upper right')1896 if output_dir =='':1897 figname = '%sxmomentum_%s%s.png' %(directory+sep,1898 base_name+number,1899 extra_plot_name)1900 else:1901 figname = '%sxmomentum_%s%s.png' %(output_dir+sep,1902 base_name+number,1903 extra_plot_name)1904 if verbose: print 'saving figure here %s' %figname1905 pylab.savefig(figname)1906 1907 if "ymomentum" in quantities:1908 pylab.figure(400+j)1909 pylab.plot(t, ymomentum, c = cstr[i], linewidth=1)1910 pylab.xlabel(time_label)1911 pylab.ylabel(ymomentum_label)1912 pylab.axis(ymomentum_axis)1913 pylab.legend(legend_list,loc='upper right')1914 if output_dir =='':1915 figname = '%symomentum_%s%s.png' %(directory+sep,1916 base_name+number,1917 extra_plot_name)1918 else:1919 figname = '%symomentum_%s%s.png' %(output_dir+sep,1920 base_name+number,1921 extra_plot_name)1922 if verbose: print 'saving figure here %s' %figname1923 pylab.savefig(figname)1924 1925 if "momentum" in quantities:1926 pylab.figure(500+j)1927 pylab.plot(t, momentum, c = cstr[i], linewidth=1)1928 pylab.xlabel(time_label)1929 pylab.ylabel(momentum_label)1930 pylab.axis(momentum_axis)1931 pylab.legend(legend_list,loc='upper right')1932 if output_dir =='':1933 figname = '%smomentum_%s%s.png' %(directory+sep,1934 base_name+number,1935 extra_plot_name)1936 else:1937 figname = '%smomentum_%s%s.png' %(output_dir+sep,1938 base_name+number,1939 extra_plot_name)1940 if verbose: print 'saving figure here %s' %figname1941 pylab.savefig(figname)1942 1943 if "bearing" in quantities:1944 pylab.figure(600+j)1945 pylab.plot(t, bearing, c = cstr[i], linewidth=1)1946 pylab.xlabel(time_label)1947 pylab.ylabel(bearing_label)1948 pylab.axis(bearing_axis)1949 pylab.legend(legend_list,loc='upper right')1950 if output_dir =='':1951 figname = '%sbearing_%s%s.png' %(output_dir+sep,1952 base_name+number,1953 extra_plot_name)1954 else:1955 figname = '%sbearing_%s%s.png' %(output_dir+sep,1956 base_name+number,1957 extra_plot_name)1958 if verbose: print 'saving figure here %s' %figname1959 pylab.savefig(figname)1960 1961 if "depth" in quantities:1962 pylab.figure(700+j)1963 pylab.plot(t, depth, c = cstr[i], linewidth=1)1964 pylab.xlabel(time_label)1965 pylab.ylabel(depth_label)1966 pylab.axis(depth_axis)1967 pylab.legend(legend_list,loc='upper right')1968 if output_dir == '':1969 figname = '%sdepth_%s%s.png' %(directory,1970 base_name+number,1971 extra_plot_name)1972 else:1973 figname = '%sdepth_%s%s.png' %(output_dir+sep,1974 base_name+number,1975 extra_plot_name)1976 if verbose: print 'saving figure here %s' %figname1977 pylab.savefig(figname)1978 1979 if verbose: print 'Closing all plots'1980 pylab.close('all')1981 del pylab1982 if verbose: print 'Finished closing plots'1530 #def OLD_csv2timeseries_graphs(directories_dic={}, 1531 # output_dir='', 1532 # base_name=None, 1533 # plot_numbers='0', 1534 # quantities=['stage'], 1535 # extra_plot_name='', 1536 # assess_all_csv_files=True, 1537 # create_latex=False, 1538 # verbose=True): 1539 # 1540 # """WARNING!! NO UNIT TESTS... could make some as to test filename..but have 1541 # spend ages on this already... 1542 # AND NOTE Create_latex is NOT implemented yet. 1543 # 1544 # 1545 # Read in csv files that have the right header information and 1546 # plot time series such as Stage, Speed, etc. Will also plot several 1547 # time series on one plot. Filenames must follow this convention, 1548 # <base_name><plot_number>.csv eg gauge_timeseries3.csv 1549 # 1550 # Each file represents a location and within each file there are 1551 # time, quantity columns. 1552 # 1553 # For example: 1554 # if "directories_dic" defines 4 directories and in each directories 1555 # there is a csv files corresponding to the right "plot_numbers", 1556 # this will create a plot with 4 lines one for each directory AND 1557 # one plot for each "quantities". 1558 # 1559 # Inputs: 1560 # directories_dic: dictionary of directory with values (plot 1561 # legend name for directory), (start time of 1562 # the time series) and the (value to add to 1563 # stage if needed). For example 1564 # {dir1:['Anuga_ons',5000, 0], 1565 # dir2:['b_emoth',5000,1.5], 1566 # dir3:['b_ons',5000,1.5]} 1567 # 1568 # output_dir: directory for the plot outputs 1569 # 1570 # base_name: common name to all the csv files to be read. if 1571 # using plot_numbers must be the maximum common string 1572 # eg. gauges0.csv, gauges1.csv and gauges2.csv 1573 # base_name must be 'gauges' not 'gau' 1574 # 1575 # plot_numbers: a String list of numbers to plot. For example 1576 # [0-4,10,15-17] will read and attempt to plot 1577 # the follow 0,1,2,3,4,10,15,16,17 1578 # quantities: Currently limited to "stage", "speed", and 1579 # "momentum", should be changed to incorporate 1580 # any quantity read from CSV file.... 1581 # NOTE: ALL QUANTITY NAMES MUST BE lower case! 1582 # 1583 # extra_plot_name: A string that is appended to the end of the 1584 # output filename. 1585 # 1586 # assess_all_csv_files: if true it will read ALL csv file with 1587 # "base_name", regardless of 'plot_numbers' 1588 # and determine a uniform set of axes for 1589 # Stage, Speed and Momentum. IF FALSE it 1590 # will only read the csv file within the 1591 # 'plot_numbers' 1592 # 1593 # create_latex: NOT IMPLEMENTED YET!! sorry Jane.... 1594 # 1595 # OUTPUTS: None, it saves the plots to 1596 # <output_dir><base_name><plot_number><extra_plot_name>.png 1597 # 1598 # Usage: csv2timeseries_graphs({'J:'+sep+'anuga_validation'+sep:['new',20,-.1], 1599 # 'J:'+sep+'conical_island'+sep:['test',0,0]}, 1600 # output_dir='', 1601 # plot_numbers=['1','3'], 1602 # quantities=['stage','depth','bearing'], 1603 # base_name='gauge_b', 1604 # assess_all_csv_files=True, 1605 # verbose=True) 1606 # This will produce one plot for each quantity (therefore 3) in the current directory, 1607 # each plot will have 2 lines on them. The first plot named 'new' will have the time 1608 # offseted by 20secs and the stage height adjusted by -0.1m 1609 # 1610 # """ 1611 ## import pylab 1612 # try: 1613 # import pylab 1614 # except ImportError: 1615 # msg='csv2timeseries_graphs needs pylab to be installed correctly' 1616 # raise msg 1617 # #ANUGA don't need pylab to work so the system doesn't 1618 # #rely on pylab being installed 1619 # return 1620 # from os import sep 1621 # from anuga.shallow_water.data_manager import \ 1622 # get_all_files_with_extension, csv2dict 1623 # #find all the files that meet the specs 1624 # #FIXME remove all references to word that such as Stage 1625 # #(with a Capital letter) could use the header info from 1626 # #the dictionary that is returned from the csv2dict this could 1627 # #be very good and very flexable.... but this works for now! 1628 # 1629 # #FIXME MAKE NICER at the end i have started a new version... 1630 # # it almost completely works... 1631 # 1632 # seconds_in_hour = 3600 1633 # time_label = 'time (hour)' 1634 # stage_label = 'wave height (m)' 1635 # speed_label = 'speed (m/s)' 1636 # momentum_label = 'momentum (m^2/sec)' 1637 # xmomentum_label = 'momentum (m^2/sec)' 1638 # ymomentum_label = 'momentum (m^2/sec)' 1639 # depth_label = 'water depth (m)' 1640 # bearing_label = 'degrees (o)' 1641 # 1642 # if extra_plot_name != '': 1643 # extra_plot_name='_'+extra_plot_name 1644 # 1645 # #finds all the files that fit the specs and return a list of them 1646 # #so to help find a uniform max and min for the plots... 1647 # list_filenames=[] 1648 # #print'oaeuaoe',verbose 1649 # if verbose: print 'Determining files to access for axes ranges \n' 1650 # #print 'heaoeu' 1651 # for i,directory in enumerate(directories_dic.keys()): 1652 # #print 'base',basename,directory 1653 # list_filenames.append(get_all_files_with_extension(directory, 1654 # base_name,'.csv')) 1655 ## print 'list_filenames',list_filenames 1656 # 1657 # #use all the files to get the values for the plot axis 1658 # max_xmom=min_xmom=max_ymom=min_ymom=max_st=max_sp=max_mom=min_st=min_sp=min_mom=\ 1659 # max_depth=min_depth=max_bearing=min_bearing=max_t=min_t=0. 1660 # max_start_time= 0. 1661 # min_start_time = 100000 1662 # 1663 # print'hello' 1664 # new_plot_numbers=[] 1665 # #change plot_numbers to list, eg ['0-4','10'] 1666 # #to ['0','1','2','3','4','10'] 1667 # for i, num_string in enumerate(plot_numbers): 1668 # if '-' in num_string: 1669 # start = int(num_string[:num_string.rfind('-')]) 1670 # end = int(num_string[num_string.rfind('-')+1:])+1 1671 # for x in range(start, end): 1672 # new_plot_numbers.append(str(x)) 1673 # else: 1674 # new_plot_numbers.append(num_string) 1675 ## print 'new_plot_numbers',new_plot_numbers 1676 # 1677 # if verbose: print 'Determining uniform axes \n' 1678 # #this entire loop is to determine the min and max range for the 1679 # #axes of the plot 1680 # for i, directory in enumerate(directories_dic.keys()): 1681 # 1682 # if assess_all_csv_files==False: 1683 # which_csv_to_assess = new_plot_numbers 1684 # else: 1685 # which_csv_to_assess = list_filenames[i] 1686 # 1687 # for j, filename in enumerate(which_csv_to_assess): 1688 # if assess_all_csv_files==False: 1689 ## dir_filename=directory+sep+base_name+filename 1690 # dir_filename=join(directory,base_name+filename) 1691 # else: 1692 # dir_filename=join(directory,filename) 1693 # print'dir_filename',dir_filename 1694 # attribute_dic, title_index_dic = csv2dict(dir_filename+ 1695 # '.csv') 1696 # 1697 # directory_start_time = directories_dic[directory][1] 1698 # directory_add_tide = directories_dic[directory][2] 1699 # 1700 # time = [float(x) for x in attribute_dic["time"]] 1701 # min_t, max_t = get_min_max_values(time,min_t,max_t) 1702 # 1703 # stage = [float(x) for x in attribute_dic["stage"]] 1704 # stage =array(stage)+directory_add_tide 1705 # min_st, max_st = get_min_max_values(stage,min_st,max_st) 1706 # 1707 # if "speed" in quantities: 1708 # speed = [float(x) for x in attribute_dic["speed"]] 1709 # min_sp, max_sp = get_min_max_values(speed,min_sp,max_sp) 1710 # 1711 # if "momentum" in quantities: 1712 # momentum = [float(x) for x in attribute_dic["momentum"]] 1713 # min_mom, max_mom = get_min_max_values(momentum, 1714 # min_mom, 1715 # max_mom) 1716 # if "xmomentum" in quantities: 1717 # xmomentum = [float(x) for x in attribute_dic["xmomentum"]] 1718 # min_xmom, max_xmom = get_min_max_values(xmomentum, 1719 # min_xmom, 1720 # max_xmom) 1721 # if "ymomentum" in quantities: 1722 # ymomentum = [float(x) for x in attribute_dic["ymomentum"]] 1723 # min_ymom, max_ymom = get_min_max_values(ymomentum, 1724 # min_ymom, 1725 # max_ymom) 1726 # if "depth" in quantities: 1727 # depth = [float(x) for x in attribute_dic["depth"]] 1728 # min_depth, max_depth = get_min_max_values(depth, 1729 # min_depth, 1730 # max_depth) 1731 # if "bearing" in quantities: 1732 # bearing = [float(x) for x in attribute_dic["bearing"]] 1733 # min_bearing, max_bearing = get_min_max_values(bearing, 1734 # min_bearing, 1735 # max_bearing) 1736 # 1737 ## print 'min_sp, max_sp',min_sp, max_sp, 1738 # print directory_start_time 1739 # if min_start_time > directory_start_time: 1740 # min_start_time = directory_start_time 1741 # if max_start_time < directory_start_time: 1742 # max_start_time = directory_start_time 1743 ## print 'start_time' , max_start_time, min_start_time 1744 # 1745 # stage_axis = (min_start_time/seconds_in_hour, 1746 # (max_t+max_start_time)/seconds_in_hour, 1747 # min_st, max_st) 1748 # speed_axis = (min_start_time/seconds_in_hour, 1749 # (max_t+max_start_time)/seconds_in_hour, 1750 # min_sp, max_sp) 1751 # xmomentum_axis = (min_start_time/seconds_in_hour, 1752 # (max_t+max_start_time)/seconds_in_hour, 1753 # min_xmom, max_xmom) 1754 # ymomentum_axis = (min_start_time/seconds_in_hour, 1755 # (max_t+max_start_time)/seconds_in_hour, 1756 # min_ymom, max_ymom) 1757 # momentum_axis = (min_start_time/seconds_in_hour, 1758 # (max_t+max_start_time)/seconds_in_hour, 1759 # min_mom, max_mom) 1760 # depth_axis = (min_start_time/seconds_in_hour, 1761 # (max_t+max_start_time)/seconds_in_hour, 1762 # min_depth, max_depth) 1763 # bearing_axis = (min_start_time/seconds_in_hour, 1764 # (max_t+max_start_time)/seconds_in_hour, 1765 # min_bearing, max_bearing) 1766 # 1767 # cstr = ['b', 'r', 'g', 'c', 'm', 'y', 'k'] 1768 # 1769 ## if verbose: print 'Now start to plot \n' 1770 # 1771 # i_max = len(directories_dic.keys()) 1772 # legend_list_dic =[] 1773 # legend_list =[] 1774 # for i, directory in enumerate(directories_dic.keys()): 1775 # if verbose: print'Plotting in %s' %(directory) 1776 # print new_plot_numbers,base_name 1777 # 1778 # if assess_all_csv_files==False: 1779 # which_csv_to_assess = new_plot_numbers 1780 # else: 1781 # which_csv_to_assess = list_filenames[i] 1782 # 1783 # for j, number in enumerate(which_csv_to_assess): 1784 # 1785 ## for j, number in enumerate(new_plot_numbers): 1786 # print 'number',number 1787 # if verbose: print'Starting %s%s'%(base_name,number) 1788 # directory_name = directories_dic[directory][0] 1789 # directory_start_time = directories_dic[directory][1] 1790 # directory_add_tide = directories_dic[directory][2] 1791 # 1792 # #create an if about the start time and tide hieght 1793 # #if they don't exist 1794 # file=directory+number 1795 # #print 'i %s,j %s, number %s, file %s' %(i,j,number,file) 1796 # attribute_dic, title_index_dic = csv2dict(file+'.csv') 1797 # #get data from dict in to list 1798 # t = [float(x) for x in attribute_dic["time"]] 1799 # 1800 # #do maths to list by changing to array 1801 # t=(array(t)+directory_start_time)/seconds_in_hour 1802 # stage = [float(x) for x in attribute_dic["stage"]] 1803 # if "speed" in quantities: 1804 # speed = [float(x) for x in attribute_dic["speed"]] 1805 # if "xmomentum" in quantities: 1806 # xmomentum = [float(x) for x in attribute_dic["xmomentum"]] 1807 # if "ymomentum" in quantities: 1808 # ymomentum = [float(x) for x in attribute_dic["ymomentum"]] 1809 # if "momentum" in quantities: 1810 # momentum = [float(x) for x in attribute_dic["momentum"]] 1811 # if "depth" in quantities: 1812 # depth = [float(x) for x in attribute_dic["depth"]] 1813 # if "bearing" in quantities: 1814 # bearing = [float(x) for x in attribute_dic["bearing"]] 1815 # 1816 # #Can add tide so plots are all on the same tide height 1817 # stage =array(stage)+directory_add_tide 1818 # 1819 # #finds the maximum elevation 1820 # max_ele=-100000 1821 # min_ele=100000 1822 ## legend_list=[] 1823 # if "elevation" in quantities: 1824 # elevation = [float(x) for x in attribute_dic["elevation"]] 1825 # 1826 # min_ele, max_ele = get_min_max_values(elevation, 1827 # min_ele, 1828 # max_ele) 1829 # if min_ele != max_ele: 1830 # print "Note! Elevation changes in %s" %dir_filename 1831 ## print 'min_ele, max_ele',min_ele, max_ele 1832 # 1833 # 1834 # #populates the legend_list_dic with dir_name and the elevation 1835 # if i==0: 1836 # legend_list_dic.append({directory_name:max_ele}) 1837 # else: 1838 # legend_list_dic[j][directory_name]=max_ele 1839 # 1840 # # creates a list for the legend after at "legend_dic" has been fully populated 1841 # # only runs on the last iteration for all the gauges(csv) files 1842 # # empties the list before creating it 1843 # 1844 # if i==i_max-1: 1845 # legend_list=[] 1846 # print 'legend',legend_list_dic, j 1847 # for k, l in legend_list_dic[j].iteritems(): 1848 ## if "elevation" in quantities: 1849 ## legend_list.append('%s (elevation = %sm)'%(k,l)) 1850 ## else: 1851 # legend_list.append('%s '%k) 1852 # #print k,l, legend_list_dic[j] 1853 # 1854 # if "stage" in quantities: 1855 # pylab.figure(100+j) 1856 # pylab.plot(t, stage, c = cstr[i], linewidth=1) 1857 # pylab.xlabel(time_label) 1858 # pylab.ylabel(stage_label) 1859 # pylab.axis(stage_axis) 1860 # pylab.legend(legend_list,loc='upper right') 1861 # if output_dir =='': 1862 # figname = '%sstage_%s%s.png' %(directory+sep, 1863 # base_name+number, 1864 # extra_plot_name) 1865 # else: 1866 # figname = '%sstage_%s%s.png' %(output_dir+sep, 1867 # base_name+number, 1868 # extra_plot_name) 1869 # if verbose: print 'saving figure here %s' %figname 1870 # pylab.savefig(figname) 1871 # 1872 # if "speed" in quantities: 1873 # pylab.figure(200+j) 1874 # pylab.plot(t, speed, c = cstr[i], linewidth=1) 1875 # pylab.xlabel(time_label) 1876 # pylab.ylabel(speed_label) 1877 # pylab.axis(speed_axis) 1878 # pylab.legend(legend_list,loc='upper right') 1879 # if output_dir =='': 1880 # figname = '%sspeed_%s%s.png' %(directory+sep, 1881 # base_name+number, 1882 # extra_plot_name) 1883 # else: 1884 # figname = '%sspeed_%s%s.png' %(output_dir+sep, 1885 # base_name+number, 1886 # extra_plot_name) 1887 # if verbose: print 'saving figure here %s' %figname 1888 # pylab.savefig(figname) 1889 # if "xmomentum" in quantities: 1890 # pylab.figure(300+j) 1891 # pylab.plot(t, xmomentum, c = cstr[i], linewidth=1) 1892 # pylab.xlabel(time_label) 1893 # pylab.ylabel(xmomentum_label) 1894 # pylab.axis(xmomentum_axis) 1895 # pylab.legend(legend_list,loc='upper right') 1896 # if output_dir =='': 1897 # figname = '%sxmomentum_%s%s.png' %(directory+sep, 1898 # base_name+number, 1899 # extra_plot_name) 1900 # else: 1901 # figname = '%sxmomentum_%s%s.png' %(output_dir+sep, 1902 # base_name+number, 1903 # extra_plot_name) 1904 # if verbose: print 'saving figure here %s' %figname 1905 # pylab.savefig(figname) 1906 # 1907 # if "ymomentum" in quantities: 1908 # pylab.figure(400+j) 1909 # pylab.plot(t, ymomentum, c = cstr[i], linewidth=1) 1910 # pylab.xlabel(time_label) 1911 # pylab.ylabel(ymomentum_label) 1912 # pylab.axis(ymomentum_axis) 1913 # pylab.legend(legend_list,loc='upper right') 1914 # if output_dir =='': 1915 # figname = '%symomentum_%s%s.png' %(directory+sep, 1916 # base_name+number, 1917 # extra_plot_name) 1918 # else: 1919 # figname = '%symomentum_%s%s.png' %(output_dir+sep, 1920 # base_name+number, 1921 # extra_plot_name) 1922 # if verbose: print 'saving figure here %s' %figname 1923 # pylab.savefig(figname) 1924 # 1925 # if "momentum" in quantities: 1926 # pylab.figure(500+j) 1927 # pylab.plot(t, momentum, c = cstr[i], linewidth=1) 1928 # pylab.xlabel(time_label) 1929 # pylab.ylabel(momentum_label) 1930 # pylab.axis(momentum_axis) 1931 # pylab.legend(legend_list,loc='upper right') 1932 # if output_dir =='': 1933 # figname = '%smomentum_%s%s.png' %(directory+sep, 1934 # base_name+number, 1935 # extra_plot_name) 1936 # else: 1937 # figname = '%smomentum_%s%s.png' %(output_dir+sep, 1938 # base_name+number, 1939 # extra_plot_name) 1940 # if verbose: print 'saving figure here %s' %figname 1941 # pylab.savefig(figname) 1942 # 1943 # if "bearing" in quantities: 1944 # pylab.figure(600+j) 1945 # pylab.plot(t, bearing, c = cstr[i], linewidth=1) 1946 # pylab.xlabel(time_label) 1947 # pylab.ylabel(bearing_label) 1948 # pylab.axis(bearing_axis) 1949 # pylab.legend(legend_list,loc='upper right') 1950 # if output_dir =='': 1951 # figname = '%sbearing_%s%s.png' %(output_dir+sep, 1952 # base_name+number, 1953 # extra_plot_name) 1954 # else: 1955 # figname = '%sbearing_%s%s.png' %(output_dir+sep, 1956 # base_name+number, 1957 # extra_plot_name) 1958 # if verbose: print 'saving figure here %s' %figname 1959 # pylab.savefig(figname) 1960 # 1961 # if "depth" in quantities: 1962 # pylab.figure(700+j) 1963 # pylab.plot(t, depth, c = cstr[i], linewidth=1) 1964 # pylab.xlabel(time_label) 1965 # pylab.ylabel(depth_label) 1966 # pylab.axis(depth_axis) 1967 # pylab.legend(legend_list,loc='upper right') 1968 # if output_dir == '': 1969 # figname = '%sdepth_%s%s.png' %(directory, 1970 # base_name+number, 1971 # extra_plot_name) 1972 # else: 1973 # figname = '%sdepth_%s%s.png' %(output_dir+sep, 1974 # base_name+number, 1975 # extra_plot_name) 1976 # if verbose: print 'saving figure here %s' %figname 1977 # pylab.savefig(figname) 1978 # 1979 # if verbose: print 'Closing all plots' 1980 # pylab.close('all') 1981 # del pylab 1982 # if verbose: print 'Finished closing plots' 1983 1983 1984 1984 def csv2timeseries_graphs(directories_dic={}, … … 1992 1992 verbose=False): 1993 1993 1994 """ Read in csv files that have the right header information and 1994 """ 1995 Read in csv files that have the right header information and 1995 1996 plot time series such as Stage, Speed, etc. Will also plot several 1996 1997 time series on one plot. Filenames must follow this convention, … … 2066 2067 dir2:['b_emoth',5000,1.5], 2067 2068 dir3:['b_ons',5000,1.5]} 2069 Having multiple directories defined will plot them on 2070 one plot, therefore there will be 3 lines on each of these 2071 plot. If you only want one line per plot call csv2timeseries_graph 2072 separately for each directory, eg only have one directory in the 2073 'directories_dic' in each call. 2068 2074 2069 output_dir: directory for the plot outputs, essential to define 2070 if you want a plot with multiple timeseries. 2075 output_dir: directory for the plot outputs. Only important to define 2076 when you have more than one directory in your directories_dic, 2077 if you have not defined it and you have multiple directories in 2078 'directories_dic' there will be plots in each directory however only 2079 one directory will contain the complete plot/graphs. 2071 2080 2072 2081 base_name: Is used a couple of times. 1) to find the csv files to be plotted … … 2082 2091 NOTE: if no plot numbers this will create 2083 2092 one plot per quantity, per gauge 2084 quantities: Currently limited to "stage", "speed", and2085 "Momentum", should be changed to incorporate2086 any quantity read from CSV file....2093 quantities: Will get available quantities from the header in the csv file. 2094 quantities must be one of these. 2095 NOTE: ALL QUANTITY NAMES MUST BE lower case! 2087 2096 2088 2097 extra_plot_name: A string that is appended to the end of the … … 2098 2107 create_latex: NOT IMPLEMENTED YET!! sorry Jane.... 2099 2108 2100 OUTPUTS: None, itsaves the plots to2109 OUTPUTS: saves the plots to 2101 2110 <output_dir><base_name><plot_number><extra_plot_name>.png 2102 2111 2103 KNOWN PROBLEMS: Currently the axis for the stage/depth will be incorporate2104 2112 2105 2113 """ 2106 2107 import pylab 2114 try: 2115 import pylab 2116 except ImportError: 2117 msg='csv2timeseries_graphs needs pylab to be installed correctly' 2118 raise msg 2119 #ANUGA don't need pylab to work so the system doesn't 2120 #rely on pylab being installed 2121 return 2122 2108 2123 from os import sep 2109 2124 from anuga.shallow_water.data_manager import \ … … 2217 2232 min_quantity_value[quantity], \ 2218 2233 max_quantity_value[quantity] = get_min_max_values(quantity_value[quantity]) 2234 2235 if quantity != 'time': 2236 min_quantity_value[quantity] = min_quantity_value[quantity] *1.1 2237 max_quantity_value[quantity] = max_quantity_value[quantity] *1.1 2238 2219 2239 2220 2240 # print '1 min,max',i,j,k,quantity, min_quantity_value[quantity],max_quantity_value[quantity],directory, filename … … 2222 2242 # print 'min,max',i,j,k,quantity, min_quantity_value[quantity],max_quantity_value[quantity],directory, filename 2223 2243 min, max = get_min_max_values(quantity_value[quantity]) 2244 # print "MIN",min, max 2224 2245 2225 if min<min_quantity_value[quantity]: min_quantity_value[quantity]=min 2226 if max>max_quantity_value[quantity]: max_quantity_value[quantity]=max 2246 #min and max are multipled by "1+increase_axis" to get axes that are slighty bigger 2247 # than the max and mins so the plots look good. 2248 2249 increase_axis = 0.05 2250 if min<=min_quantity_value[quantity]: 2251 if quantity == 'time': 2252 min_quantity_value[quantity]=min 2253 else: 2254 if round(min,1) == 0.0: 2255 min_quantity_value[quantity]=-2. 2256 #min_quantity_value[quantity]= -max_quantity_value[quantity]*increase_axis 2257 else: 2258 min_quantity_value[quantity]=min*(1+increase_axis) 2259 2260 if max>max_quantity_value[quantity]: 2261 if quantity == 'time': 2262 max_quantity_value[quantity]=max 2263 else: 2264 max_quantity_value[quantity]=max*(1+increase_axis) 2227 2265 2228 #print 'min,maj',quantity, min_quantity_value[quantity],max_quantity_value[quantity] 2229 2230 #add tide to stage if provided 2231 # if quantity == 'stage': 2232 # quantity_value[quantity]=array(quantity_value[quantity])+directory_add_tide 2233 2266 # print 'min,maj',quantity, min_quantity_value[quantity],max_quantity_value[quantity] 2267 2268 2269 2270 2234 2271 #set the time... ??? 2235 2272 if min_start_time > directory_start_time: … … 2320 2357 num=int(k*100+j) 2321 2358 pylab.figure(num) 2322 # note if elevation is greater than 0m the stage plot is called 'depth' and the stage has 2323 # the elevation minused from it. This allows the plots to make more sense see manual 2324 # section 4.7 2325 # if quantity=='stage': 2326 # quantity_value[quantity] =array(quantity_value[quantity])+directory_add_tide 2327 2328 # if min_ele>=0: 2329 # quantity_value['stage']=array(quantity_value['stage'])-min_ele 2330 # pylab.ylabel(quantities_label['depth']) 2331 # pylab.axis([quantities_axis['stage'][0], 2332 # quantities_axis['stage'][1], 2333 # quantities_axis['stage'][2], 2334 # quantities_axis['stage'][3]])#-quantities_axis['elevation'][3]]) 2335 # print "AXIS", quantities_axis['stage'] 2336 # else: 2337 # pylab.ylabel(quantities_label['stage']) 2338 # else: 2339 # pylab.ylabel(quantities_label[quantity]) 2359 2340 2360 pylab.ylabel(quantities_label[quantity]) 2341 2361 pylab.plot(t, quantity_value[quantity], c = cstr[i], linewidth=1) … … 2346 2366 if output_dir == '': 2347 2367 figname = '%s%s_%s%s.png' %(directory, 2368 filename, 2348 2369 quantity, 2349 filename,2350 2370 extra_plot_name) 2351 2371 else: 2352 2372 figname = '%s%s_%s%s.png' %(output_dir, 2373 filename, 2353 2374 quantity, 2354 filename,2355 2375 extra_plot_name) 2356 2376 if verbose: print 'saving figure here %s' %figname … … 2363 2383 2364 2384 2365 def old_get_min_max_values(list=None,min1=100000,max1=-100000):2366 """ Returns the min and max of the list it was provided.2367 NOTE: default min and max may need to change depeending on2368 your list2369 """2370 if list == None: print 'List must be provided'2371 if max(list) > max1:2372 max1 = max(list)2373 if min(list) < min1:2374 min1 = min(list)2375 2376 return min1, max12385 #def old_get_min_max_values(list=None,min1=100000,max1=-100000): 2386 # """ Returns the min and max of the list it was provided. 2387 # NOTE: default min and max may need to change depeending on 2388 # your list 2389 # """ 2390 # if list == None: print 'List must be provided' 2391 # if max(list) > max1: 2392 # max1 = max(list) 2393 # if min(list) < min1: 2394 # min1 = min(list) 2395 # 2396 # return min1, max1 2377 2397 2378 2398 def get_min_max_values(list=None): … … 2583 2603 points_writer.append(writer(file(dir_name+sep+'gauge_'+point_name[i]+'.csv', "wb"))) 2584 2604 points_writer[i].writerow(heading) 2585 2586 2605 2587 2606 if verbose: print 'Writing csv files' … … 2684 2703 return h2 2685 2704 2705 2706 def square_root(s): 2707 return sqrt(s)
Note: See TracChangeset
for help on using the changeset viewer.