Hi all,

I posted to the listserv a little over a month ago soliciting tips about
working with .nc file formats in R. THANK YOU to everyone who replied! I
couldn't reply to everyone because my inbox was overflowing. As a thank you
for your help, I thought I'd post the code I ended up using that worked well
for me:

library(ncdf4) 
nc = nc_open('C:/Users/Lindsay/Downloads/HIIG_winter_uvel_var_050.nc') #
change this pathway for your needs
str(nc)  # examine nc file structure
v1 = nc$var[[1]] # identify your variable of interest by indexing the nc file
u = ncvar_get(nc, v1) # u is a matrix of values for your v1
lon = v1$dim[[1]]$vals # dim[[1]] is lon
lat = v1$dim[[2]]$vals # dim[[2]] is lat
# Using deconstruct function c() to turn matrices into numeric class output
uvals <- c(u)
uvalsdf50w <- as.data.frame(uvals)
# Now, need to match lat (Latitude) and lon (Longitude) to data pts
# same lat applied to 7 uvals at a time
lat1list <- c(lat[1],lat[1],lat[1],lat[1],lat[1],lat[1],lat[1])
# ...and so on, repeat up to...
lat12list <- c(lat[12],lat[12],lat[12],lat[12],lat[12],lat[12],lat[12])
# Next: lon applied to 7 uvals
# 1 column and 84 rows in uvalsdf
colnames(uvalsdf50w) <- "uwinter50"
uvalsdf50w$Lat <- c(lat1list, lat2list, lat3list, lat4list, lat5list,
lat6list, lat7list, lat8list, lat9list, lat10list, lat11list, lat12list)
uvalsdf50w$Lon <- c(lon,lon,lon,lon,lon,lon,lon,lon,lon,lon,lon,lon)
head(uvalsdf50w)
#   uwinter50      Lat       Lon
#1 0.02767440 20.85938 -157.4055
#2 0.02629073 20.85938 -157.3671
#3 0.02462446 20.85938 -157.3286

# Now we have an easy-to-read df of values matched to coords. 

Hope this is useful!

Reply via email to