I am tring to resample Chirps data from 0.25 to 0.0025degrees resolution with the following code, I am still using 1 as an upscale factor to get the code to run:
import rasterio
from rasterio.enums import Resampling
import os
upscale_factor = 1
i = 0
for files in os.listdir(r'D:/CHIRPS RSDATA/CHIRPS(1981-1983)'):
if files [-4:] == '.tif':
i = i + 1
fp = r'D:\CHIRPS RSDATA\CHIRPS(1981-1983)'+'\\'+files
print(fp)
dataset = rasterio.open(fp)
data = dataset.read(out_shape=(dataset.count,
int(dataset.height*upscale_factor),
int(dataset.width*upscale_factor)),
resampling=Resampling.bilinear) #You can change the resamplng technique here
if data.shape[-1] != 0 and data.shape[-2] != 0:
transform=dataset.transform*dataset.transform.scale((dataset.width/data.shape[-1]),(dataset.height/data.shape[-2]))
height = int(dataset.height*upscale_factor)
width = int(dataset.width*upscale_factor)
with rasterio.Env():
profile = dataset.profile
profile.update(transform=transform,driver='GTiff',height=height,width=width)
with rasterio.open('D:\CHIRPS RSDATA\CHIRPS(1981-1983)\Rainfalls',"w",**profile) as output:
output.write(data)
print("Image resampled...")
and I am getting this error:
runfile('D:/CHIRPS RSDATA/CHIRPS(1981-1983)/Resampling_python.py', wdir='D:/CHIRPS RSDATA/CHIRPS(1981-1983)')
D:\CHIRPS RSDATA\CHIRPS(1981-1983)\chirps-v2.0.1981.01.01.tif
Traceback (most recent call last):
File "rasterio\_io.pyx", line 1139, in rasterio._io.DatasetWriterBase.__init__
File "rasterio\_err.pyx", line 215, in rasterio._err.exc_wrap_pointer
CPLE_OpenFailedError: Attempt to create new tiff file 'D:/CHIRPS RSDATA/CHIRPS(1981-1983)/Rainfalls' failed: Permission denied
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:\CHIRPS RSDATA\CHIRPS(1981-1983)\Resampling_python.py", line 42, in <module>
with rasterio.open('D:\CHIRPS RSDATA\CHIRPS(1981-1983)\Rainfalls',"w",**profile) as output:
File "C:\Users\khoda\anaconda3\lib\site-packages\rasterio\env.py", line 435, in wrapper
return f(*args, **kwds)
File "C:\Users\khoda\anaconda3\lib\site-packages\rasterio\__init__.py", line 225, in open
s = writer(path, mode, driver=driver,
File "rasterio\_io.pyx", line 1143, in rasterio._io.DatasetWriterBase.__init__
RasterioIOError: Attempt to create new tiff file 'D:/CHIRPS RSDATA/CHIRPS(1981-1983)/Rainfalls' failed: Permission denied
I tried changing the permissions of the folder with no luck, I also double checked the synaxes as other people have suggested. Please help.
Related
i'm using replits database but when i try to load it in it returns an error
any ideas?
cookie = db[name]
cookiepc = db[name + "cookiepc"]
increase = db[name + "increase"]
the error is
Traceback (most recent call last):
File "main.py", line 25, in <module>
cookiepc = db[name + "cookiepc"]
File "/home/runner/Cookie-clicker/venv/lib/python3.8/site-packages/replit/database/database.py", line 439, in __getitem__
raw_val = self.get_raw(key)
File "/home/runner/Cookie-clicker/venv/lib/python3.8/site-packages/replit/database/database.py", line 479, in get_raw
raise KeyError(key)
KeyError: 'shdfgwbdhfbadwcookiepc'
According to the documentation for replit-py, a KeyError is raised when an attempt is made to read from a key that doesn't exist in the database.
You can use db.get to specify a default value for if the key doesn't exist:
print(db.get("b", "default")) # default
db["b"] = "pie"
print(db.get("b", "default")) # pie
I am trying to run a simple example using sbmltoodepy and I am receiving the following error.
Traceback (most recent call last):
File "C:\Users\AppData\Roaming\JetBrains\PyCharm2021.2\scratches\scratch_79.py", line 8, in
ParseAndCreateModel(inputFilePath)#, jsonFilePath=None, outputFilePath=None)#, className="SBMLmodel")
File "C:\Users\AppData\Roaming\Python\Python39\site-packages\sbmltoodepy\utilities.py", line 66, in ParseAndCreateModel
modelData = ParseSBMLFile(inputFilePath)
File "C:\Users\AppData\Roaming\Python\Python39\site-packages\sbmltoodepy\parse.py", line 284, in ParseSBMLFile
assert(doc.getNumErrors() == 0)
AssertionError
Here is the code that I used.
import sbmltoodepy
from sbmltoodepy import ParseAndCreateModel
inputFilePath = "E:/Borisov2009_insulin_EGF.xml"
ParseAndCreateModel(inputFilePath, jsonFilePath=None, outputFilePath=None, className="SBMLmodel")
model = sbmltoodepy.SBMLmodel()
model.RunSimulation(1, absoluteTolerance = 1e-12, relativeTolerance = 1e-6)
Thank you in advance for your time.
I want to load a text file in python using NumPy library. The text file has float type data on 9516 rows & 39 columns & is 6.2mbites. The following command is used:
p=np.loadtxt(fname = "E:\PhD Data\Aphrodite data\APHRO\outfile\rain2007.txt")
& got the following errors:
Traceback (most recent call last):
File "<ipython-input-126-dfa85ca1950b>", line 1, in <module>
p=np.loadtxt(fname = "E:\PhD Data\Aphrodite data\APHRO\outfile\rain2007.txt")
File "C:\Users\Sohaib\Anaconda3\lib\site-packages\numpy\lib\npyio.py", line 962, in loadtxt
fh = np.lib._datasource.open(fname, 'rt', encoding=encoding)
File "C:\Users\Sohaib\Anaconda3\lib\site-packages\numpy\lib\_datasource.py", line 266, in open
return ds.open(path, mode, encoding=encoding, newline=newline)
File "C:\Users\Sohaib\Anaconda3\lib\site-packages\numpy\lib\_datasource.py", line 624, in open
raise IOError("%s not found." % path)
ain2007.txt not found.phrodite data\APHRO\outfile
The same command is working with a smaller size file (26 rows & 39 columns). Can you tell me the possible reasons behind this error?
Try:
p=np.loadtxt(fname = "E:/PhD Data/Aphrodite data/APHRO/outfile/rain2007.txt")
the \ is a special character in Python.
xlwings works fine on my computer, but when I try to transfer the same set up to another computer it seems to not open correctly giving me this error
Traceback (most recent call last):
File "C:\Users\base7268\AppData\Local\Programs\Python\Python37\lib\site-packages\xlwings\_xlwindows.py", line 432, in __call__
return Book(xl=self.xl(name_or_index))
File "C:\Users\base7268\AppData\Local\Programs\Python\Python37\lib\site-packages\xlwings\_xlwindows.py", line 152, in __call__
v = self._inner(*args, **kwargs)
File "C:\Users\base7268\AppData\Local\Programs\Python\Python37\lib\site-packages\win32com\client\dynamic.py", line 197, in __call__
return self._get_good_object_(self._oleobj_.Invoke(*allArgs),self._olerepr_.defaultDispatchName,None)
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, None, None, None, 0, -2147352565), None)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\base7268\AppData\Local\Programs\Python\Python37\lib\site-packages\xlwings\main.py", line 2776, in open
impl = self.impl(name)
File "C:\Users\base7268\AppData\Local\Programs\Python\Python37\lib\site-packages\xlwings\_xlwindows.py", line 434, in __call__
raise KeyError(name_or_index)
KeyError: 'output2019-06-03.11-40timeseries_5-31-2019scrubbed.xlsx'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "Computation.py", line 157, in <module>
xwWb = xw.Book("output" + timeName + os.path.split(file_path)[1])
File "C:\Users\base7268\AppData\Local\Programs\Python\Python37\lib\site-packages\xlwings\main.py", line 488, in __init__
impl = app.books.open(fullname).impl
File "C:\Users\base7268\AppData\Local\Programs\Python\Python37\lib\site-packages\xlwings\main.py", line 2787, in open
impl = self.impl.open(fullname)
File "C:\Users\base7268\AppData\Local\Programs\Python\Python37\lib\site-packages\xlwings\_xlwindows.py", line 443, in open
return Book(xl=self.xl.Open(fullname))
File "C:\Users\base7268\AppData\Local\Programs\Python\Python37\lib\site-packages\xlwings\_xlwindows.py", line 63, in __call__
v = self.__method(*args, **kwargs)
File "<COMObject <unknown>>", line 8, in Open
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, 'Microsoft Excel', 'Open method of Workbooks class failed', 'xlmain11.chm', 0, -2146827284), None)
Here is some simplified code where none of the data changing is occurring. Using ospath absolute does not help with the error. Both systems run the same 64 bit operating system and 32 bit excel. Yet the error persists on the second machine. There is no real difference I can understand that would give me an error on the 2nd system over the first one. Both machines are PCs
import xlrd
import openpyxl
import xlwings as xw
from xlwings import constants
import os
import tkinter as tk
from tkinter import filedialog
import datetime
root = tk.Tk()
root.withdraw()
file_path = filedialog.askopenfilename()
print(file_path)
start = time.time()
wb = openpyxl.load_workbook(file_path)
Returns = wb['Prices']
newWs = wb.create_sheet()
newWs.title = "NominalDailyReturns"
benchWS = wb.create_sheet()
benchWS.title = "ActiveDailyReturns"
thirdWs = wb.create_sheet()
thirdWs.title = "RawAnalysis"
thirdWs.column_dimensions["A"].width = 32
name4 = thirdWs.title
print("halfway")
print(os.path.split(file_path))
print("output" + timeName + os.path.split(file_path)[1])
wb.save("output" + timeName + os.path.split(file_path)[1])
xwWb = xw.Book(os.path.abspath("output" + timeName + os.path.split(file_path)[1]))
XnewWs = xwWb.sheets['NominalDailyReturns']
xwWb.save()
xwWb.close()
wb = openpyxl.load_workbook("output" + timeName + os.path.split(file_path)[1])
benchWS = wb['ActiveDailyReturns']
wb.save("output" + timeName + os.path.split(file_path)[1])
xwWb = xw.Book("output" + timeName + os.path.split(file_path)[1])
XthirdWs = xwWb.sheets['RawAnalysis']
xwWb.save()
xwWb.close()
I figured out the problem with my code. When I used openpyxl to edit my excel data, one of the formulas was subtracting a word from a number, raising an error and corrupting the excel file. Then xlwings tried to open a corrupted file, and it failed on me.
So the solution is to not save a corrupted excel file and expect xlwings to open it.
I am trying to run a script that will go through a specific folder and use Wand to create a .png file of every .pdf file that it finds in that folder.
from wand.image import Imageimport ospdf_dir = r"D:\Program Files\Python\Python36-32\tom's shitty programs\Downloads"for x in os.listdir(pdf_dir): if x.endswith(".pdf"): pdf_path = pdf_dir + '\\' + x with Image(filename=pdf_path, resolution=300) as pdf: page_index = 0 height = pdf.height with Image(width=pdf.width, height=len(pdf.sequence)*height) as png: for page in pdf.sequence: png.composite(page, 0, page_index * height) page_index += 1 png.save(filename=pdf_path[:-3] + "png")
This returns the following errors:
Traceback (most recent call last):
File "D:\Program Files\Python\Python36-32\tom's shitty programs\venv\lib\site-packages\wand\image.py", line 1799, in wand return self.resource
File "D:\Program Files\Python\Python36-32\tom's shitty programs\venv\lib\site-packages\wand\resource.py", line 151, in resource raise DestroyedResourceError(repr(self) + ' is destroyed already')
wand.resource.DestroyedResourceError: is destroyed already
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:/Program Files/Python/Python36-32/tom's shitty programs/wand_test.py", line 13, in with Image(width=pdf.width, height=len(pdf.sequence)*height) as png:
File "D:\Program Files\Python\Python36-32\tom's shitty programs\venv\lib\site-packages\wand\image.py", line 1817, in width return library.MagickGetImageWidth(self.wand)
File "D:\Program Files\Python\Python36-32\tom's shitty programs\venv\lib\site-packages\wand\image.py", line 1801, in wand raise ClosedImageError(repr(self) + ' is closed already')
wand.image.ClosedImageError: is closed already
Any help is appreciated... thank you
This is just some minor with ... as .. context manager issues and/or typos. As the error message states, you are attempting to work with a variable (pdf) after the resources has already been closed. Double check the indentation.
with Image(filename=pdf_path, resolution=300) as pdf:
page_index = 0
height = pdf.height
with Image(width=pdf.width, height=len(pdf.sequence)*height) as png:
for page in pdf.sequence:
png.composite(page, 0, page_index * height)
page_index += 1
png.save(filename=pdf_path[:-3] + "png")
If you're using version 0.5.0 of Wand, then you might be able to take advantage of wand.image.Image.concat method.
with Image(filename=pdf_path, resolution=300) as pdf:
pdf.concat(True)
pdf.save(filename=pdf_path[:-3] + "png")