- 积分
- 1931
- 贡献
-
- 精华
- 在线时间
- 小时
- 注册时间
- 2012-4-6
- 最后登录
- 1970-1-1
![未绑定新浪微博用户 新浪微博达人勋](source/plugin/sina_login/img/gray.png)
|
登录后查看更多精彩内容~
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
基于以下地址进行提取天气基本要素数据,pm2.5及空气质量数据
http://www.thinkpage.cn/weather/api/
本程序是由基于MATLAB提取URL上的气象数据(一)修改升级而来
代码如下:
% 研究区域气象数据提取
% 数据来源:心知天气
% 提取基本的气象数据和监测站点pm2.5的数据
clc;clear all;
city_code = 'CHSD000100';
% Get weather information from the Internet
% url = [ 'https://api.thinkpage.cn/v2/weathery/all.json?city=' city_code '&language=zh-chs&unit=c&aqi=city&key=SFUJOVZXHQ' ];
url = [ 'https://api.thinkpage.cn/v2/weather/all.json?city=' city_code ...
'&language=zh-chs&unit=c&aqi=all&key=SFUJOVZXHQ' ];
response = urlread(url);
data = parse_json(response);
data_base_txt = [ ...
data.weather{1,1}.city_name ' '...
data.weather{1,1}.city_id ' '...
data.weather{1,1}.last_update(12:16) ' '...
data.weather{1,1}.now.text ' '...
data.weather{1,1}.now.code ' '...
data.weather{1,1}.now.temperature ' '...
data.weather{1,1}.now.feels_like ' '...
data.weather{1,1}.now.wind_direction ' '...
data.weather{1,1}.now.wind_speed ' '...
data.weather{1,1}.now.wind_scale ' '...
data.weather{1,1}.now.humidity ' '...
data.weather{1,1}.now.visibility ' '...
data.weather{1,1}.now.pressure ' '...
data.weather{1,1}.now.pressure_rising ];
data_base_xls = {
data.weather{1,1}.city_name,...
data.weather{1,1}.city_id,...
data.weather{1,1}.last_update(12:16),...
data.weather{1,1}.now.text,...
data.weather{1,1}.now.code,...
data.weather{1,1}.now.temperature,...
data.weather{1,1}.now.feels_like,...
data.weather{1,1}.now.wind_direction,...
data.weather{1,1}.now.wind_speed,...
data.weather{1,1}.now.wind_scale,...
data.weather{1,1}.now.humidity,...
data.weather{1,1}.now.visibility,...
data.weather{1,1}.now.pressure,...
data.weather{1,1}.now.pressure_rising };
%% 指定文件的存储路径
file_path = 'data';
position = ['mkdir ' file_path];
if ~(exist(file_path,'dir') == 7)
eval(position);
end
file_dir = ['.\' file_path '\'];
%% 将数据写入txt文件
% 将基础数据写入txt文件:
time_base = data.weather{1,1}.last_update(1:10);
file_base_txt = [file_dir 'XZ_base_' time_base(1:4) '_' time_base(6:7) '_' time_base(9:10) '.txt'];
cunzai = exist(file_base_txt,'file');
fid_1 = fopen(file_base_txt,'a+');
if(cunzai == 2)
fprintf(fid_1,'\r\n%s',data_base_txt);
else
fprintf(fid_1,'%s',data_base_txt);
end
% 将基础数据写入xls文件:
file_base_xls = [file_dir 'XZ_base_' time_base(1:4) '_' time_base(6:7) '_' time_base(9:10) '.xls'];
cunzai = exist(file_base_xls,'file');
if(cunzai == 2)
datan = xlsread(file_base_xls);
dn = size(datan);
n = dn(1);
xlswrite(file_base_xls,data_base_xls, 'Sheet1', ['A' int2str(n+1)]);
else
xlswrite(file_base_xls,data_base_xls, 'Sheet1', 'A1');
end
%% 将空气数据写入文件
% 判断监测站的个数
celln = size(data.weather{1,1}.now.air_quality.stations);
celln = celln(2);
time_air = data.weather{1,1}.now.air_quality.stations{1}.last_update(1:10);
for cn = 1:celln
data_air_txt = [
data.weather{1,1}.now.air_quality.stations{cn}.last_update(1:10), ' '...
data.weather{1,1}.now.air_quality.stations{cn}.last_update(12:16) ' '...
data.weather{1,1}.now.air_quality.stations{cn}.station ' '...
data.weather{1,1}.now.air_quality.stations{cn}.aqi ' '...
data.weather{1,1}.now.air_quality.stations{cn}.pm25 ' '...
data.weather{1,1}.now.air_quality.stations{cn}.pm10 ' '...
data.weather{1,1}.now.air_quality.stations{cn}.so2 ' '...
data.weather{1,1}.now.air_quality.stations{cn}.no2 ' '...
data.weather{1,1}.now.air_quality.stations{cn}.co ' '...
data.weather{1,1}.now.air_quality.stations{cn}.o3 ];
data_air_xls = {
% pm2.5以及空气成分含量
data.weather{1,1}.now.air_quality.stations{cn}.last_update(1:10),...
data.weather{1,1}.now.air_quality.stations{cn}.last_update(12:16),...
data.weather{1,1}.now.air_quality.stations{cn}.station,...
data.weather{1,1}.now.air_quality.stations{cn}.aqi,...
data.weather{1,1}.now.air_quality.stations{cn}.pm25,...
data.weather{1,1}.now.air_quality.stations{cn}.pm10,...
data.weather{1,1}.now.air_quality.stations{cn}.so2,...
data.weather{1,1}.now.air_quality.stations{cn}.no2,...
data.weather{1,1}.now.air_quality.stations{cn}.co,...
data.weather{1,1}.now.air_quality.stations{cn}.o3 };
% 将空气数据写入txt文件:
file_air_txt = [file_dir 'XZ_air_' time_air(1:4) '_' time_air(6:7) '_' time_air(9:10) '.txt'];
cunzai = exist(file_air_txt,'file');
fid_2 = fopen(file_air_txt,'a+');
% save data in the file of txt
if(cunzai == 2)
fprintf(fid_2,'\r\n%s',data_air_txt);
else
fprintf(fid_2,'%s',data_air_txt);
end
% 将空气数据写入xls文件
file_air_xls = [file_dir 'XZ_air_' time_air(1:4) '_' time_air(6:7) '_' time_air(9:10) '.xls'];
cunzai = exist(file_air_xls,'file');
if(cunzai == 2)
datan = xlsread(file_air_xls);
dn = size(datan);
n = dn(1);
xlswrite(file_air_xls,data_air_xls, 'Sheet1', ['A' int2str(n+1)]);
else
xlswrite(file_air_xls,data_air_xls, 'Sheet1', 'A1');
end
% 循环结束
end
%% close file
fclose('all');
|
|