Skip to content

GitLab

  • Menu
Projects Groups Snippets
    • Loading...
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • Python_ToolBox Python_ToolBox
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Issues 0
    • Issues 0
    • List
    • Boards
    • Service Desk
    • Milestones
  • Monitor
    • Monitor
    • Metrics
    • Incidents
  • Packages & Registries
    • Packages & Registries
    • Package Registry
    • Container Registry
    • Infrastructure Registry
  • Analytics
    • Analytics
    • Value stream
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Create a new issue
  • Issue Boards
Collapse sidebar
  • Marie Brunel
  • Python_ToolBoxPython_ToolBox
  • Wiki
  • Python script

Last edited by Marie Brunel Feb 01, 2022
Page history

Python script

Header

has to be set on the top of the jupyter notebook

# TITLE
- **last review** : //2022
- **motivation** :  
- **wikipage or issue** : 

Script set up

#libraries to import
import cartopy.crs as ccrs
import cartopy.feature as cfeature
import glob
import matplotlib
from matplotlib.backends.backend_pdf import PdfPages
from matplotlib.collections import PatchCollection
from matplotlib.colors import *
import matplotlib.lines as mtplines
import matplotlib.patches as mpatches
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
from netCDF4 import *
import numpy as np
import numpy.ma as ma
import pandas as pd
from pylab import *
from scipy.io import netcdf
import seaborn as sns
import warnings
import xarray as xr
import struct
import shapefile as shp
from shapely.geometry import Polygon, Point
rc('text', usetex=False)

#text fontsize
scale = -10
fs_textongraph = 23 + scale
fs_label = 22 + scale
fs_axistitle = 25 + scale
fs_textoutgraph = 25+ scale
fs_title = 26+ scale
fs_legend = 25+ scale

def set_subplots (n,m,h,l,eh,ev): 
    ''' return a list of subplotpython how to [xposition,yposision,width,high]\n
    n: number of horizontal plots; m: number of vertical plots;\n
    h: high of plots; l: larger of plots;\n
    eh: space between plots horizontally; ev: space between plots vertically\n
    '''
    subplots = []
    for j in range (m-1,-1,-1):
        for i in range (0,n,1):
            subplots.append([i*(l+eh),j*(h+ev),l,h])
    return (subplots)

list of folders or files

#list of folders
folders = glob.glob(pathfolder/+"*/")
#list of files
files = glob.glob(pathfolder/+"*")

read lpjml output

  • open netcdf file :
nc = Dataset(file, mode='r')
years = nc.variables['time'][:]
latitudes = nc.variables['latitude'][:]
longitudes = nc.variables['longitude'][:]
data = nc.variables['namevariable'][:]
nc.close()
  • read string :
chartostring()
  • list of variable's keys and dimension :
variables = list(nc.variables.keys())
dimension = len(variables)

other file format :

read

file = open(path, "r")
lines = file.readlines()
file.close()
# split line
line.split(",")` `line.replace('"', '')

write

f = open("demofile2.txt", "a")
f.write("Now the file has more content!")
f.close()

how to

  • avoid warning
warnings.filterwarnings("ignore")
  • skip step within a loop
if (condition) :
        continue
  • try except function
try : 
   to do sth
except : 
   if error do
  • separator :
print("*----------------------------------------------------------*")
  • obtain the date :
from datetime import datetime
today = datetime.today()
today.strftime("%d/%m/%y")
#date stored in today variable
  • print integer in sentence :
print ("ERROR: year {} not in years\n".format(year))
  • free memory
import gc
del yields
del harvests
del Rharvests
del Rsurfaces
gc.collect()
Clone repository
  • Binary file
  • Brazil
  • C balance abnormally high in LPJmL 5 running @LAM
  • Cluster
  • Color and ColorMap
  • Data treatment
  • Link shelf
  • Morocco
  • NetCDF format and file
  • Numpy array
  • Plot graph and map
  • Python script
  • Run CNRM and HADgem LPJmL5 (30 minutes)
  • Home