2

I have a 3-dimensional array of daily precipitation data [lon, lat, 365] and a 3-dimension file of sowing date [lon, lat, 30]. One sowing for each year on a grid cell. I have a code (given below) to extract season total (sowing to harvest) precipitation on each grid cell.

BUT, now I want to extract precipitation values only from day 30-60 after sowing date on each grid cell (i.e. sowing = day 157 on x,y grid cell for Kharif season and day 305 is sowing date for rabi crop on x, y grid cell). The result would be a 3- dimensional array of 30 days after sowing date [lon, lat, 30days]. I need different windows of days after sowing i.e. day 30-60, 80-100. Sowing date could be different in each grid cell so day 30 after sowing can be different in different grid cells but the length of days (day 30- 60) should be same. any help to select specific days length within the season based on sowing (sdate) and harvest (hdate) files is highly appreciated please

just as an example:

#precipitation aray
prec<-array(0,dim=nyear)

for (year in c(2:30)) #first year not, because growing season might have started in year 0
{
  print(year+1980)
  inputprec.file<-paste("E:/Paper_2018/Prec/","prec_",1980+year,".nc",sep="")
  inputprec.file.previous<-paste("E:/Paper_2018/Prec/","prec_",1980+year-1,".nc",sep="")

  nc<-nc_open(inputprec.file)
  lons<-ncvar_get(nc,"longitude")
  lats<-ncvar_get(nc,"latitude")
  val<-ncvar_get(nc,"pr")
  nc_close(nc)

  x<-which(abs(lons-lon)<0.01)
  y<-which(abs(lats-lat)<0.01)
  #for kharif season harvest date (260) is > sowing (157)

  if(hdate[year]>sdate[year])
  {
    prec[year]<-sum(val[x,y,sdate[year]:hdate[year]]) # season total     
    }else

  {
#for Rabi sowing start in November and harvets occurs in March-April  next year
    prec[year]<-sum(val[x,y,1:hdate[year]]) # code to extract winter season total precipitation from sdate: hdate spans over two years (1982-1983)
    # nc<-nc_open(inputprec.file.previous)
    # val<-ncvar_get(nc,"pr")
    # nc_close(nc)
    prec[year]<-prec[year]+sum(val[x,y,sdate[year-1]:365])

  }
}
Qurat
  • 21
  • 3
  • Interesting question but hard to start helping without a [MCVE]. First, please include all `library` lines or original assignment of functions like `nc_open`. Second, please include a [small data sample](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). Third, describe issue with original code (should `for` be commented out?) and describe or better show desired results. – Parfait Mar 19 '19 at 13:33
  • The full code given below is for crop specific seasonal temperature and precipitation data extraction on a single grid cell. But I want to extract a few days i.e. 30 days within a season from all years (1982-2010). I would appreciate if I get some direct email address so I can share code and piece of data – Qurat Mar 19 '19 at 19:28
  • Can you show the grid which I think you mean R array? What is value of single grid cell? Please set up a reproducible example. – Parfait Mar 19 '19 at 19:30
  • array dimensions are 30 time steps, whereas my data hy 377 176 365 grgrid values. Can I have any email address so I can write my question indetail please – Qurat Mar 19 '19 at 19:38
  • source("functions.r") # Load required libaries library("ncdf4") install.packages("devtools") library(devtools) # install_github("easyGgplot2", "kassambara") install_github("kassambara/easyGgplot2") library(easyGgplot2) library(readxl) library(ggplot2) library(reshape) require(stats) – Qurat Mar 19 '19 at 19:45
  • these are the used libraries in my code – Qurat Mar 19 '19 at 19:46
  • Please edit your question with all the necessary information and not in comments which are difficult to read. See edit link below post. Then, come back and delete these comments. – Parfait Mar 19 '19 at 19:58

0 Answers0