Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
409 views
in Technique[技术] by (71.8m points)

geospatial - Working with irregularly spaced gridded netcdf data in R

I'll be working with modelled air pollution data soon. I've been given some sample data and it looks like the grid is very irregular ('brick' will throw an error about irregular grid), which makes it harder to get it into raster format.

    # load packages ####
    library(ncdf4)
    library(raster)
    library(rgdal)
    library(fields)
    
    # netcdf to data matrix ####
    nc_data <- nc_open('filename.nc')

    cnc_PM2_5       <- ncvar_get(nc_data, attributes(nc_data$var)$names[1])
    longitude       <- ncvar_get(nc_data, "longitude")
    latitude        <- ncvar_get(nc_data, "latitude")
    #time            <- ncvar_get(nc_data, "time")

    # plot matrix, latitude is upside down ####
    image.plot(longitude,rev(latitude), PM2_5, 
               main="PM2_5", ylab="latitude")

    # get quick shapefile for Finland (http://www.diva-gis.org/datadown)####
    finland <- readOGR("FIN_adm4.shp")
    # add map projection
    proj4string(finland) <- CRS("+proj=longlat +datum=WGS84 +init=EPSG:4326")
    helsinki <- finland[finland$NAME_4=="Helsinki",]
    # add Helsinki outline to image.plot
    plot(helsinki, add=TRUE)  

Figure1: PM25 plot for Helsinki

How do I get this irregular data into a spatial object? (SpatialPointsDataFrame or raster)

Reproducible example
    PM25 <- matrix(data=c(4.540674,3.437939,3.373072,2.335204,2.140603,2.529804,
                          4.475807,4.346074,6.42181,3.113605,2.594671,2.854138,
                          3.632539,6.097476,3.308205,5.643409,5.643409,2.140603,
                          4.151473,5.643409,2.529804,2.400071,3.827139,4.994741,
                          3.243339,3.373072,2.075737,2.400071,4.281207,3.697406,
                          2.854138,5.448809,2.075737,3.827139,3.567672,2.854138,
                          4.475807,2.983871,3.697406,5.578542,2.140603,2.20547,
                          6.22721,3.502806,5.773142,4.994741,5.254208,2.594671,
                          3.502806,3.827139,3.502806,2.400071,3.762273,2.724404,
                          3.892006,3.502806,2.724404,2.20547,4.151473,5.838009,
                          6.746144,5.059608,2.140603,2.270337,3.567672,2.140603,
                          3.437939,2.724404,2.140603,3.437939,3.308205,4.994741,
                          4.086606,2.335204,2.140603,5.578542,4.994741,2.724404,
                          3.892006,4.605541,4.281207,4.281207,2.140603,2.529804,
                          3.308205,3.892006,3.697406,3.308205,2.270337,6.097476,
                          3.308205,4.41094,5.319075,4.086606,2.854138,6.162343,
                          2.140603,3.308205,3.048738,4.02174), ncol=10, nrow=10)
    
    # latitude
    latitude <- c(60.1191177368164,60.1192321777344,60.1193504333496,
                          60.1194686889648,60.1195831298828,60.119701385498,
                          60.1198196411133,60.1199340820312,60.1200523376465,
                          60.1201667785645)
    # longitude
    longitude <- c(24.5949993133545,24.595235824585,24.5954704284668,
                          24.5957050323486,24.5959415435791,24.5961761474609,
                          24.5964126586914,24.5966472625732,24.5968818664551,
                          24.5971183776855)
    
    image.plot(longitude,latitude, PM25, main="PM2_5")

Figure2: PM25 grid example

question from:https://stackoverflow.com/questions/66047260/working-with-irregularly-spaced-gridded-netcdf-data-in-r

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This has now been answered on gis.stackexchange (#386628) by @Spacedman.

see answer here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...