按月汇总modis列表文件

时间:2017-04-20 00:10:23

标签: r-raster

我正在寻找一种更有效的方法,将每年与时间序列(2002-2016)逐月分开。我已经手工完成了,但需要花费很多时间。

mypath<-"D:/SNOWL"
myras<-list.files(path=mypath,pattern = glob2rx("*.tif$"), 
                    full.names = TRUE, recursive = TRUE)

> myras
   [1] "D:/SNOWL/MOYDSL10A1.A2002001.tif" "D:/SNOWL/MOYDSL10A1.A2002002.tif"
   [3] "D:/SNOWL/MOYDSL10A1.A2002003.tif" "D:/SNOWL/MOYDSL10A1.A2002004.tif"
   [5] "D:/SNOWL/MOYDSL10A1.A2002005.tif" "D:/SNOWL/MOYDSL10A1.A2002006.tif"
   [7] "D:/SNOWL/MOYDSL10A1.A2002007.tif" "D:/SNOWL/MOYDSL10A1.A2002008.tif"
   [9] "D:/SNOWL/MOYDSL10A1.A2002009.tif" "D:/SNOWL/MOYDSL10A1.A2002010.tif"
  [11] "D:/SNOWL/MOYDSL10A1.A2002011.tif" "D:/SNOWL/MOYDSL10A1.A2002012.tif"

serie<-orgTime(myras, nDays = "asIn", begin ="2002-01-01",end = "2016-12-31", pillow = 75, pos1 = 13, pos2 = 19)
filter<-serie$inputLayerDates 

> filter
   [1] "2002-01-01" "2002-01-02" "2002-01-03" "2002-01-04" "2002-01-05"
   [6] "2002-01-06" "2002-01-07" "2002-01-08" "2002-01-09" "2002-01-10"
  [11] "2002-01-11" "2002-01-12" "2002-01-13" "2002-01-14" "2002-01-15"
  [16] "2002-01-16" "2002-01-17" "2002-01-18" "2002-01-19" "2002-01-20"
  [21] "2002-01-21" "2002-01-22" "2002-01-23" "2002-01-24" "2002-01-25"
  [26] "2002-01-26" "2002-01-27" "2002-01-28" "2002-01-29" "2002-01-30"
  [31] "2002-01-31" "2002-02-01" "2002-02-02" "2002-02-03" "2002-02-04"
  [36] "2002-02-05" "2002-02-07" "2002-02-08" "2002-02-09" "2002-02-10"
  [41] "2002-02-11" "2002-02-12" "2002-02-13" "2002-02-14" "2002-02-15"

1 个答案:

答案 0 :(得分:0)

修改

好的,让我们试试一个完整的示例,看看它是否适合您:

# Here we generate filenames as returned from `list.files`:


rm(list = ls())

myras <- sapply(1:5465, function(i) paste0('D:/SNOWL/MOYDSL10A1.A',sample(2000:2016,1),sample(c(paste0('00',1:9),paste0('0',10:99),100:365),1),'.tif'))

head(myras)


# Let's extract the timestamps

tstmps <- regmatches(myras,regexpr('[[:digit:]]{7}',myras))

head(tstmps,50)

# And now convert the timestamps to dates 

dates <- as.Date(as.numeric(substr(tstmps,5,7)) - 1, origin = paste0(substr(tstmps,1,4),"-01-01"))

head(dates,10)


# Last step is to sort the files by month

#check months
print(month.name)

myras_byM = sapply(month.name,function(x) myras[months(dates) == x])

head(myras_byM$January)
head(myras_byM$February)
head(myras_byM$March)
head(myras_byM$April)
head(myras_byM$May)
head(myras_byM$June)
head(myras_byM$July)
head(myras_byM$August)
head(myras_byM$September)
head(myras_byM$October)
head(myras_byM$November)
head(myras_byM$December)

如果您有一致的命名约定,则可以轻松地从文件名中获取日期。

在您的情况下,我看到文件按年份和年份排序。因此,只需从文件名中删除日期,然后您可以根据需要过滤它。要做到这一点,我正在使用正则表达式。在这种情况下,我对日期和DOY字符串感兴趣,它应该始终是7个数字。因此,相应的RE是[[:digit:]]{7},这意味着7个连续的数字。 regexpr找到匹配项,regmatches返回匹配项。

dts <- regmatches(myras,regexpr('[[:digit:]]{7}',myras))

然后你只需使用substring来提取你需要的数字(这种方法假定它总是4位数,然后是3位DOY)并将其转换为日期:

dts <-as.Date(as.numeric(substr(dts,5,7)) - 1, origin = paste0(substr(dts,1,4),"-01-01"))

这会为您提供示例中filter的变量。

如果您希望按月对整个时间序列进行排序,则可以使用内置名称month.name进行sapply或lapply。基函数months将返回给定日期的月份名称:

myras_byMonth <- sapply(month.name,function(x) myras[months(dts) == x])

希望我能正确理解你的问题,这就是你要找的东西。

最佳,

缬氨酸