Skip to content

Commit 2275af7

Browse files
Lint all new recipes with black tool
1 parent 18f2ac2 commit 2275af7

3 files changed

Lines changed: 73 additions & 64 deletions

File tree

docs/source/recipes/plot_21_recipe.py

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
There are various options to do this, the recommended option is to use `cf native functions <https://ncas-cms.github.io/cf-python/class/cf.Field.html#mathematical-operations>`_, as they preserve units and metadata associated with fields. Sometimes, however, the function you need is not implemented in cf, so there are alternative methods.
88
"""
99

10-
#%% [markdown]
10+
# %% [markdown]
1111
#
1212
# .. figure:: ../images/data-operations-flowchart.png
1313
# :scale: 50 %
@@ -16,18 +16,18 @@
1616
# It is recommended to use the highest possible implementation of a given function as shown by the chart.
1717
#
1818

19-
#%%
19+
# %%
2020
# 1. Import cf-python:
2121

2222
import cf
2323

24-
#%%
24+
# %%
2525
# 2. Read the template field constructs from the example:
2626

2727
f = cf.example_field(1)
2828
print(f)
2929

30-
#%% [markdown]
30+
# %% [markdown]
3131
#
3232
# 1: Native cf
3333
# ------------
@@ -37,19 +37,19 @@
3737
# Additionally, where a function or operation has a specific domain, cf will mask any erroneous elements that were not processed properly.
3838
#
3939

40-
#%%
40+
# %%
4141
# 1. Create an instance of the template field to work with:
4242

4343
field1 = f.copy()
4444

45-
#%%
45+
# %%
4646
# 2. Calculate the sine of the elements in the data array:
4747

4848
new_field = field1.sin()
4949

5050
print(new_field.data)
5151

52-
#%%
52+
# %%
5353
# Alternatively, we can update the original field in place using the ``inplace`` parameter:
5454

5555
field1.sin(inplace=True)
@@ -59,10 +59,10 @@
5959
# cf will automatically update the units of our field depending on the operation.
6060
# Here, since the sine is a dimensionless value, we get the units "1".
6161

62-
print(f.units) # Original
63-
print(field1.units) # After operation
62+
print(f.units) # Original
63+
print(field1.units) # After operation
6464

65-
#%% [markdown]
65+
# %% [markdown]
6666
#
6767
# 2: Dask
6868
# -------
@@ -83,24 +83,24 @@
8383
# from outside of cf.
8484
#
8585

86-
#%%
86+
# %%
8787
# 1. Import the necessary Dask module:
8888

8989
import dask as da
9090

91-
#%%
91+
# %%
9292
# 2. Create an instance of the template field to work with:
9393

9494
field2 = f.copy()
9595

96-
#%%
96+
# %%
9797
# 3. Load the data from the field as a Dask array:
9898

9999
data = field2.data
100100

101101
dask_array = data.to_dask_array()
102102

103-
#%%
103+
# %%
104104
# 4. Create a new field, calculate the sine of the elements,
105105
# and write the array to the new field:
106106

@@ -112,26 +112,26 @@
112112

113113
print(new_field.data)
114114

115-
#%%
115+
# %%
116116
# 5. Manually update the units:
117117

118-
new_field.override_units('1', inplace=True)
118+
new_field.override_units("1", inplace=True)
119119

120120
print(new_field.units)
121121

122-
#%%
122+
# %%
123123
# To instead update the original field in place, as before:
124124

125125
calculated_array = da.array.sin(dask_array)
126126

127127
field2.set_data(calculated_array)
128128

129-
field2.override_units('1', inplace=True)
129+
field2.override_units("1", inplace=True)
130130

131131
print(field2.data)
132132
print(field2.units)
133133

134-
#%% [markdown]
134+
# %% [markdown]
135135
#
136136
# 3: NumPy Universal Functions
137137
# ----------------------------
@@ -145,17 +145,17 @@
145145
# As above, take care to manually update any metadata for the new field.
146146
#
147147

148-
#%%
148+
# %%
149149
# 1. Import NumPy:
150150

151151
import numpy as np
152152

153-
#%%
153+
# %%
154154
# 2. Create an instance of the template field to work with:
155155

156156
field3 = f.copy()
157157

158-
#%%
158+
# %%
159159
# 3. Create a new field, compute the sine of the elements,
160160
# and write the array to the new field:
161161

@@ -167,14 +167,14 @@
167167

168168
print(new_field.data)
169169

170-
#%%
170+
# %%
171171
# 4. Manually update the units:
172172

173-
new_field.override_units('1', inplace=True)
173+
new_field.override_units("1", inplace=True)
174174

175175
print(new_field.units)
176176

177-
#%% [markdown]
177+
# %% [markdown]
178178
#
179179
# 4: NumPy Vectorization
180180
# ----------------------
@@ -187,22 +187,22 @@
187187
# array and applying the function.
188188
#
189189

190-
#%%
190+
# %%
191191
# 1. Import our third-party function; here, from the ``math`` module:
192192

193193
import math
194194

195-
#%%
195+
# %%
196196
# 2. Create an instance of the template field to work with:
197197

198198
field4 = f.copy()
199199

200-
#%%
200+
# %%
201201
# 3. Vectorize the function with NumPy:
202202

203203
vectorized_function = np.vectorize(math.sin)
204204

205-
#%%
205+
# %%
206206
# 4. Create a new field, calculate the sine of the elements,
207207
# and write the array to the new field:
208208

@@ -214,14 +214,14 @@
214214

215215
print(new_field.data)
216216

217-
#%%
217+
# %%
218218
# 5. Manually update the units:
219219

220-
new_field.override_units('1', inplace=True)
220+
new_field.override_units("1", inplace=True)
221221

222222
print(new_field.units)
223223

224-
#%% [markdown]
224+
# %% [markdown]
225225
#
226226
# Performance
227227
# -----------

docs/source/recipes/plot_22_recipe.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
in the region and plot them using a scatter plot on a polar grid to
88
create a wind rose representing wind vectors in the given area.
99
"""
10+
1011
# %%
1112
# 1. Import cf-python, Dask.array, NumPy, and Matplotlib:
1213

@@ -18,18 +19,18 @@
1819
# %%
1920
# 2. Read the field constructs and load the wind speed component fields:
2021

21-
f = cf.read('~/recipes/data1.nc')
22+
f = cf.read("~/recipes/data1.nc")
2223
print(f)
2324

24-
U = f[2].squeeze() # Easterly wind speed component
25-
V = f[3].squeeze() # Northerly wind speed component
25+
U = f[2].squeeze() # Easterly wind speed component
26+
V = f[3].squeeze() # Northerly wind speed component
2627

2728

2829
# %%
2930
# 3. Set a bounding region for the data and discard readings outside of it:
3031

31-
tl = (41, 72) # (long, lat) of top left of bounding box.
32-
br = (65, 46) # (long, lat) of bottom right of bounding box.
32+
tl = (41, 72) # (long, lat) of top left of bounding box.
33+
br = (65, 46) # (long, lat) of bottom right of bounding box.
3334

3435
U_region = U.subspace(X=cf.wi(tl[0], br[0]), Y=cf.wi(br[1], tl[1]))
3536
V_region = V.subspace(X=cf.wi(tl[0], br[0]), Y=cf.wi(br[1], tl[1]))
@@ -56,7 +57,7 @@
5657

5758
azimuths = da.arctan2(V_sub.data, U_sub.data)
5859

59-
bearings = ((np.pi/2) - azimuths) % (np.pi*2)
60+
bearings = ((np.pi / 2) - azimuths) % (np.pi * 2)
6061

6162
# %%
6263
# 7. Flatten the two dimensions of each array for plotting with Matplotlib:
@@ -73,8 +74,8 @@
7374
fig = plt.gcf()
7475
# sphinx_gallery_end_ignore
7576
ax = plt.subplot(polar=True)
76-
ax.set_theta_zero_location("N") # Place 0 degrees at the top.
77-
ax.set_theta_direction(-1) # Arrange bearings clockwise around the plot.
77+
ax.set_theta_zero_location("N") # Place 0 degrees at the top.
78+
ax.set_theta_direction(-1) # Arrange bearings clockwise around the plot.
7879
# sphinx_gallery_start_ignore
7980
plt.close()
8081
# sphinx_gallery_end_ignore
@@ -94,15 +95,17 @@
9495
# sphinx_gallery_start_ignore
9596
plt.figure(fig)
9697
# sphinx_gallery_end_ignore
97-
plt.title(f'Wind Rose Scatter Plot\nLat: {br[1]}°-{tl[1]}°, Long: {tl[0]}°-{br[0]}°')
98+
plt.title(
99+
f"Wind Rose Scatter Plot\nLat: {br[1]}°-{tl[1]}°, Long: {tl[0]}°-{br[0]}°"
100+
)
98101

99102
ax.set_xlabel("Bearing [°]")
100103

101104
ax.set_ylabel("Speed [m/s]", rotation=45, labelpad=30, size=8)
102105

103106
ax.yaxis.set_label_coords(0.45, 0.45)
104107

105-
ax.yaxis.set_tick_params(which='both', labelrotation=45, labelsize=8)
108+
ax.yaxis.set_tick_params(which="both", labelrotation=45, labelsize=8)
106109

107110
ax.set_rlabel_position(45)
108111
# sphinx_gallery_start_ignore

0 commit comments

Comments
 (0)