Extracting stations data from a netcdf file to csv

I am trying to extract precipitation data from a NetCDF file for 425 points which I have in a CSV file, into a new CSV file. The output CSV file shows all the values as NA. What is the issue and how can I resolve it. I am attaching my code, and a snap of the output file below. Thank you
setwd("D:/Wasif/Adjusted data/Datasets")
library (raster)

station.data = read.csv(file.choose(), sep = ",", header =T) ## import station data.file
lat.long = station.data[,c(2,3)] ## extract data of all stations in station file for which point values are to be extractd
lat.long = SpatialPoints(lat.long) ## lat.long for further use
robject = brick(file.choose(), varname = "PPT")## import raster netcdf file from which point values are to be extractd
dim(robject) ## check the dimensions of project
vall = extract(robject , lat.long, method = "simple" ) ## extract values
vall = t(vall)
write.csv(vall, file = "Observed.csv", fileEncoding = "macroman") ## save output csv file containing extracted point values .

the key problem here is that the SpatialPoints() takes lon&lat (i.e. x-y) as default, but you gave it lat&lon, which might generate a wrong coordinate (switched x & y ). so nothing is going to be extracted.

> lonlat <- expand_grid(
+   x = sample(seq(4,55,.5),100,replace = T),
+   y = sample(seq(60,150,.5),100,replace = T),
+ )
> SpatialPoints(lonlat %>% select(x,y))
class       : SpatialPoints 
features    : 10000 
extent      : 4.5, 54.5, 61.5, 149.5  (xmin, xmax, ymin, ymax)
crs         : NA 
> SpatialPoints(lonlat %>% select(y,x))
class       : SpatialPoints 
features    : 10000 
extent      : 61.5, 149.5, 4.5, 54.5  (xmin, xmax, ymin, ymax)
crs         : NA 

so you'll need to correct the coords of SpatialPoints that are used to extract data.

I change the lat.lon to lon.lat and it worked. Thank you so much

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.