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

Last change on this file since 25688 was 25688, checked in by jdquinn, 4 years ago

CHG: MATLAB -> Python translations in support of tests 2005 and 2006 (still have errors to work out here); clean up; various minor improvements

File size: 4.0 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.isclimatology = 0
18 self.mass_balance = np.nan
19 self.steps_per_step = 1
20 self.requested_outputs = []
21 self.averaging = 0
22
23 nargs = len(args)
24 if nargs == 0:
25 pass
26 else:
27 raise Exception('constructor not supported')
28 #}}}
29
30 def __repr__(self): # {{{
31 s = ' surface forcings parameters:\n'
32 s += '{}\n'.format(fielddisplay(self, 'mass_balance', 'surface mass balance [m/yr ice eq]'))
33 s += '{}\n'.format(fielddisplay(self, 'isclimatology', 'repeat all forcings when past last forcing time (default false)'))
34 s += '{}\n'.format(fielddisplay(self, 'steps_per_step', 'number of smb steps per time step'))
35 s += '{}\n'.format(fielddisplay(self, 'averaging', 'averaging methods from short to long steps'))
36 s += '\t\t{}\n'.format('0: Arithmetic (default)')
37 s += '\t\t{}\n'.format('1: Geometric')
38 s += '\t\t{}\n'.format('2: Harmonic')
39 s += '{}\n'.format(fielddisplay(self, 'requested_outputs', 'additional outputs requested'))
40 return s
41 #}}}
42
43 def extrude(self, md): # {{{
44 self.mass_balance = project3d(md, 'vector', self.mass_balance, 'type', 'node')
45 return self
46 #}}}
47
48 def defaultoutputs(self, md): # {{{
49 return []
50 #}}}
51
52 def initialize(self, md): # {{{
53 if np.all(np.isnan(self.mass_balance)):
54 self.mass_balance = np.zeros((md.mesh.numberofvertices))
55 print(" no SMBforcing.mass_balance specified: values set as zero")
56 return self
57 #}}}
58
59 def checkconsistency(self, md, solution, analyses): # {{{
60 if solution == 'TransientSolution' and not md.transient.issmb:
61 return
62 if 'MasstransportAnalysis' in analyses:
63 md = checkfield(md, 'fieldname', 'smb.mass_balance', 'timeseries', 1, 'NaN', 1, 'Inf', 1)
64 if 'BalancethicknessAnalysis' in analyses:
65 md = checkfield(md, 'fieldname', 'smb.mass_balance', 'size', [md.mesh.numberofvertices], 'NaN', 1, 'Inf', 1)
66 md = checkfield(md, 'fieldname', 'smb.steps_per_step', '>=', 1, 'numel', [1])
67 md = checkfield(md, 'fieldname', 'smb.averaging', 'numel', [1], 'values', [0, 1, 2])
68 md = checkfield(md, 'fieldname', 'masstransport.requested_outputs', 'stringrow', 1)
69 md = checkfield(md, 'fieldname', 'smb.isclimatology', 'values', [0, 1])
70 if self.isclimatology:
71 md = checkfield(md, 'fieldname', 'smb.mass_balance', 'size', [md.mesh.numberofvertices + 1], 'message', 'mass_balance must have md.mesh.numberofvertices + 1 rows in order to force a climatology')
72 return md
73 # }}}
74
75 def marshall(self, prefix, md, fid): # {{{
76 yts = md.constants.yts
77 WriteData(fid, prefix, 'name', 'md.smb.model', 'data', 1, 'format', 'Integer')
78 WriteData(fid, prefix, 'object', self, 'class', 'smb', 'fieldname', 'mass_balance', 'format', 'DoubleMat', 'mattype', 1, 'scale', 1. / yts, 'timeserieslength', md.mesh.numberofvertices + 1, 'yts', yts)
79 WriteData(fid, prefix, 'object', self, 'fieldname', 'steps_per_step', 'format', 'Integer')
80 WriteData(fid, prefix, 'object', self, 'fieldname', 'averaging', 'format', 'Integer')
81
82 #process requested outputs
83 outputs = self.requested_outputs
84 indices = [i for i, x in enumerate(outputs) if x == 'default']
85 if len(indices) > 0:
86 outputscopy = outputs[0:max(0, indices[0] - 1)] + self.defaultoutputs(md) + outputs[indices[0] + 1:]
87 outputs = outputscopy
88 WriteData(fid, prefix, 'data', outputs, 'name', 'md.smb.requested_outputs', 'format', 'StringArray')
89 WriteData(fid, prefix, 'object', self, 'class', 'smb', 'fieldname', 'isclimatology', 'format', 'Boolean')
90 # }}}
Note: See TracBrowser for help on using the repository browser.