質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Q&A

1回答

1403閲覧

Python3のエラーobject has no attribute

donarudo

総合スコア15

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

0グッド

0クリップ

投稿2018/10/26 03:59

編集2018/10/26 04:33

前提・実現したいこと

Python3で以下のプログラムを作成しましたがエラーがどうしても解決出来なくて困ってます。
改善方法をご提案していただきたく、質問させていただきます。

発生したエラーメッセージ

line 502, in output_dailyrun
out['pF'] = ops.rootwater.pF
AttributeError: 'RootWater' object has no attribute 'pF'

該当するソースコード

lang

1class RootWater(object): 2 def __init__(self): 3 """ 4 Create and initialize the RootWater object. 5 6 """ 7 self.wc = 0.0 # water content (mm) 8 self.vwc = 0.0 # water content (m3/m3) 9 self.critical = 0.0 # water content, below which plant water stress occurs (m3/m3) 10 self.sat = 0.0 # saturation point (m3/m3) 11 self.fc = 0.0 # field capacity (m3/m3) 12 self.pwp = 0.0 # permanent wilting point (m3/m3) 13 self.pF = 0.0 14#途中略してます 15 def __init__(self, fname_in): 16 """ 17 Create and initialize the SoilWater object. 18 SoilWaterオブジェクトを創り、初期化 19 20 # Arguments 21 fname_in (str): path and filename of the model initialization file 22 23 """ 24 Crop.__init__(self, fname_in) # initialize the parent class first 25 26 #初期設定一覧 27 d = self.ini 28 self.numintervals = d['numintervals'] # number of intervals for integration within a day(日毎の時間間隔の調整) 29 self.rootdepth = d['rootdepth'] # rooting depth (m) 30 self.has_watertable = d['has_watertable'] # has a water table / groundwater? 31 self.numlayers = d['numlayers'] # the number of soil layers 32 self.layers = [SoilLayer() for _ in range(self.numlayers)] # create the soil layers 33 34 # read in the properties for each soil layer. If there is a water table, the last soil 35 # layer is assumed to border the water table surface 36 dl = d['layers'] 37 for i, layer in enumerate(self.layers): 38 layer.thick = dl[i]['thick'] # thickness of each soil layer(各土層の厚さ) (m) 39 layer.vwc = dl[i]['vwc'] # vol. water content(体積含水率) in m3/m3 //ここで出力(重要!!!) 40 layer.wc = layer.vwc * layer.thick * 1000 # convert from m3/m3 to mm water(体積含水率を土壌水分量に変換) 41 tex = dl[i]['texture'] # clay, sand, and OM percentages (%) 42 layer.texture = Texture(tex['clay'], tex['sand'], tex['om']) 43 44 # initialize the soil layers (those that do not change with water content) 45 for i in range(self.numlayers): 46 prevlayer = self.layers[i - 1] if i > 0 else None 47 nextlayer = self.layers[i + 1] if i < self.numlayers - 1 else None 48 self.layers[i].initialize_layer(prevlayer, nextlayer) 49 50 # internal use: speedier calculations: proxy to store intermediate water flux values 51 # fluxes within a sub-interval daily time step 52 self.__pf = [{f: 0.0 for f in SoilLayer.flux_fields} for _ in range(self.numlayers)] 53 54 # cumulative fluxes at the end of a daily time step(1日の時間ステップの終了時における累積の流束) 55 self.cf = [{f: 0.0 for f in SoilLayer.flux_fields} for _ in range(self.numlayers)] 56 # root zone water characteristics(根域の特徴): 57 self.rootwater = RootWater() 58 self.update_rootwater() # amount of water in the root zone (mm and m3/m3) 59 # reduction to evaporation and transpiration due to water stress 60 self.waterstresses = self.reduce_et() 61 self.netrain = 0.0 # net rainfall (mm/day) 62 self.aet = AET(0.0, 0.0) # actual water loss by ET (mm/day) 63 64 def net_rainfall(self): 65 """ 66 Net rainfall (mm/day). 67 有効雨量 68 # Returns 69 float: net rainfall 70 71 """ 72 fraction = max(0.7295, 1 - 0.0541 * self.lai) # net rain (mm) reaching soil depends on lai 73 return fraction * self.dayrain 74 75 def rooting_depth(self): 76 """ 77 Increase in rooting depth (m). 78 根長の増加 79 # Returns 80 float: rooting depth 81 82 """ 83 # root growth rate = 2 mm/day but limited by water stress and total soil depth 84 newdepth = self.rootdepth + (2.0 / 1000) * self.waterstresses.crop 85 depthlimit = self.layers[-1].accthick 86 return min(newdepth, depthlimit) 87 88 def update_rootwater(self): 89 """ 90 Update the water content and water characteristics in the rooting zone. 91 92 The `RootZone` object will be set here. 93 94 !!! note 95 Critical soil water content for plant water stress taken as 0.6 or 60% 96 of the difference between soil saturation and permanent wilting point. 97 植物の水ストレスのための重要な土壌水分量は、0.6または60%は土壌水分量と永久しおれ店の間で取られます。 98 # Returns 99 None: 100 101 """ 102 wc = wcsat = wcfc = wcpwp = 0.0 103 # only consider those soil layers in where the roots reside: 104 for layer in self.layers: 105 diff = layer.thick - max(0.0, layer.accthick - self.rootdepth) 106 if diff <= 0: 107 break # found all the soil layers holding the roots, so exit loop 108 wc += layer.vwc * diff 109 wcsat += layer.swc.sat * diff 110 wcfc += layer.swc.fc * diff 111 wcpwp += layer.swc.pwp * diff 112 vwc = wc / self.rootdepth # convert from m water to m3/m3 113 114 115 #A,B,cはpFの実測値がないので仮の値を設定 追加10/9 116 117 # A=5.0 118 # B=0.8 119 # c=0.4 120 # thetas=0.8 121 122 vwcsat = wcsat / self.rootdepth 123 vwcfc = wcfc / self.rootdepth 124 vwcpwp = wcpwp / self.rootdepth 125 vwccr = vwcpwp + 0.6 * (vwcsat - vwcpwp) # critical point 126 # update the RootWater object with latest values: 127 self.rootwater.wc = wc * 1000 # mm 128 self.rootwater.vwc = vwc # all the rest in m3/m3 129 # self.rootwater.hoge = hoge #追加10/9 130 self.rootwater.critical = vwccr 131 self.rootwater.sat = vwcsat 132 self.rootwater.fc = vwcfc 133 self.rootwater.pwp = vwcpwp 134 135 def reduce_et(self): 136 """ 137 # 138 Reduction in evaporation and transpiration (0-1, 1=no stress, 0=max. stress). 139 蒸発と蒸散の減少 140 # Returns 141 AET: `namedtuple` containing reduction in E and T (`float`) 142 143 """ 144 #蒸発減少係数 145 rde = 1 / (1 + (3.6073 * (self.layers[0].vwc / self.layers[0].swc.sat)) ** -9.3172) 146 vwc = self.rootwater.vwc 147 vwcpwp = self.rootwater.pwp 148 vwccr = self.rootwater.critical 149 if vwc >= vwccr: 150 rdt = 1.0 151 elif vwcpwp < vwc < vwccr: 152 rdt = (vwc - vwcpwp) / (vwccr - vwcpwp) 153 else: 154 rdt = 0.01 155 return AET(rdt, rde) 156 157 def actual_et(self, petcrop, petsoil): 158 """ 159 Actual evaporation and transpiration (mm/day). 160 実際の蒸発と蒸散 161 162 # Arguments 163 petcrop (float): potential water loss from the crop (mm/day) 164 petsoil (float): potential water loss from the soil (mm/day) 165 166 # Returns 167 AET: `namedtuple` containing actual water loss from soil and crop (`float`) 168 169 """ 170 return AET(self.waterstresses.crop * petcrop*0.55, self.waterstresses.soil * petsoil*0.55)#収穫係数0.75をかける(9/14 0.55に変更) 171 172 def influx_from_watertable(self): 173 """ 174 Influx of water from the water table (m/day). 175 176 # Returns 177 float: groundwater influx 178 179 """ 180 last = self.layers[-1] # water table assumed just beneath the last soil layer(最後の土壌層直下の地下水面) 181 k = (last.ksat - last.k) / (math.log(last.ksat) - math.log(last.k)) 182 hm = (33 - (33 - last.swc.airentry)) / 10.0 # saturated water table 183 184 pF = math.log10(hm) 185 186 self.rootwater.pF = pF 187 188 hg = last.accthick 189 190 tothead = hm + hg 191 192 return k * (tothead - last.tothead) / (last.thick * 0.5)
#facade.py def output_dailyrun(self, initialize=False): out = self.out ops = self.model if not initialize: # 1. get results for daily properties: ag_growth = ops.parts.pinnae.growth + ops.parts.rachis.growth + ops.parts.trunk.growth veg_growth = ag_growth + ops.parts.roots.growth out['age'] = ops.treeage out['tmin'] = ops.daytmin out['tmax'] = ops.daytmax out['totalrad'], out['directrad'], out['diffuserad'] = ops.dayrad out['wind'] = ops.daywind out['rain'] = ops.dayrain out['netrain'] = ops.netrain out['ambientCO2'] = ops.co2ambient out['LAI'] = ops.lai out['pinnae'] = ops.parts.pinnae.weight out['rachis'] = ops.parts.rachis.weight out['trunk'] = ops.parts.trunk.weight out['roots'] = ops.parts.roots.weight out['male'] = ops.parts.maleflo.weight out['female'] = ops.parts.femaflo.weight out['bunches'] = ops.parts.bunches.weight out['flowersex'] = ops.flowersex out['VDM'] = ops.vdmwgt out['TDM'] = ops.tdmwgt out['assim_photosyn'] = ops.dayassim out['assim_maint'] = ops.assim4maint out['assim_growth'] = ops.assim4growth out['assim_gen'] = ops.assim4gen out['VDM_growth'] = ag_growth out['TDM_growth'] = veg_growth out['yield'] = ops.bunchyield out['trunk_hgt'] = ops.trunkhgt out['rootdepth'] = ops.rootdepth out['rootzone_VWC'] = ops.rootwater.vwc out['pF'] = ops.rootwater.pF

上記のソースコードのhmの値からpF値を求めたいと思ってますが、エラーが出て困っています。
ご指摘をお願いします。

補足情報

Python3.6.1
Mas OS

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

hayataka2049

2018/10/26 04:07

'RootWater' objectとは何なのでしょうか
donarudo

2018/10/26 04:13

修正しました。Rootwaterの部分が抜けてました。文字数の都合で所々略してありますので、また疑問が生じたら連絡下さい
hayataka2049

2018/10/26 04:19 編集

エラーのスタックトレースが全文ほしいです。関係なさそうなところは端折って構いません
tachikoma

2018/10/26 04:20

もしかして__init__をoverloadしようとした?
donarudo

2018/10/26 04:28

スタックトレース文と出力プログラムfacade.pyを追加しました。多分__init__を意図せずオーバーロードしてしまったかもしれません
hayataka2049

2018/10/26 04:29

opsに何を入れたのかわかるようにしてください
tachikoma

2018/10/26 04:34

Pythonはコンストラクタ等を含む関数・メソッドのオーバーロード出来ない(後に定義した同名の関数に上書きされる)ので、デフォルト引数を使って__init__(self, fname_in=None)として__init__(self)を消すところから始めたほうがいいと思うなぁ。
hayataka2049

2018/10/26 04:36

確認ですが、 def __init__(self):の__init__と def __init__(self, fname_in):はどちらもclass RootWater(object):に属しますか? であれば、tachikomaさんのアドバイス通りにする必要があります
hayataka2049

2018/10/26 04:37

ops = self.modelを示されてもself.modelの正体がわからないのであまり意味がないのですが、、、
mather

2018/10/26 05:12

tachikoma さんの助言どおりですね。もはや回答かと…。
guest

回答1

0

コメントにあるとおりです。

投稿2019/12/08 13:53

amahara_waya

総合スコア1029

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問