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