The Open Science Project

Identification


Sign-Up


Create a new notebook

Contributors


Open Science Notebooks

Notes


Comment afficher une image en utilisant AJAX ?

Daidalos June 11, 2019

Creative Commons License

Exemple de comment afficher une image en utilisant AJAX [TOC] ### Afficher l'image quand elle existe L'objectif ici est d'afficher, en utilisant AJAX, une image dans une page html une fois que celle-ci se trouve dans un répertoire donné. Soit le dossier suivant: /TestLab/ test.html avec le fichier test.html (Note: le nom du dossier TestLab n'est pas important ici, vous pouvez le renommé): <!DOCTYPE html> <html lang="en"> <head> <title>Test</title> <scrip ...

Comment tester si un fichier existe avec du javascript ?

Daidalos June 11, 2019

Creative Commons License

Exemple de comment tester si un fichier existe avec du javascript: [TOC] ### Tester si le fichier existe Soit le fichier json suivant [data.json](/Files/data/): { "file_1": { "status": "0", "author": "John", "size": "3245" }, "file_2": { "status": "1", "author": "Jane", "size": "19462" } } Pour tester si le fichier existe avec du javascript on peut créer une simple page html (test.html par exemple) et faire comme dans cet exemple: <!DOCTY ...

Comment lire un fichier json en javascript ?

Daidalos June 10, 2019

Creative Commons License

Exemple de comment lire un fichier json en javascript: [TOC] ### Lire le fichier Soit le fichier json suivant [data.json](/Files/data/): { "file_1": { "status": "0", "author": "John", "size": "3245" }, "file_2": { "status": "1", "author": "Jane", "size": "19462" } } On peut alors créer une simple page html (test.html par exemple) et lire le fichier json comme ceci: <!DOCTYPE html> <html lang="en"> <head> <title>Test</title> <scr ...

Comment ajouter une image dans une page html en utilisant javascript ?

Daidalos June 10, 2019

Creative Commons License

Exemple de comment ajouter une image dans une page html en utilisant javascript [TOC] ### Ajouter une image avec javascript On peut tout d'abord créer une variable image avec createElement("img"): var img = document.createElement("img"); puis indiquer le nom de l'image (Note: si l'image n'est pas dans le même répertoire que le document html, on peut aussi specifier le chemin complet vers l'image par exemple './path_to_img/matplotlib-grid-02.png'): img.src = "matplotlib- ...

How to find the product of two matrices in python ?

Daidalos June 10, 2019

Creative Commons License

Examples of how to find the product of two matrices in python: [TOC] ### Function dot() To get the product of two matrices, a solution is to use the numpy function [dot()](https://docs.scipy.org/doc/numpy/reference/generated/numpy.dot.html): \begin{equation} A = \left( \begin{array}{ccc} 1 & 2 & 0 \\\\ 4 & 3 & -1 \end{array}\right) \end{equation} \begin{equation} B = \left( \begin{array}{ccc} 5 & 1 \\\\ 2 & 3 \\\\ 3 & 4 \end{array}\right) \end{equation} \beg ...

How to fix with a Django based website the AdSense: Earnings at risk - One or more of your sites does not have an ads.txt file ?

Daidalos June 09, 2019

Creative Commons License

Example of quickly fix with a django based web site the google Adsense warning message: "Earnings at risk - One or more of your sites does not have an ads.txt file". Just add the following url: url(r'^ads\.txt$', views.authorized_digital_sellers_view, name='authorized_digital_sellers'), and add the view: def authorized_digital_sellers_view(request): return HttpResponse('google.com, pub-0000000000000000, DIRECT, f08c47fec0942fa0') replace pub-0000000000000000 by your publisher i ...

How to color an area on a global map using python and basemap ?

Daidalos June 07, 2019

Creative Commons License

Examples of how to color an area on a global map using python and basemap: [TOC] ### Plot a rectangular area Using the function polygon (source: [simple_basemap_example.py](https://gist.github.com/blaylockbk/79658bdde8c1334ab88d3a67c6e57477)) [image:plot-world-map-using-matplotlib-03 size:50 caption:How to color an area on a global map using python and basemap ?] from mpl_toolkits.basemap import Basemap from matplotlib.patches import Polygon from matplotlib.collections import ...

Comment mettre en couleur une zone sur une carte globale avec python et basemap ?

Daidalos June 07, 2019

Creative Commons License

Exemples de comment mettre en couleur une zone sur une carte globale avec python et basemap: [TOC] ### Zone rectangulaire En utilisant la fonction polygon (source: [simple_basemap_example.py](https://gist.github.com/blaylockbk/79658bdde8c1334ab88d3a67c6e57477)) [image:plot-world-map-using-matplotlib-03 size:50 caption:Comment mettre en couleur une zone sur une carte globale avec python et basemap ? ] from mpl_toolkits.basemap import Basemap from matplotlib.patches import Polyg ...

How to iterate over two lists or more in python ?

Daidalos June 06, 2019

Creative Commons License

Examples of how to iterate over two lists or more in python [TOC] ### Iterate over two lists of same size A simple solution is to use the function [zip](https://docs.python.org/2/library/functions.html#zip) >>> l1 = ['a','b','c'] >>> l2 = [1,2,3] >>> for x,y in zip(l1,l2): ... print(x,y) ... a 1 b 2 c 3 ### Iterate over three lists of same size Another example: >>> l1 = ['a','b','c'] >>> l2 = [1,2,3] >>> l3 = ['hello','hi','bye'] >>> for x,y,z in z ...

How to get all the eMAS granule names from the ORACLES campaign using python 3 ?

Ben May 31, 2019

Creative Commons License

Example of how to get all the (MODIS Airborne Simulator) eMAS granule names from the ORACLES campaign using python: [TOC] ### Get ORACLES campaign days First step, let's download, using python, the json file at the root of the eMAS ORACLES campaign available [here](https://ladsweb.modaps.eosdis.nasa.gov/archive/MAS_eMAS/ORACLES/): import urllib.request, json ladsweb_url = 'https://ladsweb.modaps.eosdis.nasa.gov/archive/MAS_eMAS/ORACLES.json' with urllib.request.urlopen(lads ...

How to test if debug is true or false in a template with django ?

Daidalos May 29, 2019

Creative Commons License

Examples of how to test if debug is true or false in a template with django: [TOC] ### Approach 1 If only few templates are going to use the flag DEBUG a simple solution is to do in the file views.py: from django.conf import settings def my_view(request): debug_flag = settings.DEBUG context = {'debug_flag':debug_flag} return render(request, "my_app/my_template.html", context ) We can then use the debug flag in the template "my_template.html": {% if debug_flag % ...

How to add a title (label) on x and y-axis using python basemap module ?

Daidalos May 28, 2019

Creative Commons License

To add labels on x and y-axis while using basemap, a solution is to use the pyplot functions xlabel() and/or ylabel(): plt.xlabel('Longitude', labelpad=40) plt.ylabel('Latitude', labelpad=40) Note: the option labelpad allows to adjust the distance between the label and the axis. [image:plot-world-map-using-matplotlib-02 size:50 caption:How to add a title (label) on x and y-axis using python basemap module ?] from mpl_toolkits.basemap import Basemap import numpy as np import m ...

Comment utiliser le flag debug dans un template sous django ?

Daidalos May 27, 2019

Creative Commons License

Exemples de comment utiliser le flag debug dans un template sous django. L'objectif ici était de ne pas utiliser certains éléments (comme des pubs) quand le flag DEBUG (dans le fichier settings,py) est réglé sur False par exemple. [TOC] ### Méthode 1 Si seulement quelques pages sont concernées on peut simplement envoyer dans un template le flag DEBUG a partir d'une "view" comme ceci (dans le fichier views.py): from django.conf import settings def my_view(request): debug_flag ...

How to put the y-axis in logarithmic scale with Matplotlib ?

Daidalos May 26, 2019

Creative Commons License

To transform an axis in logarithmic scale with Matplotlib, a solution is to use the pyplot functions xscale and yscale: [TOC] ### Example 1 Let's take for example the exponential function: import matplotlib.pyplot as plt import numpy as np x_min = 0 x_max = 10.0 x = np.arange(x_min, x_max, .01) y = np.exp(x) plt.plot(x,y) plt.xlim(x_min,x_max) plt.ylim(np.exp(x_min),np.exp(x_max)) plt.grid(True,which="both", linestyle='--') plt.title('How to add a gr ...

How to add a grid on a figure in matplotlib ?

Daidalos May 26, 2019

Creative Commons License

Examples of how to add a grid on a figure in matplotlib [TOC] ### Using grid() function To add a grid in the background on a figure in matplotlib, a solution is to use the pyplot function [grid()](http://matplotlib.org/api/pyplot_api.html) pour connaitre l'ensemble des arguments possibles). Example: [image:454 size:50 caption:How to add a grid on a figure in matplotlib ?] import matplotlib.pyplot as plt import numpy as np x = np.arange(-5,5, .01) y = np.sin(2*np.pi*x) ...

How to change imshow axis values (labels) in matplotlib ?

Daidalos May 24, 2019

Creative Commons License

Examples of how to change imshow axis values (labels) in matplotlib: [TOC] Let's consider a simple figure using matplotlib imshow import numpy as np import matplotlib.pyplot as plt def f(x,y): return (x+y)*np.exp(-5.0*(x**2+y**2)) x,y = np.mgrid[-1:1:100j, -1:1:100j] z = f(x,y) plt.imshow(z) plt.colorbar() plt.title('How to change imshow axis values with matplotlib ?', fontsize=8) plt.savefig("imshow_change_values_on_axis_01.png", bbox_inches='tight') ...

Comment changer les valeurs sur les axes d'une figure imshow sous matplotlib ?

Daidalos May 24, 2019

Creative Commons License

Exemples de comment changer les valeurs sur les axes d'une figure imshow sous matplotlib [TOC] Soit une simple figure tracée en utilisant la fonction imshow de matplotlib: import numpy as np import matplotlib.pyplot as plt def f(x,y): return (x+y)*np.exp(-5.0*(x**2+y**2)) x,y = np.mgrid[-1:1:100j, -1:1:100j] z = f(x,y) plt.imshow(z) plt.colorbar() plt.title('How to change imshow axis values with matplotlib ?', fontsize=8) plt.savefig("imshow_change_valu ...

How to get a list of files located in a folder using python ?

Daidalos May 21, 2019

Creative Commons License

Examples of how to get a list of files located in a folder using python: [TOC] ### Using listdir() function Lets assume a folder that contains: dir01/ dir02/ fich01.txt fich02.txt fich02.txt To get a list of files located in the current folder using python a first solution is to use [os.listdir()](https://docs.python.org/2/library/os.html#os.listdir): >>> import os >>> list = os.listdir('.') >>> print(list) [dir01,dir02,fich01.txt,fich ...

How to download a web pdf file from its url in python ?

Daidalos May 21, 2019

Creative Commons License

To download a pdf from a given web url using python, a solution is to use the module [urllib](https://docs.python.org/2/library/urllib.html). Lets try to download the file available from the following url http://math.univ-toulouse.fr/~besse/Wikistat/pdf/st-intro.pdf using urlretrieve: >>> import urllib >>> urllib.urlretrieve('http://math.univ-toulouse.fr/~besse/Wikistat/pdf/st-intro.pdf', "st-intro.pdf") Note: it is also possible to directly change the name of the file ( ...

How to create a list of files with a given extension (.txt, .avi, .docx, etc) in python ?

Daidalos May 21, 2019

Creative Commons License

Example of how to create a list of files with a given extension in python. Lets assume we want to create a list of all the files with the extensions '.hdf' in a folder called 'output': ./output/ MYD06_L2.A2014038.2300.061.2018052102433.hdf MYD06_L2.A2014033.1930.061.2018052042944.hdf MYD06_L2.A2014046.2210.061.2018052112538.hdf MYD06_L2.A2014058.2235.061.2018052141003.hdf MYD06_L2.A2014037.2045.061.2018052094723.hdf MYD06_L2.A2014054.2125.061.2018052131042.hdf MYD06_L2.A ...

Comment créer une liste des fichiers d'un dossier se terminant avec une extension donnée (.txt, .avi, .docx, etc) sous python ?

Daidalos May 21, 2019

Creative Commons License

Exemple de cas: on veut obtenir une liste de tous les fichiers dans le dossier 'output' se terminant par '.hdf': ./output/ MYD06_L2.A2014038.2300.061.2018052102433.hdf MYD06_L2.A2014033.1930.061.2018052042944.hdf MYD06_L2.A2014046.2210.061.2018052112538.hdf MYD06_L2.A2014058.2235.061.2018052141003.hdf MYD06_L2.A2014037.2045.061.2018052094723.hdf MYD06_L2.A2014054.2125.061.2018052131042.hdf MYD06_L2.A2014034.2015.061.2018052062330.hdf MYD06_L2.A2014059.2010.061.2018052 ...

Comment changer la forme d'une figure imshow et avoir la barre de couleurs de même taille avec matplotlib ?

Daidalos May 20, 2019

Creative Commons License

Exemple de comment changer la forme d'une figure imshow et avoir la barre de couleurs de même taille avec matplotlib: [image:imshow-extent-custum-aspect-ratio-00 size:50 caption:Comment changer la forme d'une figure imshow et avoir la barre de couleurs de même taille avec matplotlib ?] import numpy as np import matplotlib.pyplot as plt data = np.random.rand(50,1000) plt.imshow(data) plt.colorbar() plt.savefig("imshow_extent_custum_aspect_ratio_00.png", bbox_inches='tight ...

How to change imshow aspect ratio and fit the colorbar size in matplotlib ?

Daidalos May 20, 2019

Creative Commons License

An example of how to change imshow aspect ratio and adjust the colorbar accordingly in matplotlib: [image:imshow-extent-custum-aspect-ratio-00 size:50 caption:Comment changer la forme d'une figure imshow et avoir la barre de couleurs de même taille avec matplotlib ?] import numpy as np import matplotlib.pyplot as plt data = np.random.rand(50,1000) plt.imshow(data) plt.colorbar() plt.savefig("imshow_extent_custum_aspect_ratio_00.png", bbox_inches='tight') plt.close() ...

How to change imshow aspect ratio in matplotlib ?

Daidalos May 20, 2019

Creative Commons License

Examples of how to change imshow aspect ratio in matplotlib: [TOC] Matplotlib imshow function can return figures with very elongated shapes, example: import numpy as np import matplotlib.pyplot as plt data = np.random.rand(50,1000) plt.imshow(data, extent=[-1,1,-10,10]) plt.savefig("imshow_extent_custum_aspect_ratio_01.png", bbox_inches='tight') plt.close() [image:imshow-extent-custum-aspect-ratio-01 size:30 caption:How to change imshow aspect ratio in matplotlib ?] ...

Comment supprimer les doublons (les répétitions) pour filtrer une requête avec Django ?

Daidalos May 20, 2019

Creative Commons License

Pour supprimer les doublons (les répétitions) pour filtrer une requête avec Django, il existe la fonction [distinct()](https://docs.djangoproject.com/en/2.2/ref/models/querysets/#distinct). Exemple, on a crée la table suivante qui sauvegarde l'adresse ip d'un visiteur, la page visitée et la date (voir [Comment afficher dans un template le nombre de visiteurs en ligne sous Django ?](https://www.science-emergence.com/Articles/Comment-afficher-dans-un-template-le-nombre-de-visiteurs-en-ligne-sous- ...

How to show in a template the number of online visitors with Django ?

Daidalos May 14, 2019

Creative Commons License

An example of solution to display in a template the number of online visitors in a django web site: [TOC] ### Create a table Visitor_Infos First, create a table in models.py called for example: Visitor_Infos: class Visitor_Infos(models.Model): ip_address = models.GenericIPAddressField() page_visited = models.TextField() event_date = models.DateTimeField(default=datetime.now) ### Edit/create the file processor.py Edit the context file processor.py: from django.conf ...

How to create a 2d histogram with matplotlib ?

Daidalos May 14, 2019

Creative Commons License

Examples of how to create a 2d histogram with matplotlib [TOC] ### Using the matplotlib hist2d function To create a 2d histogram in python there are several solutions: for example there is the matplotlib function [hist2d](https://matplotlib.org/api/_as_gen/matplotlib.pyplot.hist2d.html). from numpy import c_ import numpy as np import matplotlib.pyplot as plt import random n = 100000 x = np.random.standard_normal(n) y = 3.0 * x + 2.0 * np.random.standard_normal(n) ...

Comment afficher dans un template le nombre de visiteurs en ligne sous Django ?

Daidalos May 12, 2019

Creative Commons License

Exemple de solution pour afficher dans un template le nombre de visiteurs en ligne pour un site développé avec Django [TOC] ### Sauvegarder les visiteurs dans une table On peut tout d'abord dans le fichier models.py ajouter la table suivante: class Visitor_Infos(models.Model): ip_address = models.GenericIPAddressField() page_visited = models.TextField() event_date = models.DateTimeField(default=datetime.now) ### Editer le fichier processor.py On peut ensuite éditer le ...

How to create a json file with the names and attributes of a VIIRS L2 CLDPROP netcdf file using python 3 ?

Ben May 10, 2019

Creative Commons License

Example of python 3 code to save in a json file the names and attributes of a VIIRS L2 CLDPROP netcdf file. [TOC] Note: the json file created can be found [here](/Files/viirs-cldprop-l2-001-names-attributes/) ### Read the file import netCDF4 import numpy as np import json import pprint f = netCDF4.Dataset('CLDPROP_L2_VIIRS_SNPP.A2018046.1436.001.2019068074007.nc') ### Get all group names group_dic = f.groups.keys() ### Get names and attributes in each group ...

How to download a VIIRS L2 CLDPROP netcdf file from ladsweb using python ?

Ben May 09, 2019

Creative Commons License

Example of python script to download on your local machine a VIIRS L2 CLDPROP netcdf granule file from [ladsweb](https://ladsweb.modaps.eosdis.nasa.gov/search/) using python. [TOC] Lets consider the following data for example: year = 2018 month = 2 day = 15 hour = 14 minute = 36 Note: VIIRS granules are every 6 minutes ### Convert day to count of days First step we need to get the count of days: from datetime import date d0 = date(year, 1, 1) d1 = date(year ...

How to get visitor ip address for a django project ?

Daidalos May 07, 2019

Creative Commons License

Example of how to get visitor ip address for a django project: [TOC] ### Get visitor ip address def visitor_ip_address(request): x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR') if x_forwarded_for: ip = x_forwarded_for.split(',')[0] else: ip = request.META.get('REMOTE_ADDR') return ip ### Check if ip address is valid To check if the ip address is valid, a solution is to use the [socket](h ...

How to reverse columns order in a table with python ?

Daidalos May 07, 2019

Creative Commons License

Example of how to reverse columns order in a table with python: let's consider the following table: >>> import numpy as np >>> table = np.array([[0., 1., 2., 3., 4., 5., 6., 7.], ... [6., 6., 4., 9., 7., 5., 4., 8.], ... [4., 9., 3., 7., 5., 2., 3., 1.], ... [6., 9., 8., 1., 4., 3., 2., 1.], ... [4., 8., 7., 6., 1., 7., 1., 1.]]) \begin{equation} \begin{array}{cccccc} 0. & 1. & 2. & 3. & 4. & 5. & 6. & 7.\\\\ 6. & 6. & 4. & 9. & 7. & ...

Comment inverser l'ordre des colonnes d'un tableau en python avec numpy ?

Daidalos April 24, 2019

Creative Commons License

Exemples de comment inverser l'ordre des colonnes d'un tableau en python avec numpy. Soit un tableau quelconque: >>> import numpy as np >>> table = np.array([[0., 1., 2., 3., 4., 5., 6., 7.], ... [6., 6., 4., 9., 7., 5., 4., 8.], ... [4., 9., 3., 7., 5., 2., 3., 1.], ... [6., 9., 8., 1., 4., 3., 2., 1.], ... [4., 8., 7., 6., 1., 7., 1., 1.]]) \begin{equation} \begin{array}{cccccc} 0. & 1. & 2. & 3. & 4. & 5. & 6. & 7.\\\\ 6. & 6. & 4. & ...

How to reverse rows order in a table with python ?

Daidalos April 23, 2019

Creative Commons License

Examples of how to reverse rows order in a table with python. Let's consider a random table: >>> import numpy as np >>> table = np.array([[0., 4., 4., 2., 7., 0.], ... [1., 2., 7., 2., 3., 4.], ... [2., 2., 3., 3., 6., 3.], ... [3., 9., 0., 6., 3., 3.], ... [4., 3., 1., 8., 2., 5.], ... [5., 9., 3., 7., 6., 8.], ... [6., 9., 5., 8., 1., 8.], ... [7., 9., 6., 7., 3., 7.], ... [8., 2., 1., 4., 4., 8.], ... [9., ...

Comment inverser l'ordre des lignes d'un tableau en python avec numpy ?

Daidalos April 22, 2019

Creative Commons License

Exemples de comment inverser l'ordre des lignes d'un tableau en python avec numpy. Soit un tableau quelconque: >>> import numpy as np >>> table = np.array([[0., 4., 4., 2., 7., 0.], ... [1., 2., 7., 2., 3., 4.], ... [2., 2., 3., 3., 6., 3.], ... [3., 9., 0., 6., 3., 3.], ... [4., 3., 1., 8., 2., 5.], ... [5., 9., 3., 7., 6., 8.], ... [6., 9., 5., 8., 1., 8.], ... [7., 9., 6., 7., 3., 7.], ... [8., 2., 1., 4., 4., 8.], ...

How to find the minimum or maximum value in a matrix with python ?

Daidalos April 22, 2019

Creative Commons License

Examples of how to find the minimum and maximum values in a matrix using numpy and python: [TOC] ### Approach 1: To find the minimum and maximum values in an array, a solution is to use the numpy functions max() and min(): >>> import numpy as np >>> A = np.array(([2,7,-1],[1,9,8],[-1,5,6])) >>> A array([[ 2, 7, -1], [ 1, 9, 8], [-1, 5, 6]]) >>> np.min(A) -1 >>> np.max(A) 9 ### Approach 2: Another solutio ...

How to rotate an image using python ?

Daidalos April 20, 2019

Creative Commons License

Examples of how to rotate an image using python: [TOC] ### Rotate an image and keep the same size as the original image To rotate an image, a solution is to use the pillow function [rotate()](https://pillow.readthedocs.io/en/3.1.x/reference/Image.html), example [images:eiffel-tower;eiffel-tower-rotate-01 dim:1*2 size:75 caption:How to rotate an image using python ?] from PIL import Image im = Image.open("eiffel_tower.jpg") print(im.size) im = im.rotate(45) print(i ...

Comment tourner une image en python avec le module pillow ?

Daidalos April 19, 2019

Creative Commons License

Exemples de comment tourner une image en python avec le module pillow [TOC] ### Tourner une image en gardant la même taille Pour tourner une image, il existe la fonction pillow [rotate()](https://pillow.readthedocs.io/en/3.1.x/reference/Image.html), exemple [images:eiffel-tower;eiffel-tower-rotate-01 dim:1*2 size:75 caption:Comment tourner une image en python avec le module pillow ?] from PIL import Image im = Image.open("eiffel_tower.jpg") print(im.size) im = im.rota ...

Comment retourner une image horizontalement en python ?

Daidalos April 19, 2019

Creative Commons License

Exemples de comment retourner une image horizontalement en python [TOC] ### Obtenir une image retournée horizontalement avec pillow Pour retourner une image horizontalement en python, il existe la fonction pillow [mirror()](https://pillow.readthedocs.io/en/3.1.x/reference/Image.html), example: from PIL import Image from PIL import ImageOps im = Image.open("lena.png") im = ImageOps.mirror(im) im.save("lena_mirror.png") im.show() [images:458;lena-mirror dim:1*2 size ...

How to flip an image horizontally in python ?

Daidalos April 19, 2019

Creative Commons License

Examples of how to flip an image horizontally in python: [TOC] ### Get a vertically flipped image using pillow To flip an image horizontally a solution is to use the function [mirror()](https://pillow.readthedocs.io/en/3.1.x/reference/Image.html), example: from PIL import Image from PIL import ImageOps im = Image.open("lena.png") im = ImageOps.mirror(im) im.save("lena_mirror.png") im.show() [images:458;lena-mirror dim:1*2 size:75 caption:How to flip an image horiz ...

How to flip an image vertically in python ?

Daidalos April 19, 2019

Creative Commons License

Examples of how to flip an image vertically in python: [TOC] ### Get a vertically flipped image using pillow To flip an image vertically a solution is to use the function [rotate()](https://pillow.readthedocs.io/en/3.1.x/reference/Image.html), example: from PIL import Image im = Image.open("eiffel_tower.jpg") im = im.rotate(180) im.save("eiffel_tower_fliped_vertically.jpg") [images:eiffel-tower;eiffel-tower-fliped-vertically dim:1*2 size:75 caption:How to flip an imag ...

How to read a VIIRS L2 CLDPROP netcdf file using python ?

Ben April 16, 2019

Creative Commons License

An example of how to read a VIIRS L2 CLDPROP netcdf file using python 3: [TOC] ### Download a VIIRS L2 CLDPROP netcdf file To download a VIIRS L2 CLDPROP netcdf file, a solution is to go on [LAADS DAAC](https://ladsweb.modaps.eosdis.nasa.gov/search/order/2/CLDPROP_L2_VIIRS_SNPP--5110/2019-01-01/DB/World). Example of granule selected randomly that will be used hereafter: CLDPROP_L2_VIIRS_SNPP.A2019001.1742.001.2019062201640.nc ### Read a netCDF file with python To read the C ...

How to calculate the inverse of a matrix in python using numpy ?

Daidalos April 16, 2019

Creative Commons License

To calculate the inverse of a matrix in python, a solution is to use the linear algebra numpy method [linalg](https://docs.scipy.org/doc/numpy/reference/routines.linalg.html). Example \begin{equation} A = \left( \begin{array}{ccc} 1 & 3 & 3 \\\\ 1 & 4 & 3 \\\\ 1 & 3 & 4 \end{array}\right) \end{equation} inverse matrix A_inv \begin{equation} A^{-1} = \left( \begin{array}{ccc} 7 & -3 & -3 \\\\ -1 & 1 & 0 \\\\ -1 & 0 & 1 \end{array}\right) \end{equation} >>> import ...

How to invert the elements of a boolean array in python ?

Daidalos April 16, 2019

Creative Commons License

Examples of how to invert the elements of a boolean array in python using the numpy function [invert()](https://docs.scipy.org/doc/numpy/reference/generated/numpy.invert.html) >>> import numpy as np >>> a = np.array((True,True,False,True,False)) >>> b = np.invert(a) >>> b array([False, False, True, False, True], dtype=bool) Another example >>> import numpy as np >>> a = np.array((1,1,0,1,0), dtype=bool) >>> b = np.invert(a) >>> b array( ...

Comment inverser les éléments d'une matrice booléenne avec numpy de python ?

Daidalos April 16, 2019

Creative Commons License

Exemples de comment inverser les éléments d'une matrice booléenne en utilisant la fonction numpy [invert()](https://docs.scipy.org/doc/numpy/reference/generated/numpy.invert.html) >>> import numpy as np >>> a = np.array((True,True,False,True,False)) >>> b = np.invert(a) >>> b array([False, False, True, False, True], dtype=bool) Autre exemple: >>> import numpy as np >>> a = np.array((1,1,0,1,0), dtype=bool) >>> b = np.invert(a) >>> b arra ...

How to overlay / superimpose two images using python and pillow ?

Daidalos April 16, 2019

Creative Commons License

Examples of how to overlay / superimpose two images using python and pillow [TOC] ### Overlay two images of same size To overlay two images in python, a solution is to use the pillow function [paste()](https://pillow.readthedocs.io/en/3.1.x/reference/Image.html#PIL.Image.Image.paste), example: from PIL import Image import numpy as np img = Image.open("data_mask_1354_2030.png") background = Image.open("background_1354_2030.png") background.paste(img, (0, 0), img) ba ...

Comment superposer deux images avec le module python pillow ?

Daidalos April 16, 2019

Creative Commons License

Exemples de comment superposer deux images avec le module python pillow: [TOC] ### Superposer deux images de même taille Pour superposer deux images de même taille on peut utiliser la fonction pillow [paste()](https://pillow.readthedocs.io/en/3.1.x/reference/Image.html#PIL.Image.Image.paste), exemple: from PIL import Image import numpy as np img = Image.open("data_mask_1354_2030.png") background = Image.open("background_1354_2030.png") background.paste(img, (0, 0), i ...

How to mask an array using another array in python ?

Daidalos April 16, 2019

Creative Commons License

Examples of how to mask/select elements of an array using another array in python [TOC] ### Mask an array from another array To mask an array, there are several approaches with numpy (see the module called [ma](https://docs.scipy.org/doc/numpy/reference/maskedarray.generic.html)). A solution for example is to use [numpy.ma.masked_where](https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.ma.masked_where.html) to mask the elements of the array x if the elements of the ar ...

How to create a sitemap for a website based on Django ?

Daidalos April 15, 2019

Creative Commons License

An example of how to create a sitemap for a website based on [Django](https://docs.djangoproject.com/en/2.2/ref/contrib/sitemaps/) [TOC] Let's assume a Django website with an application called "notebooks" with the following files: database.db manage.py myproject / settings.py urls.py notebooks / views.py models.py urls.py with models.py class Article(models.Model): full_path = models.TextField() date_created = models.DateTimeField(default=datetime ...

Comment créer un sitemap avec le web-framework Django ?

Daidalos April 14, 2019

Creative Commons License

Exemple de comment créer un sitemap pour un site web basé sur [Django](https://docs.djangoproject.com/en/2.2/ref/contrib/sitemaps/) (version utilisée 1.11.10): [TOC] On va ici supposer un site avec une application intitulée "notebooks" avec l'organisation suivante: database.db manage.py myproject / settings.py urls.py notebooks / views.py models.py urls.py avec models.py class Article(models.Model): full_path = models.TextField() date_created = mod ...

Search