Changeset 1376


Ignore:
Timestamp:
May 12, 2005, 12:11:44 PM (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

    r1375 r1376  
    5050    Note: point att's have no titles - that's currently ok, since least
    5151    squares adds the point info to vertices, and they have titles
     52
     53    The format for a .tsh file is (FIXME update this)
     54   
     55    First line:  <# of vertices> <# of attributes>
     56    Following lines:  <vertex #> <x> <y> [attributes]
     57    One line:  <# of triangles>
     58    Following lines:  <triangle #> <vertex #>  <vertex #> <vertex #> <neigbouring triangle #> <neigbouring triangle #> <neigbouring triangle #> [attribute of region]
     59    One line:  <# of segments>
     60    Following lines:  <segment #> <vertex #>  <vertex #> [boundary tag]
    5261"""
    5362##FIXME (DSG-DSG) Is the dict format mentioned above a list of a numeric array?
     
    8493def import_mesh_file(ofile):
    8594    """
    86     reading triangulation and meshoutline info.
    87     import a file, ofile, with the format
    88    
    89     First line:  <# of vertices> <# of attributes>
    90     Following lines:  <vertex #> <x> <y> [attributes]
    91     One line:  <# of triangles>
    92     Following lines:  <triangle #> <vertex #>  <vertex #> <vertex #> <neigbouring triangle #> <neigbouring triangle #> <neigbouring triangle #> [attribute of region]
    93     One line:  <# of segments>
    94     Following lines:  <segment #> <vertex #>  <vertex #> [boundary tag]
     95    read a mesh file, either .tsh or .msh
     96   
    9597    Note: This might throw a can't load file error
    9698    """
     
    105107    return dict
    106108
     109
     110def export_mesh_file(ofile,mesh_dict):
     111    """
     112    write a file, ofile, with the format
     113   
     114    First line:  <# of vertices> <# of attributes>
     115    Following lines:  <vertex #> <x> <y> [attributes]
     116    One line:  <# of triangles>
     117    Following lines:  <triangle #> <vertex #>  <vertex #> <vertex #> <neigbouring triangle #> <neigbouring triangle #> <neigbouring triangle #> [attribute of region]
     118    One line:  <# of segments>
     119    Following lines:  <segment #> <vertex #>  <vertex #> [boundary tag]
     120    """
     121    #FIXME DSG-DSG: automate
     122    if not mesh_dict.has_key('points'):
     123        mesh_dict['points'] = []
     124    if not mesh_dict.has_key('point_attributes'):
     125        mesh_dict['point_attributes'] = []
     126    if not mesh_dict.has_key('outline_segments'):
     127        mesh_dict['outline_segments'] = []
     128    if not mesh_dict.has_key('outline_segment_tags'):
     129        mesh_dict['outline_segment_tags'] = []
     130    if not mesh_dict.has_key('holes'):
     131        mesh_dict['holes'] = []
     132    if not mesh_dict.has_key('regions'):
     133        mesh_dict['regions'] = []
     134       
     135    if not mesh_dict.has_key('region_tags'):
     136        mesh_dict['region_tags'] = []
     137    if not mesh_dict.has_key('region_max_areas'):
     138        mesh_dict['region_max_areas'] = []
     139    if not mesh_dict.has_key('vertices'):
     140        mesh_dict['vertices'] = []
     141    if not mesh_dict.has_key('vertex_attributes'):
     142        mesh_dict['vertex_attributes'] = []
     143    if not mesh_dict.has_key('vertex_attribute_titles'):
     144        mesh_dict['vertex_attribute_titles'] = []
     145    if not mesh_dict.has_key('segments'):
     146        mesh_dict['segments'] = []
     147    if not mesh_dict.has_key('segment_tags'):
     148        mesh_dict['segment_tags'] = []
     149    if not mesh_dict.has_key('triangles'):
     150        mesh_dict['triangles'] = []
     151    if not mesh_dict.has_key('triangle_tags'):
     152        mesh_dict['triangle_tags'] = []
     153    if not mesh_dict.has_key('triangle_neighbors'):
     154        mesh_dict['triangle_neighbors'] = []
     155    #print "DSG************"
     156    #print "mesh_dict",mesh_dict
     157    #print "DSG************"
     158   
     159    try:
     160        if (ofile[-4:] == ".tsh"):
     161            _write_tsh_file(ofile,mesh_dict)
     162        elif (ofile[-4:] == ".msh"):
     163            _write_msh_file(ofile, mesh_dict)
     164    except IOError, e:       
     165        msg = 'Could not write file %s ' %fileName
     166        raise IOError, msg
     167
     168
    107169def _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
     170    """
     171    Read the text file format for meshes
     172    """
     173    fd = open(ofile,'r')
     174    dict = _read_triangulation(fd)
     175    dict_mesh = _read_outline(fd)
     176    for element in dict_mesh.keys():
     177        dict[element] = dict_mesh[element]
     178    fd.close()
     179    return dict
    115180
    116181   
     
    374439def clean_line(line,delimiter):     
    375440    """Remove whitespace
    376     FIXME (Ole): ... or rather: Remove empty fields?
    377     I believe this could also be done by simply allowing delimiter to None.
    378     (DSG) - I just added stripping of whitespaces if the delimiter is other
    379     than splace.  simply allowing delimiter to None would not handle this
    380     situation.
    381441    """
    382442    #print ">%s" %line
     
    9501010
    9511011###
    952 LOADING XYA FILES
     1012IMPORT/EXPORT XYA FILES
    9531013###
     1014
     1015def export_points_file(ofile,point_dict):
     1016    """
     1017    write a points file, ofile, as a binary (.msh) or text (.tsh) file
     1018    """
     1019    #this was done for all keys in the mesh file.
     1020    #if not mesh_dict.has_key('points'):
     1021    #    mesh_dict['points'] = []
     1022    try:
     1023        if (ofile[-4:] == ".xya"):
     1024            _write_xya_file(ofile, point_dict)
     1025        elif (ofile[-4:] == ".pts"):
     1026            _write_pts_file(ofile, point_dict)
     1027    except IOError, e:       
     1028        msg = 'Could not write file %s ' %fileName
     1029        raise IOError, msg
     1030               
     1031def import_points_file(ofile, delimiter = None, verbose = False):
     1032    """
     1033    load an .xya or .pts file   
     1034    """
     1035   
     1036    if ofile[-4:]== ".xya":
     1037        try:
     1038            if delimiter == None:
     1039                try:
     1040                    fd = open(ofile)
     1041                    points_dict = _read_xya_file(fd, ',')
     1042                except SyntaxError:
     1043                    fd.close()
     1044                    fd = open(ofile)
     1045                    points_dict = _read_xya_file(fd, ' ')
     1046            else:
     1047                fd = open(ofile)
     1048                points_dict = _read_xya_file(fd, delimiter)
     1049            fd.close()
     1050            points_dict['geo_reference'] = None
     1051        except IOError, e:       
     1052            msg = 'Could not open file %s ' %ofile
     1053            raise IOError, msg
     1054       
     1055    elif ofile[-4:]== ".pts":
     1056        try:
     1057            points_dict = _read_pts_file(ofile, verbose)       
     1058        except IOError, e:       
     1059            msg = 'Could not open file %s ' %ofile
     1060            raise IOError, msg
     1061    else:     
     1062        msg = 'Extension %s is unknown' %ofile[-4:]
     1063        raise IOError, msg
     1064    return points_dict
    9541065
    9551066def extent_point_atts(point_atts):
     
    9781089   
    9791090def reduce_pts(infile, outfile, max_points, verbose = False):
    980     point_atts = read_pts(infile)
     1091    point_atts = _read_pts_file(infile)
    9811092    while point_atts['pointlist'].shape[0] > max_points:
    9821093        if verbose: print "point_atts['pointlist'].shape[0]"
    9831094        point_atts = half_pts(point_atts)
    984     write_pts(outfile, point_atts)
     1095    export_points_file(outfile, point_atts)
    9851096 
    9861097def produce_half_point_files(infile, max_points, delimiter, verbose = False):
    987     point_atts = read_pts(infile)
     1098    point_atts = _read_pts_file(infile)
    9881099    root, ext = splitext(infile)
    9891100    outfiles = []
     
    9941105        outfile = root + delimiter + str(point_atts['pointlist'].shape[0]) + ext
    9951106        outfiles.append(outfile)
    996         write_pts(outfile,
     1107        export_points_file(outfile,
    9971108                  point_atts)
    9981109    return outfiles
     
    10281139    return dic.keys(), point_attributes
    10291140
    1030 def read_pts(filename, verbose = False):
     1141def _read_pts_file(filename, verbose = False):
    10311142    """Read .pts NetCDF file
    10321143   
     
    10561167        fid.close()   
    10571168        msg = 'Expected keyword "points" but could not find it'
    1058         #FIXME (Ole): Also the original error e,
    1059         #could be added and raised again
    1060         # (DSG) The original error e is useful for fixing bugs
    1061         #  This is an issue with a file that has been caught,
    1062         # not a bug.
    1063         #(Ole): I see, this is true. Let's remove this conversation:-)
    10641169        raise IOError, msg
    10651170
     
    10851190    return point_atts
    10861191
    1087 def write_pts(filename, point_atts):
     1192def _write_pts_file(filename, point_atts):
    10881193    """Write .pts NetCDF file   
    10891194
     
    11231228       
    11241229    outfile.close()
    1125                  
    1126 def load_points_file(ofile, delimiter = ',', verbose = False):
    1127     """
    1128     load an .xya or .pts file   
    1129     """
    1130    
    1131     if ofile[-4:]== ".xya":
    1132         try:
    1133             fd = open(ofile)
    1134             points_dict = read_xya_file(fd, delimiter)
    1135             fd.close()
    1136         except IOError, e:       
    1137             msg = 'Could not open file %s ' %ofile
    1138             raise IOError, msg
    1139        
    1140     elif ofile[-4:]== ".pts":
    1141         try:
    1142             points_dict = read_pts(ofile, verbose)       
    1143         except IOError, e:       
    1144             msg = 'Could not open file %s ' %ofile
    1145             raise IOError, msg
    1146     else:     
    1147         msg = 'Extension %s is unknown' %ofile[-4:]
    1148         raise IOError, msg
    1149     return points_dict
    1150 
    1151 def read_xya_file(fd, delimiter):
     1230   
     1231def _read_xya_file(fd, delimiter):
    11521232    lines = fd.readlines()
    11531233    points = []
  • inundation/ga/storm_surge/pmesh/load_mesh/test_loadASCII.py

    r1375 r1376  
    434434        outfile.close()
    435435       
    436         dict = load_points_file(fileName)
     436        dict = import_points_file(fileName)
    437437        os.remove(fileName)
    438438        answer =  [[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]
     
    469469       
    470470        fileName = tempfile.mktemp(".pts")
    471         write_pts(fileName, dict)
    472         dict2 = load_points_file(fileName)
     471        export_points_file(fileName, dict)
     472        dict2 = import_points_file(fileName)
    473473        #print "fileName",fileName
    474474        os.remove(fileName)
     
    524524       
    525525        inFileName = tempfile.mktemp(".pts")
    526         write_pts(inFileName, dict)
     526        export_points_file(inFileName, dict)
    527527       
    528528        outFileName = tempfile.mktemp(".pts")
     
    531531        os.remove(inFileName)
    532532
    533         dict2 = load_points_file(outFileName)
     533        dict2 = import_points_file(outFileName)
    534534        os.remove(outFileName)
    535535        #print "dict2",dict2
     
    548548       
    549549        inFileName = tempfile.mktemp(".pts")
    550         write_pts(inFileName, dict)
     550        export_points_file(inFileName, dict)
    551551       
    552552        outFileName = tempfile.mktemp(".pts")
     
    558558        outFileName = root + delimiter + ext
    559559        #print "outFileName",outfiles 
    560         dict2 = load_points_file(outfiles[1])
     560        dict2 = import_points_file(outfiles[1])
    561561        for file in outfiles:
    562562            #print "del file",file
     
    577577        file.close()
    578578        #print fileName
    579         dict = load_points_file(fileName,delimiter = ',')
     579        dict = import_points_file(fileName,delimiter = ',')
    580580        os.remove(fileName)
    581581        assert allclose(dict['pointlist'], [[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]])
     
    595595        file.close()
    596596        #print fileName
    597         dict = load_points_file(fileName,delimiter = ' ')
     597        dict = import_points_file(fileName,delimiter = ' ')
    598598        os.remove(fileName)
    599599        assert allclose(dict['pointlist'], [[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]])
     
    614614        #print fileName
    615615        try:
    616             dict = load_points_file(fileName,delimiter = ' ')
     616            dict = import_points_file(fileName,delimiter = ' ')
    617617        except TitleAmountError:
    618618            pass
Note: See TracChangeset for help on using the changeset viewer.