|
|
## Header
|
|
|
|
|
|
has to be set on the top of the jupyter notebook
|
|
|
```plaintext
|
|
|
# TITLE
|
|
|
- **last review** : //2021
|
|
|
- **motivation** :
|
|
|
- **wikipage or issue** :
|
|
|
```
|
|
|
|
|
|
## Script set up
|
|
|
```plaintext
|
|
|
#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
|
|
|
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
|
|
|
|
|
|
```plaintext
|
|
|
#list of folders
|
|
|
folders = glob.glob(pathfolder/+"*/")
|
|
|
#list of files
|
|
|
files = glob.glob(pathfolder/+"*")
|
|
|
```
|
|
|
|
|
|
## read lpjml output
|
|
|
- open netcdf file :
|
|
|
```plaintext
|
|
|
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 :
|
|
|
```plaintextpython how to
|
|
|
variables = list(nc.variables.keys())
|
|
|
dimension = len(variables)
|
|
|
```
|
|
|
|
|
|
## other file format :
|
|
|
### read
|
|
|
```plaintext
|
|
|
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
|
|
|
|
|
|
```plaintext
|
|
|
if (condition) :
|
|
|
continue
|
|
|
```
|
|
|
|
|
|
- try except function
|
|
|
|
|
|
```plaintext
|
|
|
try :
|
|
|
to do sth
|
|
|
except :
|
|
|
if error do
|
|
|
```
|
|
|
- separator :
|
|
|
```
|
|
|
print("*----------------------------------------------------------*")
|
|
|
```
|
|
|
- obtain the date :
|
|
|
```plaintext
|
|
|
from datetime import datetime
|
|
|
today = datetime.today()
|
|
|
today.strftime("%d/%m/%y")
|
|
|
#date stored in today variable
|
|
|
``` |
|
|
\ No newline at end of file |