eskp.py
changeset 496 0641866df1b0
parent 441 c258f9b3f831
child 498 a4f39dc4db6a
     1.1 --- a/eskp.py	Fri Mar 11 09:43:34 2016 +0100
     1.2 +++ b/eskp.py	Fri Mar 11 09:45:30 2016 +0100
     1.3 @@ -19,11 +19,11 @@
     1.4  import base64
     1.5  
     1.6  LANGUAGE_SELECTED = "de"
     1.7 -#ToDo after engelish is implemented set LANGUAGE_SELECTED = None
     1.8 +# ToDo after engelish is implemented set LANGUAGE_SELECTED = None
     1.9  
    1.10  # We need the path of this file to find templates to translate
    1.11  ESKP_PATH = os.path.dirname(os.path.abspath(__file__))
    1.12 -logging.basicConfig(filename=os.path.join(ESKP_PATH, 'eskp-app.log'),level=logging.DEBUG)
    1.13 +logging.basicConfig(filename=os.path.join(ESKP_PATH, 'eskp-app.log'), level=logging.DEBUG)
    1.14  
    1.15  app = Flask(__name__)
    1.16  babel = Babel(app)
    1.17 @@ -31,12 +31,10 @@
    1.18  app.config['BABEL_DEFAULT_LOCALE'] = 'de'
    1.19  
    1.20  
    1.21 -
    1.22  def get_vcard(filename):
    1.23      filename = os.path.join(ESKP_PATH, filename)
    1.24      with codecs.open(filename, 'r', 'utf-8') as f:
    1.25 -            vcard = f.read()
    1.26 -            f.close()
    1.27 +        vcard = f.read()
    1.28      return vobject.readOne(vcard)
    1.29  
    1.30  
    1.31 @@ -46,25 +44,36 @@
    1.32      if os.path.isfile(filename):
    1.33          with codecs.open(filename, 'r', 'utf-8') as f:
    1.34              rst_data = f.read()
    1.35 -        f.close()
    1.36          content = publish_parts(rst_data, writer_name='html', settings_overrides=overrides)['html_body']
    1.37      return content
    1.38  
    1.39 +
    1.40 +def get_newest_date():
    1.41 +    files = os.listdir(os.path.join(ESKP_PATH, 'static/images/uvmap'))
    1.42 +    getdates = get_valid_dates(files)
    1.43 +    newest_date = getdates[-1]
    1.44 +
    1.45 +    return newest_date
    1.46 +
    1.47 +
    1.48  def get_topmenue():
    1.49 +    newest_date = get_newest_date()
    1.50      menue = [
    1.51 -             ('/ozoneloss', _(u'Ozoneloss'),
    1.52 -             (('/ozoneloss', _(u'overview')),
    1.53 -             ('/ozoneloss/clams/2016', _(u'calculations')),
    1.54 -             ('/ozoneloss/vpsc/2016', _(u'estimations')),
    1.55 -             ('/ozoneloss/uvi', _(u'uv increase')))),
    1.56 -
    1.57 -             ('/eskp', _(u'ESKP'),(None, None)),
    1.58 -             ('/iek-7', _(u'IEK-7'),(None, None))
    1.59 -            ]
    1.60 +        ('/ozoneloss', _(u'Ozoneloss'),
    1.61 +         (('/ozoneloss', _(u'overview')),
    1.62 +          ('/ozoneloss/clams/2016', _(u'calculations')),
    1.63 +          ('/ozoneloss/vpsc/2016', _(u'estimations')),
    1.64 +          ('/ozoneloss/uvi', _(u'uv increase')),
    1.65 +          ('/ozoneloss/uvmap/' + newest_date, _(u'uv index map')))),
    1.66 +        ('/eskp', _(u'ESKP'), (None, None)),
    1.67 +        ('/iek-7', _(u'IEK-7'), (None, None))
    1.68 +    ]
    1.69      return menue
    1.70  
    1.71 +
    1.72  app.jinja_env.globals.update(get_topmenue=get_topmenue)
    1.73  
    1.74 +
    1.75  def get_o3lossclams_dates():
    1.76      menue = [
    1.77          ('/ozoneloss/clams/2016', _(u'2016')),
    1.78 @@ -72,9 +81,48 @@
    1.79          ('/ozoneloss/clams/2012', _(u'2012')),
    1.80          ('/ozoneloss/clams/2011', _(u'2011')),
    1.81          ('/ozoneloss/clams/2010', _(u'2010')),
    1.82 -        ]
    1.83 +    ]
    1.84      return menue
    1.85  
    1.86 +
    1.87 +def get_valid_dates(files):
    1.88 +    dates = []
    1.89 +
    1.90 +    for file in files:
    1.91 +        if file.endswith('.png') and file.find('uvi') >= 0:
    1.92 +            date = file[-12:-6]
    1.93 +            dates.append(date)
    1.94 +    dates.sort()
    1.95 +    for date in dates:
    1.96 +        i = 0
    1.97 +        for param in ['uvi', 'o3col', 'do3col']:
    1.98 +            testfile = 'clams_' + param + '_' + date + '12.png'
    1.99 +            if files.count(testfile) > 0:
   1.100 +                i = i + 1
   1.101 +        if i <> 3:
   1.102 +            dates.remove(date)
   1.103 +    return dates
   1.104 +
   1.105 +
   1.106 +def get_o3lossuvmap_dates(date_show):
   1.107 +
   1.108 +    files = os.listdir(os.path.join(ESKP_PATH,'static/images/uvmap'))
   1.109 +    dates = get_valid_dates(files)
   1.110 +    ndates = len(dates)
   1.111 +    ind = dates.index(date_show)
   1.112 +    if ind ==0 :
   1.113 +        chosendates = [dates[ind+1]]
   1.114 +    elif ind >= ndates - 1:
   1.115 +        chosendates= [dates[ind-1]]
   1.116 +    else:
   1.117 +        chosendates = [dates[ind-1], dates[ind+1]]
   1.118 +    menue = []
   1.119 +    for date in chosendates:
   1.120 +        text_date = date[-2:] + '.' + date[-4:-2] + '.'
   1.121 +        menue.append(('/ozoneloss/uvmap/' + date, _(text_date)))
   1.122 +    return menue
   1.123 +
   1.124 +
   1.125  def get_vpsc_dates():
   1.126      menue = [
   1.127          ('/ozoneloss/vpsc/2016', _(u'2016')),
   1.128 @@ -84,11 +132,13 @@
   1.129          ('/ozoneloss/vpsc/2012', _(u'2012')),
   1.130          ('/ozoneloss/vpsc/2011', _(u'2011')),
   1.131          ('/ozoneloss/vpsc/2010', _(u'2010')),
   1.132 -        ]
   1.133 +    ]
   1.134      return menue
   1.135  
   1.136 +
   1.137  app.jinja_env.globals.update(get_o3lossclams_dates=get_o3lossclams_dates)
   1.138  app.jinja_env.globals.update(get_vpsc_dates=get_vpsc_dates)
   1.139 +app.jinja_env.globals.update(get_o3lossuvmap_dates=get_o3lossuvmap_dates)
   1.140  
   1.141  
   1.142  def modal_info(template, act, title, filename):
   1.143 @@ -118,6 +168,13 @@
   1.144      return render_template("/ozoneloss_clams.html", act="ozoneloss/clams/%s" % year, content=content, year=year)
   1.145  
   1.146  
   1.147 +@app.route('/ozoneloss/uvmap/<date>')
   1.148 +def ozoneloss_uvmap_date(date):
   1.149 +    filename = os.path.join("templates", get_locale(), "rst", "ozoneloss_uvmap.rst")
   1.150 +    content = get_content(filename)
   1.151 +    return render_template("/ozoneloss_uvmap.html", act="ozoneloss/uvmap/%s" % date, content=content, date=date)
   1.152 +
   1.153 +
   1.154  @app.route('/ozoneloss/vpsc/<year>')
   1.155  def ozoneloss_vspc_year(year):
   1.156      filename = os.path.join("templates", get_locale(), "rst", "ozoneloss_vpsc.rst")
   1.157 @@ -137,6 +194,7 @@
   1.158                             eskp_info=_(u'About ESKP'),
   1.159                             )
   1.160  
   1.161 +
   1.162  @app.route('/en')
   1.163  def en():
   1.164      global LANGUAGE_SELECTED
   1.165 @@ -145,6 +203,7 @@
   1.166                             eskp_info=_(u'About ESKP'),
   1.167                             )
   1.168  
   1.169 +
   1.170  @app.route("/eskp")
   1.171  def eskp():
   1.172      filename = os.path.join("templates", get_locale(), "rst", "eskp.rst")
   1.173 @@ -155,13 +214,13 @@
   1.174  
   1.175  
   1.176  def qr_image_data(card):
   1.177 -    buf= StringIO.StringIO()
   1.178 +    buf = StringIO.StringIO()
   1.179      qr = qrcode.QRCode(
   1.180          version=1,
   1.181          error_correction=qrcode.constants.ERROR_CORRECT_L,
   1.182          box_size=2,
   1.183          border=2,
   1.184 -        )
   1.185 +    )
   1.186      qr.add_data(card.serialize())
   1.187      qr.make(fit=True)
   1.188      img = qr.make_image()
   1.189 @@ -172,12 +231,12 @@
   1.190  
   1.191  @app.route("/ozoneloss")
   1.192  def ozoneloss():
   1.193 -
   1.194      filename = os.path.join("templates", get_locale(), "rst", "ozoneloss.rst")
   1.195      content = get_content(filename)
   1.196  
   1.197      vcard_file = os.path.join("vcards", "jug.vcf")
   1.198      author = u""
   1.199 +
   1.200      try:
   1.201          card = get_vcard(vcard_file)
   1.202      except IOError:
   1.203 @@ -185,12 +244,13 @@
   1.204      if card is not None:
   1.205          qr_image = qr_image_data(card)
   1.206          author = render_template("/author_info.html", act="author", title=_(u"Ozoneloss"),
   1.207 -                                  card=card, image=qr_image, contact=_(u"Contact"), exit=_(u"Close"))
   1.208 +                                 card=card, image=qr_image, contact=_(u"Contact"), exit=_(u"Close"))
   1.209  
   1.210      filename = os.path.join("templates", get_locale(), "rst", "ozoneloss_publications.rst")
   1.211      publications = modal_info("/publications_info.html", "publications", _(u"Ozoneloss"), filename)
   1.212      return render_template("/ozoneloss.html", act="ozoneloss", content=content,
   1.213 -                           author=author,card=card, publications=publications )
   1.214 +                           author=author, card=card, publications=publications)
   1.215 +
   1.216  
   1.217  @app.route("/ozoneloss/clams")
   1.218  def ozoneloss_clams():
   1.219 @@ -198,6 +258,14 @@
   1.220      content = get_content(filename)
   1.221      return render_template("/ozoneloss_clams.html", act="ozoneloss/clams", content=content)
   1.222  
   1.223 +
   1.224 +@app.route("/ozoneloss/uvmap")
   1.225 +def ozoneloss_uvmap():
   1.226 +    filename = os.path.join("templates", get_locale(), "rst", "ozoneloss_uvmap.rst")
   1.227 +    content = get_content(filename)
   1.228 +    return render_template("/ozoneloss_uvmap.html", act="ozoneloss/uvmap", content=content)
   1.229 +
   1.230 +
   1.231  @app.route("/ozoneloss/vpsc")
   1.232  def ozoneloss_vspc():
   1.233      filename = os.path.join("templates", get_locale(), "rst", "ozoneloss_vpsc.rst")
   1.234 @@ -215,10 +283,10 @@
   1.235      o3offset = 50.
   1.236      figname = "uvincr_lat%0.3i_do3%0.3i.svg" % (lat, o3offset)
   1.237  
   1.238 -
   1.239      return render_template('ozoneloss_uvi.html', act="ozoneloss/uvi", content=content, figname=figname,
   1.240                             alt=_(u"UV increase at {{lat}} degrees N for {{o3offset}} DU ozone depletion"))
   1.241  
   1.242 +
   1.243  @app.route("/ozoneloss/uvi_graph", methods=['POST'])
   1.244  def ozoneloss_uvi_graph():
   1.245      filename = os.path.join("templates", get_locale(), "rst", "ozoneloss_uvi.rst")
   1.246 @@ -236,17 +304,12 @@
   1.247          latstr2 = latstr2.replace(u'S', '')
   1.248          lat = -float(latstr2)
   1.249  
   1.250 -
   1.251      figname = "uvincr_lat%0.3i_do3%0.3i.svg" % (lat, float(o3offsetstr))
   1.252  
   1.253      return render_template('graph.html', act="ozoneloss_uvi_graph", content=content, figname=figname,
   1.254                             o3offsetstr=o3offsetstr, latstr=latstr)
   1.255  
   1.256  
   1.257 -
   1.258 -
   1.259 -
   1.260 -
   1.261  @app.route("/iek-7")
   1.262  def institute():
   1.263      filename = os.path.join("templates", get_locale(), "rst", "iek-7.rst")
   1.264 @@ -260,7 +323,7 @@
   1.265      if card is not None:
   1.266          qr_image = qr_image_data(card)
   1.267          author = render_template("/author_info.html", act="author", title=_(u"IEK-7"),
   1.268 -                                  card=card, image=qr_image, contact=_(u"Contact"), exit=_(u"Close"))
   1.269 +                                 card=card, image=qr_image, contact=_(u"Contact"), exit=_(u"Close"))
   1.270  
   1.271      return render_template("/iek-7.html", act="iek-7", content=content,
   1.272                             author=author, card=card, contact=u"IEK-7")
   1.273 @@ -273,14 +336,12 @@
   1.274      return render_template("/content.html", act="imprint", content=content)
   1.275  
   1.276  
   1.277 -
   1.278 -
   1.279 -
   1.280  @app.errorhandler(404)
   1.281  def page_not_found(e):
   1.282      msg = _(u"Url: %(url)s not found", url=request.url)
   1.283      info = _(u"This information is not available!")
   1.284      return render_template("404.html", msg=msg, info=info)
   1.285  
   1.286 +
   1.287  if __name__ == "__main__":
   1.288 -    app.run(host='localhost', port=5014)
   1.289 +    app.run(host='localhost', port=5014, debug=True)
Impressum Datenschutzerklärung