Imported GeoTIFF file returns only NAN values - import

I have been trying to import a GEOTIFF file that shows irradiance data. I am able to plot (see Plot here) the file so I am sure the file has data; however; the raster array contains only nan values. Is there any way to resolve this? I am quite new to python so perhaps I've missed something obvious, any help would be appreciated :)
This is the code I'm using:
from osgeo import gdal
import numpy as np
import matplotlib.pyplot as plt
import gc
gc.enable()
#import
ds = gdal.Open('DNI_Spain.tif')
gt = ds.GetGeoTransform()
proj = ds.GetProjection()
band = ds.GetRasterBand(1)
array = band.ReadAsArray()
max_value = np.max(array)
min_value = np.min(array)
median_value = np.median(array)
plt.figure()
plt.imshow(array)

Related

Unable to visualise NetCDF DEM using basemap in jupyter

I am trying to visualise the DEM tiles which are in a netCDF format using python, but the following code is resulting in just the country boundaries and grids and not any data plotted.
from netCDF4 import Dataset
import netCDF4
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
nc = 'C:/Users/S05E135.SRTMGL1_NC.nc'
fh = Dataset(nc, mode='r')
fh.variables.keys()
lons = fh.variables['lon'][:]
lats = fh.variables['lat'][:]
height= fh.variables['SRTMGL1_DEM'][:]
height_units = fh.variables['SRTMGL1_DEM'].units
fh.close()
# Plotting the data
# Get some parameters for the Stereographic Projection
lon_0 = lons.mean()
lat_0 = lats.mean()
m = Basemap(width=4000000,height=3500000,
resolution='l',projection='stere',\
lat_ts=40,lat_0=lat_0,lon_0=lon_0)
# use meshgrid to create 2D arrays
lon, lat = np.meshgrid(lons, lats)
xi, yi = m(lon, lat)
# Plot Data
cs = m.pcolor(xi,yi,np.squeeze(height))
# Add Grid Lines
m.drawparallels(np.arange(-80., 81., 10.), labels=[1,0,0,0], fontsize=10)
m.drawmeridians(np.arange(-180., 181., 10.), labels=[0,0,0,1], fontsize=10)
# Add Coastlines, States, and Country Boundaries
m.drawcoastlines()
m.drawstates()
m.drawcountries()
# Add Colorbar
cbar = m.colorbar(cs, location='bottom', pad="10%")
cbar.set_label(height_units)
# Add Title
plt.title('dem')
plt.show()
Image of the output coming from the code

From Workspace Data Structure

I am importing data from Simulink, flip the data and then try to import the data with the Simulink block:
from workspace
This always returns a error. I tried this, but it doesn't work:
simOut = sim('sim1', Simulation_Time);
t = simOut.P.time;
P_tilde = simOut.P.Data; % size: 2x2x3001
P_data = flip(P_tilde); % size: 2x2x3001, but with the data flipped
P_import = P_data; % import block for 'sim2'
simOut2 = sim('sim2', Simulation_Time);
The error returned:
Unsupported input format for From Workspace block 'sim2/From Workspace'. Available formats are double non-complex matrix, a structure with or without time,
or a structure with MATLAB timeseries as leaf nodes. All formats require the data to be finite (not Inf or NaN).
Does anyone has a idea how to solve it? I have tried and read the description for the "from workspace" block, but I am not any smarter. For example, the functions recommended to use; "timeseries(P_data)" or "timetable(P_data)" do not work and just return errors.
The solution is little bit too complicated, but it works.
I flip each element of mx 2x2xn matrix, then I create a timeseries():
simOut = sim('m2_simP', 'StartTime','0','StopTime', num2str(Simulation_Time),'FixedStep',num2str(Time_Step));
t = simOut.P.time;
P_tilde = simOut.P.Data;
p11 = flip(reshape(P_tilde(1,1,:),[],1));
p12 = flip(reshape(P_tilde(1,2,:),[],1));
p22 = flip(reshape(P_tilde(2,2,:),[],1));
for i = 1:size(t,1)
P_data(1,1,i) = p11(i,1);
P_data(1,2,i) = p12(i,1);
P_data(2,1,i) = p12(i,1);
P_data(2,2,i) = p22(i,1);
end
P_import = timeseries(P_data, t);

Image data can't be converted to float

#import required python modules
import cv2 # opencv
import pytesseract # tesseract
import matplotlib.pyplot as plt
import numpy as np
#read the text image using imread
org_img = cv2.imread("Poem.png")
plt.imshow(org_img) #display the read image
What is wrong with this code ??
I am getting the error that "Image data can't be converted into float"
Convert the image to RGB first:
org_img = cv2.imread("Poem.png")
org_img = cv2.cvtColor(org_img , cv2.COLOR_BGR2RGB)
plt.imshow(org_img)
plt.show()
This works for me in Python/OpenCV. You forgot to add plt.show() after plt.imshow(org_img)
#import required python modules
import cv2 # opencv
import pytesseract # tesseract
import matplotlib.pyplot as plt
import numpy as np
#read the text image using imread
org_img = cv2.imread("Poem.png")
plt.imshow(org_img) #display the read image
plt.show()

Import data series from a txt file in MATLAB

Hello I have data series in a txt file like structure below. How do I import the contents of this file in MATLAB? ({} is cell)
input:
Date,code,OPEN,HIGH,LOW,CLOSE,VOL
20081206,IDXS,9248.80,9248.80,9167.80,9178.30,8539624
output:
{Date} {code} {OPEN} {HIGH} {LOW} {CLOSE} {VOL}
{20081206} {IDXS} {9248.80} {9248.80} {9167.80} {9178.30} {8539624}
If you want everything in cells, you can do the following in releases prior to R2019a:
>> t = readtable('table.txt');
>> c = [t.Properties.VariableNames; table2cell(t)]
Since R2019a:
>> c = readcell('table.txt')
Assuming your data is saved in the fle dat.txt, then you can try the following code. Here are two approaches:
if you are using Matlab R2019:
text = readcell('dat.txt');
if you are using lower version of Matlab or Octave:
fid = fopen('dat.txt');
C = textscan(fid,"%s");
text = cellfun(#(x) strsplit(x,","), C{:},"UniformOutput",false);
fclose(fid);

How to import pts file in Matlab

I have a pts file with 2d points (x,y) that looks like this:
version: 1
n_points: 5
{
159.128 108.541
230.854 109.176
164.841 179.633
193.404 193.597
192.769 229.143
}
How can I read this file and import this data into variables in Matlab?
Thanks.
i would do it like that
FileId=fopen(Filename)
npoints=textscan(FileId,'%s %f',1,'HeaderLines',1)
points=textscan(FileId,'%f %f',npoints{2},'MultipleDelimsAsOne',1,'Headerlines',1)
% now you have the values you want you can put them in a matrix or any variable
Y=cell2mat(C);