[PYTHON] Display the integrated temperature for each field with Z-GIS

About this article

Z-GIS is a farming management system provided by JA Zen-Noh. This time, I will publish the program that displays the integrated temperature by changing the data managed by Z-GIS from the outside of the system with an application created in Python.

The code is posted on github https://github.com/keinafarm/AccTemp.

About Z-GIS

Although Z-GIS is a farming management system, it has a very unique mechanism and manages data using Excel files. Therefore, if you can change the Excel file directly, you can rewrite the data managed by Z-GIS. Of course, it is also possible with Excel.

In fact, Z-GIS itself is not specialized in farming (there is a function to support farming management), and the basic functions of the system are the data on the map and the Excel file. It's just associating the data.

Originally, it is composed of farmland and data to manage it, but without being bound by it, you can manage the traps you set up as shown in the example below. cats.jpg

You can make a gourmet map. cats2.jpg

That's why Z-GIS can be used not only for farmers but also for a wide variety of purposes. (Whether Zen-Noh wants it is another story)

About AccTemp

Motivation for creation

AccTemp created this time is a program to calculate the integrated temperature and reflect it in the Excel file. For personal use, I made it to manage the harvest time of rice. For rice, the optimum harvest time can be known from the integrated temperature from the earing season. For this reason, by registering the date when the earing season is reached for each field, it is possible to know what time the harvest time is.

Excel file format

The Excel file format in Z-GIS has the following rules

--Cell A1 contains the character string "\ _ \ _ xl $ gis \ _ \ _" (Other character strings may be included depending on the purpose of the sheet) --For cells after A2, one of the attribute values ​​of the column value (c: character string, n: integer, f: decimal, d: date) is entered. --B1 cell is "." --Column name is entered in cells after B2 --It's inconvenient if you don't format the date column to date

In general, it is more convenient to create a file with Z-GIS first, specify the position with polygons, save it once, and then add columns.

In AccTemp created this time, data is manipulated according to the following rules.

--Only the sheets whose sheet name starts with "Integrated temperature" are applicable. -Columns with column names of "Start date", "End date", "Target integrated temperature", "Current integrated temperature", "Target achievement", and "Predicted end date" are targeted for operation. --Start date: The first day to start adding as the integrated temperature (in the example of harvest time, it is the date of the panicle season) --End date: The date when the totalized temperature is finished (if a date is specified, the total is calculated up to the specified date, but if this cell is blank, the totalized temperature up to the previous day is calculated). --Target integrated temperature: If this is specified, "target achievement" and "prediction end date" will be calculated. --Current integrated temperature: Integrated temperature from the start date (if the end date is specified, until that date) --Target achievement: Current integrated temperature / Target integrated temperature --Prediction end date: The date when the integrated temperature is predicted to reach the target integrated temperature

The forecast end date is the date from the start date until the integrated temperature reaches the target integrated temperature, and if it has reached the previous day, the date of arrival will be recorded. If it has not arrived by the previous day, the arrival date is predicted based on the normal temperature.

How to start AccTemp

Since it is not converted to an Exe file, it will be started from the command line. The command format is as follows.

python AccTemp Excel file name Prefecture name Location name

The prefecture name and location name will be the name of the prefecture and location on the Past Meteorological Data Download site of the Japan Meteorological Agency.

I refuse for the time being

This is a point to note in this program.

――Since this program was created for my own use, I only handle errors such as user typos in my mood. ――Weather data is obtained from the Japan Meteorological Agency by scraping. We are not responsible for that, so please do so at your own risk. We do not accept the use of those who are not responsible. --More than that, we are not responsible for any problems that may occur as a result of using this program. If you are uneasy, please analyze the source and use it after you are fully satisfied. ――By the way, we have not conducted tests at points other than “Kochi Prefecture” and “Kubokawa”. ――In fact, the test cases are not exhaustive. ――I'm not happy

I'm using python or something

This program is developed on windows. Python is 3.9.1. The packages and versions used are as follows

package version
cssselect 1.1.0
et-xmlfile 1.0.1
jdcal 1.4.
lxml 4.6.2
numpy 1.20.0rc2
openpyxl 3.0.5
pandas 1.2.0rc0
pip 20.3.3
python-dateutil 2.8.1
pytz 2020.5
setuptools 51.1.0.post2020122
six 1.15.0

Class structure

This program mainly consists of 4 classes

--AcTemp: Main class --ATWorkBook: Excel File operation unit --TemperatureCalculator: Integrated temperature calculator --MeteorologicalAgency: Get average temperature and average temperature from the Japan Meteorological Agency website

AccTemp: Main class

AccTemp performs the following processing

--Get file name, prefecture name, location name from command line --Create an ATWorkBook object with the specified file name and read the Excel file --Determine the period to be acquired from the Japan Meteorological Agency from the start date and end date described in the Excel file. --Calculate the integrated temperature with the TemperatureCalculator object for the determined period --Overwrite Excel data to file with ATWorkBook object

ATWorkBook: Excel File operation unit

I am using openpyxl to operate Excel files. The job of this class is to make the data in the Excel file look like class attributes. ATWorkBook has a list of multiple AT Sheets. This list can be read as the sheets_list attribute.

The data provided by sheets_list is an ATSheet object. Manage data on a sheet-by-sheet basis. You can get the list of ATRow objects with data_list of ATSheet objects. The ATRow object manages data for each row.

The class attribute of the ATRow object corresponds to the cells of "Start date", "End date", "Target integrated temperature", "Current integrated temperature", "Target achievement", "Predicted end date", and the class attribute Accessing to is the same as accessing Excel data.

However, if the column corresponding to the class attribute does not exist, None will be returned whenever the class attribute is read. Also, writing to that class attribute is ignored. (For this reason, even if you set a value for this class attribute, it cannot be read. I think that this specification has failed a little)

With ATWorkBook.flash (), you can reflect the value in the actual Excel file.

TemperatureCalculator: Integrated temperature calculator

The job of this class is to calculate the integrated temperature for a specified period and to use both the average temperature and the normal temperature to find the "predicted end date" to reach the target integrated temperature.

This class holds the MeteorologicalAgency object described below. Since it is not good to bring the temperature data at the same time point from the Meteorological Agency many times, this class is designed to return the already generated object if the already loaded period of the same area is specified. ..

In this specification, AccTemp actually calculates the necessary and sufficient period and generates the Temperature Calculator, so this function will not be used.

Inside the TemperatureCalculator class, the data is stored in Pandas format. The reason is that I thought I'd do something a little more, but in reality I'm only using it for integration, so I'm wondering if it was just a list.

MeteorologicalAgency: Get average and normal temperatures from the Japan Meteorological Agency website

This part was borrowed from barusan/jmadata.py and remodeled. The main changes are "optionNumList": ' [["op1", 0]]' As a result, we have made it possible to obtain the average value.

MeteorologicalAgency.get_temperature_list You can get the acquired temperature data with, but at this point it is a Pandas DataFrame.

Operation result

The result of operating this program is shown.

cats3.jpg

This is done after copying the data in test2.xlsx to test1.xlsx python AccTemp test1.xlsx Kochi Kubokawa It is the result of executing.

If you select the label display from Z-GIS and display the "Current integrated temperature", "Target achievement level", and "Prediction end date" as labels, the optimum harvest time will be displayed for each field. (This time, since the past weather data and the actual harvest date are included, the day "I wish I had harvested on this day if it was true" and the target integrated temperature at the time of harvesting the rice It is a numerical value that shows the ratio of whether it was different)

At the end

In fact, Z-GIS has functions such as acquiring temperature, calculating integrated temperature, and predicting the achievement date. Moreover, it can be obtained with a 1km mesh instead of scraping from the Japan Meteorological Agency.

However, there is no way to connect the data to multiple fields.

The truth is, I want a function in Z-GIS to do that.

Recommended Posts

Display the integrated temperature for each field with Z-GIS
Switch the package to be installed for each environment with poetry
Search for files with the specified extension
Display Python 3 in the browser with MAMP
Limit ssh with iptables for each user
The third night of the loop with for
Display markers above the border with matplotlib
The second night of the loop with for
[IOS] Change the display time for each frame of GIF animation in Pythonista3.
Mathematics memorandum to keep up with the field # 4
Use logger with Python for the time being
I played with Floydhub for the time being
Mathematics memorandum to keep up with the field # 1
Mathematics memorandum to keep up with the field # 2
Rollback DB for each test with Flask + SQLAlchemy
Mathematics memorandum to keep up with the field # 3
Display the image after Data Augmentation with Pytorch
[Don't refer to 04.02.17] Display the temperature sensor on a real-time graph with rasberry pi 3.
How to set the development environment for each project with VSCode + Python extension + Miniconda
Save the output of conditional GAN for each class ~ With cGAN implementation by PyTorch ~