爱气象,爱气象家园! 

气象家园

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博登陆

只需一步, 快速开始

搜索
查看: 11209|回复: 16

基于MATLAB提取URL上的气象数据(一)

[复制链接]

新浪微博达人勋

发表于 2014-7-11 23:58:32 | 显示全部楼层 |阅读模式

登录后查看更多精彩内容~

您需要 登录 才可以下载或查看,没有帐号?立即注册 新浪微博登陆

x
本帖最后由 haobang008 于 2014-7-13 22:16 编辑

最近自己写了一个程序,可以从API接口中提取实时的气象数据,并且写入到txt和xls当中,粘贴如下,
另外,需要一个自编函数,下载地址如下:
http://www.mathworks.com/matlabc ... nother--json-parser
程序的主要功能就是根据提供的API接口,从API上下载数据,每4分钟更新一次数据。

% 研究区域气象数据提取   

%     data.weatherinfo.city        "北京"           城市  
%     data.weatherinfo.cityid      "101010100"      城市编码  
%     data.weatherinfo.temp        "22"             当前温度  
%     data.weatherinfo.WD      "东南风"              风向  
%     data.weatherinfo.WS      "3级"                 风速
%     data.weatherinfo.SD      "31%"                 相对湿度  
%     data.weatherinfo.WSE         "3"               风力  
%     data.weatherinfo.time        "20:15"           更新时间  
%     data.weatherinfo.isRadar "1"                   是否有雷达图   (1表示有雷达图)
%     data.weatherinfo.Radar   "JC_RADAR_AZ9010_JB"  雷达图地址(AZ9010为北京雷达)


% 循环
% 打开文件
% 读取信息,写入
% 暂停
% 重新开始循环


clc;clear all;
while 1+1 > 1

    pause(4*60);
    c1 = clock;

    % Get weather information from the Internet

    url = ['http://www.weather.com.cn/data/sk/101' '120201' '.html'];
    response = urlread(url);
    data = parse_json(response);
    str = [];

    % 修改湿度的设置:
    if data.weatherinfo.SD(2) == '%'
        daqishidu =  data.weatherinfo.SD(1);
    else
        daqishidu =  data.weatherinfo.SD(1:2);
    end

    str = [    data.weatherinfo.city ' ' data.weatherinfo.cityid '  ' data.weatherinfo.time ...
           ' ' data.weatherinfo.temp ' ' data.weatherinfo.WD ' ' data.weatherinfo.WS  ...
           ' ' daqishidu             ' ' data.weatherinfo.WSE ' ' data.weatherinfo.isRadar ...
           ' ' data.weatherinfo.Radar ];

    % Format: date month day hour minute
    format2 = ['%d  ' '%d  ' '%d  ' '%d  ' '%d'];
    fprintf(fid_txt,format2,c1(1:5));

%     open temp data file txt&xls:
    file_weather = [ int2str(c1(1)) '_' int2str(c1(2)) '_' int2str(c1(3)) '.txt'];
    cunzai = exist(file_weather,'file');
    fid_w = fopen(file_weather,'a+');
%     fid_txt = fopen('test1.txt','w'); %打开后写入数据。该文件已存在则更新;不存在则创建

    % save data in the file of txt
    if(cunzai == 2)
        fprintf(fid_w,'\r\n%s',str);
    else
        fprintf(fid_w,'%s',str);
    end

    % save data in the file of xls
    file_weather_xls = [ int2str(c1(1)) '_' int2str(c1(2)) '_' int2str(c1(3)) '.xls'];
    data_xls = {  data.weatherinfo.city,data.weatherinfo.cityid,data.weatherinfo.time, ...
                  data.weatherinfo.temp,data.weatherinfo.WD,data.weatherinfo.WS,  ...
                  data.weatherinfo.SD(1:2),data.weatherinfo.WSE,data.weatherinfo.isRadar, ...
                  data.weatherinfo.Radar };
    cunzai = exist(file_weather_xls,'file');

    if(cunzai == 2)
        datan = xlsread(file_weather_xls);
        dn = size(datan);
        n = dn(1);
        xlswrite(file_weather_xls,data_xls, 'Sheet1', ['A'  int2str(n+1)]);
    else
        xlswrite(file_weather_xls,data_xls, 'Sheet1', 'A1');
    end

%     [ndata, text, alldata] = xlsread(file_weather_xls);
%   xlswrite(file_weather_xls,magic(3), 'Sheet1')


    % close file
    fclose(fid_txt);
    fclose(fid_w);

%     break;
end



评分

参与人数 1金钱 +15 贡献 +3 收起 理由
mofangbao + 15 + 3

查看全部评分

密码修改失败请联系微信:mofangbao

新浪微博达人勋

 楼主| 发表于 2014-7-12 00:06:18 | 显示全部楼层
气象数据接口的说明参见以下链接:
http://blog.sina.com.cn/s/blog_4d2fda500101g8er.html
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2014-7-12 00:20:43 | 显示全部楼层
大赞!!!!!!!!!!
密码修改失败请联系微信:mofangbao
回复

使用道具 举报

新浪微博达人勋

发表于 2015-5-28 14:26:26 | 显示全部楼层
haobang008 发表于 2014-7-12 00:06
气象数据接口的说明参见以下链接:
http://blog.sina.com.cn/s/blog_4d2fda500101g8er.html

请问这个编写的程序可以直接提取相应经纬度的风场数据吗?
密码修改失败请联系微信:mofangbao

新浪微博达人勋

 楼主| 发表于 2015-6-11 08:11:36 | 显示全部楼层
王小燕1991 发表于 2015-5-28 14:26
请问这个编写的程序可以直接提取相应经纬度的风场数据吗?

恩,这个可以读取单点的数据,也可以读取一定行政范围内的数据,比如读取山东省内的风场数据,关键是要设置这个语句:
url = ['http://www.weather.com.cn/data/sk/101' '120201' '.html'];
其中的120201,你要具体查看说明,你明白了嘛?
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2015-6-26 10:24:22 | 显示全部楼层
haobang008 发表于 2015-6-11 08:11
恩,这个可以读取单点的数据,也可以读取一定行政范围内的数据,比如读取山东省内的风场数据,关键是要设 ...

感觉好复杂啊,要好好学习啊
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2015-7-18 21:04:13 | 显示全部楼层
密码修改失败请联系微信:mofangbao
回复

使用道具 举报

新浪微博达人勋

发表于 2015-7-20 12:33:44 | 显示全部楼层
谢谢,这个非常有用
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2015-7-23 08:29:33 | 显示全部楼层
不错,支持!
密码修改失败请联系微信:mofangbao
回复

使用道具 举报

新浪微博达人勋

发表于 2015-12-2 11:15:03 | 显示全部楼层
好复杂,学习了
密码修改失败请联系微信:mofangbao
您需要登录后才可以回帖 登录 | 立即注册 新浪微博登陆

本版积分规则

Copyright ©2011-2014 bbs.06climate.com All Rights Reserved.  Powered by Discuz! (京ICP-10201084)

本站信息均由会员发表,不代表气象家园立场,禁止在本站发表与国家法律相抵触言论

快速回复 返回顶部 返回列表