Changeset 24894


Ignore:
Timestamp:
05/22/20 16:21:43 (5 years ago)
Author:
jdquinn
Message:

CHG: MATLAB -> Python translation; cleanup

Location:
issm/trunk-jpl/src/m
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • issm/trunk-jpl/src/m/classes/mesh3dsurface.m

    r24885 r24894  
    232232                                       
    233233                                        %increase counter:
    234                                         counter=counter+3;
     234                                        counter += 3;
    235235                                end
    236236                        elseif strcmpi(geometry,'polygon'),
     237                                % TODO: Refactor the following to reduce repeated code, or
     238                                %               leave as is because it is more readable?
    237239                                if isempty(index),
    238                                         counter=1;
    239240                                        for i=1:self.numberofelements,
    240241                                                el=self.elements(i,:);
    241242                                                contours(i).x=[self.long(el(1)) self.long(el(2)) self.long(el(3)) self.long(el(1))];
    242243                                                contours(i).y=[self.lat(el(1)) self.lat(el(2)) self.lat(el(3)) self.lat(el(1))];
     244                                                contours(i).Id = i;
    243245                                                contours(i).Geometry = 'Polygon';
    244                                                 contours(i).Id = i;
    245246                                        end
    246247                                else
    247                                         counter=1;
    248248                                        for i=1:length(index),
    249249                                                el=self.elements(index(i),:);
     
    255255                                end
    256256                        else
    257                                 error(sprintf('mesh3dsurface ''export'' error message: geometry %s not supported yet (should be ''point'' or ''line''',geometry));
     257                                error(sprintf('mesh3dsurface ''export'' error message: geometry %s not supported yet (should be ''point'' or ''line'' or ''polygon'')',geometry));
    258258                        end
    259259
  • issm/trunk-jpl/src/m/classes/mesh3dsurface.py

    r24213 r24894  
     1import numpy as np
     2
     3from checkfield import checkfield
     4from fielddisplay import fielddisplay
    15from MatlabFuncs import *
    26from model import *
    3 import numpy as np
    4 from fielddisplay import fielddisplay
    5 from checkfield import checkfield
    67from WriteData import WriteData
    78
     
    1213    #   Usage:
    1314    #      mesh3dsurface = mesh3dsurface();
    14     def __init__(self, *args):  # {{{
     15    def __init__(self, *args): # {{{
    1516        self.x = np.nan
    1617        self.y = np.nan
     
    5152    # }}}
    5253
    53     def __repr__(self):  # {{{
     54    def __repr__(self): # {{{
    5455        string = '   2D tria Mesh (horizontal):'
    5556
     
    8384    # }}}
    8485
    85     def loadobj(self):  # {{{
     86    def loadobj(self): # {{{
    8687        # This def is directly called by matlab when a model() selfect is
    8788        # loaded. Update old properties here
     
    101102    # }}}
    102103
    103     def setdefaultparameters(self):  # {{{
     104    def setdefaultparameters(self): # {{{
    104105        #the connectivity is the averaged number of nodes linked to a
    105106        #given node through an edge. This connectivity is used to initially
     
    111112    # }}}
    112113
    113     def checkconsistency(self, md, solution, analyses):  # {{{
     114    def checkconsistency(self, md, solution, analyses): # {{{
    114115        md = checkfield(md, 'fieldname', 'mesh.x', 'NaN', 1, 'Inf', 1, 'size', [md.mesh.numberofvertices])
    115116        md = checkfield(md, 'fieldname', 'mesh.y', 'NaN', 1, 'Inf', 1, 'size', [md.mesh.numberofvertices])
     
    133134    # }}}
    134135
    135     def marshall(self, prefix, md, fid):  # {{{
     136    def marshall(self, prefix, md, fid): # {{{
    136137        WriteData(fid, prefix, 'name', 'md.mesh.domain_type', 'data', 'Domain' + self.domaintype(), 'format', 'String')
    137138        WriteData(fid, prefix, 'name', 'md.mesh.domain_dimension', 'data', self.dimension(), 'format', 'Integer')
     
    151152    # }}}
    152153
    153     def domaintype(self):  # {{{
     154    def domaintype(self): # {{{
    154155        return '3Dsurface'
    155156    # }}}
    156157
    157     def dimension(self):  # {{{
     158    def dimension(self): # {{{
    158159        return 2
    159160    # }}}
    160161
    161     def elementtype(self):  # {{{
     162    def elementtype(self): # {{{
    162163        return 'Tria'
    163164    # }}}
    164165
    165     def processmesh(self, options):  # {{{
     166    def processmesh(self, options): # {{{
    166167        isplanet = 1
    167168        is2d = 0
     
    174175    # }}}
    175176
    176     def savemodeljs(self, fid, modelname):  # {{{
     177    def savemodeljs(self, fid, modelname): # {{{
    177178        fid.write('  #s.mesh = new mesh3dsurface()\n' % modelname)
    178179        writejs1Darray(fid, [modelname, '.mesh.x'], self.x)
     
    195196        writejs1Darray(fid, [modelname, '.mesh.extractedvertices'], self.extractedvertices)
    196197        writejs1Darray(fid, [modelname, '.mesh.extractedelements'], self.extractedelements)
    197 
    198     # }}}
     198    # }}}
     199
     200    def export(self, *args): # {{{
     201        options = pairoptions(*args)
     202
     203        filename    = options.getfieldvalue('filename')
     204        format      = options.getfieldvalue('format', 'shp')
     205        geometry    = options.getfieldvalue('geometry', 'line')
     206        index       = options.getfieldvalue('index', [])
     207        proj        = options.getfieldvalue('projection', '')
     208
     209        #prepare contours:
     210        contours = []
     211        if geometry == 'point':
     212            for i in range(len(self.numberofvertices)):
     213                contour             = OrderedStruct()
     214                contour.x           = self.long[i]
     215                contour.y           = self.lat[i]
     216                contour.id          = i
     217                contour.Geometry    = 'Point'
     218                contours[i]         = contour
     219        elif geometry == 'line':
     220            counter = 0
     221            for i in range(len(self.numberofelements)):
     222                el = self.elements[i]
     223
     224                #first line:
     225                contour                 = OrderedStruct()
     226                contour.x               = [self.long[el[0]] self.long[el[1]]]
     227                contour.y               = [self.lat[el[0]] self.lat[el[1]]]
     228                contour.Geometry        = 'Line'
     229                contours[counter]       = contour
     230
     231                #second line:
     232                contour                 = OrderedStruct()
     233                contour.x               = [self.long[el[1]] self.long[el[2]]]
     234                contour.y               = [self.lat[el[1]] self.lat[el[2]]]
     235                contour.Geometry        = 'Line'
     236                contours[counter + 1]   = contour
     237
     238                #second line:
     239                contour                 = OrderedStruct()
     240                contour.x               = [self.long[el[2]] self.long[el[0]]]
     241                contour.y               = [self.lat[el[2]] self.lat[el[0]]]
     242                contour.Geometry        = 'Line'
     243                contours[counter + 2]   = contour
     244
     245                #increase counter:
     246                counter += 3
     247        elif geometry == 'polygon':
     248            # TODO: Refactor the following to reduce repeated code, or
     249            #       leave as is because it is more readable?
     250            if index == []:
     251                for i in range(len(self.numberofelements)):
     252                    el = self.elements[i]
     253
     254                    contour             = OrderedStruct()
     255                    contour.x           = [self.long[el[0]] self.long[el[1]] self.long[el[2]]]
     256                    contour.y           = [self.lat[el[0]] self.lat[el[1]] self.lat[el[2]]]
     257                    contour.Id          = i
     258                    contour.Geometry    = 'Polygon'
     259                    contours[i]         = contour
     260            else:
     261                for i in range(len(index)):
     262                    el = self.elements[index[i]]
     263
     264                    contour             = OrderedStruct()
     265                    contour.x           = [self.long[el[0]] self.long[el[1]] self.long[el[2]]]
     266                    contour.y           = [self.lat[el[0]] self.lat[el[1]] self.lat[el[2]]]
     267                    contour.Id          = index[i]
     268                    contour.Geometry    = 'Polygon'
     269                    contours[i]         = contour
     270        else:
     271            raise RuntimeError("mesh3dsurface 'export' error message: geometry %s not supported yet (should be 'point' or 'line' or 'polygon')" % geometry)
     272
     273        #write file:
     274        if format == 'shp':
     275            shpwrite(contours, filename)
     276        elif format == 'exp':
     277            expwrite(contours, filename)
     278        else:
     279            raise RuntimeError("mesh3dsurface 'export' error message: file format %s not supported yet" % format)
     280
     281        #write projection file:
     282        if proj != '':
     283            proj2shpprj(filename, proj)
     284
     285        #write style file:
     286        applyqgisstyle(filename, 'mesh')
     287    # }}}
  • issm/trunk-jpl/src/m/qmu/helpers.py

    r24870 r24894  
    1717    attributes other than the array
    1818
    19 List - based and struct - based behaviors work normally, however they are referenced
     19List-based and struct-based behaviors work normally, however they are referenced
    2020    as if the other does not exist; len(x) corresponds only to
    2121    the list component of x, len(x.a) corresponds to x.a, x.__dict__
    22     corresponds only to the non - x - list attributes
     22    corresponds only to the non-x-list attributes
    2323
    2424Example uses:
     
    6464    '''
    6565A form of dictionary-like structure that maintains the
    66     ordering in which its fields / attributes and their
     66    ordering in which its fields/attributes and their
    6767    corresponding values were added.
    6868
    6969OrderedDict is a similar device, however this class
    70     can be used as an "ordered struct / class" giving
     70    can be used as an "ordered struct/class" giving
    7171    it much more flexibility in practice. It is
    7272    also easier to work with fixed valued keys in-code.
     
    128128
    129129        if len(args) % 2 != 0:
    130             raise RuntimeError('OrderedStruct input error: OrderedStruct(*args) call must have an even number of inputs, in key / value pairs')
     130            raise RuntimeError('OrderedStruct input error: OrderedStruct(*args) call must have an even number of inputs, in key/value pairs')
    131131
    132132        for a, b in zip(args[0::2], args[1::2]):
     
    186186    def __copy__(self):
    187187        # shallow copy, hard copies of trivial attributes,
    188         # references to structures like lists / OrderedDicts
     188        # references to structures like lists/OrderedDicts
    189189        # unless redefined as an entirely different structure
    190190        newInstance = type(self)()
     
    234234            return True
    235235
    236     # if anything in that array / matrix is not empty, the whole thing is not empty
     236    # if anything in that array/matrix is not empty, the whole thing is not empty
    237237        try:
    238238            x = np.concatenate(x)
Note: See TracChangeset for help on using the changeset viewer.