52 netcdf
In [1]:
Copied!
# !pip install leafmap
# !pip install leafmap
In [2]:
Copied!
# !pip install xarray rioxarray netcdf4 localtileserver
# !pip install xarray rioxarray netcdf4 localtileserver
In [3]:
Copied!
from leafmap import leafmap
from leafmap import leafmap
Download a sample NetCDF dataset.
In [4]:
Copied!
url = "https://github.com/opengeos/datasets/releases/download/raster/wind_global.nc"
filename = "wind_global.nc"
url = "https://github.com/opengeos/datasets/releases/download/raster/wind_global.nc"
filename = "wind_global.nc"
In [5]:
Copied!
leafmap.download_file(url, output=filename, overwrite=True)
leafmap.download_file(url, output=filename, overwrite=True)
Downloading... From: https://github.com/opengeos/datasets/releases/download/raster/wind_global.nc To: /home/runner/work/leafmap/leafmap/docs/notebooks/wind_global.nc
0%| | 0.00/1.05M [00:00<?, ?B/s]
100%|██████████| 1.05M/1.05M [00:00<00:00, 109MB/s]
Out[5]:
'/home/runner/work/leafmap/leafmap/docs/notebooks/wind_global.nc'
Read the NetCDF dataset.
In [6]:
Copied!
data = leafmap.read_netcdf(filename)
data
data = leafmap.read_netcdf(filename)
data
Out[6]:
<xarray.Dataset> Size: 1MB Dimensions: (lat: 181, lon: 360) Coordinates: * lat (lat) float64 1kB 90.0 89.0 88.0 87.0 ... -87.0 -88.0 -89.0 -90.0 * lon (lon) float64 3kB 0.0 1.0 2.0 3.0 4.0 ... 356.0 357.0 358.0 359.0 Data variables: u_wind (lat, lon) float64 521kB ... v_wind (lat, lon) float64 521kB ... Attributes: centerName: US National Weather Service - NCEP(WMC) disciplineName: Meteorological products refTime: 2016-04-30T06:00:00.000Z
Convert the NetCDF dataset to GeoTIFF. Note that the longitude range of the NetCDF dataset is [0, 360]
. We need to convert it to [-180, 180]
by setting shift_lon=True
so that it can be displayed on the map.
In [7]:
Copied!
tif = "wind_global.tif"
leafmap.netcdf_to_tif(filename, tif, variables=["u_wind", "v_wind"], shift_lon=True)
tif = "wind_global.tif"
leafmap.netcdf_to_tif(filename, tif, variables=["u_wind", "v_wind"], shift_lon=True)
Out[7]:
'/home/runner/work/leafmap/leafmap/docs/notebooks/wind_global.tif'
Add the GeoTIFF to the map. We can also overlay the country boundary on the map.
In [8]:
Copied!
geojson = (
"https://github.com/opengeos/leafmap/raw/master/examples/data/countries.geojson"
)
geojson = (
"https://github.com/opengeos/leafmap/raw/master/examples/data/countries.geojson"
)
In [9]:
Copied!
m = leafmap.Map(layers_control=True)
m.add_raster(tif, indexes=[1], palette="coolwarm", layer_name="u_wind")
m.add_geojson(geojson, layer_name="Countries")
m
m = leafmap.Map(layers_control=True)
m.add_raster(tif, indexes=[1], palette="coolwarm", layer_name="u_wind")
m.add_geojson(geojson, layer_name="Countries")
m
You can also use the add_netcdf()
function to add the NetCDF dataset to the map without having to convert it to GeoTIFF explicitly.
In [10]:
Copied!
m = leafmap.Map(layers_control=True)
m.add_netcdf(
filename,
variables=["v_wind"],
palette="coolwarm",
shift_lon=True,
layer_name="v_wind",
indexes=[1],
)
m.add_geojson(geojson, layer_name="Countries")
m
m = leafmap.Map(layers_control=True)
m.add_netcdf(
filename,
variables=["v_wind"],
palette="coolwarm",
shift_lon=True,
layer_name="v_wind",
indexes=[1],
)
m.add_geojson(geojson, layer_name="Countries")
m
Visualizing wind velocity.
In [11]:
Copied!
m = leafmap.Map(layers_control=True)
m.add_basemap("CartoDB.DarkMatter")
m.add_velocity(
filename,
zonal_speed="u_wind",
meridional_speed="v_wind",
color_scale=[
"rgb(0,0,150)",
"rgb(0,150,0)",
"rgb(255,255,0)",
"rgb(255,165,0)",
"rgb(150,0,0)",
],
)
m
m = leafmap.Map(layers_control=True)
m.add_basemap("CartoDB.DarkMatter")
m.add_velocity(
filename,
zonal_speed="u_wind",
meridional_speed="v_wind",
color_scale=[
"rgb(0,0,150)",
"rgb(0,150,0)",
"rgb(255,255,0)",
"rgb(255,165,0)",
"rgb(150,0,0)",
],
)
m