1、程序化哈格里夫斯(Hargreaves)公式;
2、实现每日定时任务,计算作物蒸腾量,并存储数据库
| | |
| | | import com.dy.pipIrrGlobal.pojoRm.RmSoilLast; |
| | | import com.dy.pipIrrGlobal.pojoRm.RmWeatherHistory; |
| | | import com.dy.pipIrrGlobal.voRm.VoWeather; |
| | | import com.dy.pipIrrGlobal.voRm.VoWeatherMaxMinTmp; |
| | | import org.apache.ibatis.annotations.Mapper; |
| | | import org.apache.ibatis.annotations.Param; |
| | | |
| | | import java.util.List; |
| | | import java.util.Map; |
| | |
| | | */ |
| | | List<VoWeather> selectSome(Map<?, ?> params); |
| | | |
| | | List<VoWeatherMaxMinTmp> selectMaxMinTemperature(@Param("weatherId") Long weatherId, @Param("maxId") Long maxId, @Param("minId") Long minId); |
| | | |
| | | int updateByPrimaryKeySelective(RmWeatherHistory record); |
| | | |
| | | int updateByPrimaryKey(RmWeatherHistory record); |
| New file |
| | |
| | | package com.dy.pipIrrGlobal.voRm; |
| | | |
| | | import com.alibaba.fastjson2.annotation.JSONField; |
| | | import com.alibaba.fastjson2.writer.ObjectWriterImplToString; |
| | | import com.fasterxml.jackson.annotation.JsonPropertyOrder; |
| | | import lombok.Data; |
| | | |
| | | /** |
| | | * @Author: liurunyu |
| | | * @Date: 2025/8/18 16:48 |
| | | * @Description |
| | | */ |
| | | @Data |
| | | @JsonPropertyOrder({ |
| | | "id", "weatherId", |
| | | "maxAirTemperature", "minAirTemperature" |
| | | }) |
| | | public class VoWeatherMaxMinTmp { |
| | | /** |
| | | * 气象站ID |
| | | */ |
| | | public Long weatherId; |
| | | |
| | | |
| | | /** |
| | | * 最高温度 |
| | | */ |
| | | public Double maxAirTemperature ; |
| | | |
| | | |
| | | /** |
| | | * 最低温度 |
| | | */ |
| | | public Double minAirTemperature ; |
| | | } |
| | |
| | | </if> |
| | | </trim> |
| | | </select> |
| | | |
| | | |
| | | <!--根据指定条件查询温度的最大值与最小值--> |
| | | <select id="selectMaxMinTemperature" resultType="com.dy.pipIrrGlobal.voRm.VoWeatherMaxMinTmp"> |
| | | SELECT max(air_temperature) maxTmp, min(air_temperature) minTmp |
| | | FROM rm_weather_history tb |
| | | <where> |
| | | <if test="weatherId != null"> |
| | | AND tb.weather_id = #{weatherId} |
| | | </if> |
| | | <if test="startId != null"> |
| | | AND tb.id >= #{startId} |
| | | </if> |
| | | <if test="endId != null"> |
| | | AND tb.id <= #{endId} |
| | | </if> |
| | | </where> |
| | | </select> |
| | | |
| | | <delete id="deleteByPrimaryKey" parameterType="java.lang.Long"> |
| | | <!--@mbg.generated--> |
| | | delete from rm_weather_history |
| New file |
| | |
| | | package com.dy.pipIrrModel.modelCalculate; |
| | | |
| | | import net.objecthunter.exp4j.Expression; |
| | | import net.objecthunter.exp4j.ExpressionBuilder; |
| | | |
| | | /** |
| | | * @Author: liurunyu |
| | | * @Date: 2025/8/18 14:51 |
| | | * @Description 哈格里夫斯(Hargreaves)公式 计算作物实际蒸散量(mm/day) |
| | | */ |
| | | public class Hargreaves { |
| | | |
| | | /** |
| | | * 计算弧度 |
| | | * 弧度=角度× π/180 |
| | | * @param lat 地理纬度 |
| | | * @return |
| | | */ |
| | | public static Double rad(Double lat) { |
| | | Expression expression = new ExpressionBuilder("x * pi / 180") |
| | | .variables("x") |
| | | .build(); |
| | | // 设置变量值 |
| | | expression.setVariable("x", lat); |
| | | Double result = expression.evaluate(); |
| | | return result ; |
| | | } |
| | | |
| | | /** |
| | | * 太阳磁偏角 |
| | | * @param dayIndex 为年内某天的日序数(比如1月1日为1,12月31日为365或366) |
| | | * @return |
| | | */ |
| | | public static Double sunMagnetismAngular(Integer dayIndex){ |
| | | Expression expression = new ExpressionBuilder("0.409 * sin(((2 * pi) / 365) * x - 1.39)") |
| | | .variables("x") |
| | | .build(); |
| | | // 设置变量值 |
| | | expression.setVariable("x", dayIndex); |
| | | Double result = expression.evaluate(); |
| | | return result ; |
| | | } |
| | | |
| | | /** |
| | | * 日地间相对距离的倒数 |
| | | * @param dayIndex 为年内某天的日序数(比如1月1日为1,12月31日为365或366) |
| | | * @return |
| | | */ |
| | | public static Double sunEarthDistance(Integer dayIndex){ |
| | | Expression expression = new ExpressionBuilder("1 + (0.033 * cos(((2 * pi) / 365) * x))") |
| | | .variables("x") |
| | | .build(); |
| | | // 设置变量值 |
| | | expression.setVariable("x", dayIndex); |
| | | Double result = expression.evaluate(); |
| | | return result ; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 太阳时角 |
| | | * 0.409 * sin(2 * pi / 365 * x - 1.39) |
| | | * @param fai 地理弧度 |
| | | * @param sunMagnetismAngular 太阳磁偏角 |
| | | * @return |
| | | */ |
| | | public static Double sunTimeAngular(Double fai, Double sunMagnetismAngular){ |
| | | Expression expression = new ExpressionBuilder("acos(-tan(x) * tan(y))") |
| | | .variables("x", "y") |
| | | .build(); |
| | | // 设置变量值 |
| | | expression.setVariable("x", fai); |
| | | expression.setVariable("y", sunMagnetismAngular); |
| | | Double result = expression.evaluate(); |
| | | return result ; |
| | | } |
| | | |
| | | /** |
| | | * 计算天顶辐射 |
| | | * @param sunEarthDistance 日地间相对距离的倒数 |
| | | * @param sunTimeAngular 太阳时偏角 |
| | | * @param fai 地理弧度 |
| | | * @param sunMagnetismAngular 太阳磁偏角 |
| | | * @return |
| | | */ |
| | | public static Double zenithRadiation(Double sunEarthDistance, |
| | | Double sunTimeAngular, |
| | | Double fai, |
| | | Double sunMagnetismAngular){ |
| | | Expression expression = new ExpressionBuilder("((24 * 60) / pi) * 0.082 * a * ((b * sin(c) * sin(d)) + (cos(c) * cos(d) * sin(b)))") |
| | | .variables("a", "b", "c", "d") |
| | | .build(); |
| | | // 设置变量值 |
| | | expression.setVariable("a", sunEarthDistance); |
| | | expression.setVariable("b", sunTimeAngular); |
| | | expression.setVariable("c", fai); |
| | | expression.setVariable("d", sunMagnetismAngular); |
| | | Double result = expression.evaluate(); |
| | | return result ; |
| | | } |
| | | |
| | | /** |
| | | * 格里夫斯(Hargreaves)公式计算作物蒸散量(mm/day) |
| | | * @param kc 作物系数 |
| | | * @param maxT 一日内最高温度 |
| | | * @param maxT 一日内最高温度 |
| | | * @param zenithRadiation 天顶辐射 |
| | | * @return |
| | | */ |
| | | public static Double ET0(Double kc, Double maxT, Double minT, Double zenithRadiation){ |
| | | Expression expression = new ExpressionBuilder("x * (0.0023 * (((a + b) / 2) + 17.8) * ((a + b)^(1/2)) * c * 0.408)") |
| | | .variables("x", "a", "b", "c") |
| | | .build(); |
| | | // 设置变量值 |
| | | expression.setVariable("x", kc); |
| | | expression.setVariable("a", maxT); |
| | | expression.setVariable("b", minT); |
| | | expression.setVariable("c", zenithRadiation); |
| | | Double result = expression.evaluate(); |
| | | return result ; |
| | | } |
| | | |
| | | public static void main(String[] args) { |
| | | Double lat = 38.561976140977116 ; |
| | | Integer dayIndex = 180 ; |
| | | Double kc = 0.41 ;//作物系数 |
| | | Double maxT = 40.1 ;//一日内最高温度 |
| | | Double minT = 40.1 ;//一日内最低温度 |
| | | |
| | | Double fai = rad(lat); |
| | | System.out.println(fai); |
| | | |
| | | Double sunMagnetismAngular = sunMagnetismAngular(dayIndex); |
| | | System.out.println(sunMagnetismAngular); |
| | | |
| | | Double sunEarthDistance = sunEarthDistance(dayIndex); |
| | | System.out.println(sunEarthDistance); |
| | | |
| | | Double sunTimeAngular = sunTimeAngular(fai, sunMagnetismAngular); |
| | | System.out.println(sunTimeAngular); |
| | | |
| | | Double zenithRadiation = zenithRadiation(sunEarthDistance, sunTimeAngular, fai, sunMagnetismAngular); |
| | | System.out.println(zenithRadiation); |
| | | |
| | | Double et0 = ET0(kc, maxT, minT, zenithRadiation); |
| | | System.out.println(et0); |
| | | } |
| | | |
| | | } |
| New file |
| | |
| | | package com.dy.pipIrrModel.modelCalculate; |
| | | |
| | | import com.dy.common.util.DateTime; |
| | | import com.dy.pipIrrGlobal.pojoMd.MdEt0; |
| | | import com.dy.pipIrrGlobal.voMd.VoCrops; |
| | | import com.dy.pipIrrGlobal.voPr.VoWeather; |
| | | import com.dy.pipIrrGlobal.voRm.VoWeatherMaxMinTmp; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.context.annotation.Scope; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * @Author: liurunyu |
| | | * @Date: 2025/8/18 16:26 |
| | | * @Description |
| | | */ |
| | | @Slf4j |
| | | @Component(ModelCalculator.selfBeanName) |
| | | @Scope("prototype") //采用原型模式,每次请求新建一个实例对象 |
| | | public class ModelCalculator { |
| | | public static final String selfBeanName = "modelCalculator"; |
| | | |
| | | private ModelCalculatorSv sv ; |
| | | |
| | | @Autowired |
| | | public void setSv(ModelCalculatorSv sv){ |
| | | this.sv = sv ; |
| | | } |
| | | |
| | | public void execute(){ |
| | | List<VoCrops> crops = this.sv.selectAllCrops() ; |
| | | if(crops != null && crops.size() > 0){ |
| | | for (VoCrops crop : crops) { |
| | | if(crop.weatherId != null){ |
| | | try{ |
| | | VoWeather voWeather = this.sv.getWeather(crop.weatherId); |
| | | if(voWeather.lat != null){ |
| | | this.executeOnCrop(crop, voWeather); |
| | | } |
| | | }catch (Exception e){ |
| | | log.error("计算作物(id=" + crop.id + ")蒸腾数据时异常", e); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | private void executeOnCrop(VoCrops vo, VoWeather voWeather) throws Exception{ |
| | | String yesterday_ymd = DateTime.yesterday_yyyy_MM_dd(Integer.parseInt(DateTime.yyyy()), Integer.parseInt(DateTime.MM()), Integer.parseInt(DateTime.dd())) ; //昨天 |
| | | Double factor = this.getCropsFactor(vo, yesterday_ymd) ; |
| | | if(factor != null){ |
| | | //说明作物处于计算期(作物生长期)中 |
| | | List<VoWeatherMaxMinTmp> tmps = this.sv.selectYesterdayMaxMinTemperature(vo.weatherId) ; |
| | | if(tmps != null && tmps.size() > 0){ |
| | | VoWeatherMaxMinTmp voMmTmp = tmps.get(0);//只能有一条记录 |
| | | Double et0 = this.calculateEt0(yesterday_ymd, vo, voWeather, voMmTmp, factor) ;//计算蒸腾数据 |
| | | this.saveEt0(yesterday_ymd, vo, voWeather, voMmTmp, factor, et0) ; |
| | | } |
| | | } |
| | | } |
| | | private Double getCropsFactor(VoCrops vo, String yesterday) throws Exception{ |
| | | Double factor = null ; |
| | | if(vo.stopped != null && vo.stopped != 1){ |
| | | if(vo.startDt != null && vo.endDt != null){ |
| | | String start = DateTime.yyyy() + "-" + vo.startDt ; |
| | | Long days = DateTime.daysBetweenyyyy_MM_dd(yesterday, start) ; |
| | | if(days >= 0){ |
| | | if(vo.life4Start != null && vo.life4End != null){ |
| | | if(days >= vo.life4Start && days <= vo.life4End){ |
| | | factor = vo.life4Factor ; |
| | | } |
| | | if(days > vo.life4End){ |
| | | factor = null ; |
| | | } |
| | | } |
| | | if(vo.life3Start != null && vo.life3End != null){ |
| | | if(days >= vo.life3Start && days <= vo.life3End){ |
| | | factor = vo.life3Factor ; |
| | | } |
| | | } |
| | | if(vo.life2Start != null && vo.life2End != null){ |
| | | if(days >= vo.life2Start && days <= vo.life2End){ |
| | | factor = vo.life2Factor ; |
| | | } |
| | | } |
| | | if(vo.life1Start != null && vo.life1End != null){ |
| | | if(days >= vo.life1Start && days <= vo.life1End){ |
| | | factor = vo.life1Factor ; |
| | | } |
| | | if(days < vo.life1Start){ |
| | | factor = null ; |
| | | } |
| | | } |
| | | }else{ |
| | | factor = null ; |
| | | } |
| | | } |
| | | } |
| | | return factor ; |
| | | } |
| | | |
| | | private Double calculateEt0(String yesterday_ymd, VoCrops vo, VoWeather voWeather, VoWeatherMaxMinTmp voMmTmp, Double factor) throws Exception{ |
| | | Long days = DateTime.daysBetweenyyyy_MM_dd(yesterday_ymd, DateTime.yyyy() + "-01-01"); |
| | | Integer dayIndex = days.intValue() + 1 ; |
| | | |
| | | Double fai = Hargreaves.rad(voWeather.lat); |
| | | |
| | | Double sunMagnetismAngular = Hargreaves.sunMagnetismAngular(dayIndex); |
| | | |
| | | Double sunEarthDistance = Hargreaves.sunEarthDistance(dayIndex); |
| | | |
| | | Double sunTimeAngular = Hargreaves.sunTimeAngular(fai, sunMagnetismAngular); |
| | | |
| | | Double zenithRadiation = Hargreaves.zenithRadiation(sunEarthDistance, sunTimeAngular, fai, sunMagnetismAngular); |
| | | |
| | | Double et0 = Hargreaves.ET0(factor, voMmTmp.maxAirTemperature, voMmTmp.minAirTemperature, zenithRadiation); |
| | | return et0 ; |
| | | } |
| | | |
| | | private void saveEt0(String yesterday_ymd, VoCrops vo, VoWeather voWeather, VoWeatherMaxMinTmp voMmTmp, Double factor, Double et0)throws Exception{ |
| | | MdEt0 po = this.sv.selectByCropWeatherDt(vo.id, voWeather.id, yesterday_ymd); |
| | | if(po != null){ |
| | | Date yesterday = DateTime.dateFrom_yyyy_MM_dd(yesterday_ymd) ; |
| | | this.sv.saveEt0(vo.id, voWeather.id, yesterday, voMmTmp.maxAirTemperature, voMmTmp.minAirTemperature, factor, et0) ; |
| | | } |
| | | } |
| | | } |
| New file |
| | |
| | | package com.dy.pipIrrModel.modelCalculate; |
| | | |
| | | import com.dy.common.util.DateTime; |
| | | import com.dy.common.util.IDLongGenerator; |
| | | import com.dy.pipIrrGlobal.daoMd.MdCropsMapper; |
| | | import com.dy.pipIrrGlobal.daoMd.MdEt0Mapper; |
| | | import com.dy.pipIrrGlobal.daoPr.PrStWeatherMapper; |
| | | import com.dy.pipIrrGlobal.daoRm.RmWeatherHistoryMapper; |
| | | import com.dy.pipIrrGlobal.pojoMd.MdEt0; |
| | | import com.dy.pipIrrGlobal.voMd.VoCrops; |
| | | import com.dy.pipIrrGlobal.voPr.VoWeather; |
| | | import com.dy.pipIrrGlobal.voRm.VoWeatherMaxMinTmp; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.ibatis.annotations.Param; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * @Author: liurunyu |
| | | * @Date: 2025/8/18 16:34 |
| | | * @Description |
| | | */ |
| | | @Slf4j |
| | | @Service |
| | | public class ModelCalculatorSv { |
| | | private MdCropsMapper mdCropsDao ; |
| | | private PrStWeatherMapper prWeatherDao ; |
| | | private RmWeatherHistoryMapper rmWeatherHistoryDao; |
| | | private MdEt0Mapper mdEt0Dao; |
| | | |
| | | @Autowired |
| | | private void setDao(MdCropsMapper mdCropsDao, |
| | | PrStWeatherMapper prWeatherDao, |
| | | RmWeatherHistoryMapper rmWeatherHistoryDao, |
| | | MdEt0Mapper mdEt0Dao) { |
| | | this.mdCropsDao = mdCropsDao; |
| | | this.prWeatherDao = prWeatherDao; |
| | | this.rmWeatherHistoryDao = rmWeatherHistoryDao; |
| | | this.mdEt0Dao = mdEt0Dao; |
| | | } |
| | | /** |
| | | * 查询所有作物 |
| | | * @return 包含实体集合的结果对象 |
| | | */ |
| | | @SuppressWarnings("unchecked") |
| | | public List<VoCrops> selectAllCrops(){ |
| | | return this.mdCropsDao.selectAll() ; |
| | | } |
| | | |
| | | public VoWeather getWeather(Long weatherId) throws Exception{ |
| | | return this.prWeatherDao.selectOne(weatherId) ; |
| | | } |
| | | |
| | | public List<VoWeatherMaxMinTmp> selectYesterdayMaxMinTemperature(Long weatherId) throws Exception{ |
| | | String ymd = DateTime.yesterday_yyyy_MM_dd(Integer.parseInt(DateTime.yyyy()), Integer.parseInt(DateTime.MM()), Integer.parseInt(DateTime.dd())) ; //昨天 |
| | | int[] ymdGrp = DateTime.yyyy_MM_dd_2_ymdGroup(ymd) ; |
| | | Long startId = IDLongGenerator.generateOneDayStartId(ymdGrp[0], ymdGrp[1], ymdGrp[2]) ; |
| | | Long endId = IDLongGenerator.generateOneDayEndId(ymdGrp[0], ymdGrp[1], ymdGrp[2]) ; |
| | | return this.rmWeatherHistoryDao.selectMaxMinTemperature(weatherId, startId, endId); |
| | | } |
| | | |
| | | public MdEt0 selectByCropWeatherDt(Long cropId, Long weatherId, String yesterday) throws Exception{ |
| | | return this.mdEt0Dao.selectByCropWeatherDt(cropId, weatherId, yesterday) ; |
| | | } |
| | | |
| | | @Transactional(rollbackFor=Exception.class) |
| | | public int saveEt0(Long cropId, Long weatherId, Date yesterday, Double maxAirTemperature, Double minAirTemperature, Double factor, Double et0){ |
| | | MdEt0 po = new MdEt0(); |
| | | po.cropId = cropId ; |
| | | po.weatherId = weatherId ; |
| | | po.dt = yesterday ; |
| | | po.maxTmp = maxAirTemperature ; |
| | | po.minTmp = minAirTemperature ; |
| | | po.factor = factor ; |
| | | po.et0 = et0 ; |
| | | this.mdEt0Dao.insert(po) ; |
| | | return 0 ; |
| | | } |
| | | } |
| File was renamed from pipIrr-platform/pipIrr-web/pipIrr-web-model/src/main/java/com/dy/pipIrrModel/modelCalculate/CalculateJob.java |
| | |
| | | package com.dy.pipIrrModel.modelCalculate; |
| | | package com.dy.pipIrrModel.timingTask; |
| | | |
| | | import com.dy.common.multiDataSource.DataSourceContext; |
| | | import com.dy.common.schedulerTask.TaskJob; |
| | | import com.dy.common.springUtil.SpringContextUtil; |
| | | import com.dy.pipIrrModel.modelCalculate.ModelCalculator; |
| | | import org.apache.logging.log4j.LogManager; |
| | | import org.apache.logging.log4j.Logger; |
| | | import org.quartz.JobDataMap; |
| | |
| | | return ; |
| | | } |
| | | DataSourceContext.set(orgTag);//设置数据源 |
| | | ModelCalculator bean = (ModelCalculator)SpringContextUtil.getBean(ModelCalculator.selfBeanName); |
| | | if(bean != null){ |
| | | bean.execute(); |
| | | } |
| | | } |
| | | } |
| File was renamed from pipIrr-platform/pipIrr-web/pipIrr-web-model/src/main/java/com/dy/pipIrrModel/modelCalculate/ModelListener.java |
| | |
| | | package com.dy.pipIrrModel.modelCalculate; |
| | | package com.dy.pipIrrModel.timingTask; |
| | | |
| | | import com.dy.common.schedulerTask.SchedulerTaskSupport; |
| | | import com.dy.pipIrrGlobal.util.Org; |
| | |
| | | HashMap<String , Object> jobDataMap = new HashMap<String , Object>() ; |
| | | jobDataMap.put(orgKey, vo.tag) ; |
| | | SchedulerTaskSupport.addDailyJob(JobName + vo.tag, JobGroupName, CalculateJob.class, jobDataMap, startHour, startMinute ) ; |
| | | //SchedulerTaskSupport.addSecondlyJob(JobName + vo.tag, JobGroupName, TestJob.class, jobDataMap, 10, 10000, 0 ) ; |
| | | } |
| | | } |
| | | } |
| | |
| | | <version>2.18.0</version> |
| | | </dependency> |
| | | |
| | | <!-- java数学公式计算 --> |
| | | <dependency> |
| | | <groupId>net.objecthunter</groupId> |
| | | <artifactId>exp4j</artifactId> |
| | | <version>0.4.8</version> |
| | | </dependency> |
| | | |
| | | <!-- 测试 --> |
| | | <dependency> |
| | | <groupId>org.springframework.boot</groupId> |
| | |
| | | <mapstruct.version>1.5.5.Final</mapstruct.version> |
| | | <hutool-all.version>5.8.22</hutool-all.version> |
| | | <org-jdom2.version>2.0.6.1</org-jdom2.version> |
| | | <net.exp4j>0.4.8</net.exp4j> |
| | | |
| | | <spring-boot-maven-plugin.version>3.2.0</spring-boot-maven-plugin.version> |
| | | <maven-jar-plugin-plugin.version>3.3.0</maven-jar-plugin-plugin.version> |
| | |
| | | <type>pom</type> |
| | | <scope>import</scope> |
| | | </dependency> |
| | | |
| | | <!-- java数学公式计算 --> |
| | | <dependency> |
| | | <groupId>net.objecthunter</groupId> |
| | | <artifactId>exp4j</artifactId> |
| | | <version>0.4.8</version> |
| | | <type>pom</type> |
| | | <scope>import</scope> |
| | | </dependency> |
| | | |
| | | <!-- 测试 --> |
| | | <dependency> |
| | | <groupId>org.springframework.boot</groupId> |