analitics

Pages

Tuesday, January 10, 2023

Python 3.7.9 : simple zodiac with pyephem and ephem.

I don't know how to calculate a zodiac exactly, from what I understand the planets and the moon must overlap. so I tested a script that calculates the dates between the planets and the moon when they appear within one degree of each other and displays them in an array with rows and columns created from these planets and the moon. The intersections on the diagonal should be none because there's obviously no way to overlap the same object, and they only occur when there's this less than one degree rule.
If you think it is wrong then you can try to fix it.
You can install pyephem and ephem with pip tool, I used both python packages:
pip install pyephem --user
Requirement already satisfied: pyephem in 
... site-packages (9.99)
Requirement already satisfied: ephem in 
...
site-packages (from pyephem) (4.1.4)
This is the source script.
import ephem

# create a list with planets objects from ephem
planets = [ephem.Mercury(), ephem.Venus(), ephem.Mars(), ephem.Jupiter(), ephem.Saturn(), ephem.Uranus(), ephem.Neptune(), ephem.Moon()]

start_date = ephem.Date("2023/01/01")
end_date = ephem.Date("2023/12/31")
date = start_date

# create matrix to store planet names and conjunction dates
matrix = [[None for _ in range(len(planets) + 1)] for _ in range(len(planets))]

# create list to store planet names
planet_names = [planet.name for planet in planets]
# list all planets names as first row in matrix
matrix.insert(0, [""] + planet_names)

while date < end_date:
    for i, planet1 in enumerate(planets):
        for j, planet2 in enumerate(planets):
            if i < j:
                planet1.compute(date)
                planet2.compute(date)
                sep = ephem.separation(planet1, planet2) #  calculate the angular distance
                # compare the separation, if less than 0.01 degree then it's a conjunction
                if sep < 1.0:
                    date_formatted = date.datetime().strftime("%d %B %Y")
                    matrix[i+1][j+1] = date_formatted
                    break
    date = ephem.Date(date + 1)
# print a matrix with date is separation from 1 degree between planets on rows and column
for row in matrix:
    print(row)
This is the result:
['', 'Mercury', 'Venus', 'Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptune', 'Moon']
[None, None, '30 December 2023', '14 December 2023', '20 June 2023', None, None, None, None]
[None, None, None, '30 December 2023', '15 March 2023', '06 January 2023', None, None, '12 October 2023']
[None, None, None, None, None, None, '24 April 2023', None, '16 December 2023']
[None, None, None, None, None, '05 June 2023', '30 December 2023', None, None]
[None, None, None, None, None, None, None, '30 December 2023', None]
[None, None, None, None, None, None, None, '30 December 2023', None]
[None, None, None, None, None, None, None, None, '23 December 2023']
[None, None, None, None, None, None, None, None, None]

Sunday, January 8, 2023

Python 3.7.9 : The sunpy python package - part 001.

In the past, I have written several small tutorials related to this Python package. Now I have some news related to the sunpy python package:
  • HelioviewerClient is deprecated;
  • The Helioviewer Project now maintains a Python Wrapper called hvpy.
  • sunpy users are encouraged to upgrade parfive python package;
  • the sunpy.database deprecation;
  • the sample data files provided through sunpy.data.sample are now downloaded individually on demand;
  • SunPy is tested against Python 2.7, 3.5, and 3.6.;
  • SunPy no longer supports Python 3.4.;
  • easy to extract data values from a GenericMap along a curve specified by a set of coordinates using the new sunpy.map.extract_along_coord function;
  • has a new make_heliographic_header() function that simplifies creating map headers that span the whole solar surface in Carrington or Stonyhurst coordinates;
  • aiaprep is now deprecated;
  • ... and more on the official website.
I install this python package on the Windows OS with the pip tool:
pip install "sunpy[all] -U"
You can install extra available options: [asdf], [dask], [database], [image], [jpeg2000], [map], [net], [timeseries], [visualization].
You can see the version of this python package with this source code:
 import sunpy
print(sunpy.__version__)
3.1.8
I tested this package with this simple source code:
from matplotlib import pyplot as plt
import sunpy.map
import sunpy.data.sample  
sunpyAIA = sunpy.map.Map(sunpy.data.sample.AIA_171_IMAGE) 
sunpyAIA.plot()
plt.colorbar()
plt.show()
The instrument is AIA and the measurement is 171, see more on this online tool named helioviewer.
After I run I got this image:

Python 3.11.0 : The scapy python module - part 003.

Scapy is a powerful interactive packet manipulation program. It is able to forge or decode packets of a wide number of protocols, send them on the wire, capture them, match requests and replies, and much more. It can easily handle most classical tasks like scanning, tracerouting, probing, unit tests, attacks or network discovery (it can replace hping, 85% of nmap, arpspoof, arp-sk, arping, tcpdump, tshark, p0f, etc.). It also performs very well at a lot of other specific tasks that most other tools can’t handle, like sending invalid frames, injecting your own 802.11 frames, combining technics (VLAN hopping+ARP cache poisoning, VOIP decoding on WEP encrypted channel, …), etc.
First, you need to install it with pip tool: pip install scapy --user.
I used with WinPcap from this webpage, but you will see the recomandation is to use Npcap.
#!/usr/bin/env python3
import os
print(os.sys.path)
from scapy.all import *

def mysniff(interface):
    sniff(iface=interface, store=False, prn=process_sniffed_packet)

def process_sniffed_packet(packet):
    pyperclip.copy(str(packet))
    print(packet)

mysniff("Realtek PCIe GbE Family Controller")
The running result is something like this:
...
WARNING: WinPcap is now deprecated (not maintained). Please use Npcap instead
Ether / IP / TCP 104.244.42.2:https > 192.168.0.143:55478 PA / Raw
Ether / IP / TCP 192.168.0.143:55478 > 104.244.42.2:https PA / Raw
Ether / IP / TCP 192.168.0.143:55478 > 104.244.42.2:https PA / Raw
Ether / IP / TCP 104.244.42.2:https > 192.168.0.143:55478 A / Padding
Ether / IP / TCP 104.244.42.2:https > 192.168.0.143:55478 A / Padding
Ether / ARP who has 192.168.0.1 says 192.168.0.206 / Padding
...

Saturday, January 7, 2023

Python 3.7.9 : how to fix update errors between pip and setuptools.

The python language was mainly developed to emphasis on code readability.
That's why I think that it should not be affected by such errors, but they are easily fixed ...
    launcher = self._get_launcher('t')
  File "C:\Users\catafest\AppData\Roaming\Python\Python37\site-packages\pip\_vendor\distlib\scripts.py", line 404, in _get_launcher
    raise ValueError(msg)
ValueError: Unable to find resource t64.exe in package pip._vendor.distlib

[notice] A new release of pip available: 22.3 -> 22.3.1
[notice] To update, run: python.exe -m pip install --upgrade pip

C:\PythonProjects\scapy001>python -m pip uninstall pip setuptools
Found existing installation: pip 22.3
Uninstalling pip-22.3:
  Would remove:
    c:\users\catafest\appdata\roaming\python\python37\scripts\pip.exe
    c:\users\catafest\appdata\roaming\python\python37\scripts\pip3.10.exe
    c:\users\catafest\appdata\roaming\python\python37\scripts\pip3.7.exe
    c:\users\catafest\appdata\roaming\python\python37\scripts\pip3.exe
    c:\users\catafest\appdata\roaming\python\python37\site-packages\pip-22.3.dist-info\*
    c:\users\catafest\appdata\roaming\python\python37\site-packages\pip\*
Proceed (Y/n)? y
  Successfully uninstalled pip-22.3
Found existing installation: setuptools 47.1.0
Uninstalling setuptools-47.1.0:
  Would remove:
    c:\python379\lib\site-packages\easy_install.py
    c:\python379\lib\site-packages\pkg_resources\*
    c:\python379\lib\site-packages\setuptools-47.1.0.dist-info\*
    c:\python379\lib\site-packages\setuptools\*
    c:\python379\scripts\easy_install-3.7.exe
    c:\python379\scripts\easy_install.exe
Proceed (Y/n)? y
  Successfully uninstalled setuptools-47.1.0

C:\PythonProjects\scapy001>pip3 install --upgrade pip
Requirement already satisfied: pip in c:\python379\lib\site-packages (22.3.1)

C:\PythonProjects\scapy001>pip install --upgrade setuptools
Collecting setuptools
  Downloading setuptools-65.6.3-py3-none-any.whl (1.2 MB)
     ---------------------------------------- 1.2/1.2 MB 4.1 MB/s eta 0:00:00
Installing collected packages: setuptools
Successfully installed setuptools-65.6.3

Thursday, January 5, 2023

Python Qt6 : Create a tray icon application.

The notification area known as system tray is located in the Windows Taskbar area, usually at the bottom right corner.
I tested with:
Python 3.10.2 (tags/v3.10.2:a58ebcc, Jan 17 2022, 14:12:15) [MSC v.1929 64 bit (AMD64)] on win32
...
pip show pyqt6
Name: PyQt6
Version: 6.4.0
Summary: Python bindings for the Qt cross platform application toolkit
I created a trayicon application with PyQt6 that show a menu with two entry: Show Window and Exit.
The Show Window will sow a default window and the Exit will close the application.
This is the source code I tested, you need an icon.png in the same folder with this script.
import sys
from PyQt6.QtCore import Qt, QEvent, QPoint
from PyQt6.QtGui import QGuiApplication, QIcon, QAction
from PyQt6.QtWidgets import QApplication, QSystemTrayIcon, QMainWindow, QMenu

# create the default application
app = QApplication(sys.argv)

# create the main window
window = QMainWindow()
# set the title for the window
window.setWindowTitle("My Window")

# this set the tray icon and menu
tray_icon = QSystemTrayIcon()
tray_icon.setIcon(QIcon("icon.png"))
menu = QMenu()

# add an action to the menu to show the window
show_window_action = QAction("Show Window", None)
show_window_action.triggered.connect(window.show)
menu.addAction(show_window_action)

# add an action to the menu to exit the application
exit_action = QAction("Exit", None)
exit_action.triggered.connect(app.quit)
menu.addAction(exit_action)

# set the context menu
tray_icon.setContextMenu(menu)

# show the tray icon
tray_icon.show()

# run the application
app.exec()

Tuesday, January 3, 2023

Python 3.10.2 : about ChemSpiPy.

ChemSpiPy provides a way to interact with ChemSpider in Python. It allows chemical searches, chemical file downloads, depiction and retrieval of chemical properties...
You can read more about this python package on the official website and the documentation for this python package.
You have to create an account and fill in the data to get an A.P.I key...
This is the source code for this python package , I use PyQt6 to show image formula with:QPixmap.fromImage.
import chemspipy

# set the API key from https://developer.rsc.org/my-apps/
api_key = "... your A.P.I. key ..."

# import the ChemSpider class
from chemspipy import ChemSpider

# instance of the ChemSpider class
cs_inst = ChemSpider(api_key)

compound_name = "Glucose"
# search for compound: "Glucose"
compounds  = cs_inst.search(compound_name) 

# get the first compound in the list
compound = compounds[0]

# print the list of the attributes and methods of 'compound' object
print(dir(compound))

# Print the compound's properties
print("... some compound's properties !")
print(f"Name: {compound.common_name}")
print(f"Average_mass: {compound.average_mass}")
print(f"ChemSpider ID - csid: {compound.csid}")
#print(f" external_references: {compound.external_references}")
#print(f" image: {compound.image}")
#print(f" mol_2d: {compound.mol_2d}")
#print(f" mol_3d: {compound.mol_3d}")
print(f" molecular_formula: {compound.molecular_formula}")
print(f" molecular_weight: {compound.molecular_weight}")
print(f" monoisotopic_mass: {compound.monoisotopic_mass}")
print(f" nominal_mass: {compound.nominal_mass}")


import sys
from PyQt6.QtCore import Qt
from PyQt6.QtGui import QPixmap, QImage, QColor
from PyQt6.QtWidgets import QApplication, QMainWindow, QLabel

# Create the application
app = QApplication(sys.argv)

# Create the main window
window = QMainWindow()
window.setWindowTitle(compound_name)

# Load the image with fromData
image = QImage.fromData(compound.image)

# Create a label to display the image
label = QLabel()
label.setPixmap(QPixmap.fromImage(image))

# Set the label as the central widget of the window
window.setCentralWidget(label)

# Show the window
window.show()

# Run the application
app.exec()
This is the result of running the source code:

Python 3.10.2 : testing the NASA A.P.I. features.

In this tutorial I will show you how to deal with the NASA A.P.I. and python programming language.
This source code was build and tested yesterday.
This is the source code:
import requests
from datetime import date

today_data = date.today()
today = today_data.strftime("%d%m%Y")
import urllib.parse

# set your API key from nasa https://api.nasa.gov/#NHATS
api_key = "... your A.P.I. key ..."

# this is a simple example to get one day image 
base_url = "https://api.nasa.gov/planetary/apod"

# set the parameters for the API request
params = {
    "api_key": api_key
}

# the request to the API
response = requests.get(base_url, params=params)

# get data
if response.status_code == 200:
    # parse the response
    data = response.json()

    # print the image URL
    print(data["url"])
    # parse the URL
    parsed_url = urllib.parse.urlparse(data["url"])

    # extract the file name from the URL
    file_name = parsed_url.path.split("/")[-1]
    # save the image
    response_image = requests.get(data["url"])
    with open(today+'_'+file_name, "wb") as f:
        f.write(response_image.content)
else:
    # print the status code
    print(response.status_code)
I run the source code and I get these two images ...
...
01/03/2023  01:06 AM            86,943 03012023_AllPlanets_Tezel_1080_annotated.jpg
01/03/2023  04:22 PM           553,426 03012023_KembleCascade_Lease_960.jpg
...

Manim python example.

I have written before about manim as a Python package in this tutorial.
It's quite powerful for animation and I recommend it to content producers who need a tool for school board-like graphics.
Today I'm back with a link that a document written in Jupiter notebook with Manim package from manim community, see this link.

Monday, January 2, 2023

News : PyTorch machine learning framework compromised with malicious dependency.

If you installed PyTorch-nightly on Linux via pip between December 25, 2022 and December 30, 2022, please uninstall it and torchtriton immediately, and use the latest nightly binaries (newer than Dec 30th 2022).
Read more on the official website.

Python Qt6 : Show any CSV file with QTableWidget.

In this tutorial, I will show you how easy it is to work with PyQt6 and QTableWidget to display any CSV file.
The source code lines are already commented to understand how this source code works.
This is the content of the csv file named my.csv:
id,firstname,lastname,email,email2,profession
100,Maud,Callista,Maud.Callista@yopmail.com,Maud.Callista@gmail.com,police officer
101,Justinn,Rona,Justinn.Rona@yopmail.com,Justinn.Rona@gmail.com,worker
102,Gabriellia,Robertson,Gabriellia.Robertson@yopmail.com,Gabriellia.Robertson@gmail.com,police officer
103,Gwenneth,Payson,Gwenneth.Payson@yopmail.com,Gwenneth.Payson@gmail.com,firefighter
104,Lynea,Robertson,Lynea.Robertson@yopmail.com,Lynea.Robertson@gmail.com,developer
This is the source I used:
import sys
import csv
from PyQt6.QtWidgets import QApplication, QMainWindow, QTableWidget, QTableWidgetItem
from PyQt6.QtCore import Qt

# the main window class
class MainWindow(QMainWindow):
    # the init definition of the class
    def __init__(self):
        super().__init__()

        # the window title
        self.setWindowTitle('Table Viewer for any CSV file.')

        # this create the table widget
        self.table = QTableWidget(self)

        # the table dimensions is set default
        self.table.setColumnCount(0)
        self.table.setRowCount(0)

        # this read the CSV file named my.csv
        with open('my.csv', 'r') as file:
            # use the reader for file 
            reader = csv.reader(file)
            # this get the column labels from the first row
            headers = next(reader)
            # this set the number of columns based on the number of headers
            self.table.setColumnCount(len(headers))
            # this set the horizontal header labels
            self.table.setHorizontalHeaderLabels(headers)
            # for each row iterate the rows in the CSV file
            for row in reader:
                # add a row to the table
                row_index = self.table.rowCount()
                self.table.insertRow(row_index)
                # add data to cells 
                for col_index, cell in enumerate(row):
                    self.table.setItem(row_index, col_index, QTableWidgetItem(cell))
        
        # setting for the table as the central widget
        self.setCentralWidget(self.table)

# this run the application
if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec())
Here is what the result of running the source code:

Wednesday, December 28, 2022

Python 3.10.2 : Copernicus A.P.I. and python sentinelsat python package - part 001.

Last night I worked a bit with python and tested on the online tool from Copernicus.
Copernicus is the European Union's Earth observation programme, which analyzes our planet and its environment for the benefit of all European citizens.
If you want to test it with python or another A.P>I. you need to create an user and a apassword.This is the source code I used with S:
from sentinelsat import SentinelAPI, read_geojson, geojson_to_wkt
from datetime import date
# Connect to the Sentinel API
api = SentinelAPI('___', '___', 'https://scihub.copernicus.eu/dhus')
#
api.download('___from_copernicus_website___')
# Search for Sentinel-2 images covering a specific area
footprint = geojson_to_wkt(read_geojson('area_of_interest.geojson'))

products = api.query(footprint,
                     date=('20211201', '20211205'),
                     platformname='Sentinel-1')
# convert to Pandas DataFrame
products_df = api.to_dataframe(products)
print(products_df)
# sort and limit to first 5 sorted products
products_df_sorted = products_df.sort_values(['link'], ascending=[True])
products_df_sorted = products_df_sorted.head(5)

# download sorted and reduced products
api.download_all(products_df_sorted.index)
You need a JSOn file to select the area of interest:
After I run this python script, the result is this:
python test001.py
                                                                                  title  ... productconsolidation
8f12995e-8f4b-4634-91bb-4971a1bdd0c3  S1B_IW_SLC__1SDV_20211201T160049_20211201T1601...  ...                  NaN
c62ceac6-c9ac-409d-bea9-d1bc23b1b183  S1B_IW_GRDH_1SDV_20211201T160050_20211201T1601...  ...                  NaN
2d1319c5-60af-468b-904a-5dfbdd5f205c  S1B_IW_RAW__0SDV_20211201T160046_20211201T1601...  ...                SLICE

[3 rows x 36 columns]
Downloading S1B_IW_GRDH_1SDV_20211201T160050_20211201T160115_029834_038FB2_A390.zip: 100%|█| 929M/929M [02:44<00:00, 5.
Downloading products:  33%|██████████████████▋                                     | 1/3 [02:58<05:57, 178.58s/product]
Downloading S1B_IW_RAW__0SDV_20211201T160046_20211201T160119_029834_038FB2_D35D.zip:  83%|▊| 1.31G/1.58G [03:21<00:26,
Downloading S1B_IW_SLC__1SDV_20211201T160049_20211201T160116_029834_038FB2_AC13.zip:  31%|▎| 1.33G/4.35G [03:18<04:16,
...
The copernicus online map can be seen in the next inage:

News : Inkscape team hiring python developer.

For the Inkscape project and its users, interoperability with other software packages, both free and commercial, is of high importance. The PLC has decided to hire a developer for the equivalent of 1.5 months (part-time schedule available) to implement importing functionality of a file format for which Inkscape yet lacks proper support. An extension of the project to up to 3 months (with additional compensation) may be granted depending on the success of the first half ...
You can find more information on the official website.

Saturday, December 24, 2022

Python 3.8.16 : My colab tutorials - part 028.

Today I added a new example to my GitHub repo on colab with a simple example of how to search for images using Google's custom search A.P.I. You can find this example on my Github webpage example.