eskp.py
changeset 499 04b941597cc4
parent 498 a4f39dc4db6a
child 501 bc348dd68c65
child 503 f44e4855e8e5
     1.1 --- a/eskp.py	Fri Mar 11 09:57:49 2016 +0100
     1.2 +++ b/eskp.py	Fri Mar 11 11:04:44 2016 +0100
     1.3 @@ -19,11 +19,12 @@
     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 +FILES= os.listdir(os.path.join(ESKP_PATH, 'static/images/uvmap'))
    1.15  
    1.16  app = Flask(__name__)
    1.17  babel = Babel(app)
    1.18 @@ -31,12 +32,10 @@
    1.19  app.config['BABEL_DEFAULT_LOCALE'] = 'de'
    1.20  
    1.21  
    1.22 -
    1.23  def get_vcard(filename):
    1.24      filename = os.path.join(ESKP_PATH, filename)
    1.25      with codecs.open(filename, 'r', 'utf-8') as f:
    1.26 -            vcard = f.read()
    1.27 -            f.close()
    1.28 +        vcard = f.read()
    1.29      return vobject.readOne(vcard)
    1.30  
    1.31  
    1.32 @@ -46,25 +45,35 @@
    1.33      if os.path.isfile(filename):
    1.34          with codecs.open(filename, 'r', 'utf-8') as f:
    1.35              rst_data = f.read()
    1.36 -        f.close()
    1.37          content = publish_parts(rst_data, writer_name='html', settings_overrides=overrides)['html_body']
    1.38      return content
    1.39  
    1.40 +
    1.41 +def get_newest_date():
    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,46 @@
    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 +    dates = get_valid_dates(FILES)
   1.108 +    ndates = len(dates)
   1.109 +    ind = dates.index(date_show)
   1.110 +    if ind ==0 :
   1.111 +        chosendates = [dates[ind+1]]
   1.112 +    elif ind >= ndates - 1:
   1.113 +        chosendates= [dates[ind-1]]
   1.114 +    else:
   1.115 +        chosendates = [dates[ind-1], dates[ind+1]]
   1.116 +    menue = []
   1.117 +    for date in chosendates:
   1.118 +        text_date = date[-2:] + '.' + date[-4:-2] + '.'
   1.119 +        menue.append(('/ozoneloss/uvmap/' + date, _(text_date)))
   1.120 +    return menue
   1.121 +
   1.122 +
   1.123  def get_vpsc_dates():
   1.124      menue = [
   1.125          ('/ozoneloss/vpsc/2016', _(u'2016')),
   1.126 @@ -84,11 +130,13 @@
   1.127          ('/ozoneloss/vpsc/2012', _(u'2012')),
   1.128          ('/ozoneloss/vpsc/2011', _(u'2011')),
   1.129          ('/ozoneloss/vpsc/2010', _(u'2010')),
   1.130 -        ]
   1.131 +    ]
   1.132      return menue
   1.133  
   1.134 +
   1.135  app.jinja_env.globals.update(get_o3lossclams_dates=get_o3lossclams_dates)
   1.136  app.jinja_env.globals.update(get_vpsc_dates=get_vpsc_dates)
   1.137 +app.jinja_env.globals.update(get_o3lossuvmap_dates=get_o3lossuvmap_dates)
   1.138  
   1.139  
   1.140  def modal_info(template, act, title, filename):
   1.141 @@ -118,6 +166,13 @@
   1.142      return render_template("/ozoneloss_clams.html", act="ozoneloss/clams/%s" % year, content=content, year=year)
   1.143  
   1.144  
   1.145 +@app.route('/ozoneloss/uvmap/<date>')
   1.146 +def ozoneloss_uvmap_date(date):
   1.147 +    filename = os.path.join("templates", get_locale(), "rst", "ozoneloss_uvmap.rst")
   1.148 +    content = get_content(filename)
   1.149 +    return render_template("/ozoneloss_uvmap.html", act="ozoneloss/uvmap/%s" % date, content=content, date=date)
   1.150 +
   1.151 +
   1.152  @app.route('/ozoneloss/vpsc/<year>')
   1.153  def ozoneloss_vspc_year(year):
   1.154      filename = os.path.join("templates", get_locale(), "rst", "ozoneloss_vpsc.rst")
   1.155 @@ -137,6 +192,7 @@
   1.156                             eskp_info=_(u'About ESKP'),
   1.157                             )
   1.158  
   1.159 +
   1.160  @app.route('/en')
   1.161  def en():
   1.162      global LANGUAGE_SELECTED
   1.163 @@ -145,6 +201,7 @@
   1.164                             eskp_info=_(u'About ESKP'),
   1.165                             )
   1.166  
   1.167 +
   1.168  @app.route("/eskp")
   1.169  def eskp():
   1.170      filename = os.path.join("templates", get_locale(), "rst", "eskp.rst")
   1.171 @@ -155,13 +212,13 @@
   1.172  
   1.173  
   1.174  def qr_image_data(card):
   1.175 -    buf= StringIO.StringIO()
   1.176 +    buf = StringIO.StringIO()
   1.177      qr = qrcode.QRCode(
   1.178          version=1,
   1.179          error_correction=qrcode.constants.ERROR_CORRECT_L,
   1.180          box_size=2,
   1.181          border=2,
   1.182 -        )
   1.183 +    )
   1.184      qr.add_data(card.serialize())
   1.185      qr.make(fit=True)
   1.186      img = qr.make_image()
   1.187 @@ -172,12 +229,12 @@
   1.188  
   1.189  @app.route("/ozoneloss")
   1.190  def ozoneloss():
   1.191 -
   1.192      filename = os.path.join("templates", get_locale(), "rst", "ozoneloss.rst")
   1.193      content = get_content(filename)
   1.194  
   1.195      vcard_file = os.path.join("vcards", "jug.vcf")
   1.196      author = u""
   1.197 +
   1.198      try:
   1.199          card = get_vcard(vcard_file)
   1.200      except IOError:
   1.201 @@ -185,12 +242,13 @@
   1.202      if card is not None:
   1.203          qr_image = qr_image_data(card)
   1.204          author = render_template("/author_info.html", act="author", title=_(u"Ozoneloss"),
   1.205 -                                  card=card, image=qr_image, contact=_(u"Contact"), exit=_(u"Close"))
   1.206 +                                 card=card, image=qr_image, contact=_(u"Contact"), exit=_(u"Close"))
   1.207  
   1.208      filename = os.path.join("templates", get_locale(), "rst", "ozoneloss_publications.rst")
   1.209      publications = modal_info("/publications_info.html", "publications", _(u"Ozoneloss"), filename)
   1.210      return render_template("/ozoneloss.html", act="ozoneloss", content=content,
   1.211 -                           author=author,card=card, publications=publications )
   1.212 +                           author=author, card=card, publications=publications)
   1.213 +
   1.214  
   1.215  @app.route("/ozoneloss/clams")
   1.216  def ozoneloss_clams():
   1.217 @@ -198,6 +256,14 @@
   1.218      content = get_content(filename)
   1.219      return render_template("/ozoneloss_clams.html", act="ozoneloss/clams", content=content)
   1.220  
   1.221 +
   1.222 +@app.route("/ozoneloss/uvmap")
   1.223 +def ozoneloss_uvmap():
   1.224 +    filename = os.path.join("templates", get_locale(), "rst", "ozoneloss_uvmap.rst")
   1.225 +    content = get_content(filename)
   1.226 +    return render_template("/ozoneloss_uvmap.html", act="ozoneloss/uvmap", content=content)
   1.227 +
   1.228 +
   1.229  @app.route("/ozoneloss/vpsc")
   1.230  def ozoneloss_vspc():
   1.231      filename = os.path.join("templates", get_locale(), "rst", "ozoneloss_vpsc.rst")
   1.232 @@ -215,10 +281,10 @@
   1.233      o3offset = 50.
   1.234      figname = "uvincr_lat%0.3i_do3%0.3i.svg" % (lat, o3offset)
   1.235  
   1.236 -
   1.237      return render_template('ozoneloss_uvi.html', act="ozoneloss/uvi", content=content, figname=figname,
   1.238                             alt=_(u"UV increase at {{lat}} degrees N for {{o3offset}} DU ozone depletion"))
   1.239  
   1.240 +
   1.241  @app.route("/ozoneloss/uvi_graph", methods=['POST'])
   1.242  def ozoneloss_uvi_graph():
   1.243      filename = os.path.join("templates", get_locale(), "rst", "ozoneloss_uvi.rst")
   1.244 @@ -236,17 +302,12 @@
   1.245          latstr2 = latstr2.replace(u'S', '')
   1.246          lat = -float(latstr2)
   1.247  
   1.248 -
   1.249      figname = "uvincr_lat%0.3i_do3%0.3i.svg" % (lat, float(o3offsetstr))
   1.250  
   1.251      return render_template('graph.html', act="ozoneloss_uvi_graph", content=content, figname=figname,
   1.252                             o3offsetstr=o3offsetstr, latstr=latstr)
   1.253  
   1.254  
   1.255 -
   1.256 -
   1.257 -
   1.258 -
   1.259  @app.route("/iek-7")
   1.260  def institute():
   1.261      filename = os.path.join("templates", get_locale(), "rst", "iek-7.rst")
   1.262 @@ -260,7 +321,7 @@
   1.263      if card is not None:
   1.264          qr_image = qr_image_data(card)
   1.265          author = render_template("/author_info.html", act="author", title=_(u"IEK-7"),
   1.266 -                                  card=card, image=qr_image, contact=_(u"Contact"), exit=_(u"Close"))
   1.267 +                                 card=card, image=qr_image, contact=_(u"Contact"), exit=_(u"Close"))
   1.268  
   1.269      return render_template("/iek-7.html", act="iek-7", content=content,
   1.270                             author=author, card=card, contact=u"IEK-7")
   1.271 @@ -273,14 +334,12 @@
   1.272      return render_template("/content.html", act="imprint", content=content)
   1.273  
   1.274  
   1.275 -
   1.276 -
   1.277 -
   1.278  @app.errorhandler(404)
   1.279  def page_not_found(e):
   1.280      msg = _(u"Url: %(url)s not found", url=request.url)
   1.281      info = _(u"This information is not available!")
   1.282      return render_template("404.html", msg=msg, info=info)
   1.283  
   1.284 +
   1.285  if __name__ == "__main__":
   1.286 -    app.run(host='localhost', port=5014)
   1.287 +    app.run(host='localhost', port=5014, debug=True)
Impressum Datenschutzerklärung