Changeset 4612
- Timestamp:
- Jul 9, 2007, 2:59:17 PM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
anuga_core/source/anuga/shallow_water/data_manager.py
r4608 r4612 105 105 106 106 def make_filename(s): 107 """Transform argument string into a suitable filename107 """Transform argument string into a Sexsuitable filename 108 108 """ 109 109 … … 808 808 #The values are the index positions of file columns. 809 809 self._attribute_dic, self._title_index_dic = \ 810 self._load_exposure_csv(self._file_name, 811 title_check_list=title_check_list) 810 csv2dict(self._file_name, title_check_list=title_check_list) 812 811 try: 813 812 #Have code here that handles caps or lower … … 867 866 return 1 868 867 869 def _load_exposure_csv(self, file_name, title_check_list=None):870 """871 Load in the csv as a dic, title as key and column info as value, .872 Also, create a dic, title as key and column index as value,873 to keep track of the column order.874 """875 #876 attribute_dic = {}877 title_index_dic = {}878 titles_stripped = [] # list of titles879 reader = csv.reader(file(file_name))880 881 # Read in and manipulate the title info882 titles = reader.next()883 for i,title in enumerate(titles):884 titles_stripped.append(title.strip())885 title_index_dic[title.strip()] = i886 title_count = len(titles_stripped)887 #print "title_index_dic",title_index_dic888 if title_check_list is not None:889 for title_check in title_check_list:890 #msg = "Reading error. This row is not present ", title_check891 #assert title_index_dic.has_key(title_check), msg892 if not title_index_dic.has_key(title_check):893 #reader.close()894 msg = "Reading error. This row is not present ", \895 title_check896 raise IOError, msg897 898 899 900 #create a dic of colum values, indexed by column title901 for line in reader:902 if len(line) <> title_count:903 raise IOError #FIXME make this nicer904 for i, value in enumerate(line):905 attribute_dic.setdefault(titles_stripped[i],[]).append(value)906 907 return attribute_dic, title_index_dic908 868 909 869 def get_column(self, column_name, use_refind_polygon=False): … … 1002 962 self._attribute_dic[title][row_i] 1003 963 writer.writerow(line) 964 965 966 def csv2dict(file_name, title_check_list=None): 967 """ 968 Load in the csv as a dic, title as key and column info as value, . 969 Also, create a dic, title as key and column index as value, 970 to keep track of the column order. 971 972 WARNING: Vaules are returned as strings. 973 do this to change a list of strings to a list of floats 974 time = [float(x) for x in time] 975 """ 976 977 # 978 attribute_dic = {} 979 title_index_dic = {} 980 titles_stripped = [] # list of titles 981 reader = csv.reader(file(file_name)) 982 983 # Read in and manipulate the title info 984 titles = reader.next() 985 for i,title in enumerate(titles): 986 titles_stripped.append(title.strip()) 987 title_index_dic[title.strip()] = i 988 title_count = len(titles_stripped) 989 #print "title_index_dic",title_index_dic 990 if title_check_list is not None: 991 for title_check in title_check_list: 992 #msg = "Reading error. This row is not present ", title_check 993 #assert title_index_dic.has_key(title_check), msg 994 if not title_index_dic.has_key(title_check): 995 #reader.close() 996 msg = "Reading error. This row is not present ", \ 997 title_check 998 raise IOError, msg 999 1000 1001 1002 #create a dic of colum values, indexed by column title 1003 for line in reader: 1004 if len(line) <> title_count: 1005 raise IOError #FIXME make this nicer 1006 for i, value in enumerate(line): 1007 attribute_dic.setdefault(titles_stripped[i],[]).append(value) 1008 1009 return attribute_dic, title_index_dic 1004 1010 1005 1011
Note: See TracChangeset
for help on using the changeset viewer.