-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgfs.py
77 lines (63 loc) · 1.79 KB
/
gfs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# Import dependices.
import wget
from datetime import datetime, timedelta
import numpy as np
import iris
import os
from tqdm import tqdm
from astropy import units
import sys
# Define preliminary urls.
# Air temperature.
t_url = "https://nomads.ncep.noaa.gov/cgi-bin/filter_gfs_1p00.pl?file=gfs.t{}z.pgrb2.1p00.f{}&lev_850_mb=on&var_TMP=on&leftlon=0&rightlon=360&toplat=90&bottomlat=-90&dir=%2Fgfs.{}"
# Geopotential height.
z_url = "https://nomads.ncep.noaa.gov/cgi-bin/filter_gfs_1p00.pl?file=gfs.t{}z.pgrb2.1p00.f{}&lev_500_mb=on&var_HGT=on&leftlon=0&rightlon=360&toplat=90&bottomlat=-90&dir=%2Fgfs.{}"
# Retrieve date.
date = datetime.now()
date = date.replace(
hour=date.hour - (date.hour % 6),
minute=0,
second=0,
microsecond=0
) - timedelta(hours=6)
# Retrieve current conditions.
def download_var(href):
# Retrieve forecast hour.
str_date = date.strftime('%Y%m%d') + "%2F"
forecast_hour = date.strftime('%H')
# Define hour.
hour = '%03d' % 0
# Define download file.
url = href.format(
forecast_hour, hour, str_date
) + forecast_hour
url += "%2Fatmos"
# Download file.
file = wget.download(url, bar=None)
# Load file.
cube = iris.load(file)[0]
cube.data
# Remove forecast period coordinates.
cube.remove_coord('forecast_period')
# Remove forecast reference time.
cube.remove_coord('forecast_reference_time')
# Delete file.
os.remove(file)
# Return cube.
return cube
# Download.
# Progress bar.
pbar = tqdm(total=2, desc='Downloading')
# Air temperature.
t = download_var(t_url)
t.var_name = 't'
iris.save(t, 'gfs/t.nc')
pbar.update()
# Geopotential.
g = 9.80665
z = download_var(z_url) * g
z.units = 'm2 s-2'
z.standard_name = 'geopotential'
z.var_name = 'z'
iris.save(z, 'gfs/z.nc')
pbar.update()