Skip to content

Commit f09626c

Browse files
committed
test notebook II
1 parent c016311 commit f09626c

File tree

1 file changed

+108
-81
lines changed

1 file changed

+108
-81
lines changed

hands-on/session II/2.Explore_Data.ipynb

Lines changed: 108 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -116,50 +116,62 @@
116116
},
117117
"outputs": [],
118118
"source": [
119+
"is_log = True # Or False, depending on the mode\n",
120+
"EPSILON = 0.001\n",
121+
"\n",
119122
"try:\n",
123+
" # Select the colormap\n",
124+
" cmap_instance = plt.get_cmap(\"viridis\")\n",
125+
"\n",
126+
" # Process data range based on log/linear scaling you selected from dashboard\n",
127+
" # If is_log is True, we will use logarithmic scaling; otherwise, we will use linear scaling\n",
120128
" \n",
121-
" # Choose a colormap for visualizing the data\n",
122-
" # The colormap 'inferno' is used here, which is a perceptually uniform colormap\n",
123-
" cmap_instance = plt.get_cmap(\"inferno\")\n",
124-
" \n",
125-
" # Extract the latitude and longitude boundaries from the metadata\n",
126-
" lat_min = metadata[0][0] # Minimum latitude\n",
127-
" lat_max = metadata[0][1] # Maximum latitude\n",
128-
" lon_min = metadata[1][0] # Minimum longitude\n",
129-
" lon_max = metadata[1][1] # Maximum longitude\n",
129+
" if not is_log:\n",
130+
" vmin = np.min(actual_data)\n",
131+
" vmax = np.max(actual_data)\n",
132+
" norm = None # Use default linear normalization\n",
133+
" else:\n",
134+
" # Ensure data is positive for log scaling\n",
135+
" actual_data = np.clip(actual_data, EPSILON, None)\n",
136+
" vmin = np.min(actual_data)\n",
137+
" vmax = np.max(actual_data)\n",
138+
" from matplotlib.colors import LogNorm\n",
139+
" norm = LogNorm(vmin=vmin, vmax=vmax)\n",
130140
"\n",
131-
" # Plot the figure with a default range\n",
132-
" # The default range is determined by the full range of the colormap without considering the data limits\n",
141+
" # Extract bounds from metadata\n",
142+
" lat_min, lat_max = metadata[0]\n",
143+
" lon_min, lon_max = metadata[1]\n",
144+
"\n",
145+
" # Plotting\n",
133146
" fig, axs = plt.subplots(1, 1, figsize=(10, 8))\n",
134147
" axs.set_xlim(lat_min, lat_max)\n",
135148
" axs.set_ylim(lon_min, lon_max)\n",
136149
" axs.set_title(\"Selected Subregion Of Interest (Default Range)\")\n",
137150
" axs.set_xlabel(\"Longitude (Degrees)\")\n",
138151
" axs.set_ylabel(\"Latitude (Degrees)\")\n",
139-
" \n",
140-
" # Use imshow with a default color range (use the default colormap range)\n",
152+
"\n",
153+
" # Apply norm to imshow\n",
141154
" data_fig = axs.imshow(\n",
142155
" actual_data,\n",
143156
" cmap=cmap_instance,\n",
144157
" origin=\"lower\",\n",
145158
" extent=(lat_min, lat_max, lon_min, lon_max),\n",
159+
" norm=norm\n",
146160
" )\n",
147-
" \n",
148-
" # Add a colorbar to the plot\n",
161+
"\n",
162+
" # Add colorbar\n",
149163
" cbar = fig.colorbar(\n",
150164
" data_fig,\n",
151165
" ax=axs,\n",
152166
" fraction=0.046 * actual_data.shape[0] / actual_data.shape[1],\n",
153167
" pad=0.04,\n",
154168
" )\n",
155-
" \n",
156-
" print(\"You have successfully plotted your data with the default range.\")\n",
157-
" \n",
158-
" # Display the plot\n",
169+
" cbar.set_label('Value')\n",
170+
"\n",
171+
" print(\"You have successfully plotted your data with the selected color scale.\")\n",
159172
" plt.show()\n",
160173
"\n",
161174
"except Exception as e:\n",
162-
" # Handle any errors that occur during data loading or processing\n",
163175
" print(f\"Error: Failed to load or process data from '{data_file}'. {str(e)}\")\n"
164176
]
165177
},
@@ -184,55 +196,57 @@
184196
"metadata": {},
185197
"outputs": [],
186198
"source": [
199+
"is_log = True # Or False, depending on the mode\n",
200+
"EPSILON = 0.001\n",
187201
"\n",
188202
"try:\n",
203+
" # Select the colormap\n",
204+
" cmap_instance = plt.get_cmap(\"viridis\")\n",
205+
"\n",
206+
" # Process data range based on log/linear scaling you selected from dashboard\n",
207+
" # If is_log is True, we will use logarithmic scaling; otherwise, we will use linear scaling\n",
189208
" \n",
190-
" # Choose a colormap for visualizing the data\n",
191-
" # The colormap 'inferno' is used here, which is a perceptually uniform colormap\n",
192-
" cmap_instance = plt.get_cmap(\"inferno\")\n",
193-
" \n",
194-
" # Extract the latitude and longitude boundaries from the metadata\n",
195-
" lat_min = metadata[0][0] # Minimum latitude\n",
196-
" lat_max = metadata[0][1] # Maximum latitude\n",
197-
" lon_min = metadata[1][0] # Minimum longitude\n",
198-
" lon_max = metadata[1][1] # Maximum longitude\n",
209+
" if not is_log:\n",
210+
" vmin = np.min(actual_data)\n",
211+
" vmax = np.max(actual_data)\n",
212+
" norm = None # Use default linear normalization\n",
213+
" else:\n",
214+
" # Ensure data is positive for log scaling\n",
215+
" actual_data = np.clip(actual_data, EPSILON, None)\n",
216+
" vmin = np.min(actual_data)\n",
217+
" vmax = np.max(actual_data)\n",
218+
" from matplotlib.colors import LogNorm\n",
219+
" norm = LogNorm(vmin=vmin, vmax=vmax)\n",
199220
"\n",
200-
" print(\"You have successfully plotted your data with the default range.\")\n",
221+
" # Extract bounds from metadata\n",
222+
" lat_min, lat_max = metadata[0]\n",
223+
" lon_min, lon_max = metadata[1]\n",
201224
"\n",
202-
" # Replot the figure with the actual min and max range\n",
203-
" # Calculate the min and max values from the data\n",
204-
" vmin = actual_data.min()\n",
205-
" vmax = actual_data.max()\n",
206-
" \n",
225+
" # Plotting\n",
207226
" fig, axs = plt.subplots(1, 1, figsize=(10, 8))\n",
208227
" axs.set_xlim(lat_min, lat_max)\n",
209228
" axs.set_ylim(lon_min, lon_max)\n",
210-
" axs.set_title(\"Selected Subregion Of Interest (Min-Max Range)\")\n",
229+
" axs.set_title(\"Selected Subregion Of Interest (Default Range)\")\n",
211230
" axs.set_xlabel(\"Longitude (Degrees)\")\n",
212231
" axs.set_ylabel(\"Latitude (Degrees)\")\n",
213-
" \n",
214-
" # Use imshow with the calculated min and max range\n",
232+
"\n",
233+
" # Apply norm to imshow\n",
215234
" data_fig = axs.imshow(\n",
216235
" actual_data,\n",
217236
" cmap=cmap_instance,\n",
218-
" vmin=vmin,\n",
219-
" vmax=vmax,\n",
220237
" origin=\"lower\",\n",
221238
" extent=(lat_min, lat_max, lon_min, lon_max),\n",
239+
" norm=norm\n",
222240
" )\n",
223241
"\n",
224-
" # Add a colorbar with the min-max range\n",
242+
" # Add colorbar\n",
225243
" cbar = fig.colorbar(\n",
226244
" data_fig,\n",
227245
" ax=axs,\n",
228246
" fraction=0.046 * actual_data.shape[0] / actual_data.shape[1],\n",
229247
" pad=0.04,\n",
230248
" )\n",
231-
" \n",
232-
" # Set the ticks for the colorbar\n",
233-
" cbar_ticks = np.linspace(vmin, vmax, 8)\n",
234-
" cbar.set_ticks(cbar_ticks)\n",
235-
" \n",
249+
" cbar.set_label('Value')\n",
236250
" print(\"You have successfully replotted your data with the min-max range.\")\n",
237251
" \n",
238252
" # Calculate statistical values for the data\n",
@@ -250,9 +264,12 @@
250264
" # Display the plot with min-max range\n",
251265
" plt.show()\n",
252266
"\n",
267+
" print(\"You have successfully plotted your data with the selected color scale.\")\n",
268+
" plt.show()\n",
269+
"\n",
253270
"except Exception as e:\n",
254-
" # Handle any errors that occur during data loading or processing\n",
255-
" print(f\"Error: Failed to load or process data from '{data_file}'. {str(e)}\")\n"
271+
" print(f\"Error: Failed to load or process data from '{data_file}'. {str(e)}\")\n",
272+
"\n"
256273
]
257274
},
258275
{
@@ -276,69 +293,79 @@
276293
"metadata": {},
277294
"outputs": [],
278295
"source": [
279-
"is_log = True # Or False, depending on the mode\n",
280-
"EPSILON = 0.001\n",
281-
"\n",
282296
"try:\n",
283-
" # Select the colormap\n",
284-
" cmap_instance = plt.get_cmap(\"viridis\")\n",
285-
"\n",
286-
" # Process data range based on log/linear scaling you selected from dashboard\n",
287-
" # If is_log is True, we will use logarithmic scaling; otherwise, we will use linear scaling\n",
288297
" \n",
289-
" if not is_log:\n",
290-
" vmin = np.min(actual_data)\n",
291-
" vmax = np.max(actual_data)\n",
292-
" norm = None # Use default linear normalization\n",
293-
" else:\n",
294-
" # Ensure data is positive for log scaling\n",
295-
" actual_data = np.clip(actual_data, EPSILON, None)\n",
296-
" vmin = np.min(actual_data)\n",
297-
" vmax = np.max(actual_data)\n",
298-
" from matplotlib.colors import LogNorm\n",
299-
" norm = LogNorm(vmin=vmin, vmax=vmax)\n",
298+
" # Choose a colormap for visualizing the data\n",
299+
" # The colormap 'inferno' is used here, which is a perceptually uniform colormap\n",
300+
" cmap_instance = plt.get_cmap(\"inferno\")\n",
301+
" \n",
302+
" # Extract the latitude and longitude boundaries from the metadata\n",
303+
" lat_min = metadata[0][0] # Minimum latitude\n",
304+
" lat_max = metadata[0][1] # Maximum latitude\n",
305+
" lon_min = metadata[1][0] # Minimum longitude\n",
306+
" lon_max = metadata[1][1] # Maximum longitude\n",
300307
"\n",
301-
" # Extract bounds from metadata\n",
302-
" lat_min, lat_max = metadata[0]\n",
303-
" lon_min, lon_max = metadata[1]\n",
304308
"\n",
305-
" # Plotting\n",
309+
" # Replot the figure with a fixed range from 0 to 3000\n",
310+
" # This range is arbitrary and meant to highlight different data ranges\n",
311+
" fixed_vmin = 0\n",
312+
" fixed_vmax = 600\n",
313+
" \n",
306314
" fig, axs = plt.subplots(1, 1, figsize=(10, 8))\n",
307315
" axs.set_xlim(lat_min, lat_max)\n",
308316
" axs.set_ylim(lon_min, lon_max)\n",
309-
" axs.set_title(\"Selected Subregion Of Interest (Default Range)\")\n",
317+
" axs.set_title(\"Selected Subregion Of Interest (Range 0 to 3000)\")\n",
310318
" axs.set_xlabel(\"Longitude (Degrees)\")\n",
311319
" axs.set_ylabel(\"Latitude (Degrees)\")\n",
312-
"\n",
313-
" # Apply norm to imshow\n",
320+
" \n",
321+
" # Use imshow with the fixed range from 0 to 3000\n",
314322
" data_fig = axs.imshow(\n",
315323
" actual_data,\n",
316324
" cmap=cmap_instance,\n",
325+
" vmin=fixed_vmin,\n",
326+
" vmax=fixed_vmax,\n",
317327
" origin=\"lower\",\n",
318328
" extent=(lat_min, lat_max, lon_min, lon_max),\n",
319-
" norm=norm\n",
320329
" )\n",
321330
"\n",
322-
" # Add colorbar\n",
331+
" # Add a colorbar with the fixed range\n",
323332
" cbar = fig.colorbar(\n",
324333
" data_fig,\n",
325334
" ax=axs,\n",
326335
" fraction=0.046 * actual_data.shape[0] / actual_data.shape[1],\n",
327336
" pad=0.04,\n",
328337
" )\n",
329-
" cbar.set_label('Value')\n",
338+
" \n",
339+
" # Set the ticks for the colorbar\n",
340+
" cbar_ticks = np.linspace(fixed_vmin, fixed_vmax, 8)\n",
341+
" cbar.set_ticks(cbar_ticks)\n",
342+
" \n",
343+
" print(\"You have successfully replotted your data with the range 0 to 3000.\")\n",
344+
" \n",
345+
" # Calculate statistical values for the data\n",
346+
" max_slope = vmax # Maximum value in the data\n",
347+
" min_slope = vmin # Minimum value in the data\n",
348+
" avg_slope = actual_data.mean() # Average value of the data\n",
349+
" std_slope = actual_data.std() # Standard deviation of the data\n",
350+
" \n",
351+
" # Print the calculated statistical values\n",
352+
" print(f\"Maximum slope value: {max_slope}\")\n",
353+
" print(f\"Minimum slope value: {min_slope}\")\n",
354+
" print(f\"Average slope value: {avg_slope}\")\n",
355+
" print(f\"Standard deviation of slope values: {std_slope}\")\n",
330356
"\n",
331-
" print(\"You have successfully plotted your data with the selected color scale.\")\n",
357+
" # Display the plot with the fixed range\n",
332358
" plt.show()\n",
333359
"\n",
334360
"except Exception as e:\n",
335-
" print(f\"Error: Failed to load or process data from '{data_file}'. {str(e)}\")\n"
361+
" # Handle any errors that occur during data loading or processing\n",
362+
" print(f\"Error: Failed to load or process data from '{data_file}'. {str(e)}\")"
336363
]
337364
}
338365
],
339366
"metadata": {
340367
"kernelspec": {
341-
"display_name": "Python 3 (ipykernel)",
368+
"display_name": "nasa",
342369
"language": "python",
343370
"name": "python3"
344371
},
@@ -352,7 +379,7 @@
352379
"name": "python",
353380
"nbconvert_exporter": "python",
354381
"pygments_lexer": "ipython3",
355-
"version": "3.11.5"
382+
"version": "3.11.11"
356383
}
357384
},
358385
"nbformat": 4,

0 commit comments

Comments
 (0)