Changeset 4612


Ignore:
Timestamp:
Jul 9, 2007, 2:59:17 PM (17 years ago)
Author:
nick
Message:

moved def _load_exposure_csv(self, file_name, title_check_list=None)
to
csv2dict(file_name, title_check_list=None)
This allows csv2dict to be used easily by other parts of ANUGA

File:
1 edited

Legend:

Unmodified
Added
Removed
  • anuga_core/source/anuga/shallow_water/data_manager.py

    r4608 r4612  
    105105   
    106106def make_filename(s):
    107     """Transform argument string into a suitable filename
     107    """Transform argument string into a Sexsuitable filename
    108108    """
    109109
     
    808808        #The values are the index positions of file columns.
    809809        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)
    812811        try:
    813812            #Have code here that handles caps or lower
     
    867866            return 1
    868867   
    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 titles
    879         reader = csv.reader(file(file_name))
    880 
    881         # Read in and manipulate the title info
    882         titles = reader.next()
    883         for i,title in enumerate(titles):
    884             titles_stripped.append(title.strip())
    885             title_index_dic[title.strip()] = i
    886         title_count = len(titles_stripped)       
    887         #print "title_index_dic",title_index_dic
    888         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_check
    891                 #assert title_index_dic.has_key(title_check), msg
    892                 if not title_index_dic.has_key(title_check):
    893                     #reader.close()
    894                     msg = "Reading error.  This row is not present ", \
    895                           title_check                     
    896                     raise IOError, msg
    897                    
    898        
    899        
    900         #create a dic of colum values, indexed by column title
    901         for line in reader:
    902             if len(line) <> title_count:
    903                 raise IOError #FIXME make this nicer
    904             for i, value in enumerate(line):
    905                 attribute_dic.setdefault(titles_stripped[i],[]).append(value)
    906            
    907         return attribute_dic, title_index_dic
    908868
    909869    def get_column(self, column_name, use_refind_polygon=False):
     
    1002962                     self._attribute_dic[title][row_i]
    1003963            writer.writerow(line)
     964
     965
     966def 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
    10041010
    10051011
Note: See TracChangeset for help on using the changeset viewer.