Last active
August 24, 2016 23:22
-
-
Save alchzh/52efc767a580fa4ac7e3fb6d1e7428ca to your computer and use it in GitHub Desktop.
WIP GFS WRF forecast
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
A script to run a WRF forecast for Pennsylvania, | |
but it can easily be modified for anything else by | |
changing params and modifying namelist.wps files. | |
Should be run automatically by cron or equivalent | |
Requires requests and f90nml modules because I'm lazy | |
and this is really meant for (my!) personal usage... | |
Make sure WPS and WRFV3 are all in the directory | |
alongside wm.py | |
WARNING: Messy code ahead. Run before GMT00 on next day | |
and after about GMT15 | |
""" | |
import os | |
import subprocess | |
import f90nml | |
# First we need to grab the GFS data files | |
from requests import Session | |
from time import strftime, time, gmtime | |
def get_data(): | |
# I don't know if this works but might as well | |
s = Session() | |
# Quicker to change params around this way | |
params = { | |
"dir": "/gfs." + strftime("%Y%m%d") + "12", | |
"all_lev": "on", | |
"all_var": "on", | |
# "var_PTROP": "on", | |
"subregion": "", | |
"leftlon": -81, | |
"rightlon": -74.5, | |
"toplat": 42.5, | |
"bottomlat": 39.5, | |
} | |
# print() statements are just status messages | |
print("init down trying to download...") | |
# We need 36 of these... | |
for i in range(36): | |
params["file"] = "gfs.t12z.pgrb2.0p25.f%03d" % i | |
# I'm saving files to the ./GFS dir | |
with open("/home/sci/GFS/" + params["file"], "wb") as f: | |
print("opened " + params["file"]) | |
r = s.get("http://nomads.ncep.noaa.gov/cgi-bin/filter_gfs_0p25.pl", | |
params=params, | |
stream=True) # stream lets us well, stream it | |
if r.ok: | |
print("writing...") | |
# Breaks data into chunks and writes it to file | |
counter = 0 | |
for block in r.iter_content(1024): | |
f.write(block) | |
counter += 1 | |
print(" wrote " + str(counter) + "K", end="\r") | |
else: | |
print("\r\ndone writing...") | |
else: | |
# Is this the right Error? | |
raise FileNotFoundError("Data file not found, yet" + r.url) | |
def preprocess(): | |
os.chdir('WPS') | |
for f in os.listdir(): | |
if f.startswith("met_em") or "FILE" in f: | |
os.remove(f) | |
# f90nml is weird... | |
os.rename("namelist.wps", "namelist.wps.old") | |
# This is horrible but it works... | |
patch = {'share': | |
{'start_date': strftime("%Y-%m-%d_12:00:00", gmtime()), | |
'end_date': strftime("%Y-%m-%d_23:00:00", gmtime(time() + 86400)) | |
} | |
} | |
f90nml.patch("namelist.wps.old", patch, "namelist.wps") | |
subprocess.run(["tcsh", "link_grib.csh", "../GFS/*"]) | |
with open("ungrib.log", "w+") as f: | |
subprocess.run(["./ungrib.exe"], stdout=f) | |
if "Successful" not in f.readlines()[-1]: | |
raise Exception("Something went wrong! Check ungrib.log") | |
with open("metgrid.log", "w+") as f: | |
subprocess.run(["./metgrid.exe"], stdout=f) | |
if "Successful" not in f.readlines()[-1]: | |
raise Exception("Something went wrong! Check metgrid.log") | |
if __name__ == "main": | |
get_data() | |
preprocess() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
dank memes