Advertisement
Guest User

Untitled

a guest
Jul 4th, 2015
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.25 KB | None | 0 0
  1. library(ncdf)
  2. library(sp)
  3. library(gstat)
  4. files <- dir("C:/Users/Sasha/Desktop/f", recursive=TRUE, full.names=TRUE, pattern="\\.nc$")
  5. # "C:/Users/Sasha/Desktop/f" in previous row. It is path where stored my data in the following way:
  6. # year1966.nc
  7. # year1967.nc
  8. # year1968.nc...till year2014.nc (49 years)
  9. # creating function for processing these files
  10. processFile <- function(f) {
  11. nc <- open.ncdf(f) # opening files
  12. startdate <- basename(f) # get file name ("year1987.nc" for example).
  13. unlist(strsplit(startdate, "[.]"))->x # separating file name ("year1987" "nc")
  14. x[1]->x # take just first part ("year1987")
  15. unlist(strsplit(x, "year"))->x # separating "year1987" ("" "1987")
  16. unlist(strsplit(x, "year"))->x # repeat previous row in order to get only "1987"
  17. paste(x,"01-01", sep="-")-> startday # create startday ("1987-01-01")
  18. paste(x,"12-31", sep="-")-> endday # create endday ("1987-12-31")
  19. dates = seq(as.Date(startday), as.Date(endday), by="1 day") # generate dates for current year
  20. dates -> date # date will need for creating file lonlatdate (29 rows); dates - for interpolation (47 rows)
  21. rotation <- length(date) # set the value for further loop. This is important because there are leap years
  22. print(nc) # check attributes of nc file
  23. lon <- get.var.ncdf(nc,"lon") # get longitude from nc file
  24. lat <- get.var.ncdf(nc,"lat") # get latitude from nc file
  25. var <- get.var.ncdf(nc,"tasmax") # get values of max temperature from nc file
  26. merge(lon,lat)->lonlat # merge coordinates
  27. as.data.frame(lonlat)->lonlat
  28. as.data.frame(date)->date
  29. merge(lonlat,date)-> lonlatdate # merge coordinates with dates
  30. as.vector(var)->tasmax
  31. cbind(lonlatdate,tasmax)->data # add values of max temperature to lonlatdates
  32. data[complete.cases(data), ]->out # get rid of rows which contain NA
  33. # Now we have array(out)with max temperature for each grid per each day of 1987 year:
  34. out[1,]
  35. x y date tasmax
  36. 120 33.625 44.625 1987-01-01 279.2693
  37. station <- read.table("C:\\Users\\sasha\\Desktop\\station.txt",sep='',header=T) # download stations data
  38. station[1,] # how it looks like
  39. wmo x y
  40. 1 33998 34.08 44.43
  41. # And now we must to interpolate tasmax values from out to station
  42. # but before creating file for results (res)
  43. res <- c(1:5)
  44. dim(res)<-c(1,5)
  45. res[-1,]->res
  46. colnames(res)<- c("wmo","x","y","tasmax","date") # setting names for columns of res file
  47. #interpolation
  48. for (i in seq(rotation)){
  49. subset(out, date==dates[i])->test;station->test2;coordinates(test)<-~x+y;coordinates(test2)<-~x+y;
  50. idw(tasmax~1,test,test2)->z;zdf<- as.data.frame(z);cbind(station$wmo,zdf)->me;me[5]<-dates[i];
  51. colnames(me)<- c("wmo","x","y","tasmax","date");rbind(res,me)->res} # test,test2,z,zdf,me - these are temporary files
  52. as.Date(res$date,origin="1970-01-01")-> res$date # convert values in res$date to Date format
  53. # This is what we have:
  54. head(res)
  55. wmo x y tasmax date
  56. 1 33998 34.08 44.43 275.6095 1987-01-01
  57. 2 33959 34.43 44.68 276.8527 1987-01-01
  58. 3 34622 38.51 47.80 273.2416 1987-01-01
  59. 4 33958 34.35 44.75 275.8180 1987-01-01
  60. 5 34510 37.98 48.60 273.4068 1987-01-01
  61. 6 33915 33.88 46.45 276.8582 1987-01-01
  62. write.table(res,f,row.names = FALSE) # will rewriting all nc files in folder f
  63. file.info(f)$size
  64. }
  65. result <- sapply(files,processFile) # apply processFile function to all nc files in folder f
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement