# Uncomment to install climdata in Google Colab or other environments
# !pip install climdata
MSWX Dataset — climdata Example¶
MSWX (Multi-Source Weather) is a global, daily, 0.1° meteorological dataset based on the ERA5 reanalysis, bias-corrected against in-situ station observations. It is distributed via Google Drive and accessed in climdata through a Google Service Account.
Available variables:
| Variable | Description | Units |
|---|---|---|
tasmin |
Daily minimum air temperature | °C |
tasmax |
Daily maximum air temperature | °C |
tas |
Daily mean air temperature | °C |
pr |
Daily precipitation | mm/day |
rsds |
Downward shortwave radiation | W/m² |
hurs |
Relative humidity | % |
sfcWind |
Wind speed | m/s |
Prerequisites: A Google Service Account JSON key file with read access to the MSWX Google Drive folders is required for downloading data.
1. Setup & Imports¶
from climdata import ClimData
from climdata.datasets.MSWX import MSWXmirror
import xarray as xr
import pandas as pd
import logging
logging.basicConfig(
level=logging.INFO,
format="%(levelname)s | %(message)s",
force=True,
)
2. Configuration¶
Set up the configuration overrides. The google_service_account path must point to your Google Service Account JSON key.
overrides = [
"dataset=mswx", # Select the MSWX dataset
"region=germany", # Predefined bounding box
"time_range.start_date=2010-01-01", # Start date for data extraction
"time_range.end_date=2010-03-31", # End date for data extraction
"variables=[tasmin,tasmax,pr]", # Variables to extract
"data_dir=./data", # Local directory to store downloaded files
"dsinfo.mswx.params.google_service_account=./.climdata_conf/service.json", # Service account key
]
extractor = ClimData(overrides=overrides)
3. Download Only¶
fetch() is the download step on its own: for each variable it works out the expected daily filenames over time_range, skips the ones already on disk, and pulls only the missing ones from Google Drive. It returns the list of local filenames — no NetCDF file is opened and nothing is read into memory.
Use this when you just want to stage the files locally (e.g. on a login node, or ahead of a batch job) and read them later.
# Download only — no reading, no xarray.
cfg = extractor.cfg
mswx_dl = MSWXmirror(cfg)
mswx_info = cfg.dsinfo[cfg.dataset.upper()]
for var in cfg.variables:
folder_id = mswx_info["variables"][var]["folder_id"]
files = mswx_dl.fetch(folder_id, var)
print(f"{var}: {len(files)} files staged in {cfg.data_dir}/{cfg.dataset.upper()}/{var.upper()}")
4. Download & Load Data¶
extract() runs the same fetch() download above and then reads every file into a single xarray.Dataset. If you already ran the download-only cell, nothing is re-downloaded here — only the read happens.
ds = extractor.extract()
print(ds)
5. Inspect the Dataset¶
# Inspect one variable
ds['tasmin']
# Convert to pandas DataFrame for quick inspection
df_tasmin = ds['tasmin'].to_dataframe().reset_index()
df_tasmin.head()
6. Spatial Extraction — Bounding Box¶
overrides_box = [
"dataset=mswx",
"time_range.start_date=2010-01-01",
"time_range.end_date=2010-03-31",
"variables=[tasmax,pr]",
"data_dir=./data",
"dsinfo.mswx.params.google_service_account=./.climdata_conf/service.json",
]
extractor_box = ClimData(overrides=overrides_box)
mswx_box = MSWXmirror(extractor_box.cfg)
# Extract a bounding box (Germany)
mswx_box.extract(box={
"lon_min": 5.8,
"lon_max": 15.0,
"lat_min": 47.3,
"lat_max": 55.1
})
ds_box = mswx_box.load(variable="tasmax")
print(ds_box)
7. Run the Full Workflow via ClimData¶
Use ClimData.run_workflow() to chain extraction, optional imputation, climate index calculation, and saving to NetCDF in one call.
overrides_wf = [
"dataset=mswx",
"lat=52",
"lon=13",
"time_range.start_date=2010-01-01",
"time_range.end_date=2010-12-31",
"variables=[tasmin,tasmax,pr]",
"data_dir=./data",
"dsinfo.mswx.params.google_service_account=./.climdata_conf/service.json",
"index=tn10p", # Cold nights climate extreme index
"impute=BRITS", # Deep-learning gap-filling (optional)
]
seq = ["extract", "impute", "calc_index", "to_nc"]
extractor_wf = ClimData(overrides=overrides_wf)
result = extractor_wf.run_workflow(actions=seq)
result
8. Save to CSV¶
Write the extracted data out as a flat CSV file.
mswx_box.save_csv("mswx_tasmax_2010.csv")
print("Saved to mswx_tasmax_2010.csv")