Changeset 1375


Ignore:
Timestamp:
May 12, 2005, 9:50:04 AM (20 years ago)
Author:
duncan
Message:

refactoring/ changing method names

Location:
inundation/ga/storm_surge/pmesh/load_mesh
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • inundation/ga/storm_surge/pmesh/load_mesh/loadASCII.py

    r1219 r1375  
    6969#FIXME (DSG-DSG) this is used by least_squares -
    7070#                   is this the best function for it to use?
     71# (DSG-DSG) put an exception handler in import_triangulation/import_mesh_file
     72# Think about exceptions before doing this!
    7173def mesh_file_to_mesh_dictionary(fileName):
    7274    """Load a pmesh file.  Returning the mesh dictionary.
    7375    """
    7476    try:
    75         meshdic = import_triangulation(fileName)
     77        meshdic = import_mesh_file(fileName)
    7678    except IOError, e:       
    7779        msg = 'Could not open file %s ' %fileName
     
    7981    return meshdic
    8082
    81 def import_triangulation(ofile):
     83# This is used by pmesh
     84def import_mesh_file(ofile):
    8285    """
    8386    reading triangulation and meshoutline info.
     
    9497   
    9598    if ofile[-4:]== ".tsh":
    96         fd = open(ofile,'r')
    97         dict = read_triangulation(fd)
    98         dict_mesh = read_outline(fd)
    99         for element in dict_mesh.keys():
    100             dict[element] = dict_mesh[element]
    101         fd.close()
     99        dict = _read_tsh_file(ofile)
    102100    elif ofile[-4:]== ".msh":
    103         dict = read_msh_file(ofile)
     101        dict = _read_msh_file(ofile)
    104102    else:     
    105103        msg = 'Extension %s is unknown' %ofile[-4:]
     
    107105    return dict
    108106
    109 
    110 def read_triangulation(fd):
     107def _read_tsh_file(ofile):
     108        fd = open(ofile,'r')
     109        dict = _read_triangulation(fd)
     110        dict_mesh = _read_outline(fd)
     111        for element in dict_mesh.keys():
     112            dict[element] = dict_mesh[element]
     113        fd.close()
     114        return dict
     115
     116   
     117def _read_triangulation(fd):
    111118    """
    112119    Read the generated triangulation, NOT the outline.
     
    219226    return meshDict
    220227   
    221 def import_mesh(ofile):
    222     """
    223     import a file, ofile, with the format
    224    
    225     First line:  <# of vertices> <# of attributes>
    226     Following lines: <x> <y> [attributes]
    227     One line:  <# of segments>
    228     Following lines:  <vertex #>  <vertex #> [boundary tag]
    229     Note: This might throw a can't load file error
    230     """
    231     fd = open(ofile,'r')
    232     meshDict = read_outline(fd)
    233     fd.close()
    234     return meshDict
    235 
    236 def read_outline(fd):
     228def _read_outline(fd):
    237229    """
    238230    Note, if a file has no mesh info, it can still be read - the meshdic
     
    404396    return numbers
    405397
    406 #FIXME change this name
    407 def write_ASCII_triangulation(fd,
     398def _write_ASCII_triangulation(fd,
    408399                               gen_dict):
    409400    vertices = gen_dict['vertices']
     
    507498    Following lines:  <segment #> <vertex #>  <vertex #> [boundary tag]
    508499    """
    509     #FIXME (DSG-DSG) Do this to the triangulation keys as well
    510500    if not mesh_dict.has_key('points'):
    511501        mesh_dict['points'] = []
     
    525515    if not mesh_dict.has_key('region_max_areas'):
    526516        mesh_dict['region_max_areas'] = []
    527    
    528  
    529517    if not mesh_dict.has_key('vertices'):
    530518        mesh_dict['vertices'] = []
     
    549537    try:
    550538        if (ofile[-4:] == ".tsh"):
    551             fd = open(ofile,'w')
    552             write_ASCII_triangulation(fd,mesh_dict)
    553             write_ASCII_outline(fd,mesh_dict)   
    554             fd.close()
     539            _write_tsh_file(ofile,mesh_dict)
    555540        elif (ofile[-4:] == ".msh"):
    556             #print "loadascii.mesh mesh_dict",mesh_dict
    557             write_msh_file(ofile, mesh_dict)
     541            _write_msh_file(ofile, mesh_dict)
    558542    except IOError, e:       
    559543        msg = 'Could not write file %s ' %fileName
    560544        raise IOError, msg
    561545
    562 
    563 def write_ASCII_outline(fd,
     546def _write_tsh_file(ofile,mesh_dict):
     547            fd = open(ofile,'w')
     548            _write_ASCII_triangulation(fd,mesh_dict)
     549            _write_ASCII_outline(fd,mesh_dict)   
     550            fd.close()
     551
     552def _write_ASCII_outline(fd,
    564553                     dict):
    565554    points = dict['points']
     
    639628        dict['geo_reference'].write_ASCII(fd)
    640629
    641 def write_msh_file(filename, mesh):
     630def _write_msh_file(filename, mesh):
    642631    """Write .msh NetCDF file   
    643632
     
    820809
    821810   
    822 def read_msh_file(file_name):
     811def _read_msh_file(file_name):
    823812    """
    824813    Read in an msh file.
  • inundation/ga/storm_surge/pmesh/load_mesh/test_loadASCII.py

    r1183 r1375  
    11#!/usr/bin/env python
    22#
    3 
    4 #FIXME (Ole): I think we need a unit test of write_ASCII_triangulation
    5 #             in order to quash bug in merimbula data
    6 #  - to quash the bug need a raised error in pyvolution for reading xya files
    7 # when the title attribute information is not consistent with the data in the
    8 # file - DSG
    9 # also test_export_mesh_file tests write_ASCII_triangulation  - DSG
    103
    114import tempfile
     
    2114from loadASCII import *
    2215from coordinate_transforms.geo_reference import Geo_reference
     16import loadASCII
    2317
    2418class loadASCIITestCase(unittest.TestCase):
     
    170164
    171165   
    172     def test_import_mesh(self):
     166    def FUNCTION_REMOVEDtest_import_mesh(self):
    173167       
    174168        dict = self.dict
    175169        fileName = tempfile.mktemp(".txt")
    176170        fd = open(fileName,'w')
    177         write_ASCII_outline(fd,dict)
     171        loadASCII._write_ASCII_outline(fd,dict)
    178172        fd.close()
    179173        loaded_dict = import_mesh(fileName) #FIXME function names are wacky
     
    221215        fileName = tempfile.mktemp(".tsh")
    222216        export_mesh_file(fileName, meshDict)
    223         loadedDict = import_triangulation(fileName)
     217        loadedDict = import_mesh_file(fileName)
    224218       
    225219        #print "*(*( meshDict"
     
    262256    def test_read_write_msh_file(self):
    263257        dict = self.dict.copy()
    264         fileName = tempfile.mktemp(".txt")
    265         write_msh_file(fileName,dict)
    266         loaded_dict = read_msh_file(fileName)
     258        fileName = tempfile.mktemp(".msh")
     259        export_mesh_file(fileName,dict)
     260        loaded_dict = loadASCII._read_msh_file(fileName)
    267261        os.remove(fileName)
    268262        dict = self.dict
     
    276270    def test_read_write_msh_fileII(self):
    277271        dict = self.sparse_dict.copy()
    278         fileName = tempfile.mktemp(".txt")
    279         write_msh_file(fileName,dict)
    280         loaded_dict = read_msh_file(fileName)
     272        fileName = tempfile.mktemp(".msh")
     273        export_mesh_file(fileName,dict)
     274        loaded_dict = loadASCII._read_msh_file(fileName)
    281275        os.remove(fileName)
    282276        dict = self.sparse_dict
     
    290284    def test_read_write_msh_fileIII(self):
    291285        dict = self.blank_dict.copy()
    292         fileName = tempfile.mktemp(".txt")
    293         write_msh_file(fileName,dict)
    294         loaded_dict = read_msh_file(fileName)
     286        fileName = tempfile.mktemp(".msh")
     287        export_mesh_file(fileName,dict)
     288        loaded_dict = loadASCII._read_msh_file(fileName)
    295289        os.remove(fileName)
    296290        dict = self.blank_dict
     
    304298    def test_read_write_msh_file4(self):
    305299        dict = self.seg_dict.copy()
    306         fileName = tempfile.mktemp(".txt")
    307         write_msh_file(fileName,dict)
    308         loaded_dict = read_msh_file(fileName)
     300        fileName = tempfile.mktemp(".msh")
     301        export_mesh_file(fileName,dict)
     302        loaded_dict = loadASCII._read_msh_file(fileName)
    309303        os.remove(fileName)
    310304        dict = self.seg_dict
     
    318312    def test_read_write_msh_file5(self):
    319313        dict = self.triangle_tags_dict.copy()
    320         fileName = tempfile.mktemp(".txt")
    321         write_msh_file(fileName,dict)
    322         loaded_dict = read_msh_file(fileName)
     314        fileName = tempfile.mktemp(".msh")
     315        export_mesh_file(fileName,dict)
     316        loaded_dict = loadASCII._read_msh_file(fileName)
    323317        os.remove(fileName)
    324318        dict = self.seg_dict
     
    333327    def test_read_write_msh_file5(self):
    334328        dict = self.tri_dict.copy()
    335         fileName = tempfile.mktemp(".txt")
    336         write_msh_file(fileName,dict)
    337         loaded_dict = read_msh_file(fileName)
     329        fileName = tempfile.mktemp(".msh")
     330        export_mesh_file(fileName,dict)
     331        loaded_dict = loadASCII._read_msh_file(fileName)
    338332        os.remove(fileName)
    339333        dict = self.tri_dict
Note: See TracChangeset for help on using the changeset viewer.