当前位置:网站首页>(August 9, 2021) example exercise of air quality index calculation (I)
(August 9, 2021) example exercise of air quality index calculation (I)
2022-07-04 11:19:00 【Kosisi】
Air quality index calculation example exercise ( One )
1 computing method
1.1 Air quality sub index and corresponding pollutant project concentration limit
1.2 Calculation method of air quality sub index
Air quality sub index individual air quality index (IAQI): Air quality index of single pollutant .
Pollutant project P According to the formula (1) Calculation :
I A Q I P = I A Q I H i − I A Q I L o B P H i − B P L o ( C P − B P L o ) + I A Q I L o (1) IAQI_P=\frac{IAQI_{Hi}-IAQI_{Lo}}{BP_{Hi}-BP_{Lo}}(C_P-BP_{Lo})+IAQI_{Lo} \tag{1} IAQIP=BPHi−BPLoIAQIHi−IAQILo(CP−BPLo)+IAQILo(1)
In style :
I A Q I P IAQI_P IAQIP—— Pollutant project P Air quality sub index ;
C P C_P CP —— Pollutant project P Mass concentration value of ;
B P H i BP_{Hi} BPHi—— surface 1 China and C P C_P CP High value of similar pollutant concentration limit ;
B P L o BP_{Lo} BPLo—— surface 1 China and C P C_P CP Low value of similar pollutant concentration limit ;
I A Q I H i IAQI_{Hi} IAQIHi—— surface 1 China and B P H i BP_{Hi} BPHi Corresponding air quality sub index ;
I A Q I L o IAQI_{Lo} IAQILo—— surface 1 China and B P L o BP_{Lo} BPLo Corresponding air quality sub index .
This formula is equivalent to linear scaling , from [ BPL, BPH ] To [ IAQIL, IAQIH ]
1.3 Calculation method of air quality index
Air quality index air quality index(AQI): A dimensionless index that quantitatively describes the state of air quality .
A Q I = m a x { I A Q I 1 , I A Q I 2 , I A Q I 3 , . . . , I A Q I n } (2) AQI=max\left\{IAQI_1,IAQI_2,IAQI_3,...,IAQI_n\right\} \tag{2} AQI=max{ IAQI1,IAQI2,IAQI3,...,IAQIn}(2)
2 Method implementation steps
2.1 Linear scaling function
def cal_liner(iaqi_h, iaqi_l, bp_h, bp_l, cp):
''' Linear scaling function '''
iaqi_p = (iaqi_h - iaqi_l)/(bp_h - bp_l)*(cp-bp_l) + iaqi_l
return iaqi_p
2.2 PM Value correspondence IAQI Value calculation
def cal_pm(pm_val):
if 0 <= pm_val < 36:
pm_iaqi = cal_liner(50, 0, 35, 0, pm_val)
elif 36 <= pm_val < 76:
pm_iaqi = cal_liner(100, 50, 75, 35, pm_val)
elif 76 <= pm_val<116:
pm_iaqi = cal_liner(150, 100, 115, 75, pm_val)
elif 116 <= pm_val<151:
pm_iaqi = cal_liner(200, 150, 150, 115, pm_val)
else:
pass
return pm_iaqi
2.3 CO Value correspondence IAQI Value calculation
def cal_co(co_val):
if 0 <= co_val < 3:
co_iaqi = cal_liner(50, 0, 3, 0, co_val)
elif 3 <= co_val <5:
co_iaqi = cal_liner(100, 50, 4, 2, co_val)
else:
pass
return co_iaqi
2.4 AQI Calculation
def cal_aqi(param_list):
pm_val = param_list[0]
co_val = param_list[1]
pm_iaqi = cal_pm(pm_val)
co_iaqi = cal_co(co_val)
iaqi_list = []
iaqi_list.append(pm_iaqi)
iaqi_list.append(co_iaqi)
aqi = max(iaqi_list)
return aqi
2.5 The main program
def main():
print(' Please enter the following information , Split with spaces ')
input_str = input('PM2.5 CO : ')
str_list = input_str.split(' ')
pm_val = float(str_list[0])
co_val = float(str_list[1])
param_list = []
param_list.append(pm_val)
param_list.append(co_val)
aqi_final = cal_aqi(param_list)
print(' The air quality index is :{}'.format(aqi_final))
2.6 v1.0 Edition summary
This is the first version of the calculation program , All are regular grammar , What needs to be clarified is the setting of each function , Separate it , Practice hands ;
In fact, this document is also for practice Markdown Of , It's fun .
3 v2.0 Version improvements
- Read the acquired JSON Data files
- And will AQI front 5 Data output to file
3.1 JSON Brief knowledge
3.1.1 JSON Format
- JSON(JavaScript Object Notation) It's a lightweight data exchange format
- It can express and store complex data , Easy to read and understand
- The rules
1 . Data is stored in key value pairs
2 . Key value pairs are separated by commas
3 . Curly braces are used to hold objects composed of key value pairs of data
4 . Square brackets are used to hold an array of key value pairs , Separated by commas - Object of adoption 、 Key value pairs organized in an array can represent data of any structure
example :[ {key1:value1,key2:value2},
{key3:value3,key4:value4},… ] - The representation of values
value value It can be a string in double quotes (string)、 The number (number)、True、False、Null、 object (object) Or array (array), These structures can be nested .
string:“string”;
number:1265.12;
object:{id:“123”,name:“chen”};
array:[“name”,12323,“2009-11-12”];
boolean:true,false;
null:null;
3.1.2 JSON library
- JSON The library is processing JSON Format Python Standard library
- Two processes :
1 . code (encoding), take Python The data type is converted to JSON The process of formatting
2 . decode (decoding), from JSON The parsed data in the format corresponds to Python Data type process
function | meaning |
---|---|
dumps() | take python The data type is converted to JSON Format |
loads() | take JSON Format string to Python data type |
dump() | And dumps() Consistent function , output to a file |
load() | And loads() Consistent function , Read in from a file |
JSON | Python | JSON | Python |
---|---|---|---|
object | dict | number(real) | float |
array | list | true | True |
string | str | false | False |
number(int) | int | null | None |
Python | JSON | Python | JSON |
---|---|---|---|
dict | object | None | null |
list | array | True | true |
str | string | False | false |
int,float | number |
3.2 JSON Reading of data file
def read_json_file(filepath):
""" decode json file """
f = open(filepath, mode='r', encoding='utf-8')
city_list = json.load(f)
return city_list
The main program
here sort Sorting can be learned , Use lambda function , It's strange to see it for the first time , In fact, according to value Value to sort , Just using lambda Anonymous function to get value. Select the dictionary key ’aqi‘ Sort by the corresponding value
def main():
""" The main function """
filepath = input(' Please enter json File name :')
city_list = read_json_file(filepath)
# Use lambda Function to sort , obtain aqi Top five cities
city_list.sort(key=lambda cities: cities['aqi'])
top5_list = city_list[:5]
# The data of the top five cities python Type conversion to JSON
f = open('top5_aqi.json', mode='w', encoding='utf-8')
json.dump(top5_list, f, ensure_ascii=False)
f.close()
json file
[
{
"aqi":47,
"area":" Beijing ",
"pm2_5":32,
"pm2_5_24h":33,
"position_name":" Longevity West Palace ",
"primary_pollutant":null,
"quality":" optimal ",
"station_code":"1001A",
"time_point":"2017-07-29T14:00:00Z"
},
{
"aqi":63,
"area":" Beijing ",
"pm2_5":37,
"pm2_5_24h":20,
"position_name":" dingling ",
"primary_pollutant":" particulate matter (PM10)",
"quality":" good ",
"station_code":"1002A",
"time_point":"2017-07-29T14:00:00Z"
},
{
"aqi":57,
"area":" Beijing ",
"pm2_5":40,
"pm2_5_24h":36,
"position_name":" East Fourth ",
"primary_pollutant":" Fine particulate matter (PM2.5)",
"quality":" good ",
"station_code":"1003A",
"time_point":"2017-07-29T14:00:00Z"
},
{
"aqi":44,
"area":" Beijing ",
"pm2_5":24,
"pm2_5_24h":30,
"position_name":" The temple of heaven ",
"primary_pollutant":null,
"quality":" optimal ",
"station_code":"1004A",
"time_point":"2017-07-29T14:00:00Z"
},
{
"aqi":46,
"area":" Beijing ",
"pm2_5":28,
"pm2_5_24h":38,
"position_name":" Agricultural Exhibition Hall ",
"primary_pollutant":null,
"quality":" optimal ",
"station_code":"1005A",
"time_point":"2017-07-29T14:00:00Z"
},
{
"aqi":58,
"area":" Beijing ",
"pm2_5":41,
"pm2_5_24h":32,
"position_name":" Official Garden ",
"primary_pollutant":" Fine particulate matter (PM2.5)",
"quality":" good ",
"station_code":"1006A",
"time_point":"2017-07-29T14:00:00Z"
},
{
"aqi":54,
"area":" Beijing ",
"pm2_5":38,
"pm2_5_24h":28,
"position_name":" Wanliu, Haidian District ",
"primary_pollutant":" Fine particulate matter (PM2.5)",
"quality":" good ",
"station_code":"1007A",
"time_point":"2017-07-29T14:00:00Z"
},
{
"aqi":102,
"area":" Beijing ",
"pm2_5":76,
"pm2_5_24h":38,
"position_name":" Shunyi New City ",
"primary_pollutant":" Fine particulate matter (PM2.5)",
"quality":" Light pollution ",
"station_code":"1008A",
"time_point":"2017-07-29T14:00:00Z"
},
{
"aqi":80,
"area":" Beijing ",
"pm2_5":48,
"pm2_5_24h":21,
"position_name":" Huairou town ",
"primary_pollutant":" ozone 1 Hours ",
"quality":" good ",
"station_code":"1009A",
"time_point":"2017-07-29T14:00:00Z"
},
{
"aqi":77,
"area":" Beijing ",
"pm2_5":46,
"pm2_5_24h":20,
"position_name":" Changping Town ",
"primary_pollutant":" particulate matter (PM10)",
"quality":" good ",
"station_code":"1010A",
"time_point":"2017-07-29T14:00:00Z"
},
{
"aqi":60,
"area":" Beijing ",
"pm2_5":43,
"pm2_5_24h":32,
"position_name":" Olympic Sports Center ",
"primary_pollutant":" Fine particulate matter (PM2.5)",
"quality":" good ",
"station_code":"1011A",
"time_point":"2017-07-29T14:00:00Z"
},
{
"aqi":50,
"area":" Beijing ",
"pm2_5":35,
"pm2_5_24h":27,
"position_name":" ancient city ",
"primary_pollutant":null,
"quality":" optimal ",
"station_code":"1012A",
"time_point":"2017-07-29T14:00:00Z"
},
{
"aqi":58,
"area":" Beijing ",
"pm2_5":40,
"pm2_5_24h":29,
"position_name":null,
"primary_pollutant":" particulate matter (PM2.5), particulate matter (PM10)",
"quality":" good ",
"station_code":null,
"time_point":"2017-07-29T14:00:00Z"
}
]
4 Another data format CSV
- Read the acquired JSON Data files
- And turn it into CSV file
4.1 The rules
- The beginning is not left blank , Behavior unit ;
- With or without column names , Column names are listed on the first line of the file ;
- One line of data does not cross lines , There is no vacancy ;
- Comma with half angle ( namely ,) As separator , If the column is empty, its existence should also be expressed ;
- If there are half quotes in the column contents ( namely "), Replace with half angle double quotes ("") escape , Use half angle quotation marks ( namely "") Include the value of the field ;
- Quotation marks when reading and writing files , Comma reverse rule interoperability ;
- Numbers are not supported , Special characters are not supported .
4.2 usage
- CSV It's universal 、 Relatively simple file format , By user 、 Business and science are widely used ;
- “CSV” It is not a single 、 Well defined format ;
- It can generally refer to :
1. Pure text , Use a character set , such as ASCII、Unicode、EBCDIC or GB2312;
2. It's made up of records ( Typically one record per line );
3. Each record is separated by a separator into fields ( Typical delimiters are commas 、 Semicolons or tabs ; Sometimes separators can include optional spaces );
4. Each record has the same sequence of fields .
4.3 csv modular
4.3.1 csv Functions included in the module
import csv
# dir() Function can get a list
for i in dir(csv):
print(i)
Execute the following code , You can see many of its built-in functions :
Dialect
DictReader
DictWriter
Error
QUOTE_ALL
QUOTE_MINIMAL
QUOTE_NONE
QUOTE_NONNUMERIC
Sniffer
StringIO
_Dialect
__all__
__builtins__
__cached__
__doc__
__file__
__loader__
__name__
__package__
__spec__
__version__
excel
excel_tab
field_size_limit
get_dialect
list_dialects
re
reader
register_dialect
unix_dialect
unregister_dialect
writer
4.3.2 csv Module operation
There are two common methods :csv Reading and writing
reader() An iteration object is returned , It iterates over a given csvfile The lines in the . from csv Each line read from the file is returned as a string list .
writer() Returns a writer object , It is responsible for converting the user's data into the delimited string on the given class file object .
csv.reader(csvfile,dialect ='excel',** fmtparams )
csv.writer(csvfile,dialect ='excel',** fmtparams )
Use with sentence , Whether or not an exception occurs during file processing , Can guarantee with Close the file after the statement is executed . Unwanted close() sentence .
with open('filename') as somefile:
for line in somefile:
print(line)
4.4 example
take json Convert to CSV
The first step is still decoding json file , Sort ;
The second step is to sort the keys , Take the first line ;
The third step is to traverse the dictionary , Take its value , Just put the rows in sequence .
The implementation is as follows :
def main():
""" The main function """
filepath = input(' Please enter json File name :')
city_list = read_json_file(filepath)
city_list.sort(key=lambda cities: cities['aqi'])
lines = []
# Name
lines.append(list(city_list[0].keys()))
# take value Take out the value and put it in the list
for city in city_list:
lines.append(list(city.values()))
f = open('aqi.csv', 'w', encoding='utf-8', newline='')
writer = csv.writer(f)
for line in lines:
writer.writerow(line)
f.close()
5 CSV and json The synthesis of
5.1 os modular
os The module provides a connection with the system 、 Functions related to directory operation , Not limited by the platform .
function | meaning |
---|---|
os.remove() | Delete file |
os.makedirs() | Create a multi-level catalog |
os.rmdir() | Delete single level directory |
os.rename() | Rename file |
os.path.isfile() | Determine if it is a file |
os.path.isdir() | Determine whether it is a directory |
os.path.join() | Connection directory , Such as path1 Connect path2 by path1/path2 |
os.path.splitext() | Divide the file into file name and extension , Such as segmentation tmp.txt by tmp and .txt |
5.2 File decoding read operation
def read_json_file(filepath):
""" decode json file """
with open(filepath, mode='r', encoding='utf-8') as f:
city_list = json.load(f)
print(city_list)
def read_csv_file(filepath):
""" Handle csv file """
with open(filepath, mode='r', encoding='utf-8', newline='') as f:
reader = csv.reader(f)
for row in reader:
print(', '.join(row))
5.3 Judge file format , And call the corresponding function
Use os modular .
def main():
""" The main function """
filepath = input(' Please enter the file name :')
filename, file_ext = os.path.splitext(filepath)
if file_ext == '.json':
# json file
read_json_file(filepath)
elif file_ext == '.csv':
# csv file
read_csv_file(filepath)
else:
print(' Unsupported file format !')
边栏推荐
- Summary of collection: (to be updated)
- 守护进程Xinted和日志记录Syslogd
- 2022 AAAI fellow release! Yan Shuicheng, chief scientist of sail, and Feng Yan, Professor of Hong Kong University of science and technology, were selected
- Get the data of the top 100 headlines today with Tianxing data
- Application and Optimization Practice of redis in vivo push platform
- Heartbeat报错 attempted replay attack
- Postman interface test
- R built in data set
- JMeter assembly point technology and logic controller
- Using terminal connection in different modes of virtual machine
猜你喜欢
Application and Optimization Practice of redis in vivo push platform
QQ group collection
Summary of collection: (to be updated)
Take advantage of the world's sleeping gap to improve and surpass yourself -- get up early
Fundamentals of software testing
OSI model notes
QQ group administrators
Failed to configure a DataSource: ‘url‘ attribute is not specified... Bug solution
Installation of ES plug-in in Google browser
Elevator dispatching (pairing project) ④
随机推荐
SQL greatest() function instance detailed example
F12 clear the cookies of the corresponding web address
Failed to configure a DataSource: ‘url‘ attribute is not specified... Bug solution
Configure SSH key to realize login free
Appscan installation steps
The most ideal automated testing model, how to achieve layering of automated testing
Software testing related resources
Application of slice
Lvs+kept realizes four layers of load and high availability
Unittest+airtest+beatiulreport combine the three to make a beautiful test report
Is Sanli futures safe? How to open a futures account? How to reduce the handling charge of futures at present?
Shift EC20 mode and switch
Get the data of the top 100 headlines today with Tianxing data
Heartbeat报错 attempted replay attack
Canoe: distinguish VT, VN and vteststudio from their development history
Personal thoughts on the development of game automation protocol testing tool
Automatic translation between Chinese and English
Foreach (system.out:: println) usage
三立期货安全么?期货开户怎么开?目前期货手续费怎么降低?
Installation of ES plug-in in Google browser