I want to read and stack several .grd files and export it as .nc file in R. I am using the following commands
library(raster)
library(ncdf4)
library(RNetCDF)
library(rgdal)
files <-list.files(path="G:/Gridded data/",
pattern="GRD", all.files=FALSE, full.names=TRUE,recursive=TRUE)
s <- stack(files)
rstack <- raster(files[1])
But it is giving the following error
Error in .local(.Object, ...) :
Error in .rasterObjectFromFile(x, band = band, objecttype = "RasterLayer", :
Cannot create a RasterLayer object from this file.
Can anyone help? thanks in advance
I would try pattern="\\.grd$" (this means, the file ends on ".grd"). Or at least pattern="grd" instead of pattern="GRD". (or use ignore.case = TRUE)
files <- list.files(path="G:/Gridded data/", pattern="\\.grd$", full.names=TRUE,recursive=TRUE)
Related
First, I have installed the following packages in R-Markdown file
install.packages("tidyverse")
install.packages("ggplot2")
library(tidyverse)
library(ggplot2)
When I try to Knit to HTML or Word, I got this error-
*
Error in contrib.url(repos, "source") : trying to use CRAN without setting a mirror calls:... withvisible->eval->eval->install.packages->contrib.url
Execution halted*
Your help is appreciated in advance. Thank you
#ViviG where do I have to put this {r eval=FALSE}?
This is my current query:
Visualization
You can also embed the visualization, for example:
install.packages("tidyverse")
install.packages("ggplot2")
install.packages("palmerpenguins")
library(tidyverse)
library(ggplot2)
library(palmerpenguins)
ggplot(data=penguins)+
geom_point(mapping=aes(x=flipper_length_mm,y=body_mass_g,color=species))+
labs(title = "Palmer Penguins: Body Mass vs. Flipper Length", subtitle = "Sample of Three Penguin Species",
caption = "Data collected by Dr. Kristen Gorman")+
annotate("text",x=220,y=4000,label="The Gentoos are the largest",color="blue",fontface="bold",size=4.5,angle=30)
Same error displayed after knit-ing
I have been searching for following Python solution to copy selectively lines from 1 txt file to another. I can copy the whole file, but with only a few lines I get an error.
My code:
f = open(from_file, "r")
g = open(to_file, "w")
#copy = open(to_file, "w") # this instruction copies whole file
rowcond2 = 'xxxx' # look for this string sequence in every line
for line in f:
if rowcond2 in f:
copy.write(line,"w") in g # write every corresponding line to destination
f.close()
# copy.close() # code receive error to close destination
g.close()
So without the rowcond2, I can copy the whole file. Yet with the condition nothing is written to destination file.
Thank you for your help.
Why not to put your condition inside the for loop?
for line in f:
if condition:
copy.write(line)
I have been able to solve this case searching on SO:
Using python to write specific lines from one file to another file
#Lukas Graf: thank you for your detailed step wise explanation.
I have an output file from a code which its name will ends to "_x.txt" and I want to connect two codes which second code will use this file as an input and will add more data into it. Finally, it will ends into "blabla_x_f.txt"
I am trying to work it out as below, but seems it is not correct and I could not solve it. Please help:
inf = str(raw_input(*+"_x.txt"))
with open(inf+'_x.txt') as fin, open(inf+'_x_f.txt','w') as fout:
....(other operations)
The main problem is that the "blabla" part of the file could change to any thing every time and will be random strings, so the code needs to be flexible and just search for whatever ends with "_x.txt".
Have a look at Python's glob module:
import glob
files = glob.glob('*_x.txt')
gives you a list of all files ending in _x.txt. Continue with
for path in files:
newpath = path[:-4] + '_f.txt'
with open(path) as in:
with open(newpath, 'w') as out:
# do something
all!
I am trying to solve the following issue using PowerShell.
Basically, I have setup a file with the needed properties. Let's call it "FileA.xlsx".
I have a text file which contains a list of names, i.e:
FileB.xlsx
DumpA.xlsx
EditC.xlsx
What I am trying is to duplicate "FileA.xlsx" serveral times and use all the names from the text file, so in the end I should end up with 4 files (all of them are copies of "FileA.xlsx":
FileA.xlsx
FileB.xlsx
DumpA.xlsx
EditC.xlsx
Assuming that you have a file called files.txt with the following content:
bob2.txt
bob3.txt
bob4.txt
Then the following will do what you want:
$sourceFile = "FileA.xlsx"
gc .\files.txt | % {
Copy-Item $sourceFile $_
}
This will create a copy of the FileA.xlsx with the names bob2.txt, bob3.txt and bob4.txt
I am using a command like below to get list of files in a folder.
My pattern argument is not working correctly. I want to pull a list of only jpg or csv files. How should i set my pattern argument?
Moreover, that folder contains .RData and .Rhistory files that are created by the system. I didnt put them. In such case is there an easier way to ignore system files when i compile a list of files in a folder?
filenames=list.files(path = "//c:/ch7data", pattern = "*.jpg|*.csv")
-------------------------update1
as per suggestions provided in one of the answers I used below code, but it didnt seem to work :(.
I got .Rdata and .rhistory and also . and .. I dont want these files/values.
I want only jpg and csv and xlsx files in the variable filenames
filenames=list.files(path = "//C:/ch7data", all.files = TRUE)
> filenames
[1] "." ".." ".RData" ".Rhistory"
[5] "CH7Data_20130401T130110.csv" "CH7Data_20130401T130110.jpg" "CH7Data_20130401T130610.csv" "CH7Data_20130401T130610.jpg"
[9] "CH7Data_20130401T131610.csv" "CH7Data_20130401T131610.jpg" "CH7Data_20130401T135010.csv" "CH7Data_20130401T135010.jpg"
[13] "ffa.xlsx" "Thumbs.db"
-----------------------update2
I used a command as below and it avoided . , .. , .Rdata and .rhistory :)
any way to avoid thumbs.db ?
> filenames=list.files(path = "//C:/ch7data", all.files = FALSE, no..=TRUE)
> filenames
[1] "CH7Data_20130401T130110.csv" "CH7Data_20130401T130110.jpg" "CH7Data_20130401T130610.csv" "CH7Data_20130401T130610.jpg"
[5] "CH7Data_20130401T131610.csv" "CH7Data_20130401T131610.jpg" "CH7Data_20130401T135010.csv" "CH7Data_20130401T135010.jpg"
[9] "ffa.xlsx" "Thumbs.db"
Another alternative:
Sys.glob(file.path("c:","ch7data",c("*.jpg","*.csv")))
Better to use globs rather than regular expressions
Use file.path to create path in OS independent manner.
To get only files that ends with .jpg or .csv, you can use the following pattern:
list.files(path = "//c:/ch7data", pattern = "^(.*)+(\\.jpg|\\.csv)$")