| 1 | from project3d import project3d
|
|---|
| 2 | from fielddisplay import fielddisplay
|
|---|
| 3 | from pairoptions import pairoptions
|
|---|
| 4 | from checkfield import checkfield
|
|---|
| 5 | from WriteData import WriteData
|
|---|
| 6 | from ContourToMesh import ContourToMesh
|
|---|
| 7 | import numpy as np
|
|---|
| 8 | import os
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 | class regionaloutput(object):
|
|---|
| 12 | """
|
|---|
| 13 | REGIONALOUTPUT class definition
|
|---|
| 14 |
|
|---|
| 15 | Usage:
|
|---|
| 16 | regionaloutput = regionaloutput()
|
|---|
| 17 | regionaloutput = regionaloutput('name', 'Volume1', 'definitionstring', 'Outputdefinition1', 'outputnamestring', 'IceVolume', 'mask', mask)
|
|---|
| 18 | regionaloutput = regionaloutput('name', 'Volume1', 'definitionstring', 'Outputdefinition1', 'outputnamestring', 'IceVolume', 'maskexpstring', 'Exp/Mask.exp', 'model', md)
|
|---|
| 19 |
|
|---|
| 20 | where mask is a vectorial field of size md.mesh.numberofvertices, 1 : where vertices with values > 1 are to be included in the calculated region.
|
|---|
| 21 | Alternatively, the user can pass in an Argus file and model object instead of a mask, and mask will be calculated for the user
|
|---|
| 22 | """
|
|---|
| 23 |
|
|---|
| 24 | def __init__(self, *args): # {{{
|
|---|
| 25 |
|
|---|
| 26 | self.name = ''
|
|---|
| 27 | self.model= ''
|
|---|
| 28 | self.definitionstring = ''
|
|---|
| 29 | self.outputnamestring = ''
|
|---|
| 30 | self.mask = np.nan
|
|---|
| 31 | self.maskexpstring = ''
|
|---|
| 32 |
|
|---|
| 33 | #set defaults
|
|---|
| 34 | self.setdefaultparameters()
|
|---|
| 35 |
|
|---|
| 36 | #use provided options to change fields
|
|---|
| 37 | options = pairoptions(*args)
|
|---|
| 38 |
|
|---|
| 39 | #OK get other fields
|
|---|
| 40 | self = options.AssignObjectFields(self)
|
|---|
| 41 |
|
|---|
| 42 | #get name
|
|---|
| 43 | if options.getfieldvalue('model', 0):
|
|---|
| 44 | if options.getfieldvalue('maskexpstring', 0):
|
|---|
| 45 | modelname = options.getfieldvalue('model')
|
|---|
| 46 | self.maskexpstring = options.getfieldvalue('maskexpstring')
|
|---|
| 47 | self.setmaskfromexp(modelname)
|
|---|
| 48 |
|
|---|
| 49 | # if (len(self.mask) <= 1 & np.any(np.isnan(self.mask))):
|
|---|
| 50 | # raise IOError('regionaloutput error message: ''mask'' field or ''maskexpstring'' and ''model'' fields should be defined!')
|
|---|
| 51 |
|
|---|
| 52 | #}}}
|
|---|
| 53 |
|
|---|
| 54 | def __repr__(self): # {{{
|
|---|
| 55 | string = " Regionaloutput:"
|
|---|
| 56 | string = "%s\n%s" % (string, fielddisplay(self, 'name', 'identifier for this regional response'))
|
|---|
| 57 | string = "%s\n%s" % (string, fielddisplay(self, 'definitionstring', 'string that identifies this output definition uniquely, from Outputdefinition[1 - 100]'))
|
|---|
| 58 | string = "%s\n%s" % (string, fielddisplay(self, 'outputnamestring', 'string that identifies the type of output you want, eg. IceVolume, TotalSmb, GroudedArea'))
|
|---|
| 59 | string = "%s\n%s" % (string, fielddisplay(self, 'mask', 'mask vectorial field which identifies the region of interest (value > 0 will be included)'))
|
|---|
| 60 | string = "%s\n%s" % (string, fielddisplay(self, 'maskexpstring', 'name of Argus file that can be passed in to define the regional mask'))
|
|---|
| 61 | return string
|
|---|
| 62 | #}}}
|
|---|
| 63 |
|
|---|
| 64 | def extrude(self, md): # {{{
|
|---|
| 65 | self.mask = project3d(md, 'vector', self.mask, 'type', 'node')
|
|---|
| 66 | return self
|
|---|
| 67 | #}}}
|
|---|
| 68 |
|
|---|
| 69 | def setdefaultparameters(self): # {{{
|
|---|
| 70 | return self
|
|---|
| 71 | #}}}
|
|---|
| 72 |
|
|---|
| 73 | def setmaskfromexp(self, md): # {{{
|
|---|
| 74 | if len(self.maskexpstring) > 0:
|
|---|
| 75 | self.mask = ContourToMesh(md.mesh.elements, md.mesh.x, md.mesh.y, self.maskexpstring, 'node', 1)
|
|---|
| 76 |
|
|---|
| 77 | return self
|
|---|
| 78 | # }}}
|
|---|
| 79 |
|
|---|
| 80 | def checkconsistency(self, md, solution, analyses): # {{{
|
|---|
| 81 |
|
|---|
| 82 | if not isinstance(self.name, str):
|
|---|
| 83 | raise RuntimeError("regionaloutput error message: 'name' field should be a string!")
|
|---|
| 84 |
|
|---|
| 85 | if not isinstance(self.outputnamestring, str):
|
|---|
| 86 | raise RuntimeError("regionaloutput error message: 'outputnamestring' field should be a string!")
|
|---|
| 87 |
|
|---|
| 88 | if len(self.maskexpstring) > 0:
|
|---|
| 89 | if not os.path.isfile(self.maskexpstring):
|
|---|
| 90 | raise RuntimeError("regionaloutput error message: file name for mask exp does not point to a legitimate file on disk!")
|
|---|
| 91 | else:
|
|---|
| 92 | self.setmaskfromexp(md)
|
|---|
| 93 |
|
|---|
| 94 | OutputdefinitionStringArray = []
|
|---|
| 95 | for i in range(1, 100):
|
|---|
| 96 | x = 'Outputdefinition' + str(i)
|
|---|
| 97 | OutputdefinitionStringArray.append(x)
|
|---|
| 98 |
|
|---|
| 99 | md = checkfield(md, 'field', self.definitionstring, 'values', OutputdefinitionStringArray)
|
|---|
| 100 | md = checkfield(md, 'field', self.mask, 'size', [md.mesh.numberofvertices], 'NaN', 1, 'Inf', 1)
|
|---|
| 101 | return md
|
|---|
| 102 | # }}}
|
|---|
| 103 | def marshall(self, prefix, md, fid): # {{{
|
|---|
| 104 |
|
|---|
| 105 | #before marshalling, make sure mask is set:
|
|---|
| 106 | self.setmaskfromexp(md)
|
|---|
| 107 |
|
|---|
| 108 | #ok, marshall strings and mask:
|
|---|
| 109 | WriteData(fid, prefix, 'data', self.name, 'name', 'md.regionaloutput.name', 'format', 'String')
|
|---|
| 110 | WriteData(fid, prefix, 'data', self.definitionstring, 'name', 'md.regionaloutput.definitionstring', 'format', 'String')
|
|---|
| 111 | WriteData(fid, prefix, 'data', self.outputnamestring, 'name', 'md.regionaloutput.outputnamestring', 'format', 'String')
|
|---|
| 112 | WriteData(fid, prefix, 'data', self.mask, 'name', 'md.regionaloutput.mask', 'format', 'DoubleMat', 'mattype', 1)
|
|---|
| 113 |
|
|---|
| 114 | # }}}
|
|---|