Package: env-canada
Environment Canada (env_canada)This package provides access to various data sources published byEnvironment and Climate Change Canada.Weather Observations and ForecastsECWeatherprovides current conditions and forecasts. It automatically determines which weather station to use based on latitude/longitude provided. It is also possible to specify a specific station code of the formAB/s0000123based on those listed inthis CSV file. For example:importasynciofromenv_canadaimportECWeatherec_en=ECWeather(coordinates=(50,-100))ec_fr=ECWeather(station_id='ON/s0000430',language='french')asyncio.run(ec_en.update())# current conditionsec_en.conditions# daily forecastsec_en.daily_forecasts# hourly forecastsec_en.hourly_forecasts# alertsec_en.alertsWeather RadarECRadarprovides Environment Canada meteorologicalradar imagery.importasynciofromenv_canadaimportECRadarradar_coords=ECRadar(coordinates=(50,-100))# Conditions Availableanimated_gif=asyncio.run(radar_coords.get_loop())latest_png=asyncio.run(radar_coords.get_latest_frame())Air Quality Health Index (AQHI)ECAirQualityprovides Environment Canadaair qualitydata.importasynciofromenv_canadaimportECAirQualityaqhi_coords=ECAirQuality(coordinates=(50,-100))asyncio.run(aqhi_coords.update())# Data availableaqhi_coords.currentaqhi_coords.forecastsWater Level and FlowECHydroprovides Environment Canadahydrometricdata.importasynciofromenv_canadaimportECHydrohydro_coords=ECHydro(coordinates=(50,-100))asyncio.run(hydro_coords.update())# Data availablehydro_coords.measurementsHistorical Weather DataECHistoricalprovides historical daily weather data.
The ECHistorical object is instantiated with a station ID, year, language, format (one of xml or csv) and granularity (hourly, daily data).
Once updated asynchronously, historical weather data is contained with thestation_dataproperty. Ifxmlis requested,station_datawill appear in a dictionary form. Ifcsvis requested,station_datawill contain a CSV-readable buffer. For example:importasynciofromenv_canadaimportECHistorical,get_historical_stations# search for stations, response contains station_idscoordinates=[53.916944,-122.749444]# [lat, long]# coordinates: [lat, long]# radius: km# limit: response limit, value one of [10, 25, 50, 100]# The result contains station names and ID values.stations=asyncio.run(get_historical_stations(coordinates,radius=200,limit=100))ec_en_xml=ECHistorical(station_id=31688,year=2020,language="english",format="xml")ec_fr_xml=ECHistorical(station_id=31688,year=2020,language="french",format="xml")ec_en_csv=ECHistorical(station_id=31688,year=2020,language="english",format="csv")ec_fr_csv=ECHistorical(station_id=31688,year=2020,language="french",format="csv")# timeframe argument can be passed to change the granularity# timeframe=1 hourly (need to create of for every month in that case, use ECHistoricalRange to handle it automatically)# timeframe=2 daily (default)ec_en_xml=ECHistorical(station_id=31688,year=2020,month=1,language="english",format="xml",timeframe=1)ec_en_csv=ECHistorical(station_id=31688,year=2020,month=1,language="english",format="csv",timeframe=1)asyncio.run(ec_en_xml.update())asyncio.run(ec_en_csv.update())# metadata describing the stationec_en_xml.metadata# historical weather data, in dictionary formec_en_xml.station_data# csv-generated responses return csv-like station dataimportpandasaspddf=pd.read_csv(ec_en_csv.station_data)ECHistoricalRangeprovides historical weather data within a specific range and handles the update by itself.The ECHistoricalRange object is instantiated with at least a station ID and a daterange.
One could add language, and granularity (hourly, daily (default)).The data can then be used as pandas DataFrame, XML (requires pandas >=1.3.0) and csvFor example :importpandasaspdimportasynciofromenv_canadaimportECHistoricalRange,get_historical_stationsfromdatetimeimportdatetimecoordinates=['48.508333','-68.467667']stations=pd.DataFrame(asyncio.run(get_historical_stations(coordinates,start_year=2022,end_year=2022,radius=200,limit=100))).Tec=ECHistoricalRange(station_id=int(stations.iloc[0,2]),timeframe="daily",daterange=(datetime(2022,7,1,12,12),datetime(2022,8,1,12,12)))ec.get_data()#yield an XML formated str.# For more options, use ec.to_xml(*arg, **kwargs) with pandas optionsec.xml#yield an CSV formated str.# For more options, use ec.to_csv(*arg, **kwargs) with pandas optionsec.csvIn this exampleec.dfwill be:Date/TimeLongitude (x)Latitude (y)Station NameClimate IDYearMonthDayData QualityMax Temp (Â°C)Max Temp FlagMin Temp (Â°C)Min Temp FlagMean Temp (Â°C)Mean Temp FlagHeat Deg Days (Â°C)Heat Deg Days FlagCool Deg Days (Â°C)Cool Deg Days FlagTotal Rain (mm)Total Rain FlagTotal Snow (cm)Total Snow FlagTotal Precip (mm)Total Precip FlagSnow on Grnd (cm)Snow on Grnd FlagDir of Max Gust (10s deg)Dir of Max Gust FlagSpd of Max Gust (km/h)Spd of Max Gust Flag2022-07-02-68,4748,51POINTE-AU-PERE (INRS)705606820227222,812,517,70,30026372022-07-03-68,4748,51POINTE-AU-PERE (INRS)705606820227321,710,115,92,100,42850……………………………………………………………………………………2022-07-31-68,4748,51POINTE-AU-PERE (INRS)7056068202273123,514,118,800,8023312022-08-01-68,4748,51POINTE-AU-PERE (INRS)70560682022812315190102135One should note that july 1st is excluded as the time provided contains specific hours, so it yields only data after or at exactly
the time provided.To have all the july 1st data in that case, one can provide a datarange without time:datetime(2022, 7, 7)instead
ofdatetime(2022, 7, 1, 12, 12)LicenseThe code is available under terms ofMIT License