# 5.3 Matrix

This folder contains examples of matrix manipulation, calculation and graphing with originpro package.
For matrix related functions in originpro, see matrixsheet.
For project related functions, see project.

## 3D Parametric Plot From Matrix

'''This sample shows how to plot parametric 3D surface from a matrix sheet.'''
import originpro as op
import numpy as np

# Generate torus mesh
angle = np.linspace(0, 2*np.pi, 32)
theta, phi = np.meshgrid(angle, angle)
r, R = .25, 1.
X = (R + r * np.cos(phi)) * np.cos(theta)
Y = (R + r * np.cos(phi)) * np.sin(theta)
Z = r * np.sin(phi)
arr_3D = np.stack([Z,X,Y], axis=0) # Stack X,Y,Z to get a 3D array

# Pass the data to a matrix sheet and plot
ms = op.new_sheet('m')
ms.from_np(arr_3D)

gp = op.new_graph(template='GLparafunc')
gp.rescale('z')

'''
Origin2023b or later is required to run this sample code.

This example shows how to use Origin Image Window's ability to directly import an image using
a URL address, which is not yet supported in a matrix window. Once image is downloaded into an
Image Window, it can be converted to a matrix with the coordinates from the GeoTIFF retained.
The sample also shows to download a graph template from Originlab to make our plot
'''
import originpro as op
#use the smaller 2-arc sec DEM available only for Alaska from National Map
url = r'https://prd-tnm.s3.amazonaws.com/StagedProducts/Elevation/2/TIFF/historical/n59w154/USGS_2_n59w154_20210401.tif'
iw=op.new_image()
iw.from_file(url)
#create an empty matrix and tranfer the image to it
mat = op.new_book('m')
mat.from_img(iw)
#you may want to keep the image window and comment out the next line but
#it is no longer needed to make the graph
iw.destroy()
#On Template Center dialog, click any template will open the weblink to see the fid
#here 939 = RaisedReliefMap.otpu
if nn < 0:
if nn==-30:

gp = op.new_graph(template='RaisedReliefMap')

#we need to turn on speed mode and hide the speed mode banner
gp.set_int('Banner', 0)
gl = gp
gl.set_int('MatMaxPtsEnabled', 1)
gl.rescale()

#if template did not have clip data enabled
#gl.set_int('clip',1)

plot.colormap = 'Magma.PAL'
ax=gl.axis('x')
ax.sto=-153.2
ay=gl.axis('y')
ay.set_limits(58.4,59)

gl.set_int('light.direction.h', 74)
gl.set_int('light.direction.v', 27)

## Import NetCDF with Partial Settings

'''
This example shows importing from the web and to specify from Jan of each year
as well as doing proper longitude and latitude corrections.
'''
import originpro as op
mat = op.new_sheet('m')
#you can actually use URL in from_file
url = 'https://www.unidata.ucar.edu/software/netcdf/examples/tos_O1_2001-2002.nc'
#keep the connector so after the import, you can click on the icon to
#choose Options... to see open the dialog to see the details indicated by the sel string
mat.from_file(url, True, dctype='NetCDF', sel='NetCDF/tos[1:0|1-11][y#][x/2]')

## Matrix Dot Product using numpy

'''
This sample shows basic data transfer between Origin matrix and a NumPy array.
Make sure you've installed pandas before trying the following sample.
So to check for and install if needed, open the Script Window (Shift+Alt+3),
type the following and press Enter:
pip -chk pandas
'''
import numpy as np
import originpro as op

#integer matrix data
aa = np.array([	[1, 2, 3], [4, 5, 6] ])
bb = np.array([	[10,11], [20,21], [30,31] ])

#create a new hidden matrix book, and get the matrix sheet
ma=op.new_sheet('m')

#matrix sheet can hold a 3D array, shape and data type is automatically set
ma.from_np(aa)

#another sheet in same book
mb.from_np(bb)

#put result into 3rd sheet

#do the actual calculation using numpy
#here each sheet has only one matrix object, so we get it as 2d array
cc = np.dot(ma.to_np2d(), mb.to_np2d())
mc.from_np(cc)

## Plot Heatmap from Matrix

'''This sample shows how to plot a heatmap from a matrix sheet.'''
import originpro as op
import numpy as np

arr_2D = np.random.randint(0,4, size=(4,4))
ms = op.new_sheet('m')
ms.from_np(arr_2D)
ms.xymap = -3, 3, -3, 3
gp = op.new_graph(template='heatmap')
gp.rescale('z')
#default XY scale will include the half width of each data point
#so to have same scale as the matrix, we need to force it
gp.set_xlim(-3, 3)
gp.set_ylim(-3, 3)

## Use OpenCV to Load TIFF Images into Matrix Sheet

'''
This example shows how to import tif images into a 3d numpy array and then pass into Origin matrix book.
To install openCV (cv2), do the following from Script Window
pip install opencv-python
'''
import originpro as op
import numpy as np
import cv2

# import tifs into array
# Assume all images share the same dimesion
ImArray = np.array([])
for idx in range(7):
fname = op.path('e') + f'Samples\\Image Processing and Analysis\\myocyte{idx+1}.tif'
ms.show_image(True)