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()