1、编写原因 2、实现功能 3、部分代码预览 4、写在最后
基于SL773-2018计算土壤流失量的Python实现
1、编写原因
随着SL773-2018《生产建设项目土壤流失量测算导则》的发布,越来越多的水土保持方案需要用到其中公式内容进行土壤流失量预测。公式数量多,计算过程较为繁琐,如果能用批量编写好的函数运行,在减少运算时间的同时,也更容易检查(主要也是Excel水平渣渣)。于是用Python(3.9.4)代码实现自动计算并生成表格的想法应运而生。
本文用以下公式作为🌰:
植被破坏型一般扰动地表土壤流失量
①计算公式
目前R只考虑通过查表取值代入的计算方法,在之后的版本会慢慢完善。
此版代码包含的计算公式还有5个,在此不一一列出。
2、实现功能
通过一个小动图展示一下:
3、部分代码预览
(1)函数代码示例:
defMyz():
R,K,B,E,Tv =map(float,input("请依次(共5)输入计算单元降雨侵蚀力因子R、土壤可蚀性因子K、植被覆盖因子B、工程措施因子E、耕作措施因子T,并以逗号隔开:").split(","))
lamda_x,a,m =map(float,input("请依次(共3)输入计算单元斜坡长度λx、坡度θ、坡长指数m,并以逗号隔开:").split(","))
aa=math.cos(math.radians(a))
lamda=lamda_x*aa
if lamda >=100:
lamda =100
Ly_v=(lamda/20)**m
aa_2 = math.sin(math.radians(a))
Sy_v=-1.5+17/(1+2.72**(2.3-6.1*aa_2))
S=float(input("请输入计算单元宽度(单位:m):"))
A =10**(-4)*S*lamda_x*aa
Myz_v=R*K*Ly_v*Sy_v*B*E*Tv*A
lst =[Myz_v,100*Myz_v,R,K,Ly_v,lamda,lamda_x,m,a,Sy_v,B,E,Tv,S,A]return lst
(2)定义的最终函数cal()代码如下:
Step1 先输入计算单元所在工程分区(如施工生产生活区)名称
defcal():... district =input("请输入计算单元所在工程分区名称:")
Step2 输入数字选择计算方法
begin =int(input("请输入数字选择对应计算方法:\n\t1.植被破坏型\n\t2.地表 翻扰型\n\t3.上方无来水开挖\n\t4.上方有来水开挖\n\t5.上方无来水堆积\n\t6.上方有来水堆积"))
Step3 用if条件判断句判断选择的计算方法,输入所需参数
if begin ==1:... variable =["土壤流失量","土壤侵蚀模数","降雨侵蚀力因子","土壤可蚀性因子","坡长因子","投影坡长","斜坡坡长","坡长指数","坡度","坡度因子","植被覆盖因子","工程措施因子","耕作措施因子","计算单元宽度","计算单元水平投影面积(㎡)"]... variable_v = Myz()...elif begin ==2:... variable_v = Myd()... variable =["土壤流失量","土壤侵蚀模数","降雨侵蚀力因子","翻扰后土壤可蚀性因子","土壤可蚀性因子","增加系数","坡长因子","投影坡长","斜坡坡长","坡长指数","坡度","坡度因子","植被覆盖因子","工程措施因子","耕作措施因子","计算单元宽度"," 计算单元水平投影面积(㎡)"]...elif begin ==3:... variable_v = Mkw()... variable =["土壤流失量","土壤侵蚀模数","无来水开挖面土石质因子","粉粒含量","黏粒含量","土体密度","降雨侵蚀力因子","无来水开挖面坡长因子","投影坡长","斜坡坡长","坡度","无来水开挖面坡度因子","计算单元宽度","计算单元水平投影面积(㎡)"]...elif begin ==4:... variable_v = Mky()... variable =["土壤流失量","土壤侵蚀模数","有来水开挖土质因子","粉粒含量","黏粒含量","土体密度","有来水开挖面径流冲蚀力因子","上方单宽次来水总量","有来水 开挖面坡长因子","投影坡长","斜坡坡长","坡度","有来水开挖面坡度因子","计算单元宽 度","计算单元水平投影面积(㎡)"]...elif begin ==5:... variable_v = Mdw()... variable =["土壤流失量","土壤侵蚀模数","土石质因子","土石质因子系数"," 土石质因子系数","土体砾石含量","降雨侵蚀力因子","形态因子","坡长因子","投影坡长","斜坡坡长","坡长因子系数","坡度","坡度因子","坡度因子系数","计算单元宽度","计算单元水平投影面积(㎡)"]...else:... variable_v = Mdy()... variable =["土壤流失量","土壤侵蚀模数","有来水堆积土石质因子","有来水堆积土石质因子系数","有来水堆积土石质因子系数","土体砾石含量","有来水堆积体径流冲 蚀力因子","上方单宽次来水总量","有来水堆积体坡长因子","投影坡长","斜坡坡长","有 来水堆积体坡长因子系数","坡度","有来水堆积坡度因子","有来水堆积体坡度因子系数","计算单元宽度","计算单元水平投影面积(㎡)"]
Step4 计算结果及因子名称生成xlsx文件
import xlsxwriter
... directory =input("请输入保存路径,用/分级(示例:F:/20210405.xlsx):")... workbook=xlsxwriter.Workbook(directory,{'in_memory':True})... sheet1 = workbook.add_worksheet("chart")... bold=workbook.add_format()... bold.set_bold()... title =["项目","",district]... data =[variable,variable_v]... sheet1.write_row('A1',title,bold)... sheet1.write_column('A2',data[0])... sheet1.write_column('C2',data[1])... workbook.close()
Step5 生成表格预览
注:
①本次计算用到的参数为任意取值,可以根据实际情况调整;
②代码中用到math和XlsxWriter模块,需要先import,XlsxWirter如果没安装,则需打开cmd,pip3 install XlsxWriter。
4、写在最后
3年前学习过一些R语言,对代码的快、准、稳深有体会。相比起R这门偏向统计分析的语言,Python应用范围则更广泛些,如果能够掌握,会给工作、生活带来许多便利。
作为Python小白,编写代码时遇到了不少困难,主要是有些错误不知如何纠正,还是要特别感谢小伙伴帮忙debug~
除此之外,以下书籍和笔记也让我收获颇丰:
基础语法:《Python编程从入门到实践(第2版)》
xlsx表格写出:Python制作简单表格 by张晨chat
由于Python代码的逻辑与编写还在学习中,目前水平有限,本次的代码运行起来比较傻瓜。在下一个版本中,打算实现自动读写表格的功能,以避免多次输入重复参数,节省时间。
接下来会以需求驱动coding学习,不断精进自己的技术~也希望能与大家多多交流。
版权归原作者 wzr0926 所有, 如有侵权,请联系我们删除。