简体   繁体   中英

Generating a NetCDF from a csv using R

I have a.csv file with latitude and longitude coordinates for the Atlantic ocean. I associated each coordinate with a value of 1. My goal is to generate a Netcdf of this data in order to plot it. Here's a snippet of the.csv file:

lat     lon    value
24.75  -97.25    1
24.25  -97.25    1
23.75  -97.25    1
23.25  -97.25    1
22.75  -97.25    1
22.25  -97.25    1
27.25  -96.75    1
26.75  -96.75    1
26.25  -96.75    1
25.75  -96.75    1
25.25  -96.75    1

I generated a Netcdf file of the.csv file using this bit of code:

# read in csv file
atlantic <- read.csv(file = 'atlantic.csv')

# define dimensions
xvals <- unique(atlantic$lon)
xvals <- xvals[order(xvals)]
yvals <- unique(atlantic$lat)
yvals <- yvals[order(yvals)]
lon1 <- ncdim_def("longitude", "degrees_east", xvals)
lat2 <- ncdim_def("latitude", "degrees_north", yvals)

# define variables
mv <- -999 # missing value to use
var_value <- ncvar_def("Atlantic", "1",
                       list(lon1, lat2),
                       longname="Atlantic area", mv)

# add data
ncnew <- nc_create(filename = "atlantic.nc",list(var_value))

# create the indices for the dimensions
atlantic$idx_lon <- match(atlantic$lon,xvals)
atlantic$idx_lat <- match(atlantic$lat,yvals)

# create an array with the dimensions appropriate for the data
m <- array(mv,dim = c(length(yvals),length(xvals)))

# fill the array with the values
for(i in 1:NROW(atlantic)){
    m[atlantic$idx_lat[i],atlantic$idx_lon[i]] <- atlantic$value[i]
}

# write the data and close
ncvar_put(ncnew, var_value, m)
nc_close(ncnew)

However, when I plot the Netcdf using Panoply, it looks strange.

Atlantic_map

Why are there a bunch of horizontal lines? I would like it to be continuous. What am I missing here?

The link to the.csv data file: atlantic

The horizontal stripes were a result of the raw data being stretched to fill their original grids. I solved this issue by mapping the raw data to a 360 deg x 180 deg array before plotting on panoply.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM