Skip to content

Commit 1edc48e

Browse files
Small update xarray intro + regenerate notebooks/solutions
1 parent 523d56c commit 1edc48e

31 files changed

Lines changed: 1169 additions & 298 deletions

_solved/14-xarray-intro.ipynb

Lines changed: 1035 additions & 188 deletions
Large diffs are not rendered by default.

notebooks/14-xarray-intro.ipynb

Lines changed: 63 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@
465465
},
466466
"outputs": [],
467467
"source": [
468-
"herstappe = herstappe.assign_coords(band=(\"band\", [\"R\", \"G\", \"B\"]))\n",
468+
"herstappe = herstappe.assign_coords(band=(\"band\", [\"red\", \"green\", \"blue\"]))\n",
469469
"herstappe"
470470
]
471471
},
@@ -491,7 +491,7 @@
491491
},
492492
"outputs": [],
493493
"source": [
494-
"herstappe.sel(band='R')"
494+
"herstappe.sel(band='red')"
495495
]
496496
},
497497
{
@@ -552,7 +552,7 @@
552552
"outputs": [],
553553
"source": [
554554
"herstappe = xr.open_rasterio(data_file)\n",
555-
"herstappe = herstappe.assign_coords(band=(\"band\", [\"R\", \"G\", \"B\"]))"
555+
"herstappe = herstappe.assign_coords(band=(\"band\", [\"red\", \"green\", \"blue\"]))"
556556
]
557557
},
558558
{
@@ -598,7 +598,7 @@
598598
},
599599
"outputs": [],
600600
"source": [
601-
"herstappe.sel(band=\"R\")"
601+
"herstappe.sel(band=\"red\")"
602602
]
603603
},
604604
{
@@ -630,7 +630,7 @@
630630
},
631631
"outputs": [],
632632
"source": [
633-
"herstappe.sel(x=[600_000, 610_000, 620_000], method=\"nearest\").sel(band=\"R\").plot.line(hue=\"x\");"
633+
"herstappe.sel(x=[600_000, 610_000, 620_000], method=\"nearest\").sel(band=\"red\").plot.line(hue=\"x\");"
634634
]
635635
},
636636
{
@@ -653,7 +653,7 @@
653653
},
654654
"outputs": [],
655655
"source": [
656-
"herstappe.sel(x=slice(580_000, 620_000), band=\"G\").plot.imshow(cmap=\"Greens\")"
656+
"herstappe.sel(x=slice(580_000, 620_000), band=\"green\").plot.imshow(cmap=\"Greens\")"
657657
]
658658
},
659659
{
@@ -703,7 +703,7 @@
703703
"metadata": {},
704704
"outputs": [],
705705
"source": [
706-
"herstappe.where(herstappe > 0.2).sel(band=\"R\").plot.imshow()"
706+
"herstappe.where(herstappe > 0.2).sel(band=\"red\").plot.imshow()"
707707
]
708708
},
709709
{
@@ -726,7 +726,9 @@
726726
"**EXERCISE**:\n",
727727
"\n",
728728
"* Read in the file `./data/gent/raster/2020-09-17_Sentinel_2_L1C_True_color.tiff` with xarray and assign the data to a new variable `tc_data`. \n",
729+
"* Inspect the display of `tc_data`. What are the different dimensions of the array? \n",
729730
"* Select only the *second* layer of `tc_data` and assign the output to a new variable `tc_g`.\n",
731+
"* Plot the second (green) layer.\n",
730732
"* Create a new array with each of the elements in the `tc_g` array with a value above 15000 set to a new value of 65535.\n",
731733
" \n",
732734
"<details><summary>Hints</summary>\n",
@@ -756,13 +758,38 @@
756758
"execution_count": null,
757759
"metadata": {
758760
"clear_cell": true,
761+
"editable": true,
759762
"tags": []
760763
},
761764
"outputs": [],
762765
"source": [
763766
"# %load _solutions2/14-xarray-intro2.py"
764767
]
765768
},
769+
{
770+
"cell_type": "code",
771+
"execution_count": null,
772+
"metadata": {
773+
"clear_cell": true,
774+
"tags": []
775+
},
776+
"outputs": [],
777+
"source": [
778+
"# %load _solutions2/14-xarray-intro3.py"
779+
]
780+
},
781+
{
782+
"cell_type": "code",
783+
"execution_count": null,
784+
"metadata": {
785+
"clear_cell": true,
786+
"tags": []
787+
},
788+
"outputs": [],
789+
"source": [
790+
"# %load _solutions2/14-xarray-intro4.py"
791+
]
792+
},
766793
{
767794
"cell_type": "code",
768795
"execution_count": null,
@@ -775,7 +802,7 @@
775802
},
776803
"outputs": [],
777804
"source": [
778-
"# %load _solutions2/14-xarray-intro3.py"
805+
"# %load _solutions2/14-xarray-intro5.py"
779806
]
780807
},
781808
{
@@ -808,7 +835,7 @@
808835
},
809836
"outputs": [],
810837
"source": [
811-
"# %load _solutions2/14-xarray-intro4.py"
838+
"# %load _solutions2/14-xarray-intro6.py"
812839
]
813840
},
814841
{
@@ -823,7 +850,7 @@
823850
},
824851
"outputs": [],
825852
"source": [
826-
"# %load _solutions2/14-xarray-intro5.py"
853+
"# %load _solutions2/14-xarray-intro7.py"
827854
]
828855
},
829856
{
@@ -867,7 +894,7 @@
867894
},
868895
"outputs": [],
869896
"source": [
870-
"# %load _solutions2/14-xarray-intro6.py"
897+
"# %load _solutions2/14-xarray-intro8.py"
871898
]
872899
},
873900
{
@@ -882,7 +909,7 @@
882909
},
883910
"outputs": [],
884911
"source": [
885-
"# %load _solutions2/14-xarray-intro7.py"
912+
"# %load _solutions2/14-xarray-intro9.py"
886913
]
887914
},
888915
{
@@ -894,7 +921,7 @@
894921
},
895922
"outputs": [],
896923
"source": [
897-
"# %load _solutions2/14-xarray-intro8.py"
924+
"# %load _solutions2/14-xarray-intro10.py"
898925
]
899926
},
900927
{
@@ -909,7 +936,7 @@
909936
},
910937
"outputs": [],
911938
"source": [
912-
"# %load _solutions2/14-xarray-intro9.py"
939+
"# %load _solutions2/14-xarray-intro11.py"
913940
]
914941
},
915942
{
@@ -924,7 +951,7 @@
924951
},
925952
"outputs": [],
926953
"source": [
927-
"# %load _solutions2/14-xarray-intro10.py"
954+
"# %load _solutions2/14-xarray-intro12.py"
928955
]
929956
},
930957
{
@@ -943,8 +970,8 @@
943970
"outputs": [],
944971
"source": [
945972
"herstappe = xr.open_rasterio(data_file)\n",
946-
"herstappe = herstappe.assign_coords(band=(\"band\", [\"R\", \"G\", \"B\"]))\n",
947-
"herstappeR = herstappe.sel(band=\"R\")"
973+
"herstappe = herstappe.assign_coords(band=(\"band\", [\"red\", \"green\", \"blue\"]))\n",
974+
"herstappeR = herstappe.sel(band=\"red\")"
948975
]
949976
},
950977
{
@@ -1216,7 +1243,7 @@
12161243
},
12171244
"outputs": [],
12181245
"source": [
1219-
"herstappe.sel(band=\"R\") - herstappe.sel(band=\"G\")"
1246+
"herstappe.sel(band=\"red\") - herstappe.sel(band=\"green\")"
12201247
]
12211248
},
12221249
{
@@ -1336,7 +1363,7 @@
13361363
},
13371364
"outputs": [],
13381365
"source": [
1339-
"# %load _solutions2/14-xarray-intro11.py"
1366+
"# %load _solutions2/14-xarray-intro13.py"
13401367
]
13411368
},
13421369
{
@@ -1348,7 +1375,7 @@
13481375
},
13491376
"outputs": [],
13501377
"source": [
1351-
"# %load _solutions2/14-xarray-intro12.py"
1378+
"# %load _solutions2/14-xarray-intro14.py"
13521379
]
13531380
},
13541381
{
@@ -1363,7 +1390,7 @@
13631390
},
13641391
"outputs": [],
13651392
"source": [
1366-
"# %load _solutions2/14-xarray-intro13.py"
1393+
"# %load _solutions2/14-xarray-intro15.py"
13671394
]
13681395
},
13691396
{
@@ -1414,7 +1441,7 @@
14141441
},
14151442
"outputs": [],
14161443
"source": [
1417-
"# %load _solutions2/14-xarray-intro14.py"
1444+
"# %load _solutions2/14-xarray-intro16.py"
14181445
]
14191446
},
14201447
{
@@ -1427,7 +1454,7 @@
14271454
},
14281455
"outputs": [],
14291456
"source": [
1430-
"# %load _solutions2/14-xarray-intro15.py"
1457+
"# %load _solutions2/14-xarray-intro17.py"
14311458
]
14321459
},
14331460
{
@@ -1440,7 +1467,7 @@
14401467
},
14411468
"outputs": [],
14421469
"source": [
1443-
"# %load _solutions2/14-xarray-intro16.py"
1470+
"# %load _solutions2/14-xarray-intro18.py"
14441471
]
14451472
},
14461473
{
@@ -1453,7 +1480,7 @@
14531480
},
14541481
"outputs": [],
14551482
"source": [
1456-
"# %load _solutions2/14-xarray-intro17.py"
1483+
"# %load _solutions2/14-xarray-intro19.py"
14571484
]
14581485
},
14591486
{
@@ -1468,7 +1495,7 @@
14681495
},
14691496
"outputs": [],
14701497
"source": [
1471-
"# %load _solutions2/14-xarray-intro18.py"
1498+
"# %load _solutions2/14-xarray-intro20.py"
14721499
]
14731500
},
14741501
{
@@ -1504,7 +1531,7 @@
15041531
},
15051532
"outputs": [],
15061533
"source": [
1507-
"# %load _solutions2/14-xarray-intro19.py"
1534+
"# %load _solutions2/14-xarray-intro21.py"
15081535
]
15091536
},
15101537
{
@@ -1556,7 +1583,7 @@
15561583
},
15571584
"outputs": [],
15581585
"source": [
1559-
"# %load _solutions2/14-xarray-intro20.py"
1586+
"# %load _solutions2/14-xarray-intro22.py"
15601587
]
15611588
},
15621589
{
@@ -1568,7 +1595,7 @@
15681595
},
15691596
"outputs": [],
15701597
"source": [
1571-
"# %load _solutions2/14-xarray-intro21.py"
1598+
"# %load _solutions2/14-xarray-intro23.py"
15721599
]
15731600
},
15741601
{
@@ -1581,7 +1608,7 @@
15811608
},
15821609
"outputs": [],
15831610
"source": [
1584-
"# %load _solutions2/14-xarray-intro22.py"
1611+
"# %load _solutions2/14-xarray-intro24.py"
15851612
]
15861613
},
15871614
{
@@ -1594,7 +1621,7 @@
15941621
},
15951622
"outputs": [],
15961623
"source": [
1597-
"# %load _solutions2/14-xarray-intro23.py"
1624+
"# %load _solutions2/14-xarray-intro25.py"
15981625
]
15991626
},
16001627
{
@@ -1636,7 +1663,7 @@
16361663
},
16371664
"outputs": [],
16381665
"source": [
1639-
"# %load _solutions2/14-xarray-intro24.py"
1666+
"# %load _solutions2/14-xarray-intro26.py"
16401667
]
16411668
},
16421669
{
@@ -1648,7 +1675,7 @@
16481675
},
16491676
"outputs": [],
16501677
"source": [
1651-
"# %load _solutions2/14-xarray-intro25.py"
1678+
"# %load _solutions2/14-xarray-intro27.py"
16521679
]
16531680
},
16541681
{
@@ -1660,7 +1687,7 @@
16601687
},
16611688
"outputs": [],
16621689
"source": [
1663-
"# %load _solutions2/14-xarray-intro26.py"
1690+
"# %load _solutions2/14-xarray-intro28.py"
16641691
]
16651692
},
16661693
{
@@ -1685,7 +1712,7 @@
16851712
},
16861713
"outputs": [],
16871714
"source": [
1688-
"# %load _solutions2/14-xarray-intro27.py"
1715+
"# %load _solutions2/14-xarray-intro29.py"
16891716
]
16901717
},
16911718
{
@@ -1701,15 +1728,8 @@
17011728
},
17021729
"outputs": [],
17031730
"source": [
1704-
"# %load _solutions2/14-xarray-intro28.py"
1731+
"# %load _solutions2/14-xarray-intro30.py"
17051732
]
1706-
},
1707-
{
1708-
"cell_type": "code",
1709-
"execution_count": null,
1710-
"metadata": {},
1711-
"outputs": [],
1712-
"source": []
17131733
}
17141734
],
17151735
"metadata": {
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# Create the histogram plots
2-
fig, (ax0, ax1) = plt.subplots(1, 2, sharey=True)
3-
b4_data.plot.hist(bins=30, log=True, ax=ax0)
4-
b4_data_f.plot.hist(bins=30, log=True, ax=ax1);
1+
# Convert to float and make 65535 equal to Nan
2+
b4_data_f = b4_data.astype(float)
3+
b4_data_f = b4_data_f.where(b4_data != 65535)
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
# Calculate the min and max for each channel
2-
h_min = herstappe_data.min(dim=["x", "y"])
3-
h_max = herstappe_data.max(dim=["x", "y"])
1+
# Count the number of cells with value 0
2+
np.sum(np.isnan(b4_data_f))

0 commit comments

Comments
 (0)