Changeset 6072
- Timestamp:
- Dec 12, 2008, 10:35:06 AM (16 years ago)
- Location:
- anuga_core/source/anuga/abstract_2d_finite_volumes
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
anuga_core/source/anuga/abstract_2d_finite_volumes/test_util.py
r6070 r6072 1833 1833 #------------------------------------------------------------- 1834 1834 if __name__ == "__main__": 1835 #suite = unittest.makeSuite(Test_Util,'test')1836 suite = unittest.makeSuite(Test_Util,'test_remove_lone_verts')1835 suite = unittest.makeSuite(Test_Util,'test') 1836 # suite = unittest.makeSuite(Test_Util,'test_remove_lone_verts') 1837 1837 # runner = unittest.TextTestRunner(verbosity=2) 1838 1838 runner = unittest.TextTestRunner(verbosity=1) -
anuga_core/source/anuga/abstract_2d_finite_volumes/util.py
r6070 r6072 1 #123456789012345678901234567890123456789012345678901234567890123456789012345678902 1 """This module contains various auxiliary function used by pyvolution. 3 2 … … 1752 1751 # verts=take(verts,X) to Remove the loners from verts 1753 1752 # but I think it would use more memory 1754 new_i = lone_start # point at first loner - first'shuffle down' target1753 new_i = lone_start # point at first loner - 'shuffle down' target 1755 1754 for i in range(lone_start, N): 1756 1755 if loners[i] >= N: # [i] is a loner, leave alone … … 1769 1768 return verts, triangles 1770 1769 1771 1770 1771 ## 1772 # @brief Compute centroid values from vertex values 1773 # @param x Values at vertices of triangular mesh. 1774 # @param triangles Nx3 integer array pointing to vertex information. 1775 # @return [N] array of centroid values. 1772 1776 def get_centroid_values(x, triangles): 1773 1777 """Compute centroid values from vertex values … … 1778 1782 indices into x 1779 1783 """ 1780 1781 1784 1782 1785 xc = zeros(triangles.shape[0], Float) # Space for centroid info … … 1790 1793 xc[k] = (x[i0] + x[i1] + x[i2])/3 1791 1794 1792 1793 1795 return xc 1796 1794 1797 1795 1798 # @note TEMP … … 1802 1805 extra_plot_name='test' ): 1803 1806 1804 msg = 'make_plots_from_csv_file has been replaced by csv2timeseries_graphs ' ,1805 msg += 'Please use "from anuga.abstract_2d_finite_volumes.util import csv2timeseries_graphs"'1806 1807 msg = 'make_plots_from_csv_file has been replaced by csv2timeseries_graphs ' 1808 msg += 'Please use "from anuga.abstract_2d_finite_volumes.util import ' \ 1809 'csv2timeseries_graphs"' 1807 1810 raise Exception, msg 1811 1808 1812 return csv2timeseries_graphs(directories_dic, 1809 1813 output_dir, … … 1814 1818 assess_all_csv_files 1815 1819 ) 1816 1820 1821 1822 ## 1823 # @brief Plot time series from CSV files. 1824 # @param directories_dic 1825 # @param output_dir 1826 # @param base_name 1827 # @param plot_numbers 1828 # @param quantities 1829 # @param extra_plot_name 1830 # @param assess_all_csv_files 1831 # @param create_latex 1832 # @param verbose 1833 # @note Assumes that 'elevation' is in the CSV file(s). 1817 1834 def csv2timeseries_graphs(directories_dic={}, 1818 1819 1820 1821 1822 1823 assess_all_csv_files=True,1824 1825 1835 output_dir='', 1836 base_name=None, 1837 plot_numbers='', 1838 quantities=['stage'], 1839 extra_plot_name='', 1840 assess_all_csv_files=True, 1841 create_latex=False, 1842 verbose=False): 1826 1843 1827 1844 """ … … 1888 1905 verbose=True) 1889 1906 1890 This will produce one plot for each quantity (therefore 3) in the current directory, 1891 each plot will have 2 lines on them. The first plot named 'new' will have the time 1892 offseted by 20secs and the stage height adjusted by -0.1m 1907 This will produce one plot for each quantity (therefore 3) in the 1908 current directory, each plot will have 2 lines on them. The first 1909 plot named 'new' will have the time offseted by 20secs and the stage 1910 height adjusted by -0.1m 1893 1911 1894 1912 Inputs: … … 1897 1915 the time series) and the (value to add to 1898 1916 stage if needed). For example 1899 {dir1:['Anuga_ons',5000, 0],1900 dir2:['b_emoth',5000,1.5],1901 dir3:['b_ons',5000,1.5]}1917 {dir1:['Anuga_ons',5000, 0], 1918 dir2:['b_emoth',5000,1.5], 1919 dir3:['b_ons',5000,1.5]} 1902 1920 Having multiple directories defined will plot them on 1903 one plot, therefore there will be 3 lines on each of these 1904 plot. If you only want one line per plot call csv2timeseries_graph 1905 separately for each directory, eg only have one directory in the 1906 'directories_dic' in each call. 1921 one plot, therefore there will be 3 lines on each of 1922 these plot. If you only want one line per plot call 1923 csv2timeseries_graph separately for each directory, 1924 eg only have one directory in the 'directories_dic' in 1925 each call. 1907 1926 1908 output_dir: directory for the plot outputs. Only important to define 1909 when you have more than one directory in your directories_dic, 1910 if you have not defined it and you have multiple directories in 1911 'directories_dic' there will be plots in each directory however only 1912 one directory will contain the complete plot/graphs. 1913 1914 base_name: Is used a couple of times. 1) to find the csv files to be plotted 1915 if there is no 'plot_numbers' then csv files with 'base_name' are 1916 plotted and 2) in the title of the plots, the lenght of base_name is 1917 removed from the front of the filename to be used in the title. 1927 output_dir: directory for the plot outputs. Only important to define when 1928 you have more than one directory in your directories_dic, if 1929 you have not defined it and you have multiple directories in 1930 'directories_dic' there will be plots in each directory, 1931 however only one directory will contain the complete 1932 plot/graphs. 1933 1934 base_name: Is used a couple of times. 1935 1) to find the csv files to be plotted if there is no 1936 'plot_numbers' then csv files with 'base_name' are plotted 1937 2) in the title of the plots, the length of base_name is 1938 removed from the front of the filename to be used in the 1939 title. 1918 1940 This could be changed if needed. 1919 1941 Note is ignored if assess_all_csv_files=True … … 1921 1943 plot_numbers: a String list of numbers to plot. For example 1922 1944 [0-4,10,15-17] will read and attempt to plot 1923 the follow 0,1,2,3,4,10,15,16,17 1924 NOTE: if no plot numbers this will create 1925 one plot per quantity, per gauge 1926 quantities: Will get available quantities from the header in the csv file. 1927 quantities must be one of these. 1945 the follow 0,1,2,3,4,10,15,16,17 1946 NOTE: if no plot numbers this will create one plot per 1947 quantity, per gauge 1948 1949 quantities: Will get available quantities from the header in the csv 1950 file. Quantities must be one of these. 1928 1951 NOTE: ALL QUANTITY NAMES MUST BE lower case! 1929 1952 … … 1942 1965 OUTPUTS: saves the plots to 1943 1966 <output_dir><base_name><plot_number><extra_plot_name>.png 1944 1945 1946 1967 """ 1968 1947 1969 try: 1948 1970 import pylab … … 1972 1994 quantities_label['bearing'] = 'degrees (o)' 1973 1995 quantities_label['elevation'] = 'elevation (m)' 1974 1975 1996 1976 1997 if extra_plot_name != '': 1977 extra_plot_name ='_'+extra_plot_name1998 extra_plot_name = '_' + extra_plot_name 1978 1999 1979 2000 new_plot_numbers=[] … … 1983 2004 if '-' in num_string: 1984 2005 start = int(num_string[:num_string.rfind('-')]) 1985 end = int(num_string[num_string.rfind('-') +1:])+12006 end = int(num_string[num_string.rfind('-') + 1:]) + 1 1986 2007 for x in range(start, end): 1987 2008 new_plot_numbers.append(str(x)) … … 1995 2016 if verbose: print 'Determining files to access for axes ranges \n' 1996 2017 1997 #print directories_dic.keys(), base_name1998 1999 2018 for i,directory in enumerate(directories_dic.keys()): 2000 2019 all_csv_filenames.append(get_all_files_with_extension(directory, 2001 base_name,'.csv'))2020 base_name, '.csv')) 2002 2021 2003 2022 filenames=[] 2004 2023 if plot_numbers == '': 2005 2024 list_filenames.append(get_all_files_with_extension(directory, 2006 base_name,'.csv'))2025 base_name,'.csv')) 2007 2026 else: 2008 2027 for number in new_plot_numbers: 2009 # print 'number!!!', base_name, number 2010 filenames.append(base_name+number) 2011 # print filenames 2028 filenames.append(base_name + number) 2012 2029 list_filenames.append(filenames) 2013 2014 2015 2016 #print "list_filenames", list_filenames2017 2030 2018 2031 #use all the files to get the values for the plot axis … … 2020 2033 min_start_time = 100000 2021 2034 2022 2023 2035 if verbose: print 'Determining uniform axes \n' 2036 2024 2037 #this entire loop is to determine the min and max range for the 2025 2038 #axes of the plots … … 2033 2046 max_quantity_value={} 2034 2047 2035 2036 2048 for i, directory in enumerate(directories_dic.keys()): 2037 filename_quantity_value ={}2038 if assess_all_csv_files ==False:2049 filename_quantity_value = {} 2050 if assess_all_csv_files == False: 2039 2051 which_csv_to_assess = list_filenames[i] 2040 2052 else: 2041 2053 #gets list of filenames for directory "i" 2042 2054 which_csv_to_assess = all_csv_filenames[i] 2043 # print'IN DIR', list_filenames[i]2044 2045 2046 2047 2055 2048 2056 for j, filename in enumerate(which_csv_to_assess): 2049 quantity_value={} 2050 2051 dir_filename=join(directory,filename) 2052 attribute_dic, title_index_dic = csv2dict(dir_filename+ 2053 '.csv') 2057 quantity_value = {} 2058 2059 dir_filename = join(directory,filename) 2060 attribute_dic, title_index_dic = csv2dict(dir_filename + '.csv') 2054 2061 directory_start_time = directories_dic[directory][1] 2055 2062 directory_add_tide = directories_dic[directory][2] 2056 2063 2057 2064 if verbose: print 'reading: %s.csv' %dir_filename 2058 # print 'keys',attribute_dic.keys() 2065 2059 2066 #add time to get values 2060 2067 for k, quantity in enumerate(quantities): 2061 quantity_value[quantity] = [float(x) for x in attribute_dic[quantity]] 2068 quantity_value[quantity] = [float(x) for 2069 x in attribute_dic[quantity]] 2062 2070 2063 2071 #add tide to stage if provided 2064 2072 if quantity == 'stage': 2065 quantity_value[quantity]=array(quantity_value[quantity])+directory_add_tide 2073 quantity_value[quantity] = array(quantity_value[quantity]) \ 2074 + directory_add_tide 2066 2075 2067 2076 #condition to find max and mins for all the plots … … 2069 2078 # then compare to the other values to determine abs max and min 2070 2079 if i==0 and j==0: 2071 2072 2080 min_quantity_value[quantity], \ 2073 max_quantity_value[quantity] = get_min_max_values(quantity_value[quantity]) 2081 max_quantity_value[quantity] = \ 2082 get_min_max_values(quantity_value[quantity]) 2074 2083 2075 2084 if quantity != 'time': 2076 min_quantity_value[quantity] = min_quantity_value[quantity] *1.1 2077 max_quantity_value[quantity] = max_quantity_value[quantity] *1.1 2078 2079 2080 # print '1 min,max',i,j,k,quantity, min_quantity_value[quantity],max_quantity_value[quantity],directory, filename 2085 min_quantity_value[quantity] = \ 2086 min_quantity_value[quantity] *1.1 2087 max_quantity_value[quantity] = \ 2088 max_quantity_value[quantity] *1.1 2081 2089 else: 2082 # print 'min,max',i,j,k,quantity, min_quantity_value[quantity],max_quantity_value[quantity],directory, filename2083 2090 min, max = get_min_max_values(quantity_value[quantity]) 2084 # print "MIN",min, max2085 2091 2086 #min and max are multipled by "1+increase_axis" to get axes that are slighty bigger 2087 # than the max and mins so the plots look good. 2092 # min and max are multipled by "1+increase_axis" to get axes 2093 # that are slighty bigger than the max and mins 2094 # so the plots look good. 2088 2095 2089 2096 increase_axis = (max-min)*0.05 2090 # print quantity, "MIN MAX", max, min 2091 if min<=min_quantity_value[quantity]: 2097 if min <= min_quantity_value[quantity]: 2092 2098 if quantity == 'time': 2093 min_quantity_value[quantity] =min2099 min_quantity_value[quantity] = min 2094 2100 else: 2095 2101 if round(min,2) == 0.00: 2096 min_quantity_value[quantity]=-increase_axis 2097 # min_quantity_value[quantity]=-2. 2098 #min_quantity_value[quantity]= -max_quantity_value[quantity]*increase_axis 2102 min_quantity_value[quantity] = -increase_axis 2103 # min_quantity_value[quantity] = -2. 2104 #min_quantity_value[quantity] = \ 2105 # -max_quantity_value[quantity]*increase_axis 2099 2106 else: 2100 # min_quantity_value[quantity]=min*(1+increase_axis) 2107 # min_quantity_value[quantity] = \ 2108 # min*(1+increase_axis) 2101 2109 min_quantity_value[quantity]=min-increase_axis 2102 # print quantity, min_quantity_value[quantity]2103 2110 2104 if max >max_quantity_value[quantity]:2111 if max > max_quantity_value[quantity]: 2105 2112 if quantity == 'time': 2106 max_quantity_value[quantity] =max2113 max_quantity_value[quantity] = max 2107 2114 else: 2108 max_quantity_value[quantity] =max+increase_axis2115 max_quantity_value[quantity] = max + increase_axis 2109 2116 # max_quantity_value[quantity]=max*(1+increase_axis) 2110 # print quantity, max_quantity_value[quantity],increase_axis 2111 2112 # print 'min,maj',quantity, min_quantity_value[quantity],max_quantity_value[quantity] 2113 2114 2115 2116 2117 2117 2118 #set the time... ??? 2118 2119 if min_start_time > directory_start_time: … … 2120 2121 if max_start_time < directory_start_time: 2121 2122 max_start_time = directory_start_time 2122 #print 'start_time' , max_start_time, min_start_time2123 2123 2124 2124 filename_quantity_value[filename]=quantity_value … … 2130 2130 2131 2131 for i, quantity in enumerate(quantities): 2132 quantities_axis[quantity] = (float(min_start_time)/float(seconds_in_minutes), 2133 (float(max_quantity_value['time'])+float(max_start_time))\ 2134 /float(seconds_in_minutes), 2135 min_quantity_value[quantity], 2136 max_quantity_value[quantity]) 2132 quantities_axis[quantity] = (float(min_start_time) \ 2133 / float(seconds_in_minutes), 2134 (float(max_quantity_value['time']) \ 2135 + float(max_start_time)) \ 2136 / float(seconds_in_minutes), 2137 min_quantity_value[quantity], 2138 max_quantity_value[quantity]) 2139 2137 2140 if verbose and (quantity != 'time' and quantity != 'elevation'): 2138 print 'axis for quantity %s are x:(%s to %s)%s and y:(%s to %s)%s' %(quantity, 2139 quantities_axis[quantity][0], 2140 quantities_axis[quantity][1], 2141 quantities_label['time'], 2142 quantities_axis[quantity][2], 2143 quantities_axis[quantity][3], 2144 quantities_label[quantity]) 2145 2146 #print quantities_axis[quantity] 2141 print 'axis for quantity %s are x:(%s to %s)%s and y:(%s to %s)%s' \ 2142 % (quantity, 2143 quantities_axis[quantity][0], 2144 quantities_axis[quantity][1], 2145 quantities_label['time'], 2146 quantities_axis[quantity][2], 2147 quantities_axis[quantity][3], 2148 quantities_label[quantity]) 2147 2149 2148 2150 cstr = ['b', 'r', 'g', 'c', 'm', 'y', 'k'] … … 2151 2153 2152 2154 i_max = len(directories_dic.keys()) 2153 legend_list_dic ={}2154 legend_list = []2155 legend_list_dic = {} 2156 legend_list = [] 2155 2157 for i, directory in enumerate(directories_dic.keys()): 2156 2157 if verbose: print'Plotting in %s %s' %(directory, new_plot_numbers) 2158 # print 'LIST',list_filenames 2159 # FIXME THIS SORT IS VERY IMPORTANT, without it the assigned plot numbers may not work correctly2160 # there must be a better way2158 if verbose: print 'Plotting in %s %s' % (directory, new_plot_numbers) 2159 2160 # FIXME THIS SORT IS VERY IMPORTANT 2161 # Without it the assigned plot numbers may not work correctly 2162 # there must be a better way 2161 2163 list_filenames[i].sort() 2162 2164 for j, filename in enumerate(list_filenames[i]): 2163 # print'IN plot', list_filenames[i] 2164 2165 if verbose: print'Starting %s' %filename 2165 if verbose: print 'Starting %s' % filename 2166 2166 2167 directory_name = directories_dic[directory][0] 2167 2168 directory_start_time = directories_dic[directory][1] 2168 2169 directory_add_tide = directories_dic[directory][2] 2169 2170 2170 #create an if about the start time and tide hieght 2171 #if they don't exist 2172 #print 'i %s,j %s, number %s, file %s' %(i,j,number,file) 2173 attribute_dic, title_index_dic = csv2dict(directory+sep+filename+'.csv') 2171 # create an if about the start time and tide height if don't exist 2172 attribute_dic, title_index_dic = csv2dict(directory + sep 2173 + filename + '.csv') 2174 2174 #get data from dict in to list 2175 2175 #do maths to list by changing to array 2176 t=(array(directory_quantity_value[directory][filename]['time'])+directory_start_time)/seconds_in_minutes 2176 t = (array(directory_quantity_value[directory][filename]['time']) 2177 + directory_start_time) / seconds_in_minutes 2177 2178 2178 2179 #finds the maximum elevation, used only as a test … … 2187 2188 print "Note! Elevation changes in %s" %dir_filename 2188 2189 2189 # creates a dictionary with keys that is the filename and attributes are a list of 2190 # lists containing 'directory_name' and 'elevation'. This is used to make the contents 2191 # for the legends in the graphs, this is the name of the model and the elevation. 2192 # All in this great one liner from DG. If the key 'filename' doesn't exist it creates the 2193 # entry if the entry exist it appends to the key. 2194 2195 legend_list_dic.setdefault(filename,[]).append([directory_name,round(max_ele,3)]) 2196 2197 # creates a LIST for the legend on the last iteration of the directories 2198 # which is when "legend_list_dic" has been fully populated. Creates a list of strings 2199 # which is used in the legend 2190 # creates a dictionary with keys that is the filename and attributes 2191 # are a list of lists containing 'directory_name' and 'elevation'. 2192 # This is used to make the contents for the legends in the graphs, 2193 # this is the name of the model and the elevation. All in this 2194 # great one liner from DG. If the key 'filename' doesn't exist it 2195 # creates the entry if the entry exist it appends to the key. 2196 2197 legend_list_dic.setdefault(filename,[]) \ 2198 .append([directory_name, round(max_ele, 3)]) 2199 2200 # creates a LIST for the legend on the last iteration of the 2201 # directories which is when "legend_list_dic" has been fully 2202 # populated. Creates a list of strings which is used in the legend 2200 2203 # only runs on the last iteration for all the gauges(csv) files 2201 2204 # empties the list before creating it 2202 if i==i_max-1: 2203 legend_list=[]2204 2205 #print 'DIC',legend_list_dic2205 2206 if i == i_max - 1: 2207 legend_list = [] 2208 2206 2209 for name_and_elevation in legend_list_dic[filename]: 2207 legend_list.append('%s (elevation = %sm)'%(name_and_elevation[0],name_and_elevation[1])) 2210 legend_list.append('%s (elevation = %sm)'\ 2211 % (name_and_elevation[0], 2212 name_and_elevation[1])) 2208 2213 2209 #print 'filename',filename, quantities2210 2214 #skip time and elevation so it is not plotted! 2211 2215 for k, quantity in enumerate(quantities): 2212 2216 if quantity != 'time' and quantity != 'elevation': 2213 2214 num=int(k*100+j) 2217 num = int(k*100+j) 2215 2218 pylab.figure(num) 2216 2219 pylab.ylabel(quantities_label[quantity]) 2217 pylab.plot(t, directory_quantity_value[directory][filename][quantity], c = cstr[i], linewidth=1) 2220 pylab.plot(t, 2221 directory_quantity_value[directory]\ 2222 [filename][quantity], 2223 c = cstr[i], linewidth=1) 2218 2224 pylab.xlabel(quantities_label['time']) 2219 2225 pylab.axis(quantities_axis[quantity]) 2220 2226 pylab.legend(legend_list,loc='upper right') 2221 2227 2222 pylab.title('%s at %s gauge' %(quantity,filename[len(base_name):])) 2228 pylab.title('%s at %s gauge' 2229 % (quantity, filename[len(base_name):])) 2230 2223 2231 if output_dir == '': 2224 figname = '%s%s%s_%s%s.png' %(directory,sep, 2225 filename, 2226 quantity, 2227 extra_plot_name) 2232 figname = '%s%s%s_%s%s.png' \ 2233 % (directory, sep, filename, quantity, 2234 extra_plot_name) 2228 2235 else: 2229 figname = '%s%s%s_%s%s.png' %(output_dir,sep,2230 filename,2231 quantity,2232 extra_plot_name) 2236 figname = '%s%s%s_%s%s.png' \ 2237 % (output_dir, sep, filename, quantity, 2238 extra_plot_name) 2239 2233 2240 if verbose: print 'saving figure here %s' %figname 2241 2234 2242 pylab.savefig(figname) 2235 2243 2236 2244 if verbose: print 'Closing all plots' 2245 2237 2246 pylab.close('all') 2238 2247 del pylab 2248 2239 2249 if verbose: print 'Finished closing plots' 2240 2250 2251 ## 2252 # @brief Return min and max of an iterable. 2253 # @param list The iterable to return min & max of. 2254 # @return (min, max) of 'list'. 2241 2255 def get_min_max_values(list=None): 2242 2256 """ 2243 2257 Returns the min and max of the list it was provided. 2244 2258 """ 2259 2245 2260 if list == None: print 'List must be provided' 2246 2261 … … 2248 2263 2249 2264 2250 2265 ## 2266 # @brief Get runup around a point in a CSV file. 2267 # @param gauge_filename gauge file name. 2268 # @param sww_filename SWW file name. 2269 # @param runup_filename Name of file to report into. 2270 # @param size ?? 2271 # @param verbose ?? 2251 2272 def get_runup_data_for_locations_from_file(gauge_filename, 2252 2273 sww_filename, … … 2254 2275 size=10, 2255 2276 verbose=False): 2256 2257 2277 """this will read a csv file with the header x,y. Then look in a square 2258 2278 'size'x2 around this position for the 'max_inundaiton_height' in the 2259 'sww_filename' and report the findings in the 'runup_filename 2279 'sww_filename' and report the findings in the 'runup_filename'. 2260 2280 2261 2281 WARNING: NO TESTS! … … 2266 2286 csv2dict 2267 2287 2268 file = open(runup_filename, "w")2288 file = open(runup_filename, "w") 2269 2289 file.write("easting,northing,runup \n ") 2270 2290 file.close() … … 2279 2299 runup_locations=[] 2280 2300 for i, x in enumerate(northing): 2281 # print 'easting,northing',i,easting[i],northing[i]2282 2301 poly = [[int(easting[i]+size),int(northing[i]+size)], 2283 2302 [int(easting[i]+size),int(northing[i]-size)], … … 2286 2305 2287 2306 run_up, x_y = get_maximum_inundation_data(filename=sww_filename, 2288 polygon=poly, 2289 verbose=False) 2307 polygon=poly, 2308 verbose=False) 2309 2290 2310 #if no runup will return 0 instead of NONE 2291 2311 if run_up==None: run_up=0 … … 2296 2316 2297 2317 #writes to file 2298 file = open(runup_filename, "a")2299 temp = '%s,%s,%s \n' % (x_y[0], x_y[1], run_up)2318 file = open(runup_filename, "a") 2319 temp = '%s,%s,%s \n' % (x_y[0], x_y[1], run_up) 2300 2320 file.write(temp) 2301 2321 file.close() 2302 2303 2322 2323 2324 ## 2325 # @brief ?? 2326 # @param sww_file ?? 2327 # @param gauge_file ?? 2328 # @param out_name ?? 2329 # @param quantities ?? 2330 # @param verbose ?? 2331 # @param use_cache ?? 2304 2332 def sww2csv_gauges(sww_file, 2305 2333 gauge_file, 2306 2334 out_name='gauge_', 2307 quantities =['stage', 'depth', 'elevation',2308 2335 quantities=['stage', 'depth', 'elevation', 2336 'xmomentum', 'ymomentum'], 2309 2337 verbose=False, 2310 2338 use_cache = True): … … 2355 2383 2356 2384 This is really returning speed, not velocity. 2357 2358 2359 2385 """ 2360 2386 … … 2364 2390 import string 2365 2391 from anuga.shallow_water.data_manager import get_all_swwfiles 2392 2366 2393 # quantities = ['stage', 'elevation', 'xmomentum', 'ymomentum'] 2367 2394 #print "points",points 2368 2395 2369 assert type(gauge_file) == type(''),\ 2370 'Gauge filename must be a string' 2371 2372 assert type(out_name) == type(''),\ 2373 'Output filename prefix must be a string' 2396 assert type(gauge_file) == type(''), 'Gauge filename must be a string' 2397 assert type(out_name) == type(''), 'Output filename prefix must be a string' 2374 2398 2375 2399 try: 2376 # fid = open(gauge_file)2377 2400 point_reader = reader(file(gauge_file)) 2378 2379 2401 except Exception, e: 2380 msg = 'File "%s" could not be opened: Error="%s"'\ 2381 %(gauge_file, e) 2402 msg = 'File "%s" could not be opened: Error="%s"' % (gauge_file, e) 2382 2403 raise msg 2404 2383 2405 if verbose: print '\n Gauges obtained from: %s \n' %gauge_file 2384 2385 2406 2386 2407 point_reader = reader(file(gauge_file)) … … 2390 2411 #read point info from file 2391 2412 for i,row in enumerate(point_reader): 2392 # print 'i',i,'row',row2393 2413 #read header and determine the column numbers to read correcty. 2394 2414 if i==0: 2395 2415 for j,value in enumerate(row): 2396 # print 'j',j,value, row2397 2416 if value.strip()=='easting':easting=j 2398 2417 if value.strip()=='northing':northing=j … … 2400 2419 if value.strip()=='elevation':elevation=j 2401 2420 else: 2402 # print i,'easting',easting,'northing',northing, row[easting]2403 2421 points.append([float(row[easting]),float(row[northing])]) 2404 2422 point_name.append(row[name]) … … 2410 2428 2411 2429 dir_name, base = os.path.split(sww_file) 2412 #print 'dirname',dir_name, base 2430 2413 2431 #need to get current directory so when path and file 2414 2432 #are "joined" below the directory is correct … … 2419 2437 if verbose: print 'File %s exists' %(sww_file) 2420 2438 else: 2421 msg = 'File "%s" could not be opened: no read permission'\ 2422 %(sww_file) 2439 msg = 'File "%s" could not be opened: no read permission' % sww_file 2423 2440 raise msg 2424 2441 … … 2436 2453 2437 2454 for sww_file in sww_files: 2438 2439 # print 'sww_file',sww_file2440 2455 sww_file = join(dir_name, sww_file+'.sww') 2441 # print 'sww_file',sww_file, core_quantities2442 2443 2456 callable_sww = file_function(sww_file, 2444 quantities=core_quantities, 2445 interpolation_points=points_array, 2446 verbose=verbose, 2447 use_cache=use_cache) 2448 2457 quantities=core_quantities, 2458 interpolation_points=points_array, 2459 verbose=verbose, 2460 use_cache=use_cache) 2449 2461 gauge_file = out_name 2450 2462 2451 2463 heading = [quantity for quantity in quantities] 2452 2464 heading.insert(0,'time') 2453 2454 # print 'start time', callable_sww.starttime, heading, quantities2455 2465 2456 2466 #create a list of csv writers for all the points and write header 2457 2467 points_writer = [] 2458 2468 for i,point in enumerate(points): 2459 #print 'gauge file:',dir_name+sep+'gauge_'+point_name[i]+'.csv'2460 points_writer.append(writer(file(dir_name+sep+gauge_file+point_name[i]+'.csv', "wb")))2469 points_writer.append(writer(file(dir_name + sep + gauge_file 2470 + point_name[i] + '.csv', "wb"))) 2461 2471 points_writer[i].writerow(heading) 2462 2472 … … 2464 2474 2465 2475 for time in callable_sww.get_time(): 2466 2467 2476 for point_i, point in enumerate(points_array): 2468 2477 #add domain starttime to relative time. 2469 points_list = [time+callable_sww.starttime] 2470 # print'time',time,'point_i',point_i,point, points_array 2478 points_list = [time + callable_sww.starttime] 2471 2479 point_quantities = callable_sww(time,point_i) 2472 # print "quantities", point_quantities2473 2480 2474 2481 for quantity in quantities: 2475 if quantity ==NAN:2476 print 'quantity does not exist in' % callable_sww.get_name2482 if quantity == NAN: 2483 print 'quantity does not exist in' % callable_sww.get_name 2477 2484 else: 2478 2485 if quantity == 'stage': … … 2489 2496 2490 2497 if quantity == 'depth': 2491 points_list.append(point_quantities[0] - point_quantities[1])2492 2493 2498 points_list.append(point_quantities[0] 2499 - point_quantities[1]) 2500 2494 2501 if quantity == 'momentum': 2495 momentum = sqrt(point_quantities[2]**2 +\2496 point_quantities[3]**2)2502 momentum = sqrt(point_quantities[2]**2 2503 + point_quantities[3]**2) 2497 2504 points_list.append(momentum) 2498 2505 … … 2503 2510 else: 2504 2511 if point_quantities[2] < 1.0e6: 2505 momentum = sqrt(point_quantities[2]**2 +\2506 point_quantities[3]**2)2512 momentum = sqrt(point_quantities[2]**2 2513 + point_quantities[3]**2) 2507 2514 # vel = momentum/depth 2508 vel = momentum/(point_quantities[0] - point_quantities[1]) 2515 vel = momentum / (point_quantities[0] 2516 - point_quantities[1]) 2509 2517 # vel = momentum/(depth + 1.e-6/depth) 2510 2518 else: … … 2517 2525 points_list.append(calc_bearing(point_quantities[2], 2518 2526 point_quantities[3])) 2519 2520 #print 'list',points_list 2521 2527 2522 2528 points_writer[point_i].writerow(points_list) 2523 2529 2524 2530 2525 def greens_law(d1,d2,h1,verbose=False): 2526 """ 2531 ## 2532 # @brief Get a wave height at a certain depth given wave height at another depth. 2533 # @param d1 The first depth. 2534 # @param d2 The second depth. 2535 # @param h1 Wave ampitude at d1 2536 # @param verbose True if this function is to be verbose. 2537 # @return The wave height at d2. 2538 def greens_law(d1, d2, h1, verbose=False): 2539 """Green's Law 2527 2540 2528 2541 Green's Law allows an approximation of wave amplitude at … … 2547 2560 2548 2561 if d1 <= 0.0: 2549 msg = 'the first depth, d1 (%f), must be strictly positive' % (d1)2562 msg = 'the first depth, d1 (%f), must be strictly positive' % (d1) 2550 2563 raise Exception(msg) 2551 2564 2552 2565 if d2 <= 0.0: 2553 msg = 'the second depth, d2 (%f), must be strictly positive' % (d2)2566 msg = 'the second depth, d2 (%f), must be strictly positive' % (d2) 2554 2567 raise Exception(msg) 2555 2568 2556 2569 if h1 <= 0.0: 2557 msg = 'the wave amplitude, h1 (%f), must be strictly positive' % (h1)2570 msg = 'the wave amplitude, h1 (%f), must be strictly positive' % (h1) 2558 2571 raise Exception(msg) 2559 2572 … … 2565 2578 2566 2579 2580 ## 2581 # @brief Get the square-root of a value. 2582 # @param s The value to get the square-root of. 2583 # @return The square-root of 's'. 2567 2584 def square_root(s): 2568 2585 return sqrt(s) 2586 2587
Note: See TracChangeset
for help on using the changeset viewer.