eskp.py
author Jens-Uwe Grooss <j.-u.grooss@fz-juelich.de>
Mon, 17 Feb 2020 13:02:54 +0100
changeset 1087 9231ee512b51
parent 1057 53622e770eca
child 1116 0559fcb555f1
permissions -rwxr-xr-x
eskp plot update from 200217
     1 # -*- coding: utf-8 -*-
     2 #!/usr/bin/env python
     3 import logging
     4 
     5 import os
     6 import codecs
     7 import vobject
     8 import StringIO
     9 
    10 import qrcode
    11 
    12 from docutils.core import publish_parts
    13 from flask import Flask
    14 from flask import render_template
    15 from flask import request
    16 from flask_babel import gettext as _
    17 from flask_babel import Babel
    18 from config import LANGUAGES
    19 
    20 import base64
    21 
    22 LANGUAGE_SELECTED = "de"
    23 # ToDo after engelish is implemented set LANGUAGE_SELECTED = None
    24 
    25 # We need the path of this file to find templates to translate
    26 ESKP_PATH = os.path.dirname(os.path.abspath(__file__))
    27 logging.basicConfig(filename=os.path.join(ESKP_PATH, 'eskp-app.log'), level=logging.DEBUG)
    28 FILES= os.listdir(os.path.join(ESKP_PATH, 'static/images/uvmap'))
    29 
    30 app = Flask(__name__)
    31 babel = Babel(app)
    32 
    33 app.config['BABEL_DEFAULT_LOCALE'] = 'de'
    34 
    35 
    36 def get_vcard(filename):
    37     filename = os.path.join(ESKP_PATH, filename)
    38     with codecs.open(filename, 'r', 'utf-8') as f:
    39         vcard = f.read()
    40     return vobject.readOne(vcard)
    41 
    42 
    43 def get_content(filename, overrides=None):
    44     content = u""
    45     filename = os.path.join(ESKP_PATH, filename)
    46     if os.path.isfile(filename):
    47         with codecs.open(filename, 'r', 'utf-8') as f:
    48             rst_data = f.read()
    49         content = publish_parts(rst_data, writer_name='html', settings_overrides=overrides)['html_body']
    50     return content
    51 
    52 def get_html_content(filename, overrides=None):
    53     html_content = u""
    54     filename = os.path.join(ESKP_PATH, filename)
    55     if os.path.isfile(filename):
    56         with codecs.open(filename, 'r', 'utf-8') as f:
    57             html_content = f.read()
    58 
    59     return html_content
    60 
    61 def get_newest_date():
    62     getdates = get_valid_dates(FILES)
    63     newest_date = getdates[-1]
    64 
    65     return newest_date
    66 
    67 
    68 def get_topmenue():
    69     newest_date = get_newest_date()
    70     menue = [
    71         ('/ozoneloss', _(u'Ozoneloss'),
    72          (('/ozoneloss', _(u'overview')),
    73           ('/ozoneloss/clams/2020', _(u'calculations')),
    74           ('/ozoneloss/vpsc/2020', _(u'estimations')),
    75           ('/ozoneloss/uvmap/' + newest_date, _(u'uv index map')),
    76           ('/ozoneloss/uvi', _(u'uv increase')),
    77           ('/ozoneloss/decadal', _(u'decadal')))),
    78         ('/eskp', _(u'ESKP'), (None, None)),
    79         ('/iek-7', _(u'IEK-7'), (None, None))
    80     ]
    81     return menue
    82 
    83 
    84 app.jinja_env.globals.update(get_topmenue=get_topmenue)
    85 
    86 
    87 def get_o3lossclams_dates():
    88     menue = [
    89         ('/ozoneloss/clams/2020', _(u'2020')),
    90         ('/ozoneloss/clams/2019', _(u'2019')),
    91         ('/ozoneloss/clams/2018', _(u'2018')),
    92         ('/ozoneloss/clams/2017', _(u'2017')),
    93         ('/ozoneloss/clams/2016', _(u'2016')),
    94         ('/ozoneloss/clams/2015', _(u'2015')),
    95         ('/ozoneloss/clams/2012', _(u'2012')),
    96         ('/ozoneloss/clams/2011', _(u'2011')),
    97         ('/ozoneloss/clams/2010', _(u'2010')),
    98     ]
    99     return menue
   100 
   101 
   102 def get_valid_dates(files):
   103     dates = []
   104 
   105     for file in files:
   106         if file.endswith('.png') and file.find('uvi') >= 0:
   107             date = file[-12:-6]
   108             dates.append(date)
   109     dates.sort()
   110     for date in dates:
   111         i = 0
   112         for param in ['uvi', 'o3col', 'do3col']:
   113             testfile = 'clams_' + param + '_' + date + '12.png'
   114             if files.count(testfile) > 0:
   115                 i = i + 1
   116         if i <> 3:
   117             dates.remove(date)
   118     return dates
   119 
   120 
   121 def get_o3lossuvmap_dates(date_show):
   122     dates = get_valid_dates(FILES)
   123     ndates = len(dates)
   124     ind = dates.index(date_show)
   125     navitexts = []
   126     if ind ==0 :
   127         chosendates = [dates[ind+1]]
   128         navitexts.append('next ->')
   129 
   130     elif ind >= ndates - 1:
   131         chosendates= [dates[ind-1]]
   132         navitexts.append('<- prev')
   133     else:
   134         chosendates = [dates[ind-1], dates[ind+1]]
   135         navitexts.append('<- prev')
   136         navitexts.append('next ->')
   137     menue = []
   138 
   139     for i in range(len(chosendates)):
   140         date = chosendates[i]
   141         navitext = navitexts[i]
   142         menue.append(('/ozoneloss/uvmap/' + date, _(navitext)))
   143     return menue
   144 
   145 
   146 def get_vpsc_dates():
   147     menue = [
   148         ('/ozoneloss/vpsc/2020', _(u'2020')),
   149         ('/ozoneloss/vpsc/2019', _(u'2019')),
   150         ('/ozoneloss/vpsc/2018', _(u'2018')),
   151         ('/ozoneloss/vpsc/2017', _(u'2017')),
   152         ('/ozoneloss/vpsc/2016', _(u'2016')),
   153         ('/ozoneloss/vpsc/2015', _(u'2015')),
   154         ('/ozoneloss/vpsc/2014', _(u'2014')),
   155         ('/ozoneloss/vpsc/2013', _(u'2013')),
   156         ('/ozoneloss/vpsc/2012', _(u'2012')),
   157         ('/ozoneloss/vpsc/2011', _(u'2011')),
   158         ('/ozoneloss/vpsc/2010', _(u'2010')),
   159     ]
   160     return menue
   161 
   162 
   163 app.jinja_env.globals.update(get_o3lossclams_dates=get_o3lossclams_dates)
   164 app.jinja_env.globals.update(get_vpsc_dates=get_vpsc_dates)
   165 app.jinja_env.globals.update(get_o3lossuvmap_dates=get_o3lossuvmap_dates)
   166 
   167 
   168 def modal_info(template, act, title, filename):
   169     content = get_content(filename)
   170     html = render_template(template, act=act, title=title, content=content, exit=_(u"Close"))
   171     return html
   172 
   173 
   174 @babel.localeselector
   175 def get_locale():
   176     requested_language = request.accept_languages.best_match(LANGUAGES.keys())
   177     if requested_language in ("de", "en"):
   178         return requested_language or "de"
   179 
   180 @app.route("/")
   181 @app.route("/index")
   182 def index():
   183     return render_template("/index.html",
   184                            eskp_info=_(u'About ESKP'),
   185                            )
   186 
   187 
   188 @app.route('/ozoneloss/clams/<year>')
   189 def ozoneloss_clams_year(year):
   190     filename = os.path.join("templates", get_locale(), "rst", "ozoneloss_clams.rst")
   191     content = get_content(filename)
   192     return render_template("/ozoneloss_clams.html", act="ozoneloss/clams/%s" % year, content=content, year=year)
   193 
   194 
   195 @app.route('/ozoneloss/uvmap/<date>')
   196 def ozoneloss_uvmap_date(date):
   197     filename = os.path.join("templates", get_locale(), "rst", "ozoneloss_uvmap.rst")
   198     content = get_content(filename)
   199     htmlfile = os.path.join("templates", get_locale(), "html", "uvi_table.html")
   200     html_table = get_html_content(htmlfile)
   201 
   202     return render_template("/ozoneloss_uvmap.html", act="ozoneloss/uvmap/%s" % date, content=content,
   203                            html_table=html_table, date=date)
   204 
   205 
   206 @app.route('/ozoneloss/vpsc/<year>')
   207 def ozoneloss_vpsc_year(year):
   208     filename = os.path.join("templates", get_locale(), "rst", "ozoneloss_vpsc.rst")
   209     content = get_content(filename)
   210     filename = os.path.join("templates", get_locale(), "rst", "explanation_vpsc.rst")
   211     explanation = get_content(filename)
   212 
   213     return render_template("/ozoneloss_vpsc.html", act="ozoneloss/vpsc/%s" % year, content=content,
   214                            content_explanation=explanation, year=year)
   215 
   216 
   217 @app.route('/de')
   218 def de():
   219     global LANGUAGE_SELECTED
   220     LANGUAGE_SELECTED = "de"
   221     return render_template("/index.html",
   222                            eskp_info=_(u'About ESKP'),
   223                            )
   224 
   225 
   226 @app.route('/en')
   227 def en():
   228     global LANGUAGE_SELECTED
   229     LANGUAGE_SELECTED = "en"
   230     return render_template("/index.html",
   231                            eskp_info=_(u'About ESKP'),
   232                            )
   233 
   234 
   235 @app.route("/eskp")
   236 def eskp():
   237     filename = os.path.join("templates", get_locale(), "rst", "eskp.rst")
   238     content = get_content(filename)
   239     filename = os.path.join("templates", get_locale(), "rst", "eskp_title.rst")
   240     headline = get_content(filename)
   241     return render_template("/eskp.html", act="eskp", content=content, headline=headline)
   242 
   243 
   244 def qr_image_data(card):
   245     buf = StringIO.StringIO()
   246     qr = qrcode.QRCode(
   247         version=1,
   248         error_correction=qrcode.constants.ERROR_CORRECT_L,
   249         box_size=2,
   250         border=2,
   251     )
   252     qr.add_data(card.serialize())
   253     qr.make(fit=True)
   254     img = qr.make_image()
   255     img.save(buf)
   256     image = buf.getvalue()
   257     return base64.b64encode(image)
   258 
   259 
   260 @app.route("/ozoneloss")
   261 def ozoneloss():
   262     filename = os.path.join("templates", get_locale(), "rst", "ozoneloss.rst")
   263     content = get_content(filename)
   264 
   265     vcard_file = os.path.join("vcards", "jug.vcf")
   266     author = u""
   267 
   268     try:
   269         card = get_vcard(vcard_file)
   270     except IOError:
   271         card = None
   272     if card is not None:
   273         qr_image = qr_image_data(card)
   274         author = render_template("/author_info.html", act="author", title=_(u"Ozoneloss"),
   275                                  card=card, image=qr_image, contact=_(u"Contact"), exit=_(u"Close"))
   276 
   277     filename = os.path.join("templates", get_locale(), "rst", "ozoneloss_publications.rst")
   278     publications = modal_info("/publications_info.html", "publications", _(u"Ozoneloss"), filename)
   279     return render_template("/ozoneloss.html", act="ozoneloss", content=content,
   280                            author=author, card=card, publications=publications)
   281 
   282 
   283 @app.route("/ozoneloss/clams")
   284 def ozoneloss_clams():
   285     filename = os.path.join("templates", get_locale(), "rst", "ozoneloss_clams.rst")
   286     content = get_content(filename)
   287     return render_template("/ozoneloss_clams.html", act="ozoneloss/clams", content=content)
   288 
   289 
   290 @app.route("/ozoneloss/uvmap")
   291 def ozoneloss_uvmap():
   292     filename = os.path.join("templates", get_locale(), "rst", "ozoneloss_uvmap.rst")
   293     content = get_content(filename)
   294 
   295     htmlfile = os.path.join("templates", get_locale(), "html", "uvi_table.html")
   296     html_content = get_html_content(htmlfile)
   297     return render_template("/ozoneloss_uvmap.html", act="ozoneloss/uvmap", content=content,
   298                            html_content=html_content)
   299 
   300 
   301 @app.route("/ozoneloss/vpsc")
   302 def ozoneloss_vpsc():
   303     filename = os.path.join("templates", get_locale(), "rst", "ozoneloss_vpsc.rst")
   304     content = get_content(filename)
   305     return render_template("/ozoneloss_vpsc.html", act="ozoneloss/vpsc", content=content)
   306 
   307 
   308 @app.route("/ozoneloss/uvi", methods=['GET'])
   309 def ozoneloss_uvi():
   310     filename = os.path.join("templates", get_locale(), "rst", "ozoneloss_uvi.rst")
   311     content = get_content(filename)
   312     htmlfile = os.path.join("templates", get_locale(), "html", "ozoneloss_uvi_form_start.html")
   313     html_content = get_html_content(htmlfile)
   314     htmlfile = os.path.join("templates", get_locale(), "html", "uvi_table.html")
   315     html_table = get_html_content(htmlfile)
   316 
   317     lat = 50.
   318     o3offset = 50.
   319     figname = "uvincr_lat%0.3i_do3%0.3i.svg" % (lat, o3offset)
   320 
   321     return render_template('ozoneloss_uvi.html', act="ozoneloss/uvi", content=content, figname=figname,
   322                            alt=_(u"UV increase at {{lat}} degrees N for {{o3offset}} DU ozone depletion"),
   323                                  html_content=html_content, html_table=html_table)
   324 
   325 
   326 @app.route("/ozoneloss/uvi_graph", methods=['POST'])
   327 def ozoneloss_uvi_graph():
   328     filename = os.path.join("templates", get_locale(), "rst", "ozoneloss_uvi.rst")
   329     content = get_content(filename)
   330     htmlfile = os.path.join("templates", get_locale(), "html", "ozoneloss_uvi_form.html")
   331     html_content = get_html_content(htmlfile)
   332     htmlfile = os.path.join("templates", get_locale(), "html", "uvi_table.html")
   333     html_table = get_html_content(htmlfile)
   334 
   335     latstr = request.form['Gradzahl']
   336     o3offsetstr = request.form['Dobson-Unit']
   337 
   338     latstr2 = latstr.replace(u'\xb0', '')
   339     if latstr2.endswith(u'N'):
   340         latstr2 = latstr2.replace(u'N', '')
   341         lat = float(latstr2)
   342     if latstr2.endswith(u'S'):
   343         latstr2 = latstr2.replace(u'S', '')
   344         lat = -float(latstr2)
   345         
   346     html_content = html_content.replace('{{latstr}}', latstr)
   347     html_content = html_content.replace('{{o3offsetstr}}', o3offsetstr)
   348     figname = "uvincr_lat%0.3i_do3%0.3i.svg" % (lat, float(o3offsetstr))
   349 
   350     return render_template('ozoneloss_uvi.html', act="ozoneloss_uvi_graph", content=content, figname=figname,
   351                            alt=_(u"UV increase at {{lat}} degrees N for {{o3offset}} DU ozone depletion"),
   352                            html_content=html_content, html_table=html_table)
   353 
   354 @app.route("/ozoneloss/decadal")
   355 def ozoneloss_decadal():
   356     filename = os.path.join("templates", get_locale(), "rst", "ozoneloss_decadal.rst")
   357     content = get_content(filename)
   358     filename = os.path.join("templates", get_locale(), "rst", "ozoneloss_decadal_bottom.rst")
   359     explanation = get_content(filename)
   360     
   361     return render_template("/ozoneloss_decadal.html", act="ozoneloss_decadal", content=content,
   362                            content_explanation=explanation)
   363 
   364 
   365 
   366 @app.route("/iek-7")
   367 def institute():
   368     filename = os.path.join("templates", get_locale(), "rst", "iek-7.rst")
   369     content = get_content(filename)
   370     vcard_file = os.path.join("vcards", "sas.vcf")
   371     author = u""
   372     try:
   373         card = get_vcard(vcard_file)
   374     except IOError:
   375         card = None
   376     if card is not None:
   377         qr_image = qr_image_data(card)
   378         author = render_template("/author_info.html", act="author", title=_(u"IEK-7"),
   379                                  card=card, image=qr_image, contact=_(u"Contact"), exit=_(u"Close"))
   380 
   381     return render_template("/iek-7.html", act="iek-7", content=content,
   382                            author=author, card=card, contact=u"IEK-7")
   383 
   384 
   385 @app.route("/imprint")
   386 def imprint():
   387     filename = os.path.join("templates", get_locale(), "rst", "imprint.rst")
   388     content = get_content(filename)
   389     return render_template("/content.html", act="imprint", content=content)
   390 
   391 
   392 @app.route("/dataprotection")
   393 def dataprotection():
   394     filename = os.path.join("templates", get_locale(), "rst", "datenschutz.rst")
   395     content = get_content(filename)
   396     return render_template("/content.html", act="datenschutz", content=content)
   397 
   398 
   399 @app.errorhandler(404)
   400 def page_not_found(e):
   401     msg = _(u"Url: %(url)s not found", url=request.url)
   402     info = _(u"This information is not available!")
   403     return render_template("404.html", msg=msg, info=info)
   404 
   405 
   406 if __name__ == "__main__":
   407     app.run(host='localhost', port=5014)
Impressum Datenschutzerklärung