Get monthly average weather station data (Global)

1 minute read

Extract countries of interest (along with their coordinates) from the Global Historical Climatology Network - Monthly (GHCNM) Version 3

A README file can be found here

To run this script you will need to download the .dat and .inv files stored in the compressed files (ghncm.*.tar.gz) found here

The code can be downloaded from the get_station_data github repository

import numpy as np
import pandas as pd
from get_station_data import ghcnm

Name of original data file from GHCN-M

ftp://ftp.ncdc.noaa.gov/pub/data/ghcn/v3/

data_version = 'v3.3.0.20180404'
data_fname   = 'raw_data/ghcnm.tavg.'+data_version+'.qca.dat'
stn_md_fname = 'raw_data/ghcnm.tavg.'+data_version+'.qca.inv'

stn_md = ghcnm.get_stn_metadata(stn_md_fname)

stn_md.head()
station lat lon elev name country
0 10160355000 36.93 6.95 7.0 SKIKDA ALGERIA
1 10160360000 36.83 7.82 4.0 ANNABA ALGERIA
2 10160390000 36.72 3.25 25.0 DAR-EL-BEIDA ALGERIA
3 10160395001 36.52 4.18 942.0 FT. NATIONAL ALGERIA
4 10160400001 36.80 5.10 230.0 CAP CARBON ALGERIA

Specify countries to extract data from

country_names = ['Egypt', 'Libya', 'Sudan', 'ISRAEL', \
                    'SAUDI ARABIA', 'Chad', 'Jordan']

my_stns = ghcnm.extract_countries(stn_md, country_names)

my_stns.head()
station lat lon elev name country
132 11064700000 12.13 15.03 295.0 NDJAMENA CHAD
133 11064701000 14.12 15.32 355.0 MAO CHAD
134 11064702000 13.43 14.73 292.0 BOL-BERIM CHAD
135 11064705000 10.48 16.72 336.0 BOUSSO CHAD
136 11064706000 8.62 16.07 422.0 MOUNDOU CHAD

Extract data for specified stations into a Pandas DataFrame

df = ghcnm.get_data(data_fname, my_stns)
df.columns
Index(['country', 'name', 'station', 'lat', 'lon', 'elev', 'year', 'month',
       'variable', 'value', 'dmflag', 'qcflag', 'dsflag'],
      dtype='object')
df.drop(columns=['dmflag', 'qcflag', 'dsflag']).tail(n=10)
country name station lat lon elev year month variable value
79298 JORDAN AQABA AIRPORT 62440340000 29.63 35.02 51 1990 3 TAVG 18.9
79299 JORDAN AQABA AIRPORT 62440340000 29.63 35.02 51 1990 4 TAVG 24.3
79300 JORDAN AQABA AIRPORT 62440340000 29.63 35.02 51 1990 5 TAVG 26.9
79301 JORDAN AQABA AIRPORT 62440340000 29.63 35.02 51 1990 6 TAVG 30.5
79302 JORDAN AQABA AIRPORT 62440340000 29.63 35.02 51 1990 7 TAVG 32.4
79303 JORDAN AQABA AIRPORT 62440340000 29.63 35.02 51 1990 8 TAVG 31.7
79304 JORDAN AQABA AIRPORT 62440340000 29.63 35.02 51 1990 9 TAVG 28.9
79305 JORDAN AQABA AIRPORT 62440340000 29.63 35.02 51 1990 10 TAVG 26.8
79306 JORDAN AQABA AIRPORT 62440340000 29.63 35.02 51 1990 11 TAVG 23.2
79307 JORDAN AQABA AIRPORT 62440340000 29.63 35.02 51 1990 12 TAVG 18.0

Save to file

df.to_csv('Egypt_surrounding_ghcnm.csv', index=False)