public.py 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979
  1. import pandas as pd
  2. import numpy as np
  3. # 这里写所有表格的判断规则 按顺序每个表格一个函数 方便后续修改
  4. pd.set_option('display.max_columns', 1000)
  5. pd.set_option('display.width', 1000)
  6. pd.set_option('display.max_colwidth', 1000)
  7. # 表1 土壤容重机械组成数据 为了方便做具体修改
  8. def soil_bulk_density(arr): #arr为计算过的数组
  9. # (1)土壤容重不在[0.8, 1.6]范围以内的,存疑
  10. shenHeList=[] # 定义一个数组存放容重存疑的数据
  11. shenHeTarget = [] # 存放每条数据 有问题的指标
  12. try:
  13. for i in arr['土壤容重平均值(g/cm3)(计算)']:
  14. if i > 1.6 or i < 0.8:
  15. shenHeList.append('土壤容重:超阈值。')
  16. shenHeTarget.append('土壤容重平均值。')
  17. else:
  18. shenHeList.append('')
  19. shenHeTarget.append('')
  20. except Exception as err:
  21. print('土壤容重判断出错!请检查soil_bulk_density中判断土壤容重内容',err)
  22. # (2)土壤利用类型→耕地、园地→相对极差>15 %,存疑;土壤利用类型→林地、草地→相对极差>20 %,存疑
  23. tRTypeList = [] # 定义一个数组存放土壤利用类型存疑的数据
  24. tRTypeTarget= [] # 定义一个数组存放土壤利用类型存疑的数据指标名称
  25. try:
  26. for i in arr['相对极差(%)']:
  27. if i > 15 and arr.loc[arr['相对极差(%)'] == i, '土地利用类型'].iloc[0] == '耕地园地':
  28. tRTypeList.append('存疑:耕地园地相对极差>15%。')
  29. tRTypeTarget.append('耕地园地极差。')
  30. elif i > 20 and arr.loc[arr['相对极差(%)'] == i, '土地利用类型'].iloc[0] == '林地草地':
  31. tRTypeList.append('存疑:林地草地相对极差>20%。')
  32. tRTypeTarget.append('林地草地极差。')
  33. else:
  34. tRTypeList.append('')
  35. tRTypeTarget.append('')
  36. except Exception as err:
  37. print('相对极差判断、土壤利用类型判断出错!请检查soil_bulk_density中判断相对极差判断、土壤利用类型内容',err)
  38. # (3)加和不在[99.98, 100.02]范围内的,存疑
  39. plusShenHeList = [] # 定义一个数组存放加和存疑的数据
  40. plusShenHeTarget = [] # 保存土壤颗粒加和存疑的指标
  41. try:
  42. for i in arr['加和%']:
  43. if float(i) > 100.02 or float(i) < 99.98:
  44. plusShenHeList.append('土壤颗粒加和:超阈值。')
  45. plusShenHeTarget.append('土壤颗粒含量加和。')
  46. else:
  47. plusShenHeList.append('')
  48. plusShenHeTarget.append('')
  49. except Exception as err:
  50. print('颗粒含量加和判断出错!请检查soil_bulk_density中判断颗粒含量加和内容',err)
  51. # 根据国际土壤质地类型三角形编程实现对质地的分类→判断质地分类和质地名称是否正确
  52. # 判断土壤类型逻辑:
  53. soilList = [] # 定义一个数组存放土地类型的数据
  54. soilContent = []
  55. soilContentTarget = [] # 存放土壤质地异常的指标名称
  56. xSLErr = [] # 存放ph>7 洗失量为空的异常数据
  57. xSLTarget = [] # 存放异常数据 指标名称
  58. try:
  59. # 按行循环读取所有数据
  60. for index, row in arr.iterrows():
  61. # 1.将0.02-0.2,0.2-2两列加起来
  62. plusSoil = row['0.2-0.02mm颗粒含量%'] + row['2-0.2mm颗粒含量%']
  63. small_002_list = row['0.02-0.002mm颗粒含量%']
  64. small_0002_list = row['0.002mm以下颗粒含量%']
  65. if np.isnan(plusSoil) or np.isnan(small_002_list) or np.isnan(small_0002_list):
  66. soilList.append('')
  67. # 具体判断 这里为了方便看 减少了嵌套逻辑
  68. elif small_0002_list >=65 and small_0002_list <100: # 2. <0.002含量 65-100 ->重黏土
  69. soilList.append('重黏土')
  70. elif small_0002_list >= 45 and small_0002_list <65: # 3.<0.002含量 45-65 ->黏土
  71. soilList.append('黏土')
  72. elif small_0002_list >= 25 and small_0002_list <45 and small_002_list >= 45 and small_002_list <75: # 4. <0.002含量 25-45 and 0.002-0.02含量 45-75 -> 粉(砂)质黏土
  73. soilList.append('粉(砂)质黏土')
  74. elif small_0002_list >= 25 and small_0002_list<45 and small_002_list>=0 and small_002_list<45 and plusSoil>=10 and plusSoil <55: # 5. <0.002含量 25-45 and 0.002-0.02含量 0-45 and 0.02-2含量 10-55-> 壤质黏土
  75. soilList.append('壤质黏土')
  76. elif small_0002_list >= 25 and small_0002_list<45 and small_002_list>=0 and small_002_list<20 and plusSoil>=55 and plusSoil <75:# 6. <0.002含量 25-45 and 0.002-0.02含量 0-20 and 0.02-2含量 55-75-> 砂质黏土
  77. soilList.append('砂质黏土')
  78. elif small_0002_list >= 15 and small_0002_list<25 and small_002_list>=45 and small_002_list<85: # 7.<0.002含量 15-25 and 0.002-0.02含量 45-85 -> 粉(砂)质黏壤土
  79. soilList.append('粉(砂)质黏壤土')
  80. elif small_0002_list >= 15 and small_0002_list<25 and small_002_list>=20 and small_002_list<45 and plusSoil>=30 and plusSoil <55:# 8.<0.002含量 15-25 and 0.002-0.02含量 20-45 and 0.02-2含量 30-55-> 黏壤土
  81. soilList.append('黏壤土')
  82. elif small_0002_list >= 15 and small_0002_list<25 and small_002_list>=0 and small_002_list<30 and plusSoil>=55 and plusSoil <85:# 9.<0.002含量 15-25 and 0.002-0.02含量 0-30 and 0.02-2含量 55-85-> 砂质黏壤土
  83. soilList.append('砂质黏壤土')
  84. elif small_0002_list >= 0 and small_0002_list<15 and small_002_list>=45 and small_002_list<100:#10.<0.002含量 0-15 and 0.002-0.02含量 45-100 ->粉(砂)质壤土
  85. soilList.append('粉(砂)质壤土')
  86. elif small_0002_list >= 0 and small_0002_list<15 and small_002_list>=30 and small_002_list<45 and plusSoil>=40 and plusSoil <55: # 11.<0.002含量 0-15 and 0.002-0.02含量 30-45 and 0.02-2含量 40-55-> 壤土
  87. soilList.append('壤土')
  88. elif small_0002_list >= 0 and small_0002_list<15 and small_002_list>=0 and small_002_list<45 and plusSoil>=55 and plusSoil <85: # 12.<0.002含量 0-15 and 0.002-0.02含量 0-45 and 0.02-2含量 55-85-> 砂质壤土
  89. soilList.append('砂质壤土')
  90. elif small_0002_list >= 0 and small_0002_list<15 and small_002_list>=0 and small_002_list<15 and plusSoil>=85 and plusSoil <100: # 13.<0.002含量 0-15 and 0.002-0.02含量 0-15 and 0.02-2含量 85-100-> 砂土及壤质砂土
  91. soilList.append('砂土及壤质砂土')
  92. else:
  93. soilList.append('') # 除所有情况外 还有空值
  94. # 比较和原有数据是否一致
  95. arr['土壤类型(判断)'] = soilList
  96. for index, row in arr.iterrows():
  97. if (row['土壤类型(判断)'] != row['土壤质地']) and (not pd.isna(row['土壤质地'])):
  98. soilContent.append('存疑:土壤质地不一致')
  99. soilContentTarget.append('土壤质地。')
  100. else:
  101. soilContent.append('')
  102. soilContentTarget.append('')
  103. # 如果pH>7,则洗失量数据不能为空;
  104. if (not pd.isna(row['pH']) and row['pH'] > 7 and pd.isna(row['洗失量(吸管法需填)%'])):
  105. xSLErr.append('洗失量:ph>7但洗失量未检测。')
  106. xSLTarget.append('洗失量。')
  107. else:
  108. xSLErr.append('')
  109. xSLTarget.append('')
  110. except Exception as err:
  111. print('土壤类型判断出错!请检查soil_bulk_density中判断土壤类型内容', err)
  112. # 把存疑数据组合并返回
  113. # print('shenHeList--',shenHeList,len(shenHeList))
  114. # print('plusShenHeList--', plusShenHeList, len(plusShenHeList))
  115. # print('tRTypeList--', tRTypeList, len(tRTypeList))
  116. # print('soilContent--', soilContent, len(soilContent))
  117. # print('soilList--', soilList, len(soilList))
  118. pdData = pd.DataFrame({
  119. '审核结果': pd.Series(shenHeList) + pd.Series(tRTypeList) + pd.Series(plusShenHeList) + pd.Series(soilContent) + pd.Series(xSLErr),
  120. '土壤类型(判断)': soilList,
  121. '异常指标': pd.Series(shenHeTarget) + pd.Series(tRTypeTarget) + pd.Series(plusShenHeTarget) + pd.Series(soilContentTarget) + pd.Series(xSLTarget),
  122. })
  123. return pdData
  124. # 这是一个判断范围的函数 如果需要修改范围 修改start end值就行
  125. def is_not_in_range(value):
  126. return value <30 or value > 90
  127. # 表3 水稳性大团聚体规则判断函数
  128. def water_stable(arr):
  129. # (1)不在[30, 90]范围以内的,存疑
  130. shenHeList = [] # 定义一个数组存放团聚体存疑的数据
  131. shenHeTar = [] # 存放水稳异常指标
  132. # (2)总和超过90,存疑;耕地和园地>80,提示关注;林地和草地>90,提示关注
  133. plusList = []
  134. plusTar = [] # 水稳总和异常指标名称
  135. soilType = []
  136. # (3)>5mm指标占比超过10 %,存疑,应回溯
  137. rateList = []
  138. rateTar = [] # >5mm占比异常指标
  139. try:
  140. for index, row in arr.iterrows():
  141. # 规则1判断 先判断值是否存在
  142. # if (not pd.isna(row['>5mm%']) and is_not_in_range(row['>5mm%'])) or (
  143. # not pd.isna(row['3-5mm%']) and is_not_in_range(row['3-5mm%'])) or (
  144. # not pd.isna(row['2-3mm%']) and is_not_in_range(row['2-3mm%'])) or (
  145. # not pd.isna(row['1-2mm%']) and is_not_in_range(row['1-2mm%'])) or (
  146. # not pd.isna(row['0.5-1mm%']) and is_not_in_range(row['0.5-1mm%'])) or (
  147. # not pd.isna(row['0.25-0.5mm%']) and is_not_in_range(row['0.25-0.5mm%'])):
  148. # shenHeList.append('存疑:团聚体百分比不在范围以内。')
  149. # shenHeTar.append('水稳分项指标。')
  150. # else:
  151. # shenHeList.append('')
  152. # shenHeTar.append('')
  153. # 规则2判断
  154. if row['总和(%)'] > 60 or row['总和(%)'] < 1:
  155. plusList.append('水稳性大团聚体:总和超阈值。')
  156. plusTar.append('水稳总和。')
  157. else:
  158. plusList.append('')
  159. plusTar.append('')
  160. if (row['土地利用类型'] == '耕地园地' and row['总和(%)'] > 80) or (row['土地利用类型'] == '林地草地' and row['总和(%)'] > 90):
  161. soilType.append('关注:耕地园地团聚体总和大于80或林地草地团聚体总和大于90。')
  162. else:
  163. soilType.append('')
  164. if row['>5mm%'] > 10:
  165. rateList.append('存疑:>5mm占比超过10%应回溯。')
  166. rateTar.append('水稳>5mm。')
  167. else:
  168. rateList.append('')
  169. rateTar.append('')
  170. resData = pd.DataFrame({
  171. '审核结果': pd.Series(plusList) + pd.Series(soilType) + pd.Series(rateList),
  172. '异常指标': pd.Series(plusTar) + pd.Series(rateTar),
  173. })
  174. return resData
  175. except Exception as err:
  176. print('大团聚体判断出错!请检查water_stable中判断大团聚体内容', err)
  177. # 表5 pH、阳离子交换量、交换性盐基基础数据判断
  178. # 判断土壤类型和阳离子交换量 盐基饱和度的范围
  179. def soilTypeValue(row): # 传入一行数据
  180. strValue = row['土壤类型']
  181. soilType = ''
  182. if isinstance(strValue, str):
  183. # print('type---', strValue.split('_'))
  184. # print('strValue',strValue)
  185. if len(strValue.split('_')) > 1:
  186. soilType = strValue.split('_')[1] # 获取到土壤类型
  187. cationChange = row['阳离子交换量Cmol(+)/kg'] # 获取到阳离子交换量
  188. bHValue = (row['交换性盐总量Cmol(+)/kg']/row['阳离子交换量Cmol(+)/kg'] )*100# 计算出的盐基饱和度
  189. # 判断三者范围是否合理
  190. res = ''
  191. if soilType == '黄红壤':
  192. if pd.isna(cationChange) and (cationChange > 10 and cationChange<24) and pd.isna(bHValue) and (bHValue > 30 and bHValue<50):
  193. res = ''
  194. else:
  195. res = '存疑:该土壤类型的阳离子交换量或盐基饱和度范围存疑。'
  196. elif soilType == '棕红壤':
  197. if pd.isna(cationChange) and (cationChange > 6 and cationChange < 15) and pd.isna(bHValue) and (
  198. bHValue > 25 and bHValue < 70):
  199. res = ''
  200. else:
  201. res = '存疑:该土壤类型的阳离子交换量或盐基饱和度范围存疑。'
  202. elif soilType == '红壤性土':
  203. if pd.isna(cationChange) and (cationChange > 5 and cationChange < 15) and pd.isna(bHValue) and (
  204. bHValue > 10 and bHValue < 50):
  205. res = ''
  206. else:
  207. res = '存疑:该土壤类型的阳离子交换量或盐基饱和度范围存疑。'
  208. elif soilType == '典型黄壤':
  209. if pd.isna(cationChange) and (cationChange > 5 and cationChange < 15) and pd.isna(bHValue) and (
  210. bHValue < 30):
  211. res = ''
  212. else:
  213. res = '该土壤类型的阳离子交换量或盐基饱和度范围存疑。'
  214. elif soilType == '黄壤性土':
  215. if pd.isna(cationChange) and (cationChange > 10 and cationChange < 18) and pd.isna(bHValue) and (
  216. bHValue < 45):
  217. res = ''
  218. else:
  219. res = '存疑:该土壤类型的阳离子交换量或盐基饱和度范围存疑。'
  220. elif soilType == '典型黄棕壤' or soilType == '暗黄棕壤' or soilType == '黄棕壤性土':
  221. if pd.isna(cationChange) and (cationChange > 8 and cationChange < 22) and pd.isna(bHValue) and (
  222. bHValue > 30 and bHValue < 60):
  223. res = ''
  224. else:
  225. res = '存疑:该土壤类型的阳离子交换量或盐基饱和度范围存疑。'
  226. elif soilType == '典型黄褐土' or soilType == '黏盘黄褐土' or soilType == '粘盘黄褐土' or soilType == '粘盘黄褐土' or soilType=='白浆化黄褐土' or soilType=='黄褐土性土':
  227. if pd.isna(cationChange) and (cationChange > 15 and cationChange < 25) and pd.isna(bHValue) and (
  228. bHValue > 60 and bHValue < 85):
  229. res = ''
  230. else:
  231. res = '存疑:该土壤类型的阳离子交换量或盐基饱和度范围存疑。'
  232. elif soilType == '粘盘黄褐土':
  233. if pd.isna(cationChange) and (cationChange > 10 and cationChange < 30) and pd.isna(bHValue) and (
  234. bHValue > 75 and bHValue < 95):
  235. res = ''
  236. else:
  237. res = '存疑:该土壤类型的阳离子交换量或盐基饱和度范围存疑。'
  238. elif soilType == '典型棕壤' or soilType == '白浆化棕壤' or soilType == '潮棕壤' or soilType == '棕壤性土' or soilType == '棕壤性土':
  239. if pd.isna(cationChange) and (cationChange > 5 and cationChange < 20) and pd.isna(bHValue) and (
  240. cationChange > 25 and cationChange < 65):
  241. res = ''
  242. else:
  243. res = '存疑:该土壤类型的阳离子交换量或盐基饱和度范围存疑。'
  244. elif soilType == '典型山地草甸土' or soilType == '山地草原草甸土' or soilType == '山地灌丛草甸土':
  245. if pd.isna(cationChange) and (cationChange > 10 and cationChange < 20) and pd.isna(bHValue) and (
  246. bHValue > 15 and bHValue < 30):
  247. res = ''
  248. else:
  249. res = '存疑:该土壤类型的阳离子交换量或盐基饱和度范围存疑。'
  250. elif soilType == '酸性紫色土' or soilType == '中性紫色土' or soilType == '石灰性紫色土':
  251. if pd.isna(cationChange) and (cationChange > 10 and cationChange < 20) and pd.isna(bHValue) and (
  252. bHValue > 50 and bHValue < 70):
  253. res = ''
  254. else:
  255. res = '存疑:该土壤类型的阳离子交换量或盐基饱和度范围存疑。'
  256. elif soilType == '红色石灰土' or soilType == '黑色石灰土' or soilType == '棕色石灰土' or soilType == '黄色石灰土' :
  257. if pd.isna(cationChange) and (cationChange > 15 and cationChange < 30) and pd.isna(bHValue) and (
  258. bHValue > 70 and bHValue < 100):
  259. res = ''
  260. else:
  261. res = '存疑:该土壤类型的阳离子交换量或盐基饱和度范围存疑。'
  262. elif soilType == '酸性石质土' or soilType == '中性石质土' or soilType == '钙质石质土' or soilType == '含盐石质土':
  263. if pd.isna(cationChange) and (cationChange > 10 and cationChange < 15) and pd.isna(bHValue) and (
  264. bHValue > 45 and bHValue < 65):
  265. res = ''
  266. else:
  267. res = '存疑:该土壤类型的阳离子交换量或盐基饱和度范围存疑。'
  268. elif soilType == '酸性粗骨土' or soilType == '中性粗骨土' or soilType == '钙质粗骨土' or soilType == '硅质盐粗骨土':
  269. if pd.isna(cationChange) and (cationChange > 5 and cationChange < 15) and pd.isna(bHValue) and (
  270. bHValue > 20 and bHValue < 50):
  271. res = ''
  272. else:
  273. res = '存疑:该土壤类型的阳离子交换量或盐基饱和度范围存疑。'
  274. elif soilType == '典型潮土' or soilType == '灰潮土' or soilType == '脱潮土' or soilType == '湿潮土' or soilType == '盐化潮土' or soilType == '碱化潮土' or soilType == '灌於潮土':
  275. if pd.isna(cationChange) and (cationChange > 10 and cationChange < 30) and pd.isna(bHValue) and (
  276. bHValue > 70 and bHValue < 100):
  277. res = ''
  278. else:
  279. res = '存疑:该土壤类型的阳离子交换量或盐基饱和度范围存疑。'
  280. elif soilType == '典型砂姜黑土' or soilType == '石灰性砂姜黑土' or soilType == '盐化砂姜黑土' or soilType == '碱化砂姜黑土' or soilType == '黑粘土':
  281. if pd.isna(cationChange) and (cationChange > 18 and cationChange < 35) and pd.isna(bHValue) and (
  282. bHValue > 90 and bHValue < 100):
  283. res = ''
  284. else:
  285. res = '存疑:该土壤类型的阳离子交换量或盐基饱和度范围存疑。'
  286. elif soilType == '淹育水稻土':
  287. if pd.isna(cationChange) and (cationChange > 20 and cationChange < 30) and pd.isna(bHValue) and (
  288. bHValue > 85 and bHValue < 90):
  289. res = ''
  290. else:
  291. res = '存疑:该土壤类型的阳离子交换量或盐基饱和度范围存疑。'
  292. elif soilType == '潴育水稻土':
  293. if pd.isna(cationChange) and (cationChange > 12 and cationChange < 20) and pd.isna(bHValue) and (
  294. bHValue > 60 and bHValue < 80):
  295. res = ''
  296. else:
  297. res = '存疑:该土壤类型的阳离子交换量或盐基饱和度范围存疑。'
  298. elif soilType == '潜育水稻土':
  299. if pd.isna(cationChange) and (cationChange > 15 and cationChange < 25) and pd.isna(bHValue) and (
  300. bHValue > 75 and bHValue < 90):
  301. res = ''
  302. else:
  303. res = '存疑:该土壤类型的阳离子交换量或盐基饱和度范围存疑。'
  304. elif soilType == '漂洗水稻土':
  305. if pd.isna(cationChange) and (cationChange > 10 and cationChange < 20) and pd.isna(bHValue) and (
  306. bHValue > 65 and bHValue < 80):
  307. res = ''
  308. else:
  309. res = '存疑:该土壤类型的阳离子交换量或盐基饱和度范围存疑。'
  310. return res
  311. def cation_value(arr):
  312. phList = [] # 保存ph存疑数据
  313. cationList = [] # 保存阳离子存疑数据
  314. exchangeableSalt = [] # 保存交换性盐基总量存疑数据
  315. exchangeableCa = [] # 保存交换性钙总量存疑数据
  316. exchangeableMg = [] # 保存交换性镁总量存疑数据
  317. exchangeableK = [] # 保存交换性钾总量存疑数据
  318. exchangeableNa = [] # 保存交换性钠总量存疑数据
  319. summaryList = [] # 保存ph 离子和 盐基饱和度范围存疑数据
  320. soilTypeList = [] # 保存土壤类型 阳离子交换量cmol(+)/kg 和盐基饱和度范围存疑数据
  321. waterMount = [] # 保存含水量存疑数据 含水量应小于10%
  322. phTar = [] # 保存ph异常指标
  323. cationTar = [] # 保存阳离子异常指标
  324. exchangeableSaltTar = [] # 保存交换性盐基总量异常指标
  325. exchangeableCaTar = [] # 保存交换性钙异常指标
  326. exchangeableMgTar = [] # 保存交换性镁异常指标
  327. exchangeableKTar = [] # 保存交换性钾异常指标
  328. exchangeableNaTar = [] # 保存交换性钠异常指标
  329. summaryListTar = [] # 保存ph 离子和 盐基饱和度异常指标
  330. soilTypeListTar = [] # 保存土壤类型 阳离子交换量cmol(+)/kg 和盐基饱和度异常数据
  331. waterMountTar = [] # 保存含水量异常指标
  332. try:
  333. for index, row in arr.iterrows():
  334. # 风干样含水量 0.5-5,存疑
  335. if pd.isna(row['含水量']) or row['含水量'] > 5 or row['含水量'] < 0.5:
  336. waterMount.append('风干试样含水量(分析基):超阈值。')
  337. waterMountTar.append('风干试样含水量(分析基)。')
  338. else:
  339. waterMount.append('')
  340. waterMountTar.append('')
  341. # (1)pH在[4, 9]范围之外的,存疑;
  342. if pd.isna(row['pH']) or row['pH'] < 4 or row['pH'] > 9:
  343. phList.append('pH:超阈值。')
  344. phTar.append('pH。')
  345. else:
  346. phList.append('')
  347. phTar.append('')
  348. # (2)阳离子交换量在[6, 38]范围之外的,存疑;
  349. if row['阳离子交换量Cmol(+)/kg'] < 6 or row['阳离子交换量Cmol(+)/kg'] > 38:
  350. cationList.append('阳离子交换量:超阈值。')
  351. cationTar.append('阳离子交换量。')
  352. else:
  353. cationList.append('')
  354. cationTar.append('')
  355. # (3)交换性盐基总量在[3, 30]范围之外的,存疑;
  356. if row['交换性盐总量Cmol(+)/kg'] <3 or row['交换性盐总量Cmol(+)/kg'] > 30:
  357. exchangeableSalt.append('交换性盐基总量:超阈值。')
  358. exchangeableSaltTar.append('交换性盐总量。')
  359. else:
  360. exchangeableSalt.append('')
  361. exchangeableSaltTar.append('')
  362. # (4)交换性钙在[1, 25]范围之外的,存疑;
  363. if row['交换性钙Cmol(1/2Ca2+)/kg'] < 1 or row['交换性钙Cmol(1/2Ca2+)/kg'] > 25:
  364. exchangeableCa.append('交换性钙:交换性钙超阈值。')
  365. exchangeableCaTar.append('交换性钙。')
  366. else:
  367. exchangeableCa.append('')
  368. exchangeableCaTar.append('')
  369. # (5)交换性镁在[0.5, 12.5]范围之外的,存疑;
  370. if row['交换性镁cmol(1/2Mg2+)/kg'] < 0.5 or row['交换性镁cmol(1/2Mg2+)/kg'] > 12.8:
  371. exchangeableMg.append('交换性镁:超阈值。')
  372. exchangeableMgTar.append('交换性镁。')
  373. else:
  374. exchangeableMg.append('')
  375. exchangeableMgTar.append('')
  376. # (6)交换性钾在[0.1, 1.5]范围之外的,存疑;
  377. if row['交换性钾Cmol(+)/kg'] < 0.1 or row['交换性钾Cmol(+)/kg'] > 1.5:
  378. exchangeableK.append('交换性钾:超阈值。')
  379. exchangeableKTar.append('交换性钾。')
  380. else:
  381. exchangeableK.append('')
  382. exchangeableKTar.append('')
  383. # (7)交换性钠在[0.3, 1.9]范围之外的,存疑;
  384. if row['交换性钠cmol(+)/kg'] < 0.3 or row['交换性钠cmol(+)/kg'] > 1.9:
  385. exchangeableNa.append('交换性钠:超阈值。')
  386. exchangeableNaTar.append('交换性钠。')
  387. else:
  388. exchangeableNa.append('')
  389. exchangeableNaTar.append('')
  390. # (8)pH<7.5,阳离子交换量>交换性盐总量>四大离子之和,且盐基饱和度小于100 %;违反则存疑;pH≥7.5,交换性盐总量 = 四大离子之和,盐基饱和度范围在80~120 %;违反则存疑;
  391. if ((not pd.isna(row['pH']) and row['pH']<7.5 and row['阳离子交换量Cmol(+)/kg']>row['交换性盐总量Cmol(+)/kg']>row['四大离子之和'] and row['盐基饱和度%']*100 <80 ) or
  392. ((not pd.isna(row['pH']) and row['pH']>=7.5 and row['交换性盐总量Cmol(+)/kg']==row['四大离子之和'] and (row['盐基饱和度%'] <120 and row['盐基饱和度%'] >80 ))) or
  393. ((not pd.isna(row['pH']) and row['pH']<6 and row['盐基饱和度%'] >80))
  394. ):
  395. summaryList.append('')
  396. summaryListTar.append('')
  397. else:
  398. summaryList.append('存疑:ph值、阳离子交换量、交换性盐总量、离子总和、盐基饱和度之间关系存疑。')
  399. summaryListTar.append('盐基饱和度。')
  400. soilRes = soilTypeValue(row)
  401. soilTypeList.append(soilRes)
  402. # print('pd.Series(phList)',pd.Series(phList) + pd.Series(cationList)+pd.Series(exchangeableSalt))
  403. # print('pd.Series(cationList)', pd.Series(exchangeableSalt))
  404. # print('res---', pd.Series(phList)+pd.Series(cationList)+pd.Series(exchangeableSalt)+pd.Series(exchangeableCa)+pd.Series(exchangeableMg)+pd.Series(exchangeableK)+pd.Series(exchangeableNa)+pd.Series(summaryList))
  405. checkData = pd.DataFrame({
  406. '审核结果': pd.Series(phList)+pd.Series(cationList)+pd.Series(exchangeableSalt)+pd.Series(exchangeableCa)+pd.Series(exchangeableMg)+pd.Series(exchangeableK)+pd.Series(exchangeableNa)+pd.Series(summaryList)+pd.Series(soilTypeList)+pd.Series(waterMount),
  407. '异常指标': pd.Series(phTar)+pd.Series(cationTar)+pd.Series(exchangeableSaltTar)+pd.Series(exchangeableCaTar)+pd.Series(exchangeableMgTar)+pd.Series(exchangeableKTar)+pd.Series(exchangeableNaTar)+pd.Series(summaryListTar)+pd.Series(waterMountTar)
  408. })
  409. return checkData
  410. except Exception as err:
  411. print('阳离子量判断出错!请检查cation_value中判断阳离子量内容', err)
  412. # 表8 8大离子基础数据判断
  413. def eight_ion_coun(arr, summary):
  414. try:
  415. allArr = [] # 存储水溶性盐总量存疑数据
  416. conductivity = [] # 存储电导率存疑数据
  417. naArr = [] # 存储钠离子存疑数据
  418. kArr = [] # 存储钾离子存疑数据
  419. caArr = [] # 存储钙离子存疑数据
  420. mgArr = [] # 存储镁离子存疑数据
  421. coArr = [] # 存储碳酸根离子存疑数据
  422. cohArr = [] # 存储碳酸氢根离子存疑数据
  423. soArr = [] # 存储硫酸根离子存疑数据
  424. clArr = [] # 氯离子存疑数据
  425. totalCom = [] # 全盐量小于八大离子和存疑数据
  426. phCoArr = [] # ph 碳酸根存疑数据
  427. changeComArr = [] #交换性离子高于水溶性离子存疑数据
  428. rateArr = [] # (水溶性全盐量-八大离子加和)/八大离子加和×100 存疑数据
  429. subtractionArr=[] #阳离子-阴离子 不在范围内
  430. # 存放异常指标
  431. allArrTar = [] # 存储水溶性盐总量异常指标
  432. conductivityTar = [] # 存储电导率异常指标
  433. naArrTar = [] # 存储钠离子异常指标
  434. kArrTar = [] # 存储钾离子异常指标
  435. caArrTar = [] # 存储钙离子异常指标
  436. mgArrTar = [] # 存储镁离子异常指标
  437. coArrTar = [] # 存储碳酸根离子异常指标
  438. cohArrTar = [] # 存储碳酸氢根离子异常指标
  439. soArrTar = [] # 存储硫酸根离子异常指标
  440. clArrTar = [] # 氯离子异常指标
  441. totalComTar = [] # 全盐量小于八大离子和异常指标
  442. phCoArrTar = [] # ph 碳酸根异常指标
  443. changeComArrTar = [] # 交换性离子高于水溶性离子异常指标
  444. rateArrTar = [] # (水溶性全盐量-八大离子加和)/八大离子加和×100 异常指标
  445. subtractionArrTar = [] # 阳离子-阴离子 异常指标
  446. #(2)水溶性盐总量在[0.1, 2]范围之外的,存疑;
  447. for index, row in arr.iterrows():
  448. if (not pd.isna(row['水溶性全盐量g/kg']) and row['水溶性全盐量g/kg'] < 0.1) or (not pd.isna(row['水溶性全盐量g/kg']) and row['水溶性全盐量g/kg'] > 2):
  449. allArr.append('全盐量:超阈值。')
  450. allArrTar.append('全盐量。')
  451. else:
  452. allArr.append('')
  453. allArrTar.append('')
  454. #(3)电导率在[0.01, 2]范围之外的,存疑;
  455. if ( not pd.isna(row['电导率ms/cm']) and row['电导率ms/cm'] < 0.01) or (not pd.isna(row['电导率ms/cm']) and row['电导率ms/cm'] > 2):
  456. conductivity.append('电导率:超阈值。')
  457. conductivityTar.append('电导率。')
  458. else:
  459. conductivity.append('')
  460. conductivityTar.append('电导率')
  461. #(4)水溶性钠在[0.05, 0.5]范围之外的,存疑;
  462. if (not pd.isna(row['水溶性钠离子含量Cmol(Na+)/kg']) and row['水溶性钠离子含量Cmol(Na+)/kg'] <0.05) or (pd.isna(row['水溶性钠离子含量Cmol(Na+)/kg']) and row['水溶性钠离子含量Cmol(Na+)/kg'] > 0.5):
  463. naArr.append('水溶性钠离子:水溶性钠离子超阈值。')
  464. naArrTar.append('水溶性钠离子。')
  465. else:
  466. naArr.append('')
  467. naArrTar.append('')
  468. #(5)水溶性钾在[0.01, 0.5]范围之外的,存疑;
  469. if (not pd.isna(row['水溶性钾离子含量Cmol(K+)/kg']) and row['水溶性钾离子含量Cmol(K+)/kg'] <0.01) or ( not pd.isna(row['水溶性钾离子含量Cmol(K+)/kg']) and row['水溶性钾离子含量Cmol(K+)/kg'] > 0.5):
  470. kArr.append('水溶性钾离子:超阈值。')
  471. kArrTar.append('水溶性钾离子。')
  472. else:
  473. kArr.append('')
  474. kArrTar.append('')
  475. #(6)水溶性钙在[0.25, 5]范围之外的,存疑;
  476. if (not pd.isna(row['水溶性钙离子含量cmol(1/2Ca2+)/kg']) and row['水溶性钙离子含量cmol(1/2Ca2+)/kg'] <0.25) or (not pd.isna(row['水溶性钙离子含量cmol(1/2Ca2+)/kg']) and row['水溶性钙离子含量cmol(1/2Ca2+)/kg'] > 0.5):
  477. caArr.append('水溶性钙离子:超阈值。')
  478. caArrTar.append('水溶性钙离子。')
  479. else:
  480. caArr.append('')
  481. caArrTar.append('')
  482. #(7)水溶性镁在[0.125, 2.5]范围之外的,存疑;
  483. if (not pd.isna(row['水溶性镁离子Cmol(1/2Mg2+)/kg']) and row['水溶性镁离子Cmol(1/2Mg2+)/kg'] <0.125) or (not pd.isna(row['水溶性镁离子Cmol(1/2Mg2+)/kg']) and row['水溶性镁离子Cmol(1/2Mg2+)/kg'] > 2.5):
  484. mgArr.append('水溶性镁离子:超阈值。')
  485. mgArrTar.append('水溶性镁离子。')
  486. else:
  487. mgArr.append('')
  488. mgArrTar.append('')
  489. #(8)水溶性碳酸根在[0.01, 2.5]范围之外的,存疑;
  490. if (not pd.isna(row['水溶性碳酸根离子含量cmol(1/2CO32+)/kg']) and row['水溶性碳酸根离子含量cmol(1/2CO32+)/kg'] <0.01) or (not pd.isna(row['水溶性碳酸根离子含量cmol(1/2CO32+)/kg']) and row['水溶性碳酸根离子含量cmol(1/2CO32+)/kg'] > 2.5):
  491. coArr.append('水溶性碳酸根:超阈值。')
  492. coArrTar.append('水溶性碳酸根。')
  493. else:
  494. coArr.append('')
  495. coArrTar.append('')
  496. #(9)水溶性碳酸氢根在[0.05, 5]范围之外的,存疑;
  497. if (not pd.isna(row['水溶性碳酸氢离子含量cmol(1/2HCO3-)/kg']) and row['水溶性碳酸氢离子含量cmol(1/2HCO3-)/kg'] <0.05) or (not pd.isna(row['水溶性碳酸氢离子含量cmol(1/2HCO3-)/kg']) and row['水溶性碳酸氢离子含量cmol(1/2HCO3-)/kg'] > 5):
  498. cohArr.append('水溶性碳酸氢根:超阈值。')
  499. cohArrTar.append('水溶性碳酸氢根。')
  500. else:
  501. cohArr.append('')
  502. cohArrTar.append('')
  503. #(10)水溶性硫酸根在[0.25, 2.5]范围之外的,存疑;
  504. if (not pd.isna(row['水溶性硫酸根离子含量cmol(1/2SO42-)/kg']) and row['水溶性硫酸根离子含量cmol(1/2SO42-)/kg'] <0.25) or (not pd.isna(row['水溶性硫酸根离子含量cmol(1/2SO42-)/kg']) and row['水溶性硫酸根离子含量cmol(1/2SO42-)/kg'] > 2.5):
  505. soArr.append('水溶性硫酸根:超阈值。')
  506. soArrTar.append('水溶性硫酸根。')
  507. else:
  508. soArr.append('')
  509. soArrTar.append('')
  510. #(11)水溶性氯根在[0.5, 5]范围之外的,存疑;
  511. if (not pd.isna(row['水溶性氯离子含量cmol(Cl-)/kg']) and row['水溶性氯离子含量cmol(Cl-)/kg'] <0.5) or (not pd.isna(row['水溶性氯离子含量cmol(Cl-)/kg']) and row['水溶性氯离子含量cmol(Cl-)/kg'] > 5):
  512. clArr.append('水溶性氯根:超阈值。')
  513. clArrTar.append('水溶性氯根。')
  514. else:
  515. clArr.append('')
  516. clArrTar.append('')
  517. #(12)水溶性盐总量大于等于八大离子之和,违背则存疑;土地利用类型为菜地的,可能不符合这个规律;
  518. if (not pd.isna( row['水溶性全盐量g/kg']) and not pd.isna(row['八大离子加和g/kg']) and row['水溶性全盐量g/kg'] < row['八大离子加和g/kg']):
  519. totalCom.append('存疑:水溶性全盐量小于八大离子之和。')
  520. totalComTar.append('水溶性全盐量。')
  521. else:
  522. totalCom.append('')
  523. totalComTar.append('')
  524. #(13)水溶性八大离子换算为g / kg,如水溶性钠离子g / kg = 水溶性钠离子cmol(Na +) / kg×23g / mol×10 - 2; 这里在计算离子和时已转换
  525. #(14)pH<8,碳酸根基本为0
  526. if row['pH'] <8 and not pd.isna(row['水溶性碳酸根离子含量cmol(1/2CO32+)/kg']) and row['水溶性碳酸根离子含量cmol(1/2CO32+)/kg'] == 0:
  527. phCoArr.append('水溶性碳酸根:pH<8且水溶性碳酸根为0。')
  528. phCoArrTar.append('水溶性碳酸根。')
  529. else:
  530. phCoArr.append('')
  531. phCoArrTar.append('')
  532. #(15)交换性四大盐离子均要高于水溶性四大盐离子(钙镁钾钠)
  533. naBool = not pd.isna(row['水溶性钠离子含量Cmol(Na+)/kg'])
  534. kBool = not pd.isna(row['水溶性钾离子含量Cmol(K+)/kg'])
  535. caBool = not pd.isna(row['水溶性钙离子含量cmol(1/2Ca2+)/kg'])
  536. mgBool = not pd.isna(row['水溶性镁离子Cmol(1/2Mg2+)/kg'])
  537. sumNa = not pd.isna(summary.loc[index,'交换性钠'])
  538. sumK = not pd.isna(summary.loc[index, '交换性钾'])
  539. sumCa = not pd.isna(summary.loc[index, '交换性钙'])
  540. sumMg = not pd.isna(summary.loc[index, '交换性镁'])
  541. if (naBool and sumNa and row['水溶性钠离子含量Cmol(Na+)/kg']>summary.loc[index,'交换性钠']) or (
  542. kBool and sumK and row['水溶性钾离子含量Cmol(K+)/kg']>summary.loc[index,'交换性钾']) or (
  543. caBool and sumCa and row['水溶性钙离子含量cmol(1/2Ca2+)/kg']>summary.loc[index,'交换性钙']) or (
  544. mgBool and sumMg and row['水溶性镁离子Cmol(1/2Mg2+)/kg']>summary.loc[index,'交换性镁']):
  545. changeComArr.append('存疑:交换性盐基总量低于于水溶性盐离子。')
  546. else:
  547. changeComArr.append('')
  548. #(16)(水溶性全盐量 - 八大离子加和) / 八大离子加和 * 100,不超过±20 %
  549. if not pd.isna(row['(水溶性全盐量-八大离子加和)/水溶性全盐量×100']) and (row['(水溶性全盐量-八大离子加和)/水溶性全盐量×100'] < -0.2 or row['(水溶性全盐量-八大离子加和)/水溶性全盐量×100'] > 0.2) :
  550. rateArr.append('存疑:(水溶性全盐量-八大离子加和)/水溶性全盐量×100超阈值。')
  551. else:
  552. rateArr.append('')
  553. #(17)阳离子总量 - 阴离子总量应基本相等,超过±1则提示异常
  554. if not pd.isna(row['阳离子总量-阴离子总量']) and (row['阳离子总量-阴离子总量'] < -1 or row['阳离子总量-阴离子总量'] > 1) :
  555. subtractionArr.append('存疑:阳离子总量 - 阴离子总量超阈值。')
  556. else:
  557. subtractionArr.append('')
  558. resData = pd.DataFrame({
  559. '审核结果': pd.Series(allArr) + pd.Series(conductivity) + pd.Series(naArr) +
  560. pd.Series(kArr) + pd.Series(caArr) + pd.Series(mgArr) + pd.Series(coArr) + pd.Series(
  561. cohArr) + pd.Series(soArr) + pd.Series(clArr) + pd.Series(totalCom) + pd.Series(
  562. phCoArr) + pd.Series(changeComArr) + pd.Series(rateArr) + pd.Series(subtractionArr),
  563. '异常指标': pd.Series(allArrTar) + pd.Series(conductivityTar) + pd.Series(naArrTar) +
  564. pd.Series(kArrTar) + pd.Series(caArrTar) + pd.Series(mgArrTar) + pd.Series(coArrTar) + pd.Series(
  565. cohArrTar) + pd.Series(soArrTar) + pd.Series(clArrTar) + pd.Series(totalComTar) + pd.Series(
  566. phCoArrTar)
  567. })
  568. return resData
  569. except Exception as err:
  570. print('八大离子判断出错!请检查eight_ion_coun中判断离子内容', err)
  571. # 表10 有机质、全氮、全磷、全钾数据
  572. def nutrient_data(arr):
  573. try:
  574. organicMatter = [] # 有机质存疑数据
  575. NArr = [] # 全氮存疑数据
  576. PArr = [] # 全磷存疑数据
  577. KArr = [] # 全钾存疑数据
  578. availableP = [] # 有效磷存疑数据
  579. availablek = [] #速效钾存疑数据
  580. slowlyK= [] #缓效钾存疑数据
  581. organicRate = [] #有机质 / 全氮比值存疑数据
  582. availablePCom = [] #有效磷<3和大于60,提示异常;
  583. availableTxt = [] # 速效钾<50提示异常
  584. availablekCom = [] # 速效钾>缓效钾 存疑数据
  585. sKErr = [] # 保存交换性钾不等于速效钾
  586. # 异常指标
  587. organicMatterTar = [] # 有机质异常指标
  588. NArrTar = [] # 全氮异常指标
  589. PArrTar = [] # 全磷异常指标
  590. KArrTar = [] # 全钾异常指标
  591. availablePTar = [] # 有效磷异常指标
  592. availablekTar = [] # 速效钾异常指标
  593. slowlyKTar = [] # 缓效钾异常指标
  594. sKErrTar = [] # 保存交换性钾不等于速效钾
  595. for index, row in arr.iterrows():
  596. # 交换性钾 == 速效钾
  597. if not pd.isna(row['速效钾mg/kg']) and not pd.isna(row['交换性钾']) and ((row['速效钾mg/kg'] - row['交换性钾']*391)/ (row['交换性钾']*391) > 0.2 or (row['速效钾mg/kg'] - row['交换性钾']*391)/ (row['交换性钾']*391) < -0.2):
  598. sKErr.append('存疑:交换性钾和速效钾误差超20%。')
  599. sKErrTar.append('交换性钾、速效钾。')
  600. else:
  601. sKErr.append('')
  602. sKErrTar.append('')
  603. #(2)有机质在[2, 50]范围之外的,存疑;有机质<5提示异常;
  604. if row['有机质g/kg'] < 2 or row['有机质g/kg'] >50:
  605. organicMatter.append('有机质:超阈值。')
  606. organicMatterTar.append('有机质。')
  607. else:
  608. organicMatter.append('')
  609. organicMatterTar.append('')
  610. #(3)全氮在[0.1, 2.5]范围之外的;存疑;
  611. if row['全氮g/kg'] < 0.1 or row['全氮g/kg'] > 2.5:
  612. NArr.append('全氮:超阈值。')
  613. NArrTar.append('全氮。')
  614. else:
  615. NArr.append('')
  616. NArrTar.append('')
  617. #(4)全磷在[0.18, 1.5]范围之外的;存疑;
  618. if row['全磷g/kg'] < 0.18 or row['全磷g/kg'] > 1.5:
  619. PArr.append('全磷:超阈值。')
  620. PArrTar.append('全磷。')
  621. else:
  622. PArr.append('')
  623. PArrTar.append('')
  624. #(5)全钾在[10, 29]范围之外的;存疑;
  625. if row['全钾g/kg'] < 10 or row['全钾g/kg'] > 29:
  626. KArr.append('全钾:超阈值。')
  627. KArrTar.append('全钾。')
  628. else:
  629. KArr.append('')
  630. KArrTar.append('')
  631. #(6)有效磷在[1, 80]范围之外的;存疑; 耕地 超过80存疑
  632. if (row['pH'] >= 6.5 and (row['有效磷g/kg'] < 3 or row['有效磷g/kg'] > 60)) or (row['pH'] < 6.5 and (row['有效磷g/kg'] <1 or row['有效磷g/kg'] > 80) or (row['编号'][6:10] == '0101' or row['编号'][6:10] == '0102' or row['编号'][6:10] == '0103') and row['有效磷g/kg'] > 60) :
  633. availableP.append('有效磷:超阈值。')
  634. availablePTar.append('有效磷。')
  635. else:
  636. availableP.append('')
  637. availablePTar.append('')
  638. # if row['有效磷g/kg'] < 1 or row['有效磷g/kg'] > 60 or ((row['编号'][6:11] == '0101' or row['编号'][6:11] == '0102' or row['编号'][6:11] == '0103') and row['有效磷g/kg'] > 60):
  639. # availableP.append('存疑:有效磷超阈值。')
  640. # else:
  641. # availableP.append('')
  642. #(7)速效钾在[30, 300] 范围之外的;存疑; 耕地超过300存疑
  643. if row['速效钾mg/kg'] < 30 or row['速效钾mg/kg'] > 300 or ((row['编号'][6:10] == '0101' or row['编号'][6:10] == '0102' or row['编号'][6:10] == '0103') and row['速效钾mg/kg'] > 300):
  644. availablek.append('速效钾:超阈值。')
  645. availablekTar.append('速效钾。')
  646. else:
  647. availablek.append('')
  648. availablekTar.append('')
  649. #(8)缓效钾在[100, 2000]范围之外的;存疑;
  650. if row['缓效钾mg/kg'] < 100 or row['缓效钾mg/kg'] > 2000:
  651. slowlyK.append('缓效钾:超阈值。')
  652. slowlyKTar.append('缓效钾。')
  653. else:
  654. slowlyK.append('')
  655. slowlyKTar.append('')
  656. #(9)有机质 / 全氮比值≥20和≤13,提示存疑
  657. if row['有机质g/kg']/row['全氮g/kg'] >=20 or row['有机质g/kg']/row['全氮g/kg'] <=13 :
  658. organicRate.append('存疑:有机质/全氮比值超阈值。')
  659. else:
  660. organicRate.append('')
  661. #(10)有机质、全氮含量异常高,但速效养分特低,提示异常 无法量化不处理
  662. #(11)母岩为片麻岩,但全钾、速效缓效钾含量低,提示异常 无法量化不处理
  663. #(12)有效磷<3和大于60,提示异常;速效钾<50提示异常
  664. if row['有效磷g/kg'] < 3 or row['有效磷g/kg'] > 60:
  665. availablePCom.append('有效磷:超阈值。')
  666. else:
  667. availablePCom.append('')
  668. if row['速效钾mg/kg'] < 50:
  669. availableTxt.append('速效钾:超阈值。')
  670. else:
  671. availableTxt.append('')
  672. #(13)速效钾>缓效钾,提示异常
  673. if row['速效钾mg/kg'] > row['缓效钾mg/kg']:
  674. availablekCom.append('异常:速效钾大于缓效钾。')
  675. else:
  676. availablekCom.append('')
  677. resData = pd.DataFrame({
  678. '审核结果': pd.Series(organicMatter) + pd.Series(NArr) + pd.Series(PArr) +
  679. pd.Series(KArr) + pd.Series(availableP) + pd.Series(availablek) + pd.Series(slowlyK) + pd.Series(
  680. organicRate) + pd.Series(availablePCom) + pd.Series(availablekCom) + pd.Series(availableTxt) + pd.Series(availablePCom) + pd.Series(sKErr),
  681. '异常指标': pd.Series(organicMatterTar) + pd.Series(NArrTar) + pd.Series(PArrTar) +
  682. pd.Series(KArrTar) + pd.Series(availablePTar) + pd.Series(availablekTar) + pd.Series(slowlyKTar) + pd.Series(sKErrTar)
  683. })
  684. return resData
  685. except Exception as err:
  686. print('有机质、全氮、全磷、全钾数据判断出错!请检查nutrient_data中判断内容', err)
  687. # 表12 土壤指标判断规则
  688. def soil_metal(arr):
  689. try:
  690. effectiveL = [] # 有效硫存疑数据
  691. effectiveG = [] # 有效硅存疑数据
  692. effectiveT = [] # 有效铁存疑数据
  693. effectiveM = [] # 有效锰存疑数据
  694. effectiveCu = [] # 有效铜存疑数据
  695. effectiveX = [] # 有效锌存疑数据
  696. effectiveP = [] # 有效硼存疑数据
  697. effectiveMu = [] # 有效钼存疑数据
  698. # 存疑指标
  699. effectiveLTar = [] # 有效硫
  700. effectiveGTar = [] # 有效硅
  701. effectiveTTar = [] # 有效铁
  702. effectiveMTar = [] # 有效锰
  703. effectiveCuTar = [] # 有效铜
  704. effectiveXTar = [] # 有效锌
  705. effectivePTar = [] # 有效硼
  706. effectiveMutar = [] # 有效钼
  707. for index, row in arr.iterrows():
  708. #(1)有效硫在[2, 60]范围之外的,存疑;
  709. if (not pd.isna(row['有效硫mg/kg']) and row['有效硫mg/kg'] <2) or (not pd.isna(row['有效硫mg/kg']) and row['有效硫mg/kg'] >60):
  710. effectiveL.append('有效硫:超阈值。')
  711. effectiveLTar.append('有效硫。')
  712. else:
  713. effectiveL.append('')
  714. effectiveLTar.append('')
  715. #(2)有效硅在[10, 500]范围之外的,存疑;
  716. if ( not pd.isna(row['有效硅mg/kg']) and row['有效硅mg/kg'] <10) or (not pd.isna(row['有效硅mg/kg']) and row['有效硅mg/kg'] >500):
  717. effectiveG.append('有效硅:超阈值。')
  718. effectiveGTar.append('有效硅。')
  719. else:
  720. effectiveG.append('')
  721. effectiveGTar.append('')
  722. #(3)有效铁在[5, 300]范围之外的,存疑;
  723. if ( not pd.isna(row['有效铁mg/kg']) and row['有效铁mg/kg'] <5) or (not pd.isna(row['有效铁mg/kg']) and row['有效铁mg/kg'] >300):
  724. effectiveT.append('有效铁:超阈值。')
  725. effectiveTTar.append('有效铁。')
  726. else:
  727. effectiveT.append('')
  728. effectiveTTar.append('')
  729. #(4)有效锰在[5, 200]范围之外的,存疑;
  730. if (not pd.isna(row['有效锰mg/kg']) and row['有效锰mg/kg'] <5) or (not pd.isna(row['有效锰mg/kg']) and row['有效锰mg/kg'] >200) :
  731. effectiveM.append('有效锰:超阈值。')
  732. effectiveMTar.append('有效锰。')
  733. else:
  734. effectiveM.append('')
  735. effectiveMTar.append('')
  736. #(5)有效铜在[0.1, 8]范围之外的,存疑;
  737. if ( not pd.isna(row['有效铜mg/kg']) and row['有效铜mg/kg'] <0.1) or (not pd.isna(row['有效铜mg/kg']) and row['有效铜mg/kg'] >8):
  738. effectiveCu.append('有效铜:超阈值。')
  739. effectiveCuTar.append('有效铜。')
  740. else:
  741. effectiveCu.append('')
  742. effectiveCuTar.append('')
  743. #(6)有效锌在[0.1, 10] 范围之外的,存疑;
  744. if (not pd.isna(row['有效锌mg/kg']) and row['有效锌mg/kg'] <0.1) or (not pd.isna(row['有效锌mg/kg']) and row['有效锌mg/kg'] >10):
  745. effectiveX.append('有效锌:超阈值。')
  746. effectiveXTar.append('有效锌。')
  747. else:
  748. effectiveX.append('')
  749. effectiveXTar.append('')
  750. #(7)有效硼在[0.1, 2] 范围之外的,存疑;
  751. if (not pd.isna(row['有效硼mg/kg']) and row['有效硼mg/kg'] <0.1) or (not pd.isna(row['有效硼mg/kg']) and row['有效硼mg/kg'] >2):
  752. effectiveP.append('有效硼:超阈值。')
  753. effectivePTar.append('有效硼。')
  754. else:
  755. effectiveP.append('')
  756. effectivePTar.append('')
  757. #(8)有效钼在[0.03, 1]范围之外的,存疑。
  758. if (not pd.isna(row['有效钼mg/kg']) and row['有效钼mg/kg'] <0.03) or (not pd.isna(row['有效钼mg/kg']) and row['有效钼mg/kg'] >1):
  759. effectiveMu.append('有效钼:超阈值。')
  760. effectiveMutar.append('有效钼。')
  761. else:
  762. effectiveMu.append('')
  763. effectiveMutar.append('')
  764. resData = pd.DataFrame({
  765. '审核结果': pd.Series(effectiveL) + pd.Series(effectiveT) + pd.Series(effectiveG) +
  766. pd.Series(effectiveM) + pd.Series(effectiveCu) + pd.Series(effectiveX) + pd.Series(effectiveP) + pd.Series(
  767. effectiveMu),
  768. '异常指标': pd.Series(effectiveLTar) + pd.Series(effectiveTTar) + pd.Series(effectiveGTar) +
  769. pd.Series(effectiveMTar) + pd.Series(effectiveCuTar) + pd.Series(effectiveXTar) + pd.Series(effectivePTar) + pd.Series(
  770. effectiveMutar)
  771. })
  772. return resData
  773. except Exception as err:
  774. print('土壤指标数据判断出错!请检查soil_metal中判断内容', err)
  775. # 表14 土壤重金属判断
  776. # 这里风险值和管控值判断 单独写两个函数
  777. # 风险值
  778. def risk_value(arr):
  779. unnormalValue = []
  780. for index, row in arr.iterrows():
  781. str = ''
  782. if row['编号'][6:10] == '0101': # 水田
  783. # 镉
  784. if (row['pH'] <= 5.5 and row['镉mg/kg'] > 0.3) or (
  785. row['pH'] > 5.5 and row['pH']<= 6.5 and row['镉mg/kg'] > 0.4) or (
  786. row['pH'] >6.5 and row['pH'] <=7.5 and row['镉mg/kg'] > 0.6) or (
  787. row['pH'] > 7.5 and row['镉mg/kg'] > 0.8):
  788. str += '镉超污染风险值筛选值。'
  789. # 汞
  790. if (row['pH'] <= 5.5 and row['汞mg/kg'] > 0.5) or (
  791. row['pH'] > 5.5 and row['pH'] <= 6.5 and row['汞mg/kg'] > 0.5) or (
  792. row['pH'] > 6.5 and row['pH'] <= 7.5 and row['汞mg/kg'] > 0.6) or (
  793. row['pH'] > 7.5 and row['汞mg/kg'] > 1):
  794. str += '汞超污染风险值筛选值。'
  795. # 砷
  796. if (row['pH'] <= 5.5 and row['砷mg/kg'] >30) or (
  797. row['pH'] > 5.5 and row['pH'] <= 6.5 and row['砷mg/kg'] > 30) or (
  798. row['pH'] > 6.5 and row['pH'] <= 7.5 and row['砷mg/kg'] > 25) or (
  799. row['pH'] > 7.5 and row['砷mg/kg'] > 20):
  800. str += '砷超污染风险值筛选值。'
  801. # 铅
  802. if (row['pH'] <= 5.5 and row['铅mg/kg'] > 80) or (
  803. row['pH'] > 5.5 and row['pH'] <= 6.5 and row['铅mg/kg'] > 100) or (
  804. row['pH'] > 6.5 and row['pH'] <= 7.5 and row['铅mg/kg'] > 140) or (
  805. row['pH'] > 7.5 and row['铅mg/kg'] > 240):
  806. str += '铅超污染风险值筛选值。'
  807. # 铬
  808. if (row['pH'] <= 5.5 and row['铬mg/kg'] > 250) or (
  809. row['pH'] > 5.5 and row['pH'] <= 6.5 and row['铬mg/kg'] > 250) or (
  810. row['pH'] > 6.5 and row['pH'] <= 7.5 and row['铬mg/kg'] > 300) or (
  811. row['pH'] > 7.5 and row['铬mg/kg'] >350):
  812. str += '铬超污染风险值筛选值。'
  813. else:
  814. # 镉
  815. if (row['pH'] <= 5.5 and row['镉mg/kg'] > 0.3) or (
  816. row['pH'] > 5.5 and row['pH']<= 6.5 and row['镉mg/kg'] > 0.3) or (
  817. row['pH'] >6.5 and row['pH'] <=7.5 and row['镉mg/kg'] > 0.3) or (
  818. row['pH'] > 7.5 and row['镉mg/kg'] > 0.6):
  819. str += '镉超污染风险值筛选值。'
  820. # 汞
  821. if (row['pH'] <= 5.5 and row['汞mg/kg'] > 1.3) or (
  822. row['pH'] > 5.5 and row['pH'] <= 6.5 and row['汞mg/kg'] > 1.8) or (
  823. row['pH'] > 6.5 and row['pH'] <= 7.5 and row['汞mg/kg'] > 2.4) or (
  824. row['pH'] > 7.5 and row['汞mg/kg'] > 3.4):
  825. str += '汞超污染风险值筛选值。'
  826. # 砷
  827. if (row['pH'] <= 5.5 and row['砷mg/kg'] > 40) or (
  828. row['pH'] > 5.5 and row['pH'] <= 6.5 and row['砷mg/kg'] > 40) or (
  829. row['pH'] > 6.5 and row['pH'] <= 7.5 and row['砷mg/kg'] > 30) or (
  830. row['pH'] > 7.5 and row['砷mg/kg'] > 25):
  831. str += '砷超污染风险值筛选值。'
  832. # 铅
  833. if (row['pH'] <= 5.5 and row['铅mg/kg'] > 70) or (
  834. row['pH'] > 5.5 and row['pH'] <= 6.5 and row['铅mg/kg'] > 90) or (
  835. row['pH'] > 6.5 and row['pH'] <= 7.5 and row['铅mg/kg'] > 120) or (
  836. row['pH'] > 7.5 and row['铅mg/kg'] > 170):
  837. str += '铅超污染风险值筛选值。'
  838. # 铬
  839. if (row['pH'] <= 5.5 and row['铬mg/kg'] > 150) or (
  840. row['pH'] > 5.5 and row['pH'] <= 6.5 and row['铬mg/kg'] > 150) or (
  841. row['pH'] > 6.5 and row['pH'] <= 7.5 and row['铬mg/kg'] > 200) or (
  842. row['pH'] > 7.5 and row['铬mg/kg'] > 250):
  843. str += '铬超污染风险值筛选值。'
  844. if (row['pH'] <= 5.5 and row['镍mg/kg'] > 60) or (
  845. row['pH'] > 5.5 and row['pH'] <= 6.5 and row['镍mg/kg'] > 70) or (
  846. row['pH'] > 6.5 and row['pH'] <= 7.5 and row['镍mg/kg'] > 100) or (
  847. row['pH'] > 7.5 and row['镍mg/kg'] > 190):
  848. str += '镍超污染风险值筛选值。'
  849. unnormalValue.append(str)
  850. return unnormalValue
  851. # 管制值
  852. def control_value(arr):
  853. unnormalValue = []
  854. for index, row in arr.iterrows():
  855. str = ''
  856. # 镉
  857. if (row['pH'] <= 5.5 and row['镉mg/kg'] > 1.5) or (
  858. row['pH'] > 5.5 and row['pH'] <= 6.5 and row['镉mg/kg'] > 2) or (
  859. row['pH'] > 6.5 and row['pH'] <= 7.5 and row['镉mg/kg'] > 3) or (
  860. row['pH'] > 7.5 and row['镉mg/kg'] > 4):
  861. str += '镉超污染风险值管制值。'
  862. # 汞
  863. if (row['pH'] <= 5.5 and row['汞mg/kg'] > 2) or (
  864. row['pH'] > 5.5 and row['pH'] <= 6.5 and row['汞mg/kg'] > 2.5) or (
  865. row['pH'] > 6.5 and row['pH'] <= 7.5 and row['汞mg/kg'] > 4) or (
  866. row['pH'] > 7.5 and row['汞mg/kg'] > 6):
  867. str += '汞超污染风险值管制值。'
  868. # 砷
  869. if (row['pH'] <= 5.5 and row['砷mg/kg'] > 200) or (
  870. row['pH'] > 5.5 and row['pH'] <= 6.5 and row['砷mg/kg'] > 150) or (
  871. row['pH'] > 6.5 and row['pH'] <= 7.5 and row['砷mg/kg'] > 120) or (
  872. row['pH'] > 7.5 and row['砷mg/kg'] > 100):
  873. str += '砷超污染风险值管制值。'
  874. # 铅
  875. if (row['pH'] <= 5.5 and row['铅mg/kg'] >400) or (
  876. row['pH'] > 5.5 and row['pH'] <= 6.5 and row['铅mg/kg'] > 500) or (
  877. row['pH'] > 6.5 and row['pH'] <= 7.5 and row['铅mg/kg'] > 700) or (
  878. row['pH'] > 7.5 and row['铅mg/kg'] > 1000):
  879. str += '铅超污染风险值管制值。'
  880. # 铬
  881. if (row['pH'] <= 5.5 and row['铬mg/kg'] > 800) or (
  882. row['pH'] > 5.5 and row['pH'] <= 6.5 and row['铬mg/kg'] > 850) or (
  883. row['pH'] > 6.5 and row['pH'] <= 7.5 and row['铬mg/kg'] > 1000) or (
  884. row['pH'] > 7.5 and row['铬mg/kg'] > 1300):
  885. str += '铬超污染风险值管制值。'
  886. unnormalValue.append(str)
  887. return unnormalValue
  888. def last_metal(arr):
  889. try:
  890. totalGe = [] # 总镉在范围之外
  891. totalGo = [] # 总汞在范围之外
  892. totalShen = [] # 总砷在范围之外
  893. totalPb = [] # 总铅在范围之外
  894. totalG = [] # 总铬在范围之外
  895. totalN = [] # 总镍在范围之外
  896. # 异常指标
  897. totalGeTar = [] # 总镉
  898. totalGoTar = [] # 总汞
  899. totalShenTar = [] # 总砷
  900. totalPbTar = [] # 总铅
  901. totalGTar = [] # 总铬
  902. totalNTar = [] # 总镍
  903. # (1)超过风险筛选值,提示关注
  904. overValue = risk_value(arr) # 超过风险筛选值
  905. # (2)超过风险管控值,提示重点关注
  906. overLimit = control_value(arr) # 超过风险管控值
  907. for index, row in arr.iterrows():
  908. # (3)总镉在[0.03, 0.3]范围之外的,存疑
  909. if row['镉mg/kg'] < 0.03 or row['镉mg/kg'] > 0.3:
  910. totalGe.append('总镉:超阈值。')
  911. totalGeTar.append('总镉。')
  912. else:
  913. totalGe.append('')
  914. totalGeTar.append('')
  915. # (4)总汞在[0.01, 0.3]范围之外的,存疑
  916. if row['汞mg/kg'] < 0.01 or row['汞mg/kg'] > 0.3:
  917. totalGo.append('总汞:超阈值。')
  918. totalGoTar.append('总汞。')
  919. else:
  920. totalGo.append('')
  921. totalGoTar.append('')
  922. # (5)总砷在[0.5, 30]范围之外的,存疑
  923. if row['砷mg/kg'] < 0.5 or row['砷mg/kg'] > 30:
  924. totalShen.append('总砷:超阈值。')
  925. totalShenTar.append('总砷。')
  926. else:
  927. totalShen.append('')
  928. totalShenTar.append('')
  929. # (6)总铅在[2, 100]范围之外的,存疑
  930. if row['铅mg/kg'] < 2 or row['铅mg/kg'] > 100:
  931. totalPb.append('总铅:超阈值。')
  932. totalPbTar.append('总铅。')
  933. else:
  934. totalPb.append('')
  935. totalPbTar.append('')
  936. # (7)总铬在[0.4, 200]范围之外的,存疑
  937. if row['铬mg/kg'] < 0.4 or row['铬mg/kg'] > 200:
  938. totalG.append('总铬:超阈值。')
  939. totalGTar.append('总铬。')
  940. else:
  941. totalG.append('')
  942. totalGTar.append('')
  943. # (8)总镍在[0.3, 100]范围之外的,存疑
  944. if row['镍mg/kg'] < 0.3 or row['镍mg/kg'] > 100:
  945. totalN.append('总镍:超阈值。')
  946. totalNTar.append('总镍。')
  947. else:
  948. totalN.append('')
  949. totalNTar.append('')
  950. resData = pd.DataFrame({
  951. '审核结果': pd.Series(overValue) + pd.Series(overLimit) + pd.Series(totalGe) +
  952. pd.Series(totalGo) + pd.Series(totalShen) + pd.Series(totalPb) + pd.Series(
  953. totalG) + pd.Series(totalN),
  954. '异常指标': pd.Series(totalGeTar) + pd.Series(totalGoTar) + pd.Series(totalShenTar) + pd.Series(totalPbTar) + pd.Series(
  955. totalGTar) + pd.Series(totalNTar)
  956. })
  957. return resData
  958. except Exception as err:
  959. print('土壤重金属指标数据判断出错!请检查last_metal中判断内容', err)