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])
}
}