当前位置:网站首页>Meteorological data processing example - matlab string cutting matching and R language date matching (data splicing)
Meteorological data processing example - matlab string cutting matching and R language date matching (data splicing)
2022-08-05 10:01:00 【Zhang classmate who knows nothing】
Done a long time ago,Think about or throw it to review the record.
任务:Verify the forecast temperature data accuracy,The weather forecast data compared with the observatory data
数据处理目标:To the observation data of meteorological sites and crawl forecast data matching and joining together,Facilitate the prediction accuracy of subsequent processing.
图1 Observations of meteorological site data
图2 The weather forecast on the crawl data
下面开始:
Extract the station
The weather forecast data is at hand38×3=114个,Named after the place of pinyin,如"akesu.csv",站点数据95个,Named after the station number,如"50136.xlsx",Another station and place correspondingxls文件,And not all sites have corresponding weather forecast data.
First screened with weather data and the location of the site data,And the site place names corresponding,这部分我是用matlab实现(也可以用R,Because the previous plans to usematlabAnd write the code hasn't written,这里使用了strsplit、strfindFunction to cut string matching:
rawDataNum,rawDataStr]=xlsread('D:\大三\The observatory data\The name and stand number for.xls','C1:D99');
str='D:\大三\data\3new\';
files=dir(strcat(str,'*.csv'));
n=length(files);
filename=cell(1,n);
for i=1:length(files)
a=strsplit(files(i).name,'.');
filename{
i}=a{
1};
end
std=cell(99,1);
for i=1:99
std{
i}=num2str(rawDataNum(i));
end
data=[rawDataStr,std];
filename=filename';
t=1;
for i=1:n
idx=find(cellfun(@(x) ~isempty(strfind(x,filename{
i})), rawDataStr));
if ~isempty(idx)
d{
t,1}=data{
idx,1};
d{
t,2}=data{
idx,2};
t=t+1;
end
end
Extract the station and station number:
*
日期时间的处理
由于R语言读取xls、xlsx文件需要java环境,For the convenience of usingmatlab将xlsx转为csv)
Station data import,由于excelThe data format reason,The date of import display for five digits,Abnormal date display format,After all kinds of testing to find,Use the following statements into:
as.Date(x,origin="1899/12/30")
解决
Relax before long,又有新的问题,在RDates in the default format for"2011-01-01",And forecast data read format for"2011/1/1"与"2011年1月1日”,对于前者,使用 as.Date(x)即可,对于后者,as.Date(“2011年1月1日”, “%Y年%m月%d日”)
查阅资料,RLanguage commonly used time processing with “zoo"、"lubridate”,其中"lubridate"包unique(ymd_hms(date1),ymd_hms(date2))Statement can take the time variable overlap match.
ps:安装zooPackage version error,需要手动安装,具体步骤参考:https://blog.csdn.net/lym152898/article/details/77572163
lubridatePackage is not applicable to3.5、3.6版本R,而RThe use of language is the biggest characteristic package,Stalled again.
RLanguage is another feature of the data type more,Data type conversion is convenient,Then use the following small white(菜鸡)方法:
The time format to"character"类型→直接利用intersect取交集→for循环匹配
Date match with joining together
This part of an error a lot,Mainly the data itself some problem,In general it can use:
merge(a,d,by="V1",all=F)
进行拼接,But does not apply to the data,Therefore had to write cycle:
path1='D:/大三/data/'
path2='D:/大三/data/3new/'
m<-read.csv('D:/大三/The observatory data/match.csv',header=F)
m<-as.matrix(m)
library(stringr)
m[,1]<- str_replace(m[,1], "'","")
m[,2]<- str_replace(m[,2], "'","")
m[,1]<- paste(m[,1],'.csv',sep='')
m[,2]<- paste(m[,2],'.csv',sep='')
fileName1 = dir(path1)
fileName2 = dir(path2)
d3<-list()
t<-1
for (i in 16:length(fileName2))
{
if(setequal(which(m[,1]==fileName2[i]),integer(0)))
next;
a<-read.csv(file = paste(path2,fileName2[i],sep = ''),header = T)
fileName<-m[which(m[,1]==fileName2[i]),2]
if(length(fileName>1))
fileName=fileName[1]
d<-read.csv(file = paste(path1,fileName,sep=""),header=F)
b<-as.Date(d$V1,origin="1899/12/30")
c<-as.character(a$V1)
c<-gsub('/','-',c)
library('stringr')
c<-as.Date(c)
a$V1<-c
d$V1<-b
a<-as.matrix(a)
d1<-as.matrix(d)
d2<-matrix()
b<-as.character(b)
c<-as.character(c)
p<-intersect(b,c)
if (length(p)!=0)
{
d2<-matrix(0,nrow = length(p),ncol =30 )
for (j in 1:length(p))
{
mm<-which(b==p[j])
n<-which(c==p[j])
if (length(mm)>1|length(n)>1)
{
mm=mm[1]
n=n[1]
}
s<-c(a[n,1:13],d1[mm,2:18])
d2[j,]<-as.vector(s)
}
}
d3[[t]]<-d2
write.csv(d3[t],file = paste(path3,fileName2[i],sep = ''))
t<-t+1
}
Managed to spell:’
总结
This is two and a half years ago big three written code and tasks,Now is very childish,当时得出的结论是1、在学习像R、matlab这种语言,Be sure to find out whether there is a similar function to solve this
2、RThe processing of language data types,Involving a large number of data conversion details,Can only be more familiar with multi-purpose
Although the processing semi-serious,But looked from now,This type of meteorological data processing is the basic operation,After my data processing intonc数据/卫星数据/栅格数据,Site for meteorological data processing has not been doing basic,After direction should also pay attention to on the satellite data and model.But weather stations data processing was my introduction to programming,如今想来,There is some miss.
边栏推荐
- shell脚本实例
- Hundred lines of code launch red hearts, why programmers lose their girlfriends!
- ffmpeg drawtext 添加文本水印
- 无题二
- 长达四年的减肥记录
- dotnet OpenXML parsing PPT charts Getting started with area charts
- 还在找网盘资源吗?快点收藏如下几个值得收藏的网盘资源搜索神器吧!
- Two-table query average grouping in sql server
- leetcode: 529. Minesweeper Game
- 仿SBUS与串口数据固定转换
猜你喜欢

茄子科技CEO仇俊:以用户为中心,做用户真正需要的产品

First Decentralized Heist?Loss of nearly 200 million US dollars: analysis of the attack on the cross-chain bridge Nomad

mysql索引

Pytorch深度学习快速入门教程 -- 土堆教程笔记(三)

Pytorch Deep Learning Quick Start Tutorial -- Mound Tutorial Notes (3)

开源一夏|OpenHarmony如何查询设备类型(eTS)

首次去中心化抢劫?近2亿美元损失:跨链桥Nomad 被攻击事件分析

【zeno】为zeno增加子模块/新节点的最小化的例子

seata源码解析:TM RM 客户端的初始化过程

Qiu Jun, CEO of Eggplant Technology: Focus on users and make products that users really need
随机推荐
PHP operation mangoDb
Pytorch深度学习快速入门教程 -- 土堆教程笔记(三)
JS逆向入门学习之回收商网,手机号码简易加密解析
哪位大佬有20年4月或者1月的11G GI和ojvm补丁呀,帮忙发下?
2022-08-01 Review the basic binary tree and operations
Imitation SBUS fixed with serial data conversion
公众号如何运维?公众号运维专业团队
科普大佬说 | 港大黄凯斌老师带你解锁黑客帝国与6G的关系
How ali cloud storage database automatically to speed up the loading speed of www.cxsdkt.cn how to set up the case?
First Decentralized Heist?Loss of nearly 200 million US dollars: analysis of the attack on the cross-chain bridge Nomad
Happens-before rules for threads
Pytorch Deep Learning Quick Start Tutorial -- Mound Tutorial Notes (3)
leetcode: 529. 扫雷游戏
【温度预警程序de开发】事件驱动模型实例运用
使用工具类把对象中的null值转换为空字符串(集合也可以使用)
长达四年的减肥记录
eKuiper Newsletter 2022-07|v1.6.0:Flow 编排 + 更好用的 SQL,轻松表达业务逻辑
浅析WSGI协议
Redis源码解析:Redis Cluster
What is the function of the regular expression replaceFirst() method?