Subsetting and saving netcdf in R
I am trying to subset 8 netcdf files (one of them is here) in a foor loop according to a shorter period of time and then to save them as a new netcdf file.
I saw that other people already asked on how to subset netcdf files according to different time period (here or here) but once I do it on my netcdf files the "for loop" keeps on running without finishing (not even the first netcdf file) and I can't figure out why.
Here the code I use:
library(raster)
library(netcdf4)
library(lubridate)
#setting wd=indir containing netcdf files
setwd(indir)
files=list.files(pattern="nc")
for (j in seq_along(files)){
#setting wd containing netcdf files in the loop
setwd(indir)
b<-brick(files[j])
nc<-nc_open(files[j])
#variable
varname<-names(nc[['var']][3])
varunits <- ncatt_get(nc,varname,"units")[[2]]
lon<-ncvar_get(nc,"lon")
lat<-ncvar_get(nc,"lat", verbose = F)
time<-ncvar_get(nc, "time")
tunits <- ncatt_get(nc, "time", "units")[[2]]
dlname <- ncatt_get(nc, varname,"long_name")[[2]]
nc_close(nc)
#assigning a crs
proj4string(b)<-"+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0"
#setting time as.Date
tm<-ymd(getZ(b))
#setting time to rasterBrick
b<-setZ(b, tm)
# subsetting
b2<-subset(b, which(tm < as.Date('2006-01-01')))
#setting wd where I want to save the "new" netcdf files
setwd(outdir)
writeRaster(b2, filename = paste0(varname, "_1971_2006_Noce.nc"),
format="CDF", varname=varname, varunit=varunits, longname=dlname,
xname="lon", yname="lat", zname="time", zunit=tunits, overwrite=TRUE)
}
Any help on how to get the loop working would be very much appreciated!
r netcdf
add a comment |
I am trying to subset 8 netcdf files (one of them is here) in a foor loop according to a shorter period of time and then to save them as a new netcdf file.
I saw that other people already asked on how to subset netcdf files according to different time period (here or here) but once I do it on my netcdf files the "for loop" keeps on running without finishing (not even the first netcdf file) and I can't figure out why.
Here the code I use:
library(raster)
library(netcdf4)
library(lubridate)
#setting wd=indir containing netcdf files
setwd(indir)
files=list.files(pattern="nc")
for (j in seq_along(files)){
#setting wd containing netcdf files in the loop
setwd(indir)
b<-brick(files[j])
nc<-nc_open(files[j])
#variable
varname<-names(nc[['var']][3])
varunits <- ncatt_get(nc,varname,"units")[[2]]
lon<-ncvar_get(nc,"lon")
lat<-ncvar_get(nc,"lat", verbose = F)
time<-ncvar_get(nc, "time")
tunits <- ncatt_get(nc, "time", "units")[[2]]
dlname <- ncatt_get(nc, varname,"long_name")[[2]]
nc_close(nc)
#assigning a crs
proj4string(b)<-"+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0"
#setting time as.Date
tm<-ymd(getZ(b))
#setting time to rasterBrick
b<-setZ(b, tm)
# subsetting
b2<-subset(b, which(tm < as.Date('2006-01-01')))
#setting wd where I want to save the "new" netcdf files
setwd(outdir)
writeRaster(b2, filename = paste0(varname, "_1971_2006_Noce.nc"),
format="CDF", varname=varname, varunit=varunits, longname=dlname,
xname="lon", yname="lat", zname="time", zunit=tunits, overwrite=TRUE)
}
Any help on how to get the loop working would be very much appreciated!
r netcdf
1
If your code is indeedfiles=list.files(pattern="nc)
then you've missed a"
to close out that string.
– shians
Nov 21 '18 at 23:32
Try adding a printed out counter at various points in your code (ieprint(j)
orprint(varname)
) throughout your loop that might tell you where it is going wrong
– RAB
Nov 22 '18 at 7:57
Thank you shians, but unfortunately this was just a transcription error when I copy pasted the code here. In the code itself I close out that string.
– Ste
Nov 22 '18 at 8:08
thanks, I have added some print(j) and now I get this error: Error in R_nc4_create: Invalid argument (creation mode was 0) Error in ncdf4::nc_create(filename, vardef, force_v4 = force_v4) : Error in nc_create! could you help me understanding why it refers to "nc_create" in writeRaster?
– Ste
Nov 22 '18 at 9:07
hi, your code works well for me. The only thing that I notice is that when you open the subsetted nc file the dates are not well represented. Try to UPDATE your R version and related packages as a start.
– aaaaa
Nov 23 '18 at 18:32
add a comment |
I am trying to subset 8 netcdf files (one of them is here) in a foor loop according to a shorter period of time and then to save them as a new netcdf file.
I saw that other people already asked on how to subset netcdf files according to different time period (here or here) but once I do it on my netcdf files the "for loop" keeps on running without finishing (not even the first netcdf file) and I can't figure out why.
Here the code I use:
library(raster)
library(netcdf4)
library(lubridate)
#setting wd=indir containing netcdf files
setwd(indir)
files=list.files(pattern="nc")
for (j in seq_along(files)){
#setting wd containing netcdf files in the loop
setwd(indir)
b<-brick(files[j])
nc<-nc_open(files[j])
#variable
varname<-names(nc[['var']][3])
varunits <- ncatt_get(nc,varname,"units")[[2]]
lon<-ncvar_get(nc,"lon")
lat<-ncvar_get(nc,"lat", verbose = F)
time<-ncvar_get(nc, "time")
tunits <- ncatt_get(nc, "time", "units")[[2]]
dlname <- ncatt_get(nc, varname,"long_name")[[2]]
nc_close(nc)
#assigning a crs
proj4string(b)<-"+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0"
#setting time as.Date
tm<-ymd(getZ(b))
#setting time to rasterBrick
b<-setZ(b, tm)
# subsetting
b2<-subset(b, which(tm < as.Date('2006-01-01')))
#setting wd where I want to save the "new" netcdf files
setwd(outdir)
writeRaster(b2, filename = paste0(varname, "_1971_2006_Noce.nc"),
format="CDF", varname=varname, varunit=varunits, longname=dlname,
xname="lon", yname="lat", zname="time", zunit=tunits, overwrite=TRUE)
}
Any help on how to get the loop working would be very much appreciated!
r netcdf
I am trying to subset 8 netcdf files (one of them is here) in a foor loop according to a shorter period of time and then to save them as a new netcdf file.
I saw that other people already asked on how to subset netcdf files according to different time period (here or here) but once I do it on my netcdf files the "for loop" keeps on running without finishing (not even the first netcdf file) and I can't figure out why.
Here the code I use:
library(raster)
library(netcdf4)
library(lubridate)
#setting wd=indir containing netcdf files
setwd(indir)
files=list.files(pattern="nc")
for (j in seq_along(files)){
#setting wd containing netcdf files in the loop
setwd(indir)
b<-brick(files[j])
nc<-nc_open(files[j])
#variable
varname<-names(nc[['var']][3])
varunits <- ncatt_get(nc,varname,"units")[[2]]
lon<-ncvar_get(nc,"lon")
lat<-ncvar_get(nc,"lat", verbose = F)
time<-ncvar_get(nc, "time")
tunits <- ncatt_get(nc, "time", "units")[[2]]
dlname <- ncatt_get(nc, varname,"long_name")[[2]]
nc_close(nc)
#assigning a crs
proj4string(b)<-"+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0"
#setting time as.Date
tm<-ymd(getZ(b))
#setting time to rasterBrick
b<-setZ(b, tm)
# subsetting
b2<-subset(b, which(tm < as.Date('2006-01-01')))
#setting wd where I want to save the "new" netcdf files
setwd(outdir)
writeRaster(b2, filename = paste0(varname, "_1971_2006_Noce.nc"),
format="CDF", varname=varname, varunit=varunits, longname=dlname,
xname="lon", yname="lat", zname="time", zunit=tunits, overwrite=TRUE)
}
Any help on how to get the loop working would be very much appreciated!
r netcdf
r netcdf
edited Nov 22 '18 at 9:20
Bart
4,77032043
4,77032043
asked Nov 21 '18 at 22:53
SteSte
11
11
1
If your code is indeedfiles=list.files(pattern="nc)
then you've missed a"
to close out that string.
– shians
Nov 21 '18 at 23:32
Try adding a printed out counter at various points in your code (ieprint(j)
orprint(varname)
) throughout your loop that might tell you where it is going wrong
– RAB
Nov 22 '18 at 7:57
Thank you shians, but unfortunately this was just a transcription error when I copy pasted the code here. In the code itself I close out that string.
– Ste
Nov 22 '18 at 8:08
thanks, I have added some print(j) and now I get this error: Error in R_nc4_create: Invalid argument (creation mode was 0) Error in ncdf4::nc_create(filename, vardef, force_v4 = force_v4) : Error in nc_create! could you help me understanding why it refers to "nc_create" in writeRaster?
– Ste
Nov 22 '18 at 9:07
hi, your code works well for me. The only thing that I notice is that when you open the subsetted nc file the dates are not well represented. Try to UPDATE your R version and related packages as a start.
– aaaaa
Nov 23 '18 at 18:32
add a comment |
1
If your code is indeedfiles=list.files(pattern="nc)
then you've missed a"
to close out that string.
– shians
Nov 21 '18 at 23:32
Try adding a printed out counter at various points in your code (ieprint(j)
orprint(varname)
) throughout your loop that might tell you where it is going wrong
– RAB
Nov 22 '18 at 7:57
Thank you shians, but unfortunately this was just a transcription error when I copy pasted the code here. In the code itself I close out that string.
– Ste
Nov 22 '18 at 8:08
thanks, I have added some print(j) and now I get this error: Error in R_nc4_create: Invalid argument (creation mode was 0) Error in ncdf4::nc_create(filename, vardef, force_v4 = force_v4) : Error in nc_create! could you help me understanding why it refers to "nc_create" in writeRaster?
– Ste
Nov 22 '18 at 9:07
hi, your code works well for me. The only thing that I notice is that when you open the subsetted nc file the dates are not well represented. Try to UPDATE your R version and related packages as a start.
– aaaaa
Nov 23 '18 at 18:32
1
1
If your code is indeed
files=list.files(pattern="nc)
then you've missed a "
to close out that string.– shians
Nov 21 '18 at 23:32
If your code is indeed
files=list.files(pattern="nc)
then you've missed a "
to close out that string.– shians
Nov 21 '18 at 23:32
Try adding a printed out counter at various points in your code (ie
print(j)
or print(varname)
) throughout your loop that might tell you where it is going wrong– RAB
Nov 22 '18 at 7:57
Try adding a printed out counter at various points in your code (ie
print(j)
or print(varname)
) throughout your loop that might tell you where it is going wrong– RAB
Nov 22 '18 at 7:57
Thank you shians, but unfortunately this was just a transcription error when I copy pasted the code here. In the code itself I close out that string.
– Ste
Nov 22 '18 at 8:08
Thank you shians, but unfortunately this was just a transcription error when I copy pasted the code here. In the code itself I close out that string.
– Ste
Nov 22 '18 at 8:08
thanks, I have added some print(j) and now I get this error: Error in R_nc4_create: Invalid argument (creation mode was 0) Error in ncdf4::nc_create(filename, vardef, force_v4 = force_v4) : Error in nc_create! could you help me understanding why it refers to "nc_create" in writeRaster?
– Ste
Nov 22 '18 at 9:07
thanks, I have added some print(j) and now I get this error: Error in R_nc4_create: Invalid argument (creation mode was 0) Error in ncdf4::nc_create(filename, vardef, force_v4 = force_v4) : Error in nc_create! could you help me understanding why it refers to "nc_create" in writeRaster?
– Ste
Nov 22 '18 at 9:07
hi, your code works well for me. The only thing that I notice is that when you open the subsetted nc file the dates are not well represented. Try to UPDATE your R version and related packages as a start.
– aaaaa
Nov 23 '18 at 18:32
hi, your code works well for me. The only thing that I notice is that when you open the subsetted nc file the dates are not well represented. Try to UPDATE your R version and related packages as a start.
– aaaaa
Nov 23 '18 at 18:32
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53421561%2fsubsetting-and-saving-netcdf-in-r%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53421561%2fsubsetting-and-saving-netcdf-in-r%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
If your code is indeed
files=list.files(pattern="nc)
then you've missed a"
to close out that string.– shians
Nov 21 '18 at 23:32
Try adding a printed out counter at various points in your code (ie
print(j)
orprint(varname)
) throughout your loop that might tell you where it is going wrong– RAB
Nov 22 '18 at 7:57
Thank you shians, but unfortunately this was just a transcription error when I copy pasted the code here. In the code itself I close out that string.
– Ste
Nov 22 '18 at 8:08
thanks, I have added some print(j) and now I get this error: Error in R_nc4_create: Invalid argument (creation mode was 0) Error in ncdf4::nc_create(filename, vardef, force_v4 = force_v4) : Error in nc_create! could you help me understanding why it refers to "nc_create" in writeRaster?
– Ste
Nov 22 '18 at 9:07
hi, your code works well for me. The only thing that I notice is that when you open the subsetted nc file the dates are not well represented. Try to UPDATE your R version and related packages as a start.
– aaaaa
Nov 23 '18 at 18:32