Blame view

web/run.py 53.8 KB
bde97e4d   Goutte   Add more changes ...
1
2
# coding=utf-8

9390ec89   Goutte   Initial experimen...
3
import StringIO
bc18b96c   Goutte   Implement first (...
4
import datetime
8644387c   Goutte   Use real data.
5
import gzip
bc18b96c   Goutte   Implement first (...
6
7
8
import json
import logging
import random
2fedd73b   Goutte   Initial implement...
9
import tarfile
bde97e4d   Goutte   Add more changes ...
10
import time
8644387c   Goutte   Use real data.
11
import urllib
9f57dceb   Goutte   Add half the brid...
12
13
import requests

9390ec89   Goutte   Initial experimen...
14
from csv import writer as csv_writer
596da00d   Goutte   Add more exceptio...
15
from math import sqrt, isnan
bc18b96c   Goutte   Implement first (...
16
17
18
from os import environ, remove as removefile
from os.path import isfile, join, abspath, dirname

fb383448   Goutte   Implement the cac...
19
from dateutil.relativedelta import relativedelta
9390ec89   Goutte   Initial experimen...
20
from flask import Flask
9390ec89   Goutte   Initial experimen...
21
from flask import request
bc18b96c   Goutte   Implement first (...
22
from flask import url_for, send_from_directory, abort as abort_flask
bde97e4d   Goutte   Add more changes ...
23
from jinja2 import Environment, FileSystemLoader, Markup
57493104   Goutte   Add the time to t...
24
from netCDF4 import Dataset, date2num
bc18b96c   Goutte   Implement first (...
25
from yaml import load as yaml_load
9390ec89   Goutte   Initial experimen...
26

dabb9d5f   Goutte   Fix the layers' c...
27

9390ec89   Goutte   Initial experimen...
28
29
30
31
32
33
# PATH RELATIVITY #############################################################

THIS_DIRECTORY = dirname(abspath(__file__))


def get_path(relative_path):
a4a9ef03   Goutte   Cache generated C...
34
    """Get an absolute path from the relative path to this script directory."""
9390ec89   Goutte   Initial experimen...
35
36
37
38
39
40
41
42
43
44
45
46
47
    return abspath(join(THIS_DIRECTORY, relative_path))


# COLLECT GLOBAL INFORMATION FROM SOURCES #####################################

# VERSION
with open(get_path('../VERSION'), 'r') as version_file:
    version = version_file.read().strip()

# CONFIG
with open(get_path('../config.yml'), 'r') as config_file:
    config = yaml_load(config_file.read())

c0df94bc   Goutte   Adding more logs.
48
FILE_DATE_FMT = "%Y-%m-%dT%H:%M:%S"
1185f353   Goutte   Fix the CME Catal...
49
MOMENT_DATE_FMT = "%Y-%m-%dT%H:%M:%SZ"
c0df94bc   Goutte   Adding more logs.
50

9390ec89   Goutte   Initial experimen...
51

f75faf5f   Goutte   WIP
52
53
# LOGGING #####################################################################

1324cc91   Goutte   Make the footer i...
54
55
LOG_FILE = get_path('run.log')

f75faf5f   Goutte   WIP
56
log = logging.getLogger("HelioPropa")
9bfa6c42   Goutte   More bug hunting.
57
log.setLevel(logging.DEBUG)
077980eb   Goutte   Improve availabil...
58
# log.setLevel(logging.ERROR)                        # <-- set log level here !
1324cc91   Goutte   Make the footer i...
59
logHandler = logging.FileHandler(LOG_FILE)
b2837a08   Goutte   Add three retries...
60
61
62
63
logHandler.setFormatter(logging.Formatter(
    "%(asctime)s - %(levelname)s - %(message)s"
))
log.addHandler(logHandler)
f75faf5f   Goutte   WIP
64
65


e18701b6   Goutte   Cache clear (remo...
66
67
# HARDCODED CONFIGURATION #####################################################

a2034dd9   Goutte   Convert from kilo...
68
69
ASTRONOMICAL_UNIT_IN_KM = 1.496e8

952e3d8f   Goutte   Move to another s...
70
71
72
# Absolute path to the installed CDF library from https://cdf.gsfc.nasa.gov/
CDF_LIB = '/usr/local/lib/libcdf'

e18701b6   Goutte   Cache clear (remo...
73
74
75
76
# Absolute path to the data cache directory
CACHE_DIR = get_path('../cache')

# These two configs are not in the YAML config because adding a new parameter
ea45ebf9   Goutte   Add the mocks of ...
77
# will not work as-is, you'd have to edit some netcdf-related code.
e18701b6   Goutte   Cache clear (remo...
78
79
80
81
82
83

# The slugs of the available parameters in the generated CSV files.
# The order matters. If you change this you also need to change the
# innermost loop of `get_data_for_target`.
# The javascript knows the targets' properties under these names.
PROPERTIES = ('time', 'vrad', 'vtan', 'vtot', 'btan', 'temp', 'pdyn', 'dens',
d1c44c51   Goutte   Enable Earth
84
              'atse', 'xhee', 'yhee')
e18701b6   Goutte   Cache clear (remo...
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

# The parameters that the users can handle.
# The slug MUST be one of the properties above.
PARAMETERS = {
    'pdyn': {
        'slug': 'pdyn',
        'name': 'Dyn. Pressure',
        'title': 'The dynamic pressure.',
        'units': 'nPa',
        'active': True,
        'position': 10,
    },
    'vtot': {
        'slug': 'vtot',
        'name': 'Velocity',
        'title': 'The velocity of the particles.',
        'units': 'km/s',
        'active': False,
        'position': 20,
    },
    'btan': {
        'slug': 'btan',
        'name': 'B Tangential',
        'title': 'B Tangential.',
        'units': 'nT',
        'active': False,
        'position': 30,
    },
    'temp': {
        'slug': 'temp',
        'name': 'Temperature',
60b73eb1   Goutte   Change temperatur...
116
117
        'title': 'The temperature.',
        'units': 'eV',
e18701b6   Goutte   Cache clear (remo...
118
119
120
121
122
123
124
        'active': False,
        'position': 40,
    },
    'dens': {
        'slug': 'dens',
        'name': 'Density',
        'title': 'The density N.',
aa7247d6   Goutte   Generate a CDF fi...
125
        'units': 'cm^-3',
e18701b6   Goutte   Cache clear (remo...
126
127
128
        'active': False,
        'position': 50,
    },
d1c44c51   Goutte   Enable Earth
129
130
    'atse': {
        'slug': 'atse',
e18701b6   Goutte   Cache clear (remo...
131
132
133
134
135
136
137
138
139
        'name': 'Angle T-S-E',
        'title': 'Angle Target-Sun-Earth.',
        'units': 'deg',
        'active': False,
        'position': 60,
    },
}


2fe06b17   Goutte   Move the ENV dire...
140
141
142
143
144
145
# ENV #########################################################################

environ['SPACEPY'] = CACHE_DIR
environ['CDF_LIB'] = CDF_LIB


9390ec89   Goutte   Initial experimen...
146
147
148
149
# SETUP FLASK ENGINE ##########################################################

app = Flask(__name__, root_path=THIS_DIRECTORY)
app.debug = environ.get('DEBUG') == 'true'
b2837a08   Goutte   Add three retries...
150
if app.debug:
2fedd73b   Goutte   Initial implement...
151
    log.info("Starting Flask app IN DEBUG MODE...")
b2837a08   Goutte   Add three retries...
152
153
else:
    log.info("Starting Flask app...")
9390ec89   Goutte   Initial experimen...
154
155
156
157
158
159
160
161
162
163
164


# SETUP JINJA2 TEMPLATE ENGINE ################################################

def static_global(filename):
    return url_for('static', filename=filename)


def shuffle_filter(seq):
    """
    This shuffles the sequence it is applied to.
2fedd73b   Goutte   Initial implement...
165
    Jinja2 _should_ provide this.
9390ec89   Goutte   Initial experimen...
166
167
168
169
170
171
172
173
174
175
176
    """
    try:
        result = list(seq)
        random.shuffle(result)
        return result
    except:
        return seq


def markdown_filter(value, nl2br=False, p=True):
    """
2fedd73b   Goutte   Initial implement...
177
    Converts markdown into html.
9390ec89   Goutte   Initial experimen...
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
    nl2br: set to True to replace line breaks with <br> tags
    p: set to False to remove the enclosing <p></p> tags
    """
    from markdown import markdown
    from markdown.extensions.nl2br import Nl2BrExtension
    from markdown.extensions.abbr import AbbrExtension
    extensions = [AbbrExtension()]
    if nl2br is True:
        extensions.append(Nl2BrExtension())
    markdowned = markdown(value, output_format='html5', extensions=extensions)
    if p is False:
        markdowned = markdowned.replace(r"<p>", "").replace(r"</p>", "")
    return markdowned


bde97e4d   Goutte   Add more changes ...
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
_js_escapes = {
        '\\': '\\u005C',
        '\'': '\\u0027',
        '"': '\\u0022',
        '>': '\\u003E',
        '<': '\\u003C',
        '&': '\\u0026',
        '=': '\\u003D',
        '-': '\\u002D',
        ';': '\\u003B',
        u'\u2028': '\\u2028',
        u'\u2029': '\\u2029'
}
# Escape every ASCII character with a value less than 32.
_js_escapes.update(('%c' % z, '\\u%04X' % z) for z in xrange(32))


def escapejs_filter(value):
    escaped = []
    for letter in value:
        if letter in _js_escapes:
            escaped.append(_js_escapes[letter])
        else:
            escaped.append(letter)

    return Markup("".join(escaped))

9390ec89   Goutte   Initial experimen...
220
221
222
223
224
225
226
227
228
229
230
231
tpl_engine = Environment(loader=FileSystemLoader([get_path('view')]),
                         trim_blocks=True,
                         lstrip_blocks=True)

tpl_engine.globals.update(
    url_for=url_for,
    static=static_global,
)

tpl_engine.filters['markdown'] = markdown_filter
tpl_engine.filters['md'] = markdown_filter
tpl_engine.filters['shuffle'] = shuffle_filter
bde97e4d   Goutte   Add more changes ...
232
tpl_engine.filters['escapejs'] = escapejs_filter
9390ec89   Goutte   Initial experimen...
233
234
235
236
237
238
239
240
241
242
243

tpl_global_vars = {
    'request': request,
    'version': version,
    'config': config,
    'now': datetime.datetime.now(),
}


# HELPERS #####################################################################

57f42bd7   Goutte   Log the abortions.
244
def abort(code, message):
b52b494b   Goutte   Add even more logs.
245
    log.error("Abort: " + message)
57f42bd7   Goutte   Log the abortions.
246
247
248
    abort_flask(code, message)


9390ec89   Goutte   Initial experimen...
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
def render_view(view, context=None):
    """
    A simple helper to render [view] template with [context] vars.
    It automatically adds the global template vars defined above, too.
    It returns a string, usually the HTML contents to display.
    """
    context = {} if context is None else context
    return tpl_engine.get_template(view).render(
        dict(tpl_global_vars.items() + context.items())
    )


# def render_page(page, title="My Page", context=None):
#     """
#     A simple helper to render the md_page.html template with [context] vars &
#     the additional contents of `page/[page].md` in the `md_page` variable.
#     It automagically adds the global template vars defined above, too.
#     It returns a string, usually the HTML contents to display.
#     """
#     if context is None:
#         context = {}
#     context['title'] = title
#     context['md_page'] = ''
#     with file(get_path('page/%s.md' % page)) as f:
#         context['md_page'] = f.read()
#     return tpl_engine.get_template('md_page.html').render(
#         dict(tpl_global_vars.items() + context.items())
#     )

077980eb   Goutte   Improve availabil...
278

bc18b96c   Goutte   Implement first (...
279
280
281
282
283
284
285
def is_list_in_list(needle, haystack):
    for n in needle:
        if n not in haystack:
            return False
    return True


1324cc91   Goutte   Make the footer i...
286
287
288
289
290
291
292
293
294
295
296
297
298
def round_time(dt=None, round_to=60):
    """
    Round a datetime object to any time laps in seconds
    dt : datetime.datetime object, default now.
    roundTo : Closest number of seconds to round to, default 1 minute.
    """
    if dt is None:
        dt = datetime.datetime.now()
    seconds = (dt.replace(tzinfo=None) - dt.min).seconds
    rounding = (seconds + round_to / 2) // round_to * round_to
    return dt + datetime.timedelta(0, rounding-seconds, -dt.microsecond)


2d2af24b   Goutte   Add a basic orbit...
299
def datetime_from_list(time_list):
0b9821dd   Goutte   Clean up.
300
    """
2fedd73b   Goutte   Initial implement...
301
    Datetimes in retrieved CDFs are stored as lists of numbers,
80352490   Goutte   Multi model suppo...
302
303
    with DayOfYear starting at 0. We want it starting at 1 because it's what
    vendor parsers use, both in python and javascript.
0b9821dd   Goutte   Clean up.
304
    """
2d2af24b   Goutte   Add a basic orbit...
305
306
307
    # Day Of Year starts at 0, but for our datetime parser it starts at 1
    doy = '{:03d}'.format(int(''.join(time_list[4:7])) + 1)
    return datetime.datetime.strptime(
50d4f638   Goutte   Hotfix for a very...
308
        "%s%s%s" % (''.join(time_list[0:4]), doy, ''.join(time_list[7:-1])),
2d2af24b   Goutte   Add a basic orbit...
309
310
        "%Y%j%H%M%S%f"
    )
9390ec89   Goutte   Initial experimen...
311

ce8af118   Goutte   Fix the favicon.
312

927c69c3   Goutte   Make the local ca...
313
314
315
316
317
318
319
320
321
322
323
324
325
326
def get_local_filename(url):
    """
    Build the local cache filename for the distant file
    :param url: string
    :return: string
    """
    from slugify import slugify
    n = len('http://')
    if url.startswith('https'):
        n += 1
    s = url[n:]
    return slugify(s)


180d7d97   Goutte   Refactor heavily.
327
def get_target_config(slug):
2fedd73b   Goutte   Initial implement...
328
    for s in config['targets']:  # dumb
8644387c   Goutte   Use real data.
329
330
        if s['slug'] == slug:
            return s
180d7d97   Goutte   Refactor heavily.
331
    raise Exception("No target found in configuration for '%s'." % slug)
8644387c   Goutte   Use real data.
332
333


180d7d97   Goutte   Refactor heavily.
334
335
336
337
def check_target_config(slug):
    get_target_config(slug)


fb383448   Goutte   Implement the cac...
338
339
340
341
342
def get_active_targets():
    all_targets = config['targets']
    return [t for t in all_targets if not ('locked' in t and t['locked'])]


9f57dceb   Goutte   Add half the brid...
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
def retrieve_auroral_emissions(target_name):
    """
    Work In Progress.
    :param target_name: You should probably not let users choose this value,
                        as our sanitizing for ADQL may not be 100% safe.
                        Use values from YAML configuration, instead.
                        Below is a list of the ids we found to be existing.
                        > SELECT DISTINCT target_name FROM apis.epn_core
                        - Mars
                        - MERCURY
                        - Jupiter
                        - Titan
                        - Io
                        - VENUS
                        - Ganymede
                        - Uranus
                        - Callisto
                        - Europa
                        - Saturn
    :return: 
    """
    api_url = "http://voparis-tap.obspm.fr/__system__/tap/run/tap/sync"
    d_started_at = datetime.datetime.now()
    t_started_at = time.mktime(d_started_at.timetuple()) - 3600 * 24 * 365 * 10  # fixme
    # t_started_at = 1
    d_stopped_at = datetime.datetime.now()
    t_stopped_at = time.mktime(d_stopped_at.timetuple())

    def to_jday(timestamp):
        return timestamp / 86400.0 + 2440587.5

    query = """
SELECT
  time_min,
  time_max,
  thumbnail_url,
  external_link
FROM apis.epn_core
WHERE target_name='{target_name}'
AND   dataproduct_type='im'
AND   time_min > {jday_start}
AND   time_min < {jday_stop}
ORDER BY time_min, granule_gid
""".format(
        target_name=target_name.replace("'", "\\'"),
        jday_start=to_jday(t_started_at),
        jday_stop=to_jday(t_stopped_at)
    )

#     query = """
# SELECT DISTINCT target_name FROM apis.epn_core
# """

    try:
        response = requests.post(api_url, {
            'REQUEST': 'doQuery',
            'LANG':    'ADQL',
            'QUERY':   query,
            'TIMEOUT': '30',
            'FORMAT':  'VOTable/td'
        })

        response_xml = response.text

        import xml.etree.ElementTree as ET
        root = ET.fromstring(response_xml)
        namespaces = {'vo': 'http://www.ivoa.net/xml/VOTable/v1.3'}
        rows_xpath = "./vo:RESOURCE/vo:TABLE/vo:DATA/vo:TABLEDATA/vo:TR"
        rows = []
        for row in root.findall(rows_xpath, namespaces):
            rows.append({
                'time_min': row[0].text,
                'time_max': row[1].text,
                'thumbnail_url': row[2].text,
                'external_link': row[3].text,
            })

        # print(rows)
        return rows
    except Exception as e:
        print("Failed to retrieve auroral emissions :")
        print(e)

    # print(query)


180d7d97   Goutte   Refactor heavily.
429
def retrieve_amda_netcdf(orbiter, what, started_at, stopped_at):
8644387c   Goutte   Use real data.
430
    """
91c3d52d   Goutte   Add more logs.
431
432
    Handles remote querying AMDA's API for URLs, and then downloading,
    extracting and caching the netCDF files.
8644387c   Goutte   Use real data.
433
434
435
436
437
438
439
440
441
442
443
444
    :param orbiter: key of the source in the YAML config
    :param what: either 'model' or 'orbit', a key in the config of the source
    :param started_at:
    :param stopped_at:
    :return: a list of local file paths to netCDF (.nc) files
    """

    url = config['amda'].format(
        dataSet=what,
        startTime=started_at.isoformat(),
        stopTime=stopped_at.isoformat()
    )
c50cc9d8   Goutte   Continue fixing.
445
    log.info("Fetching remote gzip files list at '%s'." % url)
b2837a08   Goutte   Add three retries...
446
447
    retries = 0
    success = False
92abc15b   Goutte   Mistrust the API ...
448
    errors = []
b2837a08   Goutte   Add three retries...
449
450
451
452
453
454
455
456
    remote_gzip_files = []
    while not success and retries < 3:
        try:
            response = urllib.urlopen(url)
            remote_gzip_files = json.loads(response.read())
            if not remote_gzip_files:
                raise Exception("Failed to fetch data at '%s'." % url)
            if remote_gzip_files == 'NODATASET':
92abc15b   Goutte   Mistrust the API ...
457
458
459
                raise Exception("API says there's no dataset at '%s'." % url)
            if remote_gzip_files == 'ERROR':
                raise Exception("API returned an error at '%s'." % url)
077980eb   Goutte   Improve availabil...
460
            if remote_gzip_files == ['OUTOFTIME']:  # it happens
80352490   Goutte   Multi model suppo...
461
462
                return []
                # raise Exception("API says it's out of time at '%s'." % url)
b2837a08   Goutte   Add three retries...
463
464
465
            success = True
        except Exception as e:
            log.warn("Failed (%d/3) '%s' : %s" % (retries+1, url, e.message))
92abc15b   Goutte   Mistrust the API ...
466
467
            remote_gzip_files = []
            errors.append(e)
b2837a08   Goutte   Add three retries...
468
469
470
        finally:
            retries += 1
    if not remote_gzip_files:
b52b494b   Goutte   Add even more logs.
471
        log.error("Failed to retrieve data from AMDA.")
91c3d52d   Goutte   Add more logs.
472
473
        log.error("Failed to fetch gzip files list for %s at '%s' : %s" %
                   (orbiter, url, errors))
08abc2d4   Goutte   Remove duplicate ...
474
475
476
477
        abort(400, "Failed to fetch gzip files list for %s at '%s' : %s" %
                   (orbiter, url, errors))
    else:
        remote_gzip_files = list(set(remote_gzip_files))
9bfa6c42   Goutte   More bug hunting.
478
479

    log.debug("Fetched remote gzip files list : %s." % str(remote_gzip_files))
8644387c   Goutte   Use real data.
480

8644387c   Goutte   Use real data.
481
482
    local_gzip_files = []
    for remote_gzip_file in remote_gzip_files:
077980eb   Goutte   Improve availabil...
483
484
485
        # hotfixes to remove when fixed upstream @Myriam
        if remote_gzip_file in ['OUTOFTIME', 'ERROR']:
            continue  # sometimes half the response is okay, the other not
8644387c   Goutte   Use real data.
486
        if remote_gzip_file.endswith('/.gz'):
80352490   Goutte   Multi model suppo...
487
            continue  # this is just a plain bug
8644387c   Goutte   Use real data.
488
        remote_gzip_file = remote_gzip_file.replace('cdpp1', 'cdpp', 1)
077980eb   Goutte   Improve availabil...
489
        ################################################
e18701b6   Goutte   Cache clear (remo...
490
        local_gzip_file = join(CACHE_DIR, get_local_filename(remote_gzip_file))
8644387c   Goutte   Use real data.
491
492
        local_gzip_files.append(local_gzip_file)
        if not isfile(local_gzip_file):
9bfa6c42   Goutte   More bug hunting.
493
            log.debug("Retrieving '%s'..." % local_gzip_file)
8644387c   Goutte   Use real data.
494
            urllib.urlretrieve(remote_gzip_file, local_gzip_file)
9bfa6c42   Goutte   More bug hunting.
495
            log.debug("Retrieved '%s'." % local_gzip_file)
dc0be992   Goutte   Support having no...
496
497
        else:
            log.debug("Found '%s' in the cache." % local_gzip_file)
8644387c   Goutte   Use real data.
498
499
500
501

    local_netc_files = []
    for local_gzip_file in local_gzip_files:
        local_netc_file = local_gzip_file[0:-3]
9bfa6c42   Goutte   More bug hunting.
502
        log.debug("Unzipping '%s'..." % local_gzip_file)
3c064b17   Goutte   Ignore failures w...
503
504
        success = True
        try:
5ef50583   Goutte   Clean up.
505
            with gzip.open(local_gzip_file) as f:
3c064b17   Goutte   Ignore failures w...
506
507
508
509
510
                file_content = f.read()
                with open(local_netc_file, 'w+b') as g:
                    g.write(file_content)
        except Exception as e:
            success = False
dc0be992   Goutte   Support having no...
511
512
513
514
515
            log.error("Cannot process gz file '%s' from '%s' : %s" %
                      (local_gzip_file, url, e))
            # Sometimes, the downloaded gz is corrupted, and CRC checks fail.
            # We want to delete the local gz file and try again next time.
            removefile(local_gzip_file)
3c064b17   Goutte   Ignore failures w...
516
        if success:
dc0be992   Goutte   Support having no...
517
            local_netc_files.append(local_netc_file)
3c064b17   Goutte   Ignore failures w...
518
            log.debug("Unzipped '%s'." % local_gzip_file)
8644387c   Goutte   Use real data.
519

ea6c8d5d   Goutte   Add interval cons...
520
    return list(set(local_netc_files))  # remove possible dupes
8644387c   Goutte   Use real data.
521
522


596da00d   Goutte   Add more exceptio...
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
# class DataParser:
#     """
#     Default data parser
#     A wip to try to handle code exeptions sanely.
#     """
#
#     # Override these using the model configuration
#     default_nc_keys = {
#         'hee': 'HEE',
#         'vtot': 'V',
#         'magn': 'B',
#         'temp': 'T',
#         'dens': 'N',
#         'pdyn': 'P_dyn',
#         'atse': 'Delta_angle',
#     }
#
#     def __init__(self, target, model):
#         self.target = target
#         self.model = model
#         pass
#
#     def _read_var(self, nc, _keys, _key, mandatory=False):
#         try:
#             return nc.variables[_keys[_key]]
#         except KeyError:
#             pass
#         if mandatory:
#             raise Exception("No variable '%s' found in NetCDF." % _keys[_key])
#         return [None] * len(nc.variables['Time'])  # slow -- use numpy?
#
#     def parse(self, cdf_handle):
#         nc_keys = self.default_nc_keys.copy()
#
#         times = cdf_handle.variables['Time']  # YYYY DOY HH MM SS .ms
#         data_v = self._read_var(cdf_handle, nc_keys, 'vtot')
#         data_b = self._read_var(cdf_handle, nc_keys, 'magn')
#         data_t = self._read_var(cdf_handle, nc_keys, 'temp')
#         data_n = self._read_var(cdf_handle, nc_keys, 'dens')
#         data_p = self._read_var(cdf_handle, nc_keys, 'pdyn')
#         data_a = self._read_var(cdf_handle, nc_keys, 'atse')
#
#         return zip()


de97d643   Goutte   Fix more bugs.
568
569
def get_data_for_target(target_config, input_slug,
                        started_at, stopped_at):
180d7d97   Goutte   Refactor heavily.
570
571
572
573
    """
    :return: dict whose keys are datetime as str, values tuples of data
    """
    log.debug("Grabbing data for '%s'..." % target_config['slug'])
80352490   Goutte   Multi model suppo...
574

8644387c   Goutte   Use real data.
575
    try:
297a7dfc   Goutte   Add support for i...
576
        models = target_config['models'][input_slug]
077980eb   Goutte   Improve availabil...
577
578
    except Exception as e:
        abort(500, "Invalid model configuration for '%s' : %s"
180d7d97   Goutte   Refactor heavily.
579
580
              % (target_config['slug'], str(e)))
    try:
80352490   Goutte   Multi model suppo...
581
        orbits = target_config['orbit']['models']
d1c44c51   Goutte   Enable Earth
582
583
584
585
    except KeyError as e:
        orbits = []
        # abort(500, "Invalid orbit configuration for '%s' : %s"
        #       % (target_config['slug'], str(e)))
28ef3790   Goutte   Clean up.
586

58bfe281   Goutte   Handle start and ...
587
588
589
590
591
592
593
594
595
596
597
    def _sta_sto(_cnf, _sta, _sto):
        if 'started_at' in _cnf:
            _s0 = datetime.datetime.strptime(_cnf['started_at'], FILE_DATE_FMT)
            _s0 = max(_s0, _sta)
        else:
            _s0 = _sta
        if 'stopped_at' in _cnf:
            _s1 = datetime.datetime.strptime(_cnf['stopped_at'], FILE_DATE_FMT)
            _s1 = min(_s1, _sto)
        else:
            _s1 = _sto
aa7247d6   Goutte   Generate a CDF fi...
598
        return _s0, _s1
80352490   Goutte   Multi model suppo...
599

d1c44c51   Goutte   Enable Earth
600
601
602
603
604
605
606
607
608
    def _read_var(_nc, _keys, _key, mandatory=False):
        try:
            return _nc.variables[_keys[_key]]
        except KeyError:
            pass
        if mandatory:
            raise Exception("No variable '%s' found in NetCDF." % _keys[_key])
        return [None] * len(_nc.variables['Time'])  # slow -- use numpy!

fd1829c3   Goutte   Use the new AMDA API
609
    # Override these using the model configuration in config.yml
d1c44c51   Goutte   Enable Earth
610
611
612
613
614
615
616
617
618
619
    default_nc_keys = {
        'hee': 'HEE',
        'vtot': 'V',
        'magn': 'B',
        'temp': 'T',
        'dens': 'N',
        'pdyn': 'P_dyn',
        'atse': 'Delta_angle',
    }

80352490   Goutte   Multi model suppo...
620
    precision = "%Y-%m-%dT%H"  # model and orbits times are only equal-ish
180d7d97   Goutte   Refactor heavily.
621
    orbit_data = {}  # keys are datetime as str, values arrays of XY
ea6c8d5d   Goutte   Add interval cons...
622
623

    for orbit in orbits:
58bfe281   Goutte   Handle start and ...
624
        s0, s1 = _sta_sto(orbit, started_at, stopped_at)
ea6c8d5d   Goutte   Add interval cons...
625

d1c44c51   Goutte   Enable Earth
626
627
628
629
        nc_keys = default_nc_keys.copy()
        if 'parameters' in orbit:
            nc_keys.update(orbit['parameters'])

ea6c8d5d   Goutte   Add interval cons...
630
631
632
633
634
635
636
637
        orbit_files = retrieve_amda_netcdf(
            target_config['slug'], orbit['slug'], s0, s1
        )
        for orbit_file in orbit_files:
            log.debug("%s: opening orbit NETCDF4 '%s'..." %
                      (target_config['name'], orbit_file))
            cdf_handle = Dataset(orbit_file, "r", format="NETCDF4")
            times = cdf_handle.variables['Time']  # YYYY DOY HH MM SS .ms
d1c44c51   Goutte   Enable Earth
638
            data_hee = _read_var(cdf_handle, nc_keys, 'hee', mandatory=True)
ea6c8d5d   Goutte   Add interval cons...
639
640
641

            log.debug("%s: aggregating data from '%s'..." %
                      (target_config['name'], orbit_file))
d1c44c51   Goutte   Enable Earth
642
            for ltime, datum_hee in zip(times, data_hee):
1324cc91   Goutte   Make the footer i...
643
                try:
d1c44c51   Goutte   Enable Earth
644
645
646
647
                    dtime = datetime_from_list(ltime)
                except Exception:
                    log.error("Failed to parse time from %s." % ltime)
                    raise
ea6c8d5d   Goutte   Add interval cons...
648
                if s0 <= dtime <= s1:
1324cc91   Goutte   Make the footer i...
649
                    dkey = round_time(dtime, 60*60).strftime(precision)
ea6c8d5d   Goutte   Add interval cons...
650
651
                    orbit_data[dkey] = datum_hee
            cdf_handle.close()
180d7d97   Goutte   Refactor heavily.
652

8644387c   Goutte   Use real data.
653
    all_data = {}  # keys are datetime as str, values tuples of data
58bfe281   Goutte   Handle start and ...
654
655
656
657
658
    for model in models:
        s0, s1 = _sta_sto(model, started_at, stopped_at)
        model_files = retrieve_amda_netcdf(
            target_config['slug'], model['slug'], s0, s1
        )
d1c44c51   Goutte   Enable Earth
659
660
661
662
        nc_keys = default_nc_keys.copy()
        if 'parameters' in model:
            nc_keys.update(model['parameters'])

129181a6   Goutte   Add more logs to ...
663
664
665
666
        if len(model_files) == 0:
            log.warn("No model data for '%s' '%s'."
                     % (target_config['slug'], model['slug']))

58bfe281   Goutte   Handle start and ...
667
        for model_file in model_files:
58bfe281   Goutte   Handle start and ...
668
669
670
            log.debug("%s: opening model NETCDF4 '%s'..." %
                      (target_config['name'], model_file))
            cdf_handle = Dataset(model_file, "r", format="NETCDF4")
596da00d   Goutte   Add more exceptio...
671

d1c44c51   Goutte   Enable Earth
672
            # log.debug(cdf_handle.variables.keys())
596da00d   Goutte   Add more exceptio...
673

58bfe281   Goutte   Handle start and ...
674
            times = cdf_handle.variables['Time']  # YYYY DOY HH MM SS .ms
d1c44c51   Goutte   Enable Earth
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
            data_v = _read_var(cdf_handle, nc_keys, 'vtot')
            data_b = _read_var(cdf_handle, nc_keys, 'magn')
            data_t = _read_var(cdf_handle, nc_keys, 'temp')
            data_n = _read_var(cdf_handle, nc_keys, 'dens')
            data_p = _read_var(cdf_handle, nc_keys, 'pdyn')
            data_a = _read_var(cdf_handle, nc_keys, 'atse')

            # Usually:
            # Time, StartTime, StopTime, V, B, N, T, Delta_angle, P_dyn
            # Earth:
            # Time, BartelsNumber, ImfID, SwID, ImfPoints,
            # SwPoints, B_M_av, B_Vec_av, B_Theta_av,
            # B_Phi_av, B, T, N, V, Vlat, Vlon,
            # Alpha, RamP, E, Beta, Ma, Kp, R, DST,
            # AE, Flux, Flag, F10_Index, StartTime, StopTime

58bfe281   Goutte   Handle start and ...
691
692
            log.debug("%s: aggregating data from '%s'..." %
                      (target_config['name'], model_file))
d1c44c51   Goutte   Enable Earth
693
694
695
            for ltime, datum_v, datum_b, datum_t, datum_n, datum_p, datum_a \
                    in zip(times, data_v, data_b, data_t, data_n, data_p, data_a):

58bfe281   Goutte   Handle start and ...
696
                try:
d1c44c51   Goutte   Enable Earth
697
698
699
700
                    dtime = datetime_from_list(ltime)
                except Exception:
                    log.error("Failed to parse time from %s." % ltime)
                    raise
596da00d   Goutte   Add more exceptio...
701

58bfe281   Goutte   Handle start and ...
702
703
                if s0 <= dtime <= s1:
                    dkey = round_time(dtime, 60*60).strftime(precision)
d1c44c51   Goutte   Enable Earth
704

596da00d   Goutte   Add more exceptio...
705
706
707
                    x_hee = None
                    y_hee = None
                    if dkey in orbit_data:
a2034dd9   Goutte   Convert from kilo...
708
709
                        x_hee = orbit_data[dkey][0] / ASTRONOMICAL_UNIT_IN_KM
                        y_hee = orbit_data[dkey][1] / ASTRONOMICAL_UNIT_IN_KM
596da00d   Goutte   Add more exceptio...
710
711

                    # First exception: V may be a vector instead of a scalar
d1c44c51   Goutte   Enable Earth
712
713
714
715
716
717
718
719
720
                    if hasattr(datum_v, '__len__'):
                        vrad = datum_v[0]
                        vtan = datum_v[1]
                        vtot = sqrt(vrad * vrad + vtan * vtan)
                    else:  # eg: Earth
                        vrad = None
                        vtan = None
                        vtot = datum_v

596da00d   Goutte   Add more exceptio...
721
                    # Second exception: Earth is always at (1, 0)
d1c44c51   Goutte   Enable Earth
722
723
724
                    if target_config['slug'] == 'earth':
                        x_hee = 1
                        y_hee = 0
596da00d   Goutte   Add more exceptio...
725
726
727
728
729
730
731
732

                    # Third exception: B is a Vector3 or a Vector2 for Earth
                    if target_config['slug'] == 'earth':
                        if model['slug'] == 'omni_hour_all':  # Vector3
                            datum_b = datum_b[0]
                        # if model['slug'] == 'ace_swepam_real':  # Vector2
                        #     datum_b = datum_b[0]
                        if model['slug'] == 'omni_hour_all':
2c8f64ea   Goutte   Adjust Earth's dy...
733
                            datum_p = datum_n * vtot * vtot * 1.6726e-6
596da00d   Goutte   Add more exceptio...
734
                        if model['slug'] == 'ace_swepam_real':
2c8f64ea   Goutte   Adjust Earth's dy...
735
                            datum_p = datum_n * vtot * vtot * 1.6726e-6
596da00d   Goutte   Add more exceptio...
736
737
738
739
740
                        if vtot is None or isnan(vtot):
                            continue

                    # Keep adding exceptions here until you can't or become mad

56d84302   Goutte   Continue the grea...
741
                    if dkey not in all_data:  # since first model has priority
fd1829c3   Goutte   Use the new AMDA API
742
743
744
745
746
747
748
                        # /!\ MUST be in the same order as PROPERTIES
                        all_data[dkey] = (
                            dtime.strftime("%Y-%m-%dT%H:%M:%S+00:00"),
                            vrad, vtan, vtot,
                            datum_b, datum_t, datum_p, datum_n, datum_a,
                            x_hee, y_hee
                        )
58bfe281   Goutte   Handle start and ...
749
            cdf_handle.close()
8644387c   Goutte   Use real data.
750

180d7d97   Goutte   Refactor heavily.
751
752
753
    return all_data


297a7dfc   Goutte   Add support for i...
754
def generate_csv_contents(target_slug, input_slug, started_at, stopped_at):
180d7d97   Goutte   Refactor heavily.
755
756
757
758
759
760
    target_config = get_target_config(target_slug)
    log.debug("Crunching CSV contents for '%s'..." % target_config['name'])
    si = StringIO.StringIO()
    cw = csv_writer(si)
    cw.writerow(PROPERTIES)

297a7dfc   Goutte   Add support for i...
761
762
763
764
    all_data = get_data_for_target(
        target_config=target_config, input_slug=input_slug,
        started_at=started_at, stopped_at=stopped_at
    )
180d7d97   Goutte   Refactor heavily.
765
766

    log.debug("Writing and sorting CSV for '%s'..." % target_config['slug'])
8644387c   Goutte   Use real data.
767
768
    for dkey in sorted(all_data):
        cw.writerow(all_data[dkey])
2d2af24b   Goutte   Add a basic orbit...
769

180d7d97   Goutte   Refactor heavily.
770
    log.info("Generated CSV contents for '%s'." % target_config['slug'])
2d2af24b   Goutte   Add a basic orbit...
771
772
    return si.getvalue()

8644387c   Goutte   Use real data.
773

de97d643   Goutte   Fix more bugs.
774
775
def generate_csv_file_if_needed(target_slug, input_slug,
                                started_at, stopped_at):
297a7dfc   Goutte   Add support for i...
776
777
778
    filename = "%s_%s_%s_%s.csv" % (target_slug, input_slug,
                                    started_at.strftime(FILE_DATE_FMT),
                                    stopped_at.strftime(FILE_DATE_FMT))
e18701b6   Goutte   Cache clear (remo...
779
    local_csv_file = join(CACHE_DIR, filename)
80352490   Goutte   Multi model suppo...
780
781
782

    generate = True
    if isfile(local_csv_file):
297a7dfc   Goutte   Add support for i...
783
        # It needs to have more than one line to not be empty (headers)
80352490   Goutte   Multi model suppo...
784
785
786
787
788
789
790
791
792
        with open(local_csv_file) as f:
            cnt = 0
            for _ in f:
                cnt += 1
                if cnt > 1:
                    generate = False
                    break

    if generate:
c0df94bc   Goutte   Adding more logs.
793
794
795
        log.info("Generating CSV '%s'..." % local_csv_file)
        try:
            with open(local_csv_file, mode="w+") as f:
de97d643   Goutte   Fix more bugs.
796
797
798
799
                f.write(generate_csv_contents(
                    target_slug=target_slug, input_slug=input_slug,
                    started_at=started_at, stopped_at=stopped_at
                ))
c0df94bc   Goutte   Adding more logs.
800
801
            log.info("Generation of '%s' done." % filename)
        except Exception as e:
d1c44c51   Goutte   Enable Earth
802
803
804
            from sys import exc_info
            from traceback import extract_tb
            exc_type, exc_value, exc_traceback = exc_info()
dc0be992   Goutte   Support having no...
805
            log.error(e)
d1c44c51   Goutte   Enable Earth
806
807
            for trace in extract_tb(exc_traceback):
                log.error(trace)
5ede388f   Goutte   Make sure failed ...
808
            if isfile(local_csv_file):
92abc15b   Goutte   Mistrust the API ...
809
                log.warn("Removing failed CSV '%s'..." % local_csv_file)
5ede388f   Goutte   Make sure failed ...
810
                removefile(local_csv_file)
9bfa6c42   Goutte   More bug hunting.
811
            abort(500, "Failed creating CSV '%s' : %s" % (filename, e))
c0df94bc   Goutte   Adding more logs.
812
813


e18701b6   Goutte   Cache clear (remo...
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
def remove_all_files(in_directory):
    """
    Will throw if something horrible happens.
    Does not remove recursively (could be done with os.walk if needed).
    Does not remove directories either.
    :param in_directory: absolute path to directory
    :return:
    """
    import os

    if not os.path.isdir(in_directory):
        raise ValueError("No directory to clean at '%s'.")

    removed_files = []
    for file_name in os.listdir(in_directory):
        file_path = os.path.join(in_directory, file_name)
        if os.path.isfile(file_path):
            os.remove(file_path)
            removed_files.append(file_path)

    return removed_files


28bb4b28   Goutte   API for the cache...
837
838
def remove_files_created_before(date, in_directory):
    """
077980eb   Goutte   Improve availabil...
839
840
841
    Will throw if something horrible happens.
    Does not remove recursively (could be done with os.walk if needed).
    Does not remove directories either.
28bb4b28   Goutte   API for the cache...
842
    :param date: datetime object
077980eb   Goutte   Improve availabil...
843
    :param in_directory: absolute path to directory
28bb4b28   Goutte   API for the cache...
844
845
846
847
848
849
850
    :return:
    """
    import os
    import time

    secs = time.mktime(date.timetuple())

077980eb   Goutte   Improve availabil...
851
852
    if not os.path.isdir(in_directory):
        raise ValueError("No directory to clean at '%s'.")
28bb4b28   Goutte   API for the cache...
853
854
855
856

    removed_files = []
    for file_name in os.listdir(in_directory):
        file_path = os.path.join(in_directory, file_name)
077980eb   Goutte   Improve availabil...
857
858
859
860
861
        if os.path.isfile(file_path):
            t = os.stat(file_path)
            if t.st_ctime < secs:
                os.remove(file_path)
                removed_files.append(file_path)
28bb4b28   Goutte   API for the cache...
862
863
864
865

    return removed_files


297a7dfc   Goutte   Add support for i...
866
867
868
869
870
def get_input_slug_from_query(inp=None):
    if inp is None:
        input_slug = request.args.get('input_slug', 'l1')
    else:
        input_slug = inp
de97d643   Goutte   Fix more bugs.
871
    if input_slug not in [i['slug'] for i in config['inputs']]:
297a7dfc   Goutte   Add support for i...
872
873
874
875
        input_slug = 'l1'  # be tolerant instead of yelling loudly
    return input_slug


284f4688   Goutte   Continue layers i...
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
def get_interval_from_query():
    """
    Get the interval from the query, or from defaults.
    """
    before = relativedelta(months=2)
    after = relativedelta(months=1)
    today = datetime.datetime.now().replace(hour=0, minute=0, second=0)
    started_at = today - before
    stopped_at = today + after
    default_started_at = started_at.strftime(FILE_DATE_FMT)
    default_stopped_at = stopped_at.strftime(FILE_DATE_FMT)

    started_at = request.args.get('started_at', default_started_at)
    stopped_at = request.args.get('stopped_at', default_stopped_at)

    return started_at, stopped_at


1185f353   Goutte   Fix the CME Catal...
894
def get_catalog_layers(input_slug, target_slug, started_at, stopped_at):
0332f168   Goutte   Initial support f...
895
    """
284f4688   Goutte   Continue layers i...
896
897
    In the JSON file we have "columns" and "data".
    Of course, each JSON file has its own columns, with different conventions.
0332f168   Goutte   Initial support f...
898
899
900
901
902
    
    :param input_slug: 
    :param target_slug: 
    :return: 
    """
0332f168   Goutte   Initial support f...
903
    import json
2204c3f7   Goutte   Improve layers co...
904
905

    def _get_index_of_key(_data, _key):
2204c3f7   Goutte   Improve layers co...
906
907
908
909
910
911
912
        try:
            index = _data['columns'].index(_key)
        except ValueError:
            log.error("Key %s not found in columns of %s" % (_key, _data))
            raise
        return index

1185f353   Goutte   Fix the CME Catal...
913
914
915
916
917
918
919
920
921
    try:
        started_at = datetime.datetime.strptime(started_at, FILE_DATE_FMT)
    except:
        abort(400, "Invalid started_at parameter : '%s'." % started_at)
    try:
        stopped_at = datetime.datetime.strptime(stopped_at, FILE_DATE_FMT)
    except:
        abort(400, "Invalid stopped_at parameter : '%s'." % stopped_at)

0332f168   Goutte   Initial support f...
922
923
924
925
    catalog_layers = {}
    for config_layer in config['layers']:
        if 'data' not in config_layer:
            continue
0332f168   Goutte   Initial support f...
926
927
928
929
930
931
        catalog_layers[config_layer['slug']] = []
        for cl_datum in config_layer['data']:
            if input_slug not in cl_datum:
                continue
            if target_slug not in cl_datum[input_slug]:
                continue
b03d5eb1   Goutte   Add the last CME ...
932
933
934
935
936
            if cl_datum[input_slug][target_slug] is None:
                # We used ~ in the config, there are no constraints
                constraints = []
            else:
                constraints = cl_datum[input_slug][target_slug]['constraints']
1185f353   Goutte   Fix the CME Catal...
937

0332f168   Goutte   Initial support f...
938
939
            with open(get_path("../data/catalog/%s" % cl_datum['file'])) as f:
                json_data = json.load(f)
284f4688   Goutte   Continue layers i...
940
941
942
                if 'start' not in cl_datum:
                    log.error("Invalid configuration: 'start' is missing.")
                    continue  # skip this
1185f353   Goutte   Fix the CME Catal...
943
944
945
                if 'format' not in cl_datum:
                    log.error("Invalid configuration: 'format' is missing.")
                    continue  # skip this
284f4688   Goutte   Continue layers i...
946
947
948
949
950
                start_index = _get_index_of_key(json_data, cl_datum['start'])
                if 'stop' not in cl_datum:
                    stop_index = start_index
                else:
                    stop_index = _get_index_of_key(json_data, cl_datum['stop'])
2204c3f7   Goutte   Improve layers co...
951

0332f168   Goutte   Initial support f...
952
                for json_datum in json_data['data']:
2204c3f7   Goutte   Improve layers co...
953
                    validates_any_constraint = False
b03d5eb1   Goutte   Add the last CME ...
954
955
                    if 0 == len(constraints):
                        validates_any_constraint = True
2204c3f7   Goutte   Improve layers co...
956
957
958
959
960
961
962
963
964
965
966
967
968
                    for constraint in constraints:
                        validates_constraint = True
                        for key, possible_values in constraint.iteritems():
                            actual_value = json_datum[_get_index_of_key(
                                json_data, key
                            )]
                            if actual_value not in possible_values:
                                validates_constraint = False
                                break
                        if validates_constraint:
                            validates_any_constraint = True
                            break
                    if not validates_any_constraint:
0332f168   Goutte   Initial support f...
969
                        continue
284f4688   Goutte   Continue layers i...
970
971
                    start_time = json_datum[start_index]
                    stop_time = json_datum[stop_index]
1185f353   Goutte   Fix the CME Catal...
972
973
974
975
976
977
978
979
980
981
982

                    start_time = datetime.datetime.strptime(
                        start_time, cl_datum['format']
                    )
                    stop_time = datetime.datetime.strptime(
                        stop_time, cl_datum['format']
                    )

                    if start_time < started_at:
                        continue

0332f168   Goutte   Initial support f...
983
                    catalog_layers[config_layer['slug']].append({
1185f353   Goutte   Fix the CME Catal...
984
985
                        'start': start_time.strftime(MOMENT_DATE_FMT),
                        'stop':  stop_time.strftime(MOMENT_DATE_FMT),
0332f168   Goutte   Initial support f...
986
                    })
0332f168   Goutte   Initial support f...
987
988
989
990

    return catalog_layers


077980eb   Goutte   Improve availabil...
991
992
993
994
995
996
997
998
999
1000
1001
def get_hit_counter():
    hit_count_path = get_path("../VISITS")

    if isfile(hit_count_path):
        hit_count = int(open(hit_count_path).read())
    else:
        hit_count = 1

    return hit_count


a4a9ef03   Goutte   Cache generated C...
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
def increment_hit_counter():
    hit_count_path = get_path("../VISITS")

    if isfile(hit_count_path):
        hit_count = int(open(hit_count_path).read())
        hit_count += 1
    else:
        hit_count = 1

    hit_counter_file = open(hit_count_path, 'w')
    hit_counter_file.write(str(hit_count))
    hit_counter_file.close()

    return hit_count


4aaf6874   Goutte   Try fixing the ge...
1018
1019
1020
1021
def update_spacepy():
    """
    Importing pydcf will fail if the toolbox is not up to date.
    """
4aaf6874   Goutte   Try fixing the ge...
1022
1023
1024
1025
1026
1027
1028
1029
1030
    try:
        log.info("Updating spacepy's toolbox…")
        import spacepy.toolbox

        spacepy.toolbox.update()
    except Exception as e:
        log.error("Failed to update spacepy : %s." % e)


077980eb   Goutte   Improve availabil...
1031
1032
1033
tpl_global_vars['visits'] = get_hit_counter()


a4a9ef03   Goutte   Cache generated C...
1034
1035
1036
# ROUTING #####################################################################

@app.route('/favicon.ico')
bde97e4d   Goutte   Add more changes ...
1037
def favicon():  # we want it served from the root, not from static/
a4a9ef03   Goutte   Cache generated C...
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
    return send_from_directory(
        join(app.root_path, 'static', 'img'),
        'favicon.ico', mimetype='image/vnd.microsoft.icon'
    )


@app.route("/")
@app.route("/home.html")
@app.route("/index.html")
def home():
077980eb   Goutte   Improve availabil...
1048
    increment_hit_counter()
bde97e4d   Goutte   Add more changes ...
1049
1050
    parameters = PARAMETERS.values()
    parameters.sort(key=lambda x: x['position'])
297a7dfc   Goutte   Add support for i...
1051
    input_slug = get_input_slug_from_query()
0332f168   Goutte   Initial support f...
1052
    targets = [t for t in config['targets'] if not t['locked']]
284f4688   Goutte   Continue layers i...
1053
    started_at, stopped_at = get_interval_from_query()
0332f168   Goutte   Initial support f...
1054
1055
    for i, target in enumerate(targets):
        targets[i]['catalog_layers'] = get_catalog_layers(
1185f353   Goutte   Fix the CME Catal...
1056
            input_slug, target['slug'], started_at, stopped_at
0332f168   Goutte   Initial support f...
1057
        )
a4a9ef03   Goutte   Cache generated C...
1058
    return render_view('home.html.jinja2', {
0332f168   Goutte   Initial support f...
1059
1060
        # 'targets': config['targets'],
        'targets': targets,
bde97e4d   Goutte   Add more changes ...
1061
        'parameters': parameters,
297a7dfc   Goutte   Add support for i...
1062
        'input_slug': input_slug,
284f4688   Goutte   Continue layers i...
1063
1064
        'started_at': started_at,
        'stopped_at': stopped_at,
a4a9ef03   Goutte   Cache generated C...
1065
1066
1067
        'planets': [s for s in config['targets'] if s['type'] == 'planet'],
        'probes':  [s for s in config['targets'] if s['type'] == 'probe'],
        'comets':  [s for s in config['targets'] if s['type'] == 'comet'],
077980eb   Goutte   Improve availabil...
1068
        'visits':  get_hit_counter(),
a4a9ef03   Goutte   Cache generated C...
1069
1070
1071
    })


64a671cd   Goutte   Add an About page.
1072
1073
@app.route("/about.html")
def about():
36961d6f   Goutte   Slightly improve ...
1074
    import uuid
64a671cd   Goutte   Add an About page.
1075
1076
    increment_hit_counter()
    return render_view('about.html.jinja2', {
36961d6f   Goutte   Slightly improve ...
1077
1078
1079
        'authors_emails': [a['mail'] for a in config['authors']],
        'uuid4': str(uuid.uuid4())[0:3],
        'visits': get_hit_counter(),
64a671cd   Goutte   Add an About page.
1080
1081
1082
    })


297a7dfc   Goutte   Add support for i...
1083
1084
@app.route("/<target>_<inp>_<started_at>_<stopped_at>.csv")
def download_target_csv(target, inp, started_at, stopped_at):
a4a9ef03   Goutte   Cache generated C...
1085
1086
1087
1088
    """
    Grab data and orbit data for the specified `target`,
    rearrange it and return it as a CSV file.
    `started_at` and `stopped_at` should be UTC.
297a7dfc   Goutte   Add support for i...
1089
    `inp` is the input slug, l1 or sa or sb.
a4a9ef03   Goutte   Cache generated C...
1090
    """
180d7d97   Goutte   Refactor heavily.
1091
    check_target_config(target)
a4a9ef03   Goutte   Cache generated C...
1092
    try:
c0df94bc   Goutte   Adding more logs.
1093
        started_at = datetime.datetime.strptime(started_at, FILE_DATE_FMT)
a4a9ef03   Goutte   Cache generated C...
1094
1095
1096
    except:
        abort(400, "Invalid started_at parameter : '%s'." % started_at)
    try:
c0df94bc   Goutte   Adding more logs.
1097
        stopped_at = datetime.datetime.strptime(stopped_at, FILE_DATE_FMT)
a4a9ef03   Goutte   Cache generated C...
1098
1099
    except:
        abort(400, "Invalid stopped_at parameter : '%s'." % stopped_at)
297a7dfc   Goutte   Add support for i...
1100
    input_slug = get_input_slug_from_query(inp=inp)
a4a9ef03   Goutte   Cache generated C...
1101

297a7dfc   Goutte   Add support for i...
1102
1103
1104
    filename = "%s_%s_%s_%s.csv" % (target, input_slug,
                                    started_at.strftime(FILE_DATE_FMT),
                                    stopped_at.strftime(FILE_DATE_FMT))
e18701b6   Goutte   Cache clear (remo...
1105
    local_csv_file = join(CACHE_DIR, filename)
297a7dfc   Goutte   Add support for i...
1106
1107
1108
1109
    generate_csv_file_if_needed(
        target_slug=target, input_slug=input_slug,
        started_at=started_at, stopped_at=stopped_at
    )
a4a9ef03   Goutte   Cache generated C...
1110
1111
1112
    if not isfile(local_csv_file):
        abort(500, "Could not cache CSV file at '%s'." % local_csv_file)

e18701b6   Goutte   Cache clear (remo...
1113
    return send_from_directory(CACHE_DIR, filename)
a4a9ef03   Goutte   Cache generated C...
1114
1115


297a7dfc   Goutte   Add support for i...
1116
1117
@app.route("/<targets>_<inp>_<started_at>_<stopped_at>.tar.gz")
def download_targets_tarball(targets, inp, started_at, stopped_at):
b2837a08   Goutte   Add three retries...
1118
    """
bc18b96c   Goutte   Implement first (...
1119
1120
1121
    Grab data and orbit data for each of the specified `targets`,
    in their own CSV file, and make a tarball of them.
    `started_at` and `stopped_at` should be UTC strings.
b2837a08   Goutte   Add three retries...
1122

ea6c8d5d   Goutte   Add interval cons...
1123
1124
    Note: we do not use this route anymore, but let's keep it shelved for now.

2fedd73b   Goutte   Initial implement...
1125
    targets: string list of targets' slugs, separated by `-`.
b2837a08   Goutte   Add three retries...
1126
    """
2fedd73b   Goutte   Initial implement...
1127
    separator = '-'
0511eed7   Goutte   Tarball generatio...
1128
1129
    targets = targets.split(separator)
    targets.sort()
2fedd73b   Goutte   Initial implement...
1130
1131
    targets_configs = []
    for target in targets:
b2837a08   Goutte   Add three retries...
1132
1133
        if not target:
            abort(400, "Invalid targets format : `%s`." % targets)
180d7d97   Goutte   Refactor heavily.
1134
        targets_configs.append(get_target_config(target))
2fedd73b   Goutte   Initial implement...
1135
    if 0 == len(targets_configs):
b2837a08   Goutte   Add three retries...
1136
1137
        abort(400, "No valid targets specified. What are you doing?")

b2837a08   Goutte   Add three retries...
1138
    try:
ea6c8d5d   Goutte   Add interval cons...
1139
        started_at = datetime.datetime.strptime(started_at, FILE_DATE_FMT)
b2837a08   Goutte   Add three retries...
1140
1141
1142
    except:
        abort(400, "Invalid started_at parameter : '%s'." % started_at)
    try:
ea6c8d5d   Goutte   Add interval cons...
1143
        stopped_at = datetime.datetime.strptime(stopped_at, FILE_DATE_FMT)
b2837a08   Goutte   Add three retries...
1144
1145
    except:
        abort(400, "Invalid stopped_at parameter : '%s'." % stopped_at)
ea6c8d5d   Goutte   Add interval cons...
1146
1147
    sta = started_at.strftime(FILE_DATE_FMT)
    sto = stopped_at.strftime(FILE_DATE_FMT)
b2837a08   Goutte   Add three retries...
1148

297a7dfc   Goutte   Add support for i...
1149
1150
1151
1152
1153
    input_slug = get_input_slug_from_query(inp=inp)

    gzip_filename = "%s_%s_%s_%s.tar.gz" % (
        separator.join(targets), input_slug, sta, sto
    )
e18701b6   Goutte   Cache clear (remo...
1154
    local_gzip_file = join(CACHE_DIR, gzip_filename)
2fedd73b   Goutte   Initial implement...
1155
1156

    if not isfile(local_gzip_file):
0511eed7   Goutte   Tarball generatio...
1157
        log.debug("Creating the CSV files for the tarball...")
2fedd73b   Goutte   Initial implement...
1158
        for target_config in targets_configs:
297a7dfc   Goutte   Add support for i...
1159
1160
1161
            filename = "%s_%s_%s_%s.csv" % (
                target_config['slug'], input_slug, sta, sto
            )
e18701b6   Goutte   Cache clear (remo...
1162
            local_csv_file = join(CACHE_DIR, filename)
2fedd73b   Goutte   Initial implement...
1163
1164
            if not isfile(local_csv_file):
                with open(local_csv_file, mode="w+") as f:
297a7dfc   Goutte   Add support for i...
1165
1166
1167
1168
1169
1170
                    f.write(generate_csv_contents(
                        target_slug=target_config['slug'],
                        started_at=started_at,
                        stopped_at=stopped_at,
                        input_slug=input_slug
                    ))
2fedd73b   Goutte   Initial implement...
1171

0511eed7   Goutte   Tarball generatio...
1172
        log.debug("Creating the tarball '%s'..." % local_gzip_file)
2fedd73b   Goutte   Initial implement...
1173
1174
        with tarfile.open(local_gzip_file, "w:gz") as tar:
            for target_config in targets_configs:
297a7dfc   Goutte   Add support for i...
1175
1176
1177
                filename = "%s_%s_%s_%s.csv" % (
                    target_config['slug'], input_slug, sta, sto
                )
e18701b6   Goutte   Cache clear (remo...
1178
                local_csv_file = join(CACHE_DIR, filename)
2fedd73b   Goutte   Initial implement...
1179
1180
1181
                tar.add(local_csv_file, arcname=filename)

    if not isfile(local_gzip_file):
0511eed7   Goutte   Tarball generatio...
1182
        abort(500, "No tarball to serve. Looked at '%s'." % local_gzip_file)
2fedd73b   Goutte   Initial implement...
1183

e18701b6   Goutte   Cache clear (remo...
1184
    return send_from_directory(CACHE_DIR, gzip_filename)
b2837a08   Goutte   Add three retries...
1185

28bb4b28   Goutte   API for the cache...
1186

297a7dfc   Goutte   Add support for i...
1187
1188
@app.route("/<targets>_<inp>_<params>_<started_at>_<stopped_at>.nc")
def download_targets_netcdf(targets, inp, params, started_at, stopped_at):
bc18b96c   Goutte   Implement first (...
1189
    """
4aaf6874   Goutte   Try fixing the ge...
1190
1191
    NOTE : This is not used anymore.
    
bc18b96c   Goutte   Implement first (...
1192
    Grab data and orbit data for the specified `target`,
aa7247d6   Goutte   Generate a CDF fi...
1193
    rearrange it and return it as a NetCDF file.
e18701b6   Goutte   Cache clear (remo...
1194
    `started_at` and `stopped_at` are expected to be UTC.
bc18b96c   Goutte   Implement first (...
1195
1196
1197
1198

    targets: string list of targets' slugs, separated by `-`.
    params: string list of targets' parameters, separated by `-`.
    """
e18701b6   Goutte   Cache clear (remo...
1199
    separator = '-'  # /!\ this char should never be in target's slugs
bc18b96c   Goutte   Implement first (...
1200
1201
1202
1203
1204
1205
1206
1207
1208
    targets = targets.split(separator)
    targets.sort()
    targets_configs = []
    for target in targets:
        if not target:
            abort(400, "Invalid targets format : `%s`." % targets)
        targets_configs.append(get_target_config(target))
    if 0 == len(targets_configs):
        abort(400, "No valid targets specified. What are you doing?")
4aaf6874   Goutte   Try fixing the ge...
1209

bc18b96c   Goutte   Implement first (...
1210
1211
1212
1213
1214
1215
1216
    params = params.split(separator)
    params.sort()
    if 0 == len(params):
        abort(400, "No valid parameters specified. What are you doing?")
    if not is_list_in_list(params, PARAMETERS.keys()):
        abort(400, "Some parameters are not recognized in '%s'." % str(params))

57493104   Goutte   Add the time to t...
1217
    date_fmt = FILE_DATE_FMT
bc18b96c   Goutte   Implement first (...
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
    try:
        started_at = datetime.datetime.strptime(started_at, date_fmt)
    except:
        abort(400, "Invalid started_at parameter : '%s'." % started_at)
    try:
        stopped_at = datetime.datetime.strptime(stopped_at, date_fmt)
    except:
        abort(400, "Invalid stopped_at parameter : '%s'." % stopped_at)
    sta = started_at.strftime(date_fmt)
    sto = stopped_at.strftime(date_fmt)

297a7dfc   Goutte   Add support for i...
1229
1230
1231
1232
1233
    input_slug = get_input_slug_from_query(inp=inp)

    nc_filename = "%s_%s_%s_%s_%s.nc" % (
        separator.join(targets), separator.join(params), input_slug, sta, sto
    )
e18701b6   Goutte   Cache clear (remo...
1234
    nc_path = join(CACHE_DIR, nc_filename)
bc18b96c   Goutte   Implement first (...
1235
1236
1237
1238
1239

    if not isfile(nc_path):
        log.debug("Creating the NetCDF file '%s'..." % nc_filename)
        nc_handle = Dataset(nc_path, "w", format="NETCDF4")
        try:
ea6c8d5d   Goutte   Add interval cons...
1240
            nc_handle.description = "Model and orbit data for targets"  # todo
bc18b96c   Goutte   Implement first (...
1241
            nc_handle.history = "Created " + time.ctime(time.time())
ea6c8d5d   Goutte   Add interval cons...
1242
            nc_handle.source = "Heliopropa (CDDP)"
bc18b96c   Goutte   Implement first (...
1243
1244
1245
1246
1247
            available_params = list(PROPERTIES)
            for target in targets_configs:
                target_slug = target['slug']
                log.debug("Adding group '%s' to the NetCDF..." % target_slug)
                nc_group = nc_handle.createGroup(target_slug)
297a7dfc   Goutte   Add support for i...
1248
1249
1250
1251
                data = get_data_for_target(
                    target_config=target, input_slug=input_slug,
                    started_at=started_at, stopped_at=stopped_at
                )
bc18b96c   Goutte   Implement first (...
1252
                dkeys = sorted(data)
ceeb2f4a   Goutte   Add the target co...
1253
1254
                dimension = 'dim_'+target_slug
                nc_handle.createDimension(dimension, len(dkeys))
57493104   Goutte   Add the time to t...
1255
1256

                # TIME #
ceeb2f4a   Goutte   Add the target co...
1257
                nc_time = nc_group.createVariable('time', 'i8', (dimension,))
57493104   Goutte   Add the time to t...
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
                nc_time.units = "hours since 1970-01-01 00:00:00"
                nc_time.calendar = "standard"
                times = []
                for dkey in dkeys:
                    time_as_string = data[dkey][0][:-6]  # remove +00:00 tail
                    date = datetime.datetime.strptime(time_as_string, date_fmt)
                    times.append(date2num(
                        date, units=nc_time.units, calendar=nc_time.calendar
                    ))
                nc_time[:] = times

                # SELECTED PARAMETERS #
bc18b96c   Goutte   Implement first (...
1270
1271
1272
1273
                nc_vars = []
                indices = []
                for param in params:
                    indices.append(available_params.index(param))
ceeb2f4a   Goutte   Add the target co...
1274
                    nc_var = nc_group.createVariable(param, 'f8', (dimension,))
5a6d4498   Goutte   Add a title to ea...
1275
                    nc_var.units = PARAMETERS[param]['units']
bc18b96c   Goutte   Implement first (...
1276
1277
1278
1279
1280
1281
1282
1283
                    nc_vars.append(nc_var)
                for i, nc_var in enumerate(nc_vars):
                    index = indices[i]
                    values = []
                    for dkey in dkeys:
                        dval = data[dkey]
                        values.append(dval[index])
                    nc_var[:] = values
ceeb2f4a   Goutte   Add the target co...
1284
1285

                # ORBIT #
6491a1f1   Goutte   Fix up the bugs l...
1286
                nc_x = nc_group.createVariable('xhee', 'f8', (dimension,))
ceeb2f4a   Goutte   Add the target co...
1287
                nc_x.units = 'Au'
6491a1f1   Goutte   Fix up the bugs l...
1288
                nc_y = nc_group.createVariable('yhee', 'f8', (dimension,))
ceeb2f4a   Goutte   Add the target co...
1289
1290
1291
                nc_y.units = 'Au'
                values_x = []
                values_y = []
6491a1f1   Goutte   Fix up the bugs l...
1292
1293
                index_x = available_params.index('xhee')
                index_y = available_params.index('yhee')
ceeb2f4a   Goutte   Add the target co...
1294
1295
1296
1297
1298
1299
1300
1301
                for dkey in dkeys:
                    dval = data[dkey]
                    values_x.append(dval[index_x])
                    values_y.append(dval[index_y])
                nc_x[:] = values_x
                nc_y[:] = values_y
            log.debug("Writing NetCDF '%s'..." % nc_filename)

d1c44c51   Goutte   Enable Earth
1302
        except Exception:
57493104   Goutte   Add the time to t...
1303
            log.error("Failed to generate NetCDF '%s'." % nc_filename)
d1c44c51   Goutte   Enable Earth
1304
            raise
bc18b96c   Goutte   Implement first (...
1305
1306
1307
1308
1309
1310
        finally:
            nc_handle.close()

    if not isfile(nc_path):
        abort(500, "No NetCDF to serve. Looked at '%s'." % nc_path)

e18701b6   Goutte   Cache clear (remo...
1311
    return send_from_directory(CACHE_DIR, nc_filename)
bc18b96c   Goutte   Implement first (...
1312
1313


297a7dfc   Goutte   Add support for i...
1314
1315
@app.route("/<targets>_<inp>_<started_at>_<stopped_at>.cdf")
def download_targets_cdf(targets, inp, started_at, stopped_at):
aa7247d6   Goutte   Generate a CDF fi...
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
    """
    Grab data and orbit data for the specified `target`,
    rearrange it and return it as a CDF file.
    `started_at` and `stopped_at` are expected to be UTC.

    targets: string list of targets' slugs, separated by `-`.
    params: string list of targets' parameters, separated by `-`.
    """
    separator = '-'  # /!\ this char should never be in target's slugs
    targets = targets.split(separator)
    targets.sort()
    targets_configs = []
    for target in targets:
        if not target:
            abort(400, "Invalid targets format : `%s`." % targets)
        targets_configs.append(get_target_config(target))
    if 0 == len(targets_configs):
        abort(400, "No valid targets specified. What are you doing?")

    params = PARAMETERS.keys()
aa7247d6   Goutte   Generate a CDF fi...
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347

    try:
        started_at = datetime.datetime.strptime(started_at, FILE_DATE_FMT)
    except:
        abort(400, "Invalid started_at parameter : '%s'." % started_at)
    try:
        stopped_at = datetime.datetime.strptime(stopped_at, FILE_DATE_FMT)
    except:
        abort(400, "Invalid stopped_at parameter : '%s'." % stopped_at)
    sta = started_at.strftime(FILE_DATE_FMT)
    sto = stopped_at.strftime(FILE_DATE_FMT)

297a7dfc   Goutte   Add support for i...
1348
1349
1350
1351
1352
    input_slug = get_input_slug_from_query(inp=inp)

    cdf_filename = "%s_%s_%s_%s.cdf" % (
        separator.join(targets), input_slug, sta, sto
    )
aa7247d6   Goutte   Generate a CDF fi...
1353
1354
1355
1356
    cdf_path = join(CACHE_DIR, cdf_filename)

    if not isfile(cdf_path):
        log.debug("Creating the CDF file '%s'..." % cdf_filename)
604616e4   Goutte   Misc changes from...
1357
        try:
8a48d5fa   Goutte   Make sure spacepy...
1358
            from spacepy import pycdf
4aaf6874   Goutte   Try fixing the ge...
1359
1360
1361
1362
1363
1364
1365
1366
1367
        except ImportError:
            # If spacepy's toolbox is not up-to-date, importing will fail.
            # So, let's update and try again !
            update_spacepy()
            try:
                from spacepy import pycdf
            except ImportError as e:
                log.error("Failed to import pycdf from spacepy : %s" % e)
                raise
2fe06b17   Goutte   Move the ENV dire...
1368
1369
1370
        except Exception as e:
            log.error("Failed to import pycdf from spacepy : %s" % e)
            raise
4aaf6874   Goutte   Try fixing the ge...
1371

8a48d5fa   Goutte   Make sure spacepy...
1372
        try:
952e3d8f   Goutte   Move to another s...
1373
            cdf_handle = pycdf.CDF(cdf_path, masterpath='')
aa7247d6   Goutte   Generate a CDF fi...
1374
1375
            description = "Model and orbit data for %s." % \
                ', '.join([t['name'] for t in targets_configs])
952e3d8f   Goutte   Move to another s...
1376
1377
1378
            cdf_handle.attrs['Description'] = description
            cdf_handle.attrs['Author'] = "Heliopropa.irap.omp.eu (CDPP)"
            cdf_handle.attrs['Created'] = str(time.ctime(time.time()))
aa7247d6   Goutte   Generate a CDF fi...
1379
1380
1381
1382

            available_params = list(PROPERTIES)
            for target in targets_configs:
                target_slug = target['slug']
297a7dfc   Goutte   Add support for i...
1383
1384
1385
1386
                data = get_data_for_target(
                    target_config=target, input_slug=input_slug,
                    started_at=started_at, stopped_at=stopped_at
                )
aa7247d6   Goutte   Generate a CDF fi...
1387
1388
1389
                dkeys = sorted(data)

                values = []
aa7247d6   Goutte   Generate a CDF fi...
1390
                for dkey in dkeys:
952e3d8f   Goutte   Move to another s...
1391
1392
1393
1394
                    time_str = data[dkey][0][:-6]  # remove +00:00 tail
                    date = datetime.datetime.strptime(time_str, FILE_DATE_FMT)
                    values.append(date)
                kt = "%s_time" % target_slug
4aaf6874   Goutte   Try fixing the ge...
1395
                cdf_handle.new(kt, type=pycdf.const.CDF_EPOCH)
952e3d8f   Goutte   Move to another s...
1396
                cdf_handle[kt] = values
4aaf6874   Goutte   Try fixing the ge...
1397
                # cdf_handle[kt].attrs['FIELDNAM'] = "Time since 0 A.D"
aa7247d6   Goutte   Generate a CDF fi...
1398
1399
1400

                for param in params:
                    k = "%s_%s" % (target_slug, param)
4aaf6874   Goutte   Try fixing the ge...
1401
                    # print("PARAM %s" % k)
aa7247d6   Goutte   Generate a CDF fi...
1402
1403
                    values = []
                    i = available_params.index(param)
4aaf6874   Goutte   Try fixing the ge...
1404
                    has_nones = False
aa7247d6   Goutte   Generate a CDF fi...
1405
                    for dkey in dkeys:
4aaf6874   Goutte   Try fixing the ge...
1406
1407
1408
1409
1410
1411
1412
1413
1414
                        value = data[dkey][i]
                        if value is None:
                            has_nones = True
                        values.append(value)
                    if has_nones:
                        # PyCDF hates it when there are Nones.
                        # Since we don't know what value to set instead,
                        # let's skip the param altogether.
                        continue
aa7247d6   Goutte   Generate a CDF fi...
1415
                    cdf_handle[k] = values
952e3d8f   Goutte   Move to another s...
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
                    attrs = cdf_handle[k].attrs
                    attrs['UNITS'] = PARAMETERS[param]['units']
                    attrs['LABLAXIS'] = PARAMETERS[param]['name']
                    attrs['FIELDNAM'] = PARAMETERS[param]['title']
                    if values:
                        attrs['VALIDMIN'] = min(values)
                        attrs['VALIDMAX'] = max(values)

                kx = "%s_xhee" % target_slug
                ky = "%s_yhee" % target_slug
aa7247d6   Goutte   Generate a CDF fi...
1426
1427
1428
1429
1430
                values_xhee = []
                values_yhee = []
                index_x = available_params.index('xhee')
                index_y = available_params.index('yhee')
                for dkey in dkeys:
391d581c   Goutte   Make the CDF down...
1431
1432
1433
                    value_xhee = data[dkey][index_x]
                    value_yhee = data[dkey][index_y]
                    # We've got some `None`s cropping up in the data sometimes.
4aaf6874   Goutte   Try fixing the ge...
1434
                    # PyCDF does not digest Nones at all.
391d581c   Goutte   Make the CDF down...
1435
1436
1437
1438
1439
                    # While they solve this upstream, let's make an ugly fix!
                    if (value_xhee is not None) and (value_yhee is not None):
                        values_xhee.append(value_xhee)
                        values_yhee.append(value_yhee)
                    else:
4aaf6874   Goutte   Try fixing the ge...
1440
1441
                        values_xhee.append(0)
                        values_yhee.append(0)
391d581c   Goutte   Make the CDF down...
1442
                        log.warn("Orbit data for %s has NaNs." % target_slug)
952e3d8f   Goutte   Move to another s...
1443
1444
1445
1446
                cdf_handle[kx] = values_xhee
                cdf_handle[ky] = values_yhee
                cdf_handle[kx].attrs['UNITS'] = 'Au'
                cdf_handle[ky].attrs['UNITS'] = 'Au'
aa7247d6   Goutte   Generate a CDF fi...
1447
1448

            log.debug("Writing CDF '%s'..." % cdf_filename)
952e3d8f   Goutte   Move to another s...
1449
1450
            cdf_handle.close()
            log.debug("Wrote CDF '%s'." % cdf_filename)
aa7247d6   Goutte   Generate a CDF fi...
1451
1452
1453

        except Exception as e:
            log.error("Failed to generate CDF '%s'." % cdf_filename)
952e3d8f   Goutte   Move to another s...
1454
1455
            if isfile(cdf_path):
                removefile(cdf_path)
aa7247d6   Goutte   Generate a CDF fi...
1456
1457
            raise

aa7247d6   Goutte   Generate a CDF fi...
1458
1459
1460
1461
1462
1463
    if not isfile(cdf_path):
        abort(500, "No CDF to serve. Looked at '%s'." % cdf_path)

    return send_from_directory(CACHE_DIR, cdf_filename)


28bb4b28   Goutte   API for the cache...
1464
1465
# API #########################################################################

e18701b6   Goutte   Cache clear (remo...
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
@app.route("/cache/clear")
def cache_clear():
    """
    Removes all files from the cache.
    Note: It also removes the .gitkeep file. Not a problem for prod.
    """
    removed_files = remove_all_files(CACHE_DIR)
    count = len(removed_files)
    return "Cache cleared! Removed %d file%s." \
           % (count, 's' if count != 1 else '')


d9710a98   Goutte   Rename the cleanu...
1478
1479
@app.route("/cache/cleanup")
def cache_cleanup():
28bb4b28   Goutte   API for the cache...
1480
1481
    """
    Removes all files from the cache that are older than roughly one month.
e18701b6   Goutte   Cache clear (remo...
1482
    Note: It also removes the .gitkeep file. Maybe it should not, but hey.
28bb4b28   Goutte   API for the cache...
1483
1484
    """
    a_month_ago = datetime.datetime.now() - datetime.timedelta(days=32)
e18701b6   Goutte   Cache clear (remo...
1485
    removed_files = remove_files_created_before(a_month_ago, CACHE_DIR)
d9710a98   Goutte   Rename the cleanu...
1486
1487
1488
    count = len(removed_files)
    return "Cache cleaned! Removed %d old file%s." \
           % (count, 's' if count != 1 else '')
28bb4b28   Goutte   API for the cache...
1489
1490


b500e561   Goutte   Invert the orbits...
1491
1492
1493
1494
@app.route("/cache/warmup")
def cache_warmup():
    """
    Warms up the cache for the current day.
b500e561   Goutte   Invert the orbits...
1495
    """
390a3587   Goutte   Add a CRON config...
1496
    warmup_started_at = datetime.datetime.now()
284f4688   Goutte   Continue layers i...
1497
    sta, sto = get_interval_from_query()
297a7dfc   Goutte   Add support for i...
1498
    inp = 'l1'  # default input, maybe warm them all up ?
fb383448   Goutte   Implement the cac...
1499
1500

    targets = get_active_targets()
fb383448   Goutte   Implement the cac...
1501
1502
    targets_slugs = [target['slug'] for target in targets]
    targets_slugs.sort()
4aaf6874   Goutte   Try fixing the ge...
1503
1504
1505
1506

    update_spacepy()
    for target in targets:
        download_target_csv(target['slug'], inp, sta, sto)
297a7dfc   Goutte   Add support for i...
1507
    download_targets_cdf('-'.join(targets_slugs), inp, sta, sto)
fb383448   Goutte   Implement the cac...
1508

390a3587   Goutte   Add a CRON config...
1509
1510
1511
1512
    warmup_ended_at = datetime.datetime.now()
    warmup_timedelta = warmup_ended_at - warmup_started_at

    return "Done in %s." % str(warmup_timedelta)
b500e561   Goutte   Invert the orbits...
1513
1514


1324cc91   Goutte   Make the footer i...
1515
@app.route("/log")
596da00d   Goutte   Add more exceptio...
1516
@app.route("/log.html")
1324cc91   Goutte   Make the footer i...
1517
1518
def log_show():
    with open(LOG_FILE, 'r') as f:
bde97e4d   Goutte   Add more changes ...
1519
        contents = f.read()
fb383448   Goutte   Implement the cac...
1520
    return "<pre>" + contents + "</pre>"
bde97e4d   Goutte   Add more changes ...
1521
1522


1324cc91   Goutte   Make the footer i...
1523
1524
1525
1526
1527
1528
1529
@app.route("/log/clear")
def log_clear():
    with open(LOG_FILE, 'w') as f:
        f.truncate()
    return "Log cleared successfully."


1754789b   Goutte   Decorate and clea...
1530
1531
1532
1533
# DEV TOOLS ###################################################################

# @app.route("/inspect")
# def analyze_cdf():
a4a9ef03   Goutte   Cache generated C...
1534
#     """
1754789b   Goutte   Decorate and clea...
1535
#     For debug purposes.
a4a9ef03   Goutte   Cache generated C...
1536
#     """
1754789b   Goutte   Decorate and clea...
1537
1538
#     cdf_to_inspect = get_path("../res/dummy.nc")
#     cdf_to_inspect = get_path("../res/dummy_jupiter_coordinates.nc")
a4a9ef03   Goutte   Cache generated C...
1539
1540
#
#     si = StringIO.StringIO()
1754789b   Goutte   Decorate and clea...
1541
1542
#     cw = csv.DictWriter(si, fieldnames=['Name', 'Shape', 'Length'])
#     cw.writeheader()
a4a9ef03   Goutte   Cache generated C...
1543
#
1754789b   Goutte   Decorate and clea...
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
#     # Time, StartTime, StopTime, V, B, N, T, Delta_angle, P_dyn, QualityFlag
#     cdf_handle = Dataset(cdf_to_inspect, "r", format="NETCDF4")
#     for variable in cdf_handle.variables:
#         v = cdf_handle.variables[variable]
#         cw.writerow({
#             'Name': variable,
#             'Shape': v.shape,
#             'Length': v.size,
#         })
#     cdf_handle.close()
a4a9ef03   Goutte   Cache generated C...
1554
1555
1556
1557
#
#     return si.getvalue()


9390ec89   Goutte   Initial experimen...
1558
1559
1560
# MAIN ########################################################################

if __name__ == "__main__":
952e3d8f   Goutte   Move to another s...
1561
    # Debug mode is on, as the production server does not use this but run.wsgi
9390ec89   Goutte   Initial experimen...
1562
1563
    extra_files = [get_path('../config.yml')]
    app.run(debug=True, extra_files=extra_files)