[R-sig-Geo] Automate the extraction of climate variable at different time depths from netcdf in r
Robert J. Hijmans
r.hijmans at gmail.com
Sun Sep 7 05:23:10 CEST 2014
Barnabas, ,
You can try something like this:
b <- brick("~sst.mnmean.nc", varname="sst")
# loop over species, or combine all species into one data.frame?
mydata <- read.csv("~Species one.csv")
extract.mydata <- extract(b, mydata[,5:6])
write.csv(extract.mydata, file = "Species_one_extracted.csv")
Robert
On Sat, Sep 6, 2014 at 10:10 AM, Barnabas Daru <darunabas at gmail.com> wrote:
> Dear all,
> I write to seek help with extracting climate data at various times and
> depth from NETCDF in R.
> The climate data is sea surface temperature monthly means from 1854 to
> 2014.
> My goal is to automate the extraction of SST climate data for a set of
> points with different time depths.
>
> I have successfully used the 'brick' function in the raster package to get
> climate data at various time depths as follows:
>
> b <- brick("~sst.mnmean.nc", varname="sst")
>
> mydate <- b[["X1869.09.01"]] # SST for September in 1869
>
> mydata <- read.csv("~Species one.csv")
>
> mydataSPDF <- SpatialPointsDataFrame(mydata[,5:6],data.frame(mydata))
>
> extract.mydata <- extract(mydate, mydataSPDF, sp=TRUE)
>
> extract.mydata <- data.frame(extract.mydata)
>
> write.csv(extract.mydata, file = "Species_one_extracted.csv")
>
> My major challenge is that I have to keep extracting manually for each time
> depth, then copy and paste in the main dataframe and the data has about
> 3000 observations, meaning I have to keep copying and pasting ~3000 times
> for one species!
> Is there a way I can iterate the process in R to loop through each month's
> climate and extract the data for each point and time depth rather than to
> do it manually?
>
> Here's how my data frame looks like:
> Date year month day lon lat SST 1855-01-01 1855 1 1 11.6861 57.7254
> 3.440000057 1859-07-26 1859 7 26 25.46122 60.26766 13.25 1860-01-01 1860 1
> 1 10.9154 53.9484 15.10999966 1861-07-10 1861 7 10 7.79588 58.08673
> 15.71000004 1861-07-26 1861 7 26 -2.84072 54.0778 1861-08-17 1861 8 17
> 11.9792 57.51298 1862-01-01 1862 1 1 22.20467 60.27955 1862-08-05 1862 8 5
> 11.78316 57.61649 1862-08-23 1862 8 23 11.9792 57.51298 1863-08-26 1863 8
> 26 10.72237 59.97258 1863-08-28 1863 8 28 15.53721 56.20091 1863-09-22
> 1863 9 22 16.37849 56.84255 1864-07-05 1864 7 5 -4.07097 51.09542
> 1864-07-18 1864 7 18 -4.21368 51.0928 1864-08-26 1864 8 26 -4.07097
> 51.09542 1865-09-07 1865 9 7 10.76144 59.91271 1865-09-27 1865 9 27
> 10.53107 60.43395 1867-08-28 1867 8 28 12.82669 55.86822 1868-01-01 1868 1
> 1 12.8944 56.6897 1868-01-01 1868 1 1 4.82685 53.13793
> Thanks and kind regards
> Barnabas Daru
>
> --
>
> \-/
> /\
> /--|
> /---/ *Daru, Barnabas Haruna*
> |--/ PhD Candidate,
> \-/ African Centre for DNA Barcoding,
> /\ University of Johannesburg,
> /--\ PO BOX 524 Auckland Park, 2006,
> |---\ South Africa
> \---\ Lab: +27 11 559 3477
> \--| Mobile: +27 7381 89 583
> \-/
> /\ My homepage: http://barnabasdaru.com
> <http://acdb.co.za/index.php?page=mr-barnabas-h-daru>
> /--\
>
> [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
More information about the R-sig-Geo
mailing list