简体   繁体   English

如何从netcdf文件中减去连续的每日数据及其后继日期?

[英]How to subtract consecutive daily data with its successor date in netcdf file?

I want to subtract my first daily date of a month with its second date than the second date data with the third date and so on using DIff in R.213-Lon,78-Lat,30-days data. 我想用第二个日期减去一个月的第一个每日日期,而不是用第三个日期减去第二个日期数据,依此类推,在R.213-Lon,78-Lat,30天数据中使用DIff。 I am getting zero as output. 我的输出为零。

My code begains: 我的代码开始:

cdf<-nc_open(file.choose()) //open my netcdf file cdf <-nc_open(file.choose())//打开我的netcdf文件

rain1 <- ncvar_get( cdf, "RAINNC" ) //extrct varaible rain1 <-ncvar_get(cdf,“ RAINNC”)// extrct变量

dim(rain1) 暗(rain1)

[1] 213 78 30 [1] 213 78 30

obs1<-array(0,c(213,78,30))// dummay array for output obs1 <-array(0,c(213,78,30))//用于输出的虚拟数组

no<-array(0,c(213,78,30)) 否<-array(0,c(213,78,30))

for( i in 1 :dim(rain1)[1]){ for(i in 1:dim(rain1)[1]){

     for( j in 1 :dim(rain1)[2]){

      for( k in 1 :dim(rain1)[3]){

            if(!is.na(rain1)[3]){

              obs[i,j] <- diff(k ,lag = 1)}

             else { no[i,j] <- NA }
}}}

You can use something like that 你可以用这样的东西

# generate data
my_dates <- as.Date(c("2019-01-01", "2019-01-02", "2019-01-05"), "%Y-%m-%d")
# substract each date data from the preceding one
my_dates[1:(length(my_dates)-1)] - my_dates[2:length(my_dates)]
# see output
Time differences in days
[1] -1 -3

If you need it other way around (substract date data from the following) you can of course simply change the order to my_dates[2:length(my_dates)] - my_dates[1:(length(my_dates)-1)] 如果您需要其他方法(从以下数据中减去日期数据),您当然可以简单地将顺序更改为my_dates[2:length(my_dates)] - my_dates[1:(length(my_dates)-1)]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM