Changeset 7858 for trunk/anuga_core/source/anuga/file/csv_file.py
- Timestamp:
- Jun 18, 2010, 4:43:10 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/anuga_core/source/anuga/file/csv_file.py
r7854 r7858 11 11 """ 12 12 13 13 14 import csv 14 15 import numpy as num 16 import anuga.utilities.log as log 15 17 16 18 … … 168 170 if completed: 169 171 try: 170 file = str(kwargs['file_name'])172 file_name = str(kwargs['file_name']) 171 173 except: 172 raise 'kwargs must have file_name'174 raise Exception('kwargs must have file_name') 173 175 else: 174 176 # write temp file in output directory 175 177 try: 176 file = str(kwargs['output_dir']) + 'detail_temp.csv'178 file_name = str(kwargs['output_dir']) + 'detail_temp.csv' 177 179 except: 178 raise 'kwargs must have output_dir'180 raise Exception('kwargs must have output_dir') 179 181 180 182 # extracts the header info and the new line info … … 199 201 # try to open! 200 202 try: 201 fid = open(file , 'r')203 fid = open(file_name, 'r') 202 204 file_header = fid.readline() 203 205 fid.close() 204 206 if verbose: log.critical('read file header %s' % file_header) 205 except: 206 msg = 'try to create new file: %s' % file 207 if verbose: log.critical(msg) 207 except Exception: 208 msg = 'try to create new file: %s' % file_name 209 if verbose: 210 log.critical(msg) 208 211 #tries to open file, maybe directory is bad 209 212 try: 210 fid = open(file , 'w')213 fid = open(file_name, 'w') 211 214 fid.write(header) 212 215 fid.close() … … 218 221 # if header is same or this is a new file 219 222 if file_header == str(header): 220 fid = open(file , 'a')223 fid = open(file_name, 'a') 221 224 fid.write(line) 222 225 fid.close() … … 225 228 # if header is different and has completed will append info to 226 229 # end of details_temp.cvs file in output directory 227 file = str(kwargs['output_dir']) + 'detail_temp.csv'228 fid = open(file , 'a')230 file_name = str(kwargs['output_dir']) + 'detail_temp.csv' 231 fid = open(file_name, 'a') 229 232 fid.write(header) 230 233 fid.write(line) … … 244 247 245 248 def load_csv_as_building_polygons(file_name, 246 floor_height=3, 247 clipping_polygons=None): 249 floor_height=3): 248 250 """ 249 251 Convert CSV files of the form: … … 285 287 286 288 287 ##288 # @brief Convert CSV file into a dictionary of polygons and associated values.289 # @param filename The path to the file to read, value_name name for the 4th column290 289 def load_csv_as_polygons(file_name, 291 290 value_name='value', … … 338 337 339 338 msg = 'Did not find expected column header: northing' 340 assert 'northing' in X.keys(), northing339 assert 'northing' in X.keys(), msg 341 340 342 341 msg = 'Did not find expected column header: northing' … … 357 356 past_ids = {} 358 357 last_id = None 359 for i, id in enumerate(X['id']):358 for i, poly_id in enumerate(X['id']): 360 359 361 360 # Check for duplicate polygons 362 if id in past_ids:361 if poly_id in past_ids: 363 362 msg = 'Polygon %s was duplicated in line %d' % (id, i) 364 363 raise Exception, msg 365 364 366 if id not in polygons:365 if poly_id not in polygons: 367 366 # Start new polygon 368 polygons[ id] = []367 polygons[poly_id] = [] 369 368 if values is not None: 370 values[ id] = X[value_name][i]369 values[poly_id] = X[value_name][i] 371 370 372 371 # Keep track of previous polygon ids … … 385 384 386 385 if exclude is True: 387 excluded_polygons[ id]=True388 389 polygons[ id].append(point)386 excluded_polygons[poly_id]=True 387 388 polygons[poly_id].append(point) 390 389 391 390 # Check that value is the same across each polygon 392 391 msg = 'Values must be the same across each polygon.' 393 msg += 'I got %s in line %d but it should have been %s' % (X[value_name][i], i, values[id]) 394 assert values[id] == X[value_name][i], msg 395 396 last_id = id 392 msg += 'I got %s in line %d but it should have been %s' % \ 393 (X[value_name][i], i, values[poly_id]) 394 assert values[poly_id] == X[value_name][i], msg 395 396 last_id = poly_id 397 397 398 398 # Weed out polygons that were not wholly inside clipping polygons 399 for id in excluded_polygons:400 del polygons[ id]399 for poly_id in excluded_polygons: 400 del polygons[poly_id] 401 401 402 402 return polygons, values
Note: See TracChangeset
for help on using the changeset viewer.