Changeset 4510
- Timestamp:
- May 29, 2007, 11:31:42 AM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
anuga_core/source/anuga/abstract_2d_finite_volumes/util.py
r4495 r4510 529 529 def start_screen_catcher(dir_name, myid='', numprocs='', extra_info='', 530 530 print_to_screen=False, verbose=False): 531 """ 532 Used to store screen output and errors to file, if run on multiple 533 processes eachprocessor will have its own output and error file. 534 535 extra_info - is used as a string that can identify outputs with another 536 string eg. '_other' 537 """ 538 539 dir_name = dir_name 540 if access(dir_name,W_OK) == 0: 541 if verbose: print 'Make directory %s' %dir_name 542 if verbose: print "myid", myid 543 mkdir (dir_name,0777) 544 if myid <>'': 545 myid = '_'+str(myid) 546 if numprocs <>'': 547 numprocs = '_'+str(numprocs) 548 if extra_info <>'': 549 extra_info = '_'+str(extra_info) 550 screen_output_name = dir_name + "screen_output%s%s%s.txt" %(myid,numprocs,extra_info) 551 screen_error_name = dir_name + "screen_error%s%s%s.txt" %(myid,numprocs,extra_info) 552 print screen_output_name 553 #used to catch screen output to file 554 sys.stdout = Screen_Catcher(screen_output_name) 555 sys.stderr = Screen_Catcher(screen_error_name) 556 557 class Screen_Catcher: 558 """this simply catches the screen output and stores it to file defined by 559 start_screen_catcher (above) 560 """ 561 562 def __init__(self, filename): 563 self.filename = filename 564 565 if exists(self.filename)is True: 566 print'Old existing file "%s" has been deleted' %(self.filename) 567 remove(self.filename) 568 569 def write(self, stuff): 570 fid = open(self.filename, 'a') 571 fid.write(stuff) 572 # if print_to_screen: print stuff 531 """Temporary Interface to new location""" 532 533 534 print 'start_screen_catcher has moved from util.py. ', 535 print 'Please use "from anuga.shallow_water.data_manager import start_screen_catcher"' 536 537 return shallow_water.data_manager.start_screen_catcher(dir_name, myid='', numprocs='', extra_info='', 538 print_to_screen=False, verbose=False) 573 539 574 540 def get_revision_number(): … … 1404 1370 # but any number of files. 1405 1371 def copy_code_files(dir_name, filename1, filename2): 1406 """Copies "filename1" and "filename2" to "dir_name". Very useful for 1407 information management 1408 filename1 and filename2 are both absolute pathnames 1409 """ 1410 1411 if access(dir_name,F_OK) == 0: 1412 print 'Make directory %s' %dir_name 1413 mkdir (dir_name,0777) 1414 copy(filename1, dir_name + sep + basename(filename1)) 1415 copy(filename2, dir_name + sep + basename(filename2)) 1416 # copy (__file__, project.output_run_time_dir + basename(__file__)) 1417 print 'Files %s and %s copied' %(filename1, filename2) 1372 """Temporary Interface to new location""" 1373 1374 1375 print 'copy_code_files has moved from util.py. ', 1376 print 'Please use "from anuga.shallow_water.data_manager import copy_code_files"' 1377 1378 return shallow_water.data_manager.copy_code_files(dir_name, filename1, filename2) 1418 1379 1419 1380 … … 1436 1397 1437 1398 def get_data_from_file(filename,separator_value = ','): 1438 """ 1439 Read in data information from file 1440 NOTE: wont deal with columns with different lenghts and there must be 1441 no blank lines at the end. 1442 """ 1443 from os import sep, getcwd, access, F_OK, mkdir 1444 from Numeric import array, resize,shape,Float 1445 import string 1446 fid = open(filename) 1447 lines = fid.readlines() 1448 1449 fid.close() 1450 1451 header_line = lines[0] 1452 header_fields = header_line.split(separator_value) 1453 1454 #array to store data, number in there is to allow float... 1455 #i'm sure there is a better way! 1456 data=array([],typecode=Float) 1457 data=resize(data,((len(lines)-1),len(header_fields))) 1458 # print 'number of fields',range(len(header_fields)) 1459 # print 'number of lines',len(lines), shape(data) 1460 # print'data',data[1,1],header_line 1461 1462 array_number = 0 1463 line_number = 1 1464 while line_number < (len(lines)): 1465 for i in range(len(header_fields)): 1466 #this get line below the header, explaining the +1 1467 #and also the line_number can be used as the array index 1468 fields = lines[line_number].split(separator_value) 1469 #assign to array 1470 data[array_number,i] = float(fields[i]) 1471 1472 line_number = line_number +1 1473 array_number = array_number +1 1474 1475 return header_fields, data 1399 """Temporary Interface to new location""" 1400 1401 print 'get_data_from_file has moved from util.py. ', 1402 print 'Please use "from anuga.shallow_water.data_manager import get_data_from_file"' 1403 1404 return shallow_water.data_manager.get_data_from_file(filename,separator_value = ',') 1476 1405 1477 1406 def store_parameters(verbose=False,**kwargs): 1478 """ 1479 Must have a file_name keyword arg, this is what is writing to. 1480 might be a better way to do this using CSV module Writer and writeDict 1481 1482 writes file to "output_dir" unless "completed" is in kwargs, then it writes to 1483 "file_name" kwargs 1484 Returns a object which is a subset of the original 1485 and the data points and attributes in this new object refer to 1486 the indices provided 1487 1488 Input 1489 indices- a list of integers that represent the new object 1490 Output 1491 New geospatial data object representing points specified by 1492 the indices 1493 """ 1494 import types 1495 import os 1496 1497 # Check that kwargs is a dictionary 1498 if type(kwargs) != types.DictType: 1499 raise TypeError 1500 1501 try: 1502 kwargs['completed'] 1503 completed=True 1504 except: 1505 completed=False 1506 1507 # assert that a file_name exists 1508 1509 #get file name and removes from dict 1510 if completed: 1511 try: 1512 file = str(kwargs.pop('file_name')) 1513 except: 1514 raise 'kwargs must have file_name' 1515 else: 1516 try: 1517 file = str(kwargs.pop('output_dir'))+'detail_temp.csv' 1518 except: 1519 raise 'kwargs must have output_dir' 1520 1521 1522 # print kwargs 1523 #extracts the header info and the new line info 1524 line='' 1525 header='' 1526 count=0 1527 keys = kwargs.keys() 1528 # print 'keys',keys 1529 keys.sort() 1530 # print 'keys',keys 1531 1532 # for k in kwargs.keys(): 1533 #used the sorted keys to create the header and line data 1534 for k in keys: 1535 print "%s = %s" %(k, kwargs[k]) 1536 header = header+str(k) 1537 line = line+str(kwargs[k]) 1538 count+=1 1539 if count <len(kwargs): 1540 header = header+',' 1541 line = line+',' 1542 1543 1544 # checks the header info, if the same, then write, if not create a new file 1545 #try to open! 1546 # print'file name',file 1547 try: 1548 fid = open(file,"r") 1549 file_header=fid.readline() 1550 fid.close() 1551 if verbose: print 'read file header %s' %file_header 1552 1553 except: 1554 msg = 'try to create new file',file 1555 if verbose: print msg 1556 #tries to open file, maybe directory is bad 1557 try: 1558 fid = open(file,"w") 1559 fid.writelines(header+'\n') 1560 fid.close() 1561 file_header=header 1562 except: 1563 msg = 'cannot create new file',file 1564 raise msg 1565 1566 #if header is same or this is a new file 1567 if file_header.strip('\n')==header: 1568 fid=open(file,"a") 1569 #write new line 1570 fid.writelines(line+'\n') 1571 fid.close() 1572 else: 1573 #backup plan, if header is different and has completed will append info to 1574 #end of details_temp.cvs file in output directory 1575 file = str(kwargs['output_dir'])+'detail_temp.csv' 1576 fid=open(file,"a") 1577 fid.writelines(header+'\n') 1578 fid.writelines(line+'\n') 1579 fid.close() 1580 msg = 'file header does not match input info, the input variables have changed, change file name' 1581 raise msg 1582 1407 """Temporary Interface to new location""" 1408 1409 print 'store_parameters has moved from util.py. ', 1410 print 'Please use "from anuga.shallow_water.data_manager import store_parameters"' 1411 1412 return shallow_water.data_manager.get_data_from_file(filename,separator_value = ',') 1583 1413 1584 1414 def remove_lone_verts(verts, triangles, number_of_full_nodes=None):
Note: See TracChangeset
for help on using the changeset viewer.