I am using a for loop in R to read netCDF file from a folder and extract values for given list of longitude, latitude. It looks like working, except ONE PROBLEM. When the loop returns values against date, it creates January 29 to 31 after February 28. I want, as usual, March 01 after February 28 or 29 (for leap year). Here is my R code:
# given latitude, longitude list
sb1 <- data.frame(longitude=1:10,latitude =1:10)
# Extracting zonal or sub-basin average rainfall from netCDF file
sb1_r <- c()
date <- c()
rain_month <- c()
rain_year <- c()
for (year in 1998:1998){
for (month in 1:3){
for (day in seq_along(1:31)){
FileName <- paste('3B42_daily',year,sprintf("%02d",month),sprintf("%02d", day),'7.SUB.nc', sep='.')
if (!file.exists(FileName)){
next
} else {
File <- nc_open(FileName)
rain <- ncvar_get(File, 'r')
sb1_r[day] <- mean(apply(sb1,1,function(x)rain[x[1],x[2]]),na.rm = TRUE)
date[day] <- paste(year,sprintf("%02d", month),sprintf("%02d", day),sep='-')
rain_month <- data.frame(date,sb1_r)
nc_close(File)
}
}
rain_year <- rbind(rain_year,rain_month)
}
}
You can find daily netCDF data for three months to this link: https://drive.google.com/open?id=0B8rqKaYt0VEaMWVGc1gzdXI1U28