public.py 60 KB

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