source: issm/trunk-jpl/src/m/classes/SMBforcing.py@ 27453

Last change on this file since 27453 was 27453, checked in by jdquinn, 2 years ago

CHG: MATLAB > Python translations; cleanup

File size: 3.7 KB
Line 
1import numpy as np
2
3from checkfield import checkfield
4from fielddisplay import fielddisplay
5from project3d import project3d
6from WriteData import WriteData
7
8
9class SMBforcing(object):
10 """SMBFORCING class definition
11
12 Usage:
13 SMB = SMBforcing()
14 """
15
16 def __init__(self, *args): # {{{
17 self.mass_balance = np.nan
18 self.steps_per_step = 1
19 self.requested_outputs = []
20 self.averaging = 0
21
22 nargs = len(args)
23 if nargs == 0:
24 self.setdefaultparameters()
25 elif nargs == 1:
26 # TODO: Replace the following with constructor
27 self.setdefaultparameters()
28 else:
29 raise Exception('constructor not supported')
30 #}}}
31
32 def __repr__(self): # {{{
33 s = ' surface forcings parameters:\n'
34 s += '{}\n'.format(fielddisplay(self, 'mass_balance', 'surface mass balance [m/yr ice eq]'))
35 s += '{}\n'.format(fielddisplay(self, 'steps_per_step', 'number of smb steps per time step'))
36 s += '{}\n'.format(fielddisplay(self, 'averaging', 'averaging methods from short to long steps'))
37 s += '\t\t{}\n'.format('0: Arithmetic (default)')
38 s += '\t\t{}\n'.format('1: Geometric')
39 s += '\t\t{}\n'.format('2: Harmonic')
40 s += '{}\n'.format(fielddisplay(self, 'requested_outputs', 'additional outputs requested'))
41 return s
42 #}}}
43
44 def extrude(self, md): # {{{
45 self.mass_balance = project3d(md, 'vector', self.mass_balance, 'type', 'node')
46 return self
47 #}}}
48
49 def defaultoutputs(self, md): # {{{
50 return ['SmbMassBalance']
51 #}}}
52
53 def initialize(self, md): # {{{
54 if np.all(np.isnan(self.mass_balance)):
55 self.mass_balance = np.zeros((md.mesh.numberofvertices))
56 print(" no smb.mass_balance specified: values set as zero")
57 return self
58 #}}}
59
60 def checkconsistency(self, md, solution, analyses): # {{{
61 if solution == 'TransientSolution' and not md.transient.issmb:
62 return
63 if 'MasstransportAnalysis' in analyses:
64 md = checkfield(md, 'fieldname', 'smb.mass_balance', 'timeseries', 1, 'NaN', 1, 'Inf', 1)
65 if 'BalancethicknessAnalysis' in analyses:
66 md = checkfield(md, 'fieldname', 'smb.mass_balance', 'size', [md.mesh.numberofvertices], 'NaN', 1, 'Inf', 1)
67 md = checkfield(md, 'fieldname', 'smb.steps_per_step', '>=', 1, 'numel', [1])
68 md = checkfield(md, 'fieldname', 'smb.averaging', 'numel', [1], 'values', [0, 1, 2])
69 md = checkfield(md, 'fieldname', 'masstransport.requested_outputs', 'stringrow', 1)
70 return md
71 # }}}
72
73 def marshall(self, prefix, md, fid): # {{{
74 yts = md.constants.yts
75 WriteData(fid, prefix, 'name', 'md.smb.model', 'data', 1, 'format', 'Integer')
76 WriteData(fid, prefix, 'object', self, 'class', 'smb', 'fieldname', 'mass_balance', 'format', 'DoubleMat', 'mattype', 1, 'scale', 1 / yts, 'timeserieslength', md.mesh.numberofvertices + 1, 'yts', yts)
77 WriteData(fid, prefix, 'object', self, 'fieldname', 'steps_per_step', 'format', 'Integer')
78 WriteData(fid, prefix, 'object', self, 'fieldname', 'averaging', 'format', 'Integer')
79
80 #process requested outputs
81 outputs = self.requested_outputs
82 indices = [i for i, x in enumerate(outputs) if x == 'default']
83 if len(indices) > 0:
84 outputscopy = outputs[0:max(0, indices[0] - 1)] + self.defaultoutputs(md) + outputs[indices[0] + 1:]
85 outputs = outputscopy
86 WriteData(fid, prefix, 'data', outputs, 'name', 'md.smb.requested_outputs', 'format', 'StringArray')
87 # }}}
88
89 def setdefaultparameters(self): #{{{
90 self.requested_outputs = ['SmbMassBalance']
91 return self
92 # }}}
Note: See TracBrowser for help on using the repository browser.