【问题标题】:converting bi-monthly Julian days to date in raster image在光栅图像中将双月儒略日转换为日期
【发布时间】:2019-12-14 15:14:51
【问题描述】:

我很少有代表双月数据的光栅图像。我想将两个月的数据转换为月度数据,取两张图像的平均值。

总共有 23 张图像(单波段或单层)如果我使用 list.files 中的 stack() 堆叠图像,由于某种原因它读取 46 层,但如果我使用光栅函数单独打开所有光栅图像并且然后堆叠它只读取 23 层。

我单独打开图像,然后将其堆叠,但是当我转换双月儒略日时,它在第 4 个月后无法正确读取。

library(raster)
setwd("F:/LANDSAT-NDVI/testAverage")
x1<-raster("landsatNDVISC05SLC2000001.tif")
x2<-raster("landsatNDVISC05SLC2000017.tif")
x3<-raster("landsatNDVISC05SLC2000033.tif")
x4<-raster("landsatNDVISC05SLC2000049.tif")
x5<-raster("landsatNDVISC05SLC2000065.tif")
x6<-raster("landsatNDVISC05SLC2000081.tif")
x7<-raster("landsatNDVISC05SLC2000097.tif")
x8<-raster("landsatNDVISC05SLC2000113.tif")
x9<-raster("landsatNDVISC05SLC2000129.tif")
x10<-raster("landsatNDVISC05SLC2000145.tif")
x11<-raster("landsatNDVISC05SLC2000161.tif")
x12<-raster("landsatNDVISC05SLC2000177.tif")
x13<-raster("landsatNDVISC05SLC2000193.tif")
x14<-raster("landsatNDVISC05SLC2000209.tif")
x15<-raster("landsatNDVISC05SLC2000225.tif")
x16<-raster("landsatNDVISC05SLC2000241.tif")
x17<-raster("landsatNDVISC05SLC2000257.tif")
x18<-raster("landsatNDVISC05SLC2000273.tif")
x19<-raster("landsatNDVISC05SLC2000289.tif")
x20<-raster("landsatNDVISC05SLC2000305.tif")
x21<-raster("landsatNDVISC05SLC2000321.tif")
x22<-raster("landsatNDVISC05SLC2000337.tif")
x23<-raster("landsatNDVISC05SLC2000353.tif")

data<stack(x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14,x15,x16,x17,x18,x19,x20,x21,x22,x23)

julday <-c("landsatNDVISC05SLC2000001.tif","landsatNDVISC05SLC2000017.tif","landsatNDVISC05SLC2000033.tif",
         "landsatNDVISC05SLC2000049.tif","landsatNDVISC05SLC2000065.tif","landsatNDVISC05SLC2000081.tif",
         "landsatNDVISC05SLC2000097.tif","landsatNDVISC05SLC2000113.tif","landsatNDVISC05SLC2000129.tif",
         "landsatNDVISC05SLC2000145.tif","landsatNDVISC05SLC2000161.tif","landsatNDVISC05SLC2000177.tif",
         "landsatNDVISC05SLC2000193.tif","landsatNDVISC05SLC2000209.tif","landsatNDVISC05SLC2000225.tif",
         "landsatNDVISC05SLC2000241.tif","landsatNDVISC05SLC2000257.tif","landsatNDVISC05SLC2000273.tif",
         "landsatNDVISC05SLC2000289.tif","landsatNDVISC05SLC2000305.tif","landsatNDVISC05SLC2000321.tif",
         "landsatNDVISC05SLC2000337.tif","landsatNDVISC05SLC2000353.tif")

julday <- as.numeric(substr(julday, 24,26)) #24 to 26th digit in the file name represents Julian days#

dates <- as.Date(julday, origin=as.Date("2000-01-01"))

combinddat <- setZ(data, dates) 

monthly <- zApply(combinddat, by = format(dates,"%Y-%m"), fun = mean, na.rm = T)

使用该数据生成的日期是错误的;结果如下

> dates
 [1] "2000-01-02" "2000-01-18" "2000-02-03" "2000-02-19" "2000-03-06"
 [6] "2000-03-22" "2000-04-07" "2000-01-14" "2000-01-30" "2000-02-15"
[11] "2000-03-02" "2000-03-18" "2000-04-03" "2000-01-10" "2000-01-26"
[16] "2000-02-11" "2000-02-27" "2000-03-14" "2000-03-30" "2000-01-06"
[21] "2000-01-22" "2000-02-07" "2000-02-23"

但我希望日期为 12 个月,基于我的儒略日。

【问题讨论】:

  • dates &lt;- as.Date(substr(julday, 19, 25), format = '%Y%j')
  • 感谢 Alistaire,虽然最后一位数字是第 26 位,但这就是我输入错误格式的原因。

标签: r


【解决方案1】:

这并不能回答您的问题,但您可能可以通过以下方式改进您的代码:

setwd("F:/LANDSAT-NDVI/testAverage")
library(raster)
f <- list.files(pattern="\\.tif$")
f <- sort(f)
data <- stack(f)

【讨论】:

  • 感谢 Robert Hijmans 提供更简洁的代码,但我不明白为什么如果我使用 list.files 23 层变为 46,但如果我使用 raster() 单独读取,那么它会读取 23
猜你喜欢
  • 2013-07-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-11
  • 2021-12-27
  • 1970-01-01
  • 1970-01-01
  • 2013-07-21
相关资源
最近更新 更多