-
Notifications
You must be signed in to change notification settings - Fork 0
/
tasks.py
166 lines (136 loc) · 6 KB
/
tasks.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
from celery import registry
from cera.models import DataArray, ModelRun, Variable
from cera.query import *
from cera import app_settings as settings
from cera.views import WMSAdapter, WMS, metric
from ga_ows.tasks import DeferredRenderer
from celery.task import task
import os
from datetime import timedelta
from tempfile import NamedTemporaryFile
import netCDF4
import cPickle as pickle
from logging import getLogger
import numpy as np
from ga_ows.views import wms
_index = None
log = getLogger(__name__)
# copy netcdf file over OR open via OpenDAP and copy elements over into bare arrays
def get_remote_paths(when):
for runid in settings.VALID_RUN_ID:
run = settings.ADCIRC_MOUNTPOINT + settings.ADCIRC_NETCDF_PATH.format(
yyyymmdd = when.strftime('%Y%m%d'),
runid = runid)
print run
this_run = ModelRun.objects.filter(when=when+timedelta(hours=int(runid))).first()
if not this_run and os.path.exists(run):
ModelRun.objects.create(when=when+timedelta(hours=int(runid)), name=run, version=when.strftime('%Y%m%d') + runid)
for filename in os.listdir(run):
for var in settings.VARIABLES:
if ("max" + var) in filename:
yield runid, run, ("max" + var) , filename
elif var in filename:
yield runid, run, var, filename
# copy a path and gunzip it, because all netcdfs in NCFS are gzipped.
def copy_and_expand(orig, extn):
print ('copying and expanding {filename}'.format(filename=orig))
tf = NamedTemporaryFile(suffix=extn)
os.system('cp "{filename}" {tempfile}'.format(filename=orig, tempfile=tf.name))
return tf
# copy, expand, and provide tempfiles for all the netcdf files for a particular ADCIRC run.
def expand_all(when):
print ("copying and expanding all files for the newest run.")
return [(runid, run, var, copy_and_expand(run + path, '.nc')) for runid, run, var, path in get_remote_paths(when)]
_triangles = None
def read_array(filename, varname, when):
global _triangles
print ('reading {varname} array for {when}00 from {filename} '.format(
varname=varname,
filename=filename,
when=when.strftime('%Y.%m.%d.%H')
))
if _triangles is None:
_triangles = np.load(settings.BATHYMETRY_INDEX_FILE + 'bathymetry.nodes.npy')
try:
ds = netCDF4.Dataset(filename)
for name, var in ds.variables.items():
Variable.objects.get_or_create(basename=varname, name=name, long_name=var.long_name if hasattr(var, 'long_name') else name)
dims = var.dimensions
log.debug('reading {name} from {varname} with {dims}'.format(**locals()))
if var.dimensions == settings.DATASET_DIMENSIONS:
log.debug('loading {name} into database'.format(**locals()))
for time in (int(t) for t in ds.variables['time']):
arr = var[:]
DataArray.objects.create(
basename = varname,
name = name,
long_name = var.long_name if hasattr(var, 'long_name') else name,
time = when, # + timedelta(hours=time),
data = pickle.dumps(arr, protocol = -1),
version = when.strftime('%Y%m%d%H')
)
except Exception as e:
log.error('Failed to load file for {varname} at {when} in file {filename}'.format(varname=varname,when=when,filename=filename))
log.error(e)
def read_arrays(when):
print ('reading arrays for run at {when}'.format(when=when.strftime('%Y.%m.%d.%H')))
for runid, run, var, tempfile in expand_all(when):
read_array(tempfile.name, var, when + timedelta(hours=int(runid)))
#
# Tasks below as opposed to functions
#
@task
class DeferredCERARenderer(DeferredRenderer):
adapter = WMSAdapter(metric)
deferred_cera_renderer = registry.tasks[DeferredCERARenderer.name]
class DeferredWMSView(WMS):
"""Allows you to distribute the rendering across multiple machines for high volumes"""
task = deferred_cera_renderer
@task
def deferred_values_for_triangles(*args, **kwargs):
return list(values_for_triangles(*args, **kwargs))
@task
def deferred_mean_values_for_triangles(*args, **kwargs):
return list(mean_values_for_triangles(*args, **kwargs))
@task
def deferred_bbox_values_for_triangles(*args, **kwargs):
return list(bbox_values_for_triangles(*args, **kwargs))
@task
def deferred_bbox_mean_values_for_triangles(*args, **kwargs):
return list(bbox_mean_values_for_triangles(*args, **kwargs))
@task
def deferred_value_nearest(*args, **kwargs):
return value_nearest(*args, **kwargs)
@task(ignore_result=True)
def append_new_run():
"""
Looks for the newest ADCIRC run on the NFS mount and if it's new, copies it over, appends file to the database
Call graph::
append_new_run -> read_arrays -> read_array(array)
for all array in expand_all
expand_all -> copy_and_expand(file)
for all file in get_remote_paths
"""
print ('appending new ADCIRC run to the database')
read_arrays(today())
# clean out old arrays
@task(ignore_result=True)
def remove_old_data():
when = datetime.now()
when = datetime(when.year, when.month, when.day) - settings.DELETE_ARRAYS_OLDER_THAN
ModelRun.objects.filter(when__lt=when, hold=False).delete()
DataArray.objects.filter(when__lt=when, hold=False).delete()
# clean out caches
@task(ignore_result=True)
def clean_web_cache():
cache = wms.WMSCache('cera')
cache.flush_lru(settings.MAXIMUM_CACHE_SIZE)
@task(ignore_result=True)
def destroy_bathymetry():
global _index
print ("Destroying bathymetry")
if os.path.exists(settings.BATHYMETRY_INDEX_FILE + 'bathymetry.spatialite'):
os.unlink(settings.BATHYMETRY_INDEX_FILE + 'bathymetry.spatialite')
os.unlink(settings.BATHYMETRY_INDEX_FILE + 'bathymetry.coords.npy')
os.unlink(settings.BATHYMETRY_INDEX_FILE + 'bathymetry.nodes.npy')
_index = None