target

Fig.1 提取示意图

Fig.1 提取示意图

Fig.2 单点不同层的温度随时间变化图示

Fig.2 单点不同层的温度随时间变化图示

variation of moisture in a pixel as time changes

setwd("E:/test/Netcdf/data/extrct_result/tmp")
years <- c("2006","2007","2008","2009","2010")
depthes<-c("0_10cm","10_40cm","40_100cm","100_200cm")
output1 <- list()
for(i in 1:4) 
{
  for(year in years)
  {
    var <- read.csv(paste0(year,"SoilTMP",depthes[i],"_inst_day.csv"))
    #读取文件,定位,取一行
    pix <-var[ which(var$lon==90.125 & var$lat==35.125),]
    # 将该行转为一列
    pix_vec <- unlist(pix[,c(-1:-3)])
    output1[[depthes[i]]] <- c(output1[[depthes[i]]],pix_vec)        #怎么把向量加入列表一列呢?
  }
}
  pix_year_depth_tmp<-data.frame(output1) -272.15
  # nrow(pix_year_depth_tmp)
  pix_year_depth_tmp$day<-c(1:1826)
  library(reshape2)
  # time series
  te<-ts(pix_year_depth_tmp)
  # wide to slim
  t<-melt(te)
  names(t)[1:2]<-c("days","depth")
  # plot 
  ggplot(t)+
    geom_line(aes(x=days,y=value,colour=depth))+
    xlab("days")+
    scale_color_brewer(palette = "Set1")+
    ylab("tmp(℃)")+
    labs(title=paste0("2006-2010_","temperature"))

we can see, daymean of every year is different ,so we have a right extraction of temperature(moisture is not shown). next ,we will calulate mean temperature of wram seasons and cold seasons for each pixel.

means moisture of warm or cold seasons

target: each pixel will have 2 seasons * 4 depthes= 8 value

do you understand? we will obtain 4 sheets including 2 coloumes for warm and cold seasons.

moisture mean

setwd("E:/test/Netcdf/data/extrct_result/moi")
for(year in years)
{
  for(depth in depthes){
  # 
  filelist<-list.files(pattern=paste0(year,"_day",depth))
  file <- filelist[1]
  file_cs <- read.csv(file)
  # the begin and end of warm season and cold season
  warmday1 <- which(colnames(file_cs)==paste0("X",year,"0501"))
  warmday2 <- which(colnames(file_cs)==paste0("X",year,"1030"))
  coldday1 <- which(colnames(file_cs)==paste0("X",year,"0101"))
  coldday2 <- warmday1-1
  coldday3 <- warmday2+1
  coldday4 <- which(colnames(file_cs)==paste0("X",year,"1231"))
  # the first three cols of new sheet
  moi_0_10cm_mean <- file_cs[,1:3]
  # mean for warm seasons
  moi_0_10cm_mean$warm_mean <- apply(file_cs[,c(warmday1:warmday2)],1,mean)
  # mean for cold seasons
  moi_0_10cm_mean$cold_mean <- apply(file_cs[,c(coldday1:coldday2,coldday3:coldday4)],1,mean)
  # write
  write.csv(moi_0_10cm_mean,paste0(year,depth,"_mean_tmp.csv"),row.names = F )
}
}

tem mean

getwd()
setwd("E:/test/Netcdf/data/extrct_result/tmp")
for(year in years)
{
  for(depth in depthes){
  filelist<-list.files(pattern=paste0(year,"SoilTMP",depth))
  file <- filelist[1]
  
  file_cs <- read.csv(file)
  warmday1 <- which(colnames(file_cs)==paste0("X",year,"0501"))
  warmday2 <- which(colnames(file_cs)==paste0("X",year,"1030"))
  coldday1 <- which(colnames(file_cs)==paste0("X",year,"0101"))
  coldday2 <- warmday1-1
  coldday3 <- warmday2+1
  coldday4 <- which(colnames(file_cs)==paste0("X",year,"1231"))
  moi_0_10cm_mean <- file_cs[,1:3]
  moi_0_10cm_mean$warm_mean <- apply(file_cs[,c(warmday1:warmday2)],1,mean)
  moi_0_10cm_mean$cold_mean <- apply(file_cs[,c(coldday1:coldday2,coldday3:coldday4)],1,mean)
  write.csv(moi_0_10cm_mean,paste0(year,depth,"_mean_tmp.csv"),row.names = F )
}
}

what I have learned

# continue to put vector into "a"coloume of list p
# a null list is needed at first 
p <- list() 
p[["a"]] <-c(p[["a"]],vector) 
# okye!
library(reshape2)
# melt()
aRowDf # this is a row in a dataframe
# use unlist() to make it as a col
aColList <- unlist(aRowDf)
output1<-list ()
for(i in 1:4)
{
  for(year in years)
  {
    pix_vec <- c(1,2,3,4)
    output1[[depthes[i]]] <- c(output1[[depthes[i]]],pix_vec) 
  }
}
output1
data.frame(output1)