Skip to content

Instantly share code, notes, and snippets.

@MiCurry
Last active December 10, 2017 07:11
Show Gist options
  • Save MiCurry/f4f05db23840f415eea2dd1999adc6a9 to your computer and use it in GitHub Desktop.
Save MiCurry/f4f05db23840f415eea2dd1999adc6a9 to your computer and use it in GitHub Desktop.
Update Manage.py
#!/usr/bin/env python
import os
import sys , traceback
import argparse
import time
import SharkEyesCore.startup as startup
from django.core.management import execute_from_command_line
from django.conf import settings
""" manage.py - Information
Baisc usages
`python manage.py download -r -w -n` # Download Roms, wind and nams
`python manage.py plot -i 23 34 45 -r` # Plot rom df with id 23, 34 and 45
With manage.py plot you can only plot one file type at a time. So if 23, 34
were ROMS datafiles and 45 was a wave file, you'll get an error.
`python manage.py plot-all -r` # Download and plot all of roms. Fresh
plots!
`python manage.py plot-l -d today -r -w` # Plot today's plots
Options for plot-l are `latest`, `all`, `today`.
"""
verbose = 0
def download(roms=False, wave=False,
wind=False, hycom=False,
ncep=False,
num_dl=None):
from pl_download.models import DataFileManager
ids = []
if roms:
roms_ids = []
roms_ids = DataFileManager.download_osu_roms()
print("OSU ROM dl ids:", roms_ids)
ids.append(roms_ids)
if wave:
wave_ids = []
wave_ids = DataFileManager.get_latest_wave_watch_files()
print("OSU WW3 dl ids:", wave_ids)
ids.append(wave_ids)
if wind:
wind_ids = []
wind_ids = DataFileManager.get_wind_file()
print("NAM Wind dl ids:", wind_ids)
ids.append(wind_ids)
if hycom:
hycom_ids = []
hycom_ids = DataFileManager.hycom_download(count=num_dl)
print("HYCOM dl ids:", hycom_ids)
ids.append(hycom_ids)
if ncep:
ncep_ids = []
ncep_ids = DataFileManager.ww3_download()
print("NCEP dl ids:", ncep_ids)
ids.append(ncep_ids)
return ids
def plot(ids,
num_plots=5, tile=False, full_roms=True,
roms=False,
wave=False,
wind=False,
hycom=False,
ncep=False):
''' Just generates plots. You need to pass in the df id to get a plot! Pass it in manually
or by using one of the functions below which grabs them using the database or via downloading!
'''
from pl_plot.models import OverlayManager as om
from pl_chop.tasks import tile_overlay
if roms:
roms = []
if len(ids) > 1:
for id in ids:
for i in range(num_plots):
roms.append(om.make_plot(settings.OSU_ROMS_SST, i, id))
roms.append(om.make_plot(settings.OSU_ROMS_SUR_CUR, i, id))
if full_roms:
roms.append(om.make_plot(settings.OSU_ROMS_SUR_SAL, i, id))
roms.append(om.make_plot(settings.OSU_ROMS_BOT_SAL, i, id))
roms.append(om.make_plot(settings.OSU_ROMS_BOT_TEMP, i, id))
roms.append(om.make_plot(settings.OSU_ROMS_SSH, i, id))
else:
for i in range(num_plots):
roms.append(om.make_plot(settings.OSU_ROMS_SST, i, id))
roms.append(om.make_plot(settings.OSU_ROMS_SUR_CUR, i, id))
if full_roms:
roms.append(om.make_plot(settings.OSU_ROMS_SUR_SAL, i, id))
roms.append(om.make_plot(settings.OSU_ROMS_BOT_SAL, i, id))
roms.append(om.make_plot(settings.OSU_ROMS_BOT_TEMP, i, id))
roms.append(om.make_plot(settings.OSU_ROMS_SSH, i, id))
if tile:
for f in roms:
tile_overlay(f)
return
if wave:
waves = []
if len(ids) > 1:
for id in ids:
for i in range(num_plots):
waves.append(om.make_wave_watch_plot(settings.OSU_WW3_HI, i, id))
waves.append(om.make_wave_watch_plot(settings.OSU_WW3_DIR, i, id))
else:
for i in range(num_plots):
waves.append(om.make_wave_watch_plot(settings.OSU_WW3_HI, i, id))
waves.append(om.make_wave_watch_plot(settings.OSU_WW3_DIR, i, id))
if tile:
for f in waves:
tile_overlay(f)
return
if wind:
winds = []
for id in ids:
for i in range(num_plots):
winds.append(om.make_plot(settings.NAMS_WIND, i, id))
if tile:
for f in winds:
tile_overlay(f)
return
if hycom:
hycoms = []
for id in ids:
for i in range(num_plots):
hycoms.append(om.make_plot(settings.HYCOM_SST, i, id))
hycoms.append(om.make_plot(settings.HYCOM_SUR_CUR, i, id))
if tile:
for f in hycoms:
tile_overlay(f)
return
if ncep:
nceps = []
for id in ids:
for i in range(num_plots):
nceps.append(om.make_wave_watch_plot(settings.NCEP_WW3_DIR, i, id))
nceps.append(om.make_wave_watch_plot(settings.NCEP_WW3_HI, i, id))
if tile:
for f in nceps:
tile_overlay(f)
return
def plot_new(num_plots=5, tile=False, full_roms=True,
roms=False, wave=False, wind=False,
hycom=False, ncep=False):
# num_plots = The number of plots you want for each file - save time!
# Download and plot the newset freshest files from ze interwebz
from pl_plot.models import OverlayManager as om
from pl_chop.tasks import tile_overlay
if roms:
ids = download(roms=True)
roms = []
plot( ids, roms=True, num_plots=num_plots, tile=tile, full_roms=full_roms )
if wave:
ids = download(wave=True)
waves = []
plot( ids, wave=True, num_plots=num_plots, tile=tile )
if wind:
ids = download(wind=True)
winds = []
plot( ids, wind=True, num_plots=num_plots, tile=tile )
if hycom:
ids = download(hycom=True, num_dl=num_plots)
hycoms = []
plot( ids, hycom=True, num_plots=num_plots, tile=tile )
if ncep:
ids = download(ncep=True)
nceps = []
plot( ids, ncep=True, num_plots=num_plots, tile=tile )
def plot_latest(num_plots=5, tile=False, full_roms=True, date='today',
roms=False, wave=False, wind=False,
hycom=False, ncep=False):
# num_plots = The number of plots you want for each file - save time!
# Pull the latest files from the database and plot those
from pl_download.models import DataFile as df
from datetime import datetime
# Today
if date == 'today':
today = datetime.now().date()
elif date == 'all':
pass
elif date == 'latest':
pass
else:
print "Wrong date paramater. Please use - 'today', 'all', or 'latest'"
return
if roms:
roms = []
ids = []
if date == "today":
ids = df.objects.filter(type='NCDF').get(model_date=today).id
elif date == "latest":
ids = df.objects.filter(type='NCDF').latest('model_date').id
elif date == "all":
ids = df.objects.all().filter(type = "NCDF").id
ids = [id.id for id in ids] # Unwrap ids
plot( ids, roms=True, num_plots=num_plots, tile=tile, full_roms=full_roms )
if wave:
waves = []
ids = []
if date == "today":
ids = df.objects.filter(type='WAVE').get(model_date=today).id
elif date == "latest":
ids = df.objects.filter(type='WAVE').latest('model_date').id
elif date == "all":
ids = df.objects.all().filter(type = "WAVE").id
ids = [id.id for id in ids] # Unwrap ids
plot( ids, wave=True, num_plots=num_plots, tile=tile )
if wind:
winds = []
ids = []
if date is "today":
ids = df.objects.filter(type='WIND').get(model_date=today).id
elif date is "latest":
ids = df.objects.filter(type='WIND').latest('model_date').id
elif date is "all":
ids = df.objects.all().filter(type = "WIND").id
ids = [id.id for id in ids] # Unwrap ids
plot( ids, wind=True, num_plots=num_plots, tile=tile )
if hycom:
hycoms = []
ids = []
if date == "today":
ids.append(df.objects.filter(type='HYCOM').get(model_date=today))
elif date == "latest":
ids.append(df.objects.filter(type='HYCOM').latest('model_date'))
elif date == "all":
ids.append(df.objects.all().filter(type = "HYCOM"))
ids = [id.id for id in ids] # Unwrap ids
print ids
plot( ids, hycom=True, num_plots=num_plots, tile=tile )
if ncep:
nceps = []
ids = []
if date is "today":
ids = df.objects.filter(type='NCEP').get(model_date=today).id
elif date is "latest":
ids = df.objects.filter(type='NCEP').latest('model_date').id
elif date is "all":
ids = df.objects.all().filter(type = "NCEP").id
ids = [id.id for id in ids] # Unwrap ids
plot( ids, ncep=True, num_plots=num_plots, tile=tile )
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "SharkEyesCore.settings")
startup.run()
parser = argparse.ArgumentParser(description='Easy way to produce plots of\
seacast fields.')
task = parser.add_argument_group('Task', 'The task you want to preform.')
task.add_argument('task',
help='The manage.py command you want to run. Options are \n' \
"\t 'download' & 'plot'",
type=str)
model = parser.add_argument_group('Models', 'Enable models by using these commands')
model.add_argument("-r", '--roms',
help='Toggle on OSU ROMS in this task call',
action="store_true")
model.add_argument("-w", '--wave',
help='Toggle on OSU WW3 in this task call',
action="store_true")
model.add_argument("-n", '--nams',
help='Toggle on NAMS in this task call',
action="store_true")
model.add_argument("-p", '--hycom',
help='Toggle on HYCOM in this task call',
action="store_true")
model.add_argument("-c", '--ncep',
help='Toggle on NCEP WW3 in this task Call',
action="store_true")
other = parser.add_argument_group('Other')
other.add_argument("-T", '--tile',
help='Toggle on to produce tiles in plot',
action="store_true")
other.add_argument("-k", '--num',
help='Number of plots to generate in plot',
type=int)
other.add_argument("-f", '--fullRoms',
help='Run the full number of roms. Default is True',
type=bool,
default=True)
other.add_argument("-i", '--ids',
help='Toggle on to produce tiles in plot',
default=[],
action="append",
dest='ids')
other.add_argument("-v", '--verbose',
help='Turn on vebrosity level 0 (default), 1, 2, 3, 9000',
default=0,
type=int)
other.add_argument("-d", '--date',
help='today, latest, ',
default="all",
type=str)
args = parser.parse_args()
vebrose = args.verbose
if args.task == "download":
print download(roms=args.roms,
wave=args.wave,
wind=args.nams,
hycom=args.hycom,
ncep=args.ncep,
num_dl=args.num)
sys.exit()
elif args.task == "plot-all":
plot_all(num_plots=args.num,
tile=args.tile,
roms=args.roms,
wave=args.wave,
wind=args.nams,
hycom=args.hycom,
ncep=args.ncep)
sys.exit()
elif args.task == "plot":
plot(num_plots=args.num,
tile=args.tile,
roms=args.roms,
wave=args.wave,
wind=args.nams,
hycom=args.hycom,
ncep=args.ncep)
sys.exit()
elif args.task == "plot-l":
plot_latest(num_plots=args.num,
tile=args.tile,
roms=args.roms,
wave=args.wave,
wind=args.nams,
hycom=args.hycom,
ncep=args.ncep,
date=args.date)
sys.exit()
elif args.task == "test":
pass
sys.exit()
execute_from_command_line(sys.argv)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment