<commit_before># -*- coding: utf-8 -*-
u"""SRW execution template.

:copyright: Copyright (c) 2015 RadiaSoft LLC.  All Rights Reserved.
:license: http://www.apache.org/licenses/LICENSE-2.0.html
"""
from __future__ import absolute_import, division, print_function

import glob
import json
import math
import numpy as np
import os
import py.path
import re
import shutil
import sirepo.importer
import traceback
import zipfile

from pykern import pkio
from pykern.pkdebug import pkdc, pkdp
from pykern import pkjinja
from pykern import pkresource
from sirepo import simulation_db
from sirepo.template import template_common
import bnlcrl.pkcli.simulate
import uti_plot_com

WANT_BROWSER_FRAME_CACHE = False

_MULTI_ELECTRON_FILENAME_FOR_MODEL = {
    'fluxAnimation': 'res_spec_me.dat',
    'multiElectronAnimation': 'res_int_pr_me.dat',
}

_EXAMPLE_FOLDERS = {
    'Bending Magnet Radiation': '/SR Calculator',
    'Diffraction by an Aperture': '/Wavefront Propagation',
    'Ellipsoidal Undulator Example': '/Examples',
    'Focusing Bending Magnet Radiation': '/Examples',
    'Gaussian X-ray Beam Through Perfect CRL': '/Examples',
    'Gaussian X-ray beam through a Beamline containing Imperfect Mirrors': '/Examples',
    'Idealized Free Electron Laser Pulse': '/SR Calculator',
    'LCLS SXR beamline - Simplified': '/Light Source Facilities/LCLS',
    'LCLS SXR beamline': '/Light Source Facilities/LCLS',
    'NSLS-II CHX beamline': '/Light Source Facilities/NSLS-II',
    'Polarization of Bending Magnet Radiation': '/Examples',
    'Soft X-Ray Undulator Radiation Containing VLS Grating': '/Examples',
    'Tabulated Undulator Example': '/Examples',
    'Undulator Radiation': '/SR Calculator',
    'Young\'s Double Slit Experiment (green laser)': '/Wavefront Propagation',
    'Young\'s Double Slit Experiment (green laser, no lens)': '/Wavefront Propagation',
    'Young\'s Double Slit Experiment': '/Wavefront Propagation',
}

_PREDEFINED_MAGNETIC_ZIP_FILE = 'magnetic_measurements.zip'

#: Where server files and static files are found
_STATIC_FOLDER = py.path.local(pkresource.filename('static'))

with open(str(_STATIC_FOLDER.join('json/beams.json'))) as f:
    _PREDEFINED_BEAMS = json.load(f)

with open(str(_STATIC_FOLDER.join('json/mirrors.json'))) as f:
    _PREDEFINED_MIRRORS = json.load(f)

with open(str(_STATIC_FOLDER.join('json/srw-schema.json'))) as f:
    _SCHEMA = json.load(f)


def background_percent_complete(report, run_dir, is_running, schema):
    filename = str(run_dir.join(_MULTI_ELECTRON_FILENAME_FOR_MODEL[report]))
    if os.path.isfile(filename):
        data = simulation_db.read_json(run_dir.join(template_common.INPUT_BASE_NAME))
        return {
            'percent_complete': 100,
            'frame_count': 1,
            'total_frames': 1,
            'last_update_time': os.path.getmtime(filename),
            'start_time': data['models']['simulationStatus'][report]['startTime'] if report in data['models']['simulationStatus'] else None,
        }
    return {
        'percent_complete': 0,
        'frame_count': 0,
        'total_frames': 0,
    }


def copy_related_files(data, source_path, target_path):
    pass


def extract_report_data(filename, model_data):
    flux_type = 1
    if 'report' in model_data and model_data['report'] == 'fluxAnimation':
        flux_type = int(model_data['animationArgs'])
    elif 'models' in model_data:
        flux_type = model_data['models']['fluxReport']['fluxType']
    sValShort = 'Flux'; sValType = 'Flux through Finite Aperture'; sValUnit = 'ph/s/.1%bw'
    if flux_type == 2:
        sValShort = 'Intensity'
        sValUnit = 'ph/s/.1%bw/mm^2'
    is_gaussian = False
    if 'models' in model_data and model_data['models']['simulation']['sourceType'] == 'g':
        is_gaussian = True
    files_3d = ['res_pow.dat', 'res_int_se.dat', 'res_int_pr_se.dat', 'res_mirror.dat', 'res_int_pr_me.dat']
    if model_data['report'] == 'initialIntensityReport':
        before_propagation_name = 'Before Propagation (E={photonEnergy} eV)'
    else:
        before_propagation_name = 'E={photonEnergy} eV'
    file_info = {
        'res_trj.dat': [['Longitudinal Position', 'Position', 'Electron Trajectory'], ['m', 'm']],
        'res_spec_se.dat': [['Photon Energy', 'Intensity', 'On-Axis Spectrum from Filament Electron Beam'], ['eV', _intensity_units(is_gaussian, model_data)]],
        'res_spec_me.dat': [['Photon Energy', sValShort, sValType], ['eV', sValUnit]],
        'res_pow.dat': [['Horizontal Position', 'Vertical Position', 'Power Density', 'Power Density'], ['m', 'm', 'W/mm^2']],
        'res_int_se.dat': [['Horizontal Position', 'Vertical Position', before_propagation_name, 'Intensity'], ['m', 'm', _intensity_units(is_gaussian, model_data)]],
        #TODO(pjm): improve multi-electron label
        'res_int_pr_me.dat': [['Horizontal Position', 'Vertical Position', 'After Propagation', 'Intensity'], ['m', 'm', _intensity_units(is_gaussian, model_data)]],
        'res_int_pr_se.dat': [['Horizontal Position', 'Vertical Position', 'After Propagation (E={photonEnergy} eV)', 'Intensity'], ['m', 'm', _intensity_units(is_gaussian, model_data)]],
        'res_mirror.dat': [['Horizontal Position', 'Vertical Position', 'Optical Path Difference', 'Optical Path Difference'], ['m', 'm', 'm']],
    }

    if model_data['report'] == 'trajectoryReport':
        data, mode, allrange, arLabels, arUnits = uti_plot_com.file_load(
            filename,
            traj_report=True,
            traj_axis=model_data['models']['trajectoryReport']['plotAxis'],
        )
    else:
        data, mode, allrange, arLabels, arUnits = uti_plot_com.file_load(filename)
    filename = os.path.basename(filename)

    title = file_info[filename][0][2]
    if '{photonEnergy}' in title:
        title = title.format(photonEnergy=model_data['models']['simulation']['photonEnergy'])
    info = {
        'title': title,
        'x_range': [allrange[0], allrange[1]],
        'y_label': _superscript(file_info[filename][0][1] + ' [' + file_info[filename][1][1] + ']'),
        'x_label': file_info[filename][0][0] + ' [' + file_info[filename][1][0] + ']',
        'x_units': file_info[filename][1][0],
        'y_units': file_info[filename][1][1],
        'points': data,
    }
    if filename in files_3d:
        info = _remap_3d(info, allrange, file_info[filename][0][3], file_info[filename][1][2])
    return info<commit_msg>#183 - vertical axis labels are updated automatically when the axis of interest is changed.<commit_after># -*- coding: utf-8 -*-
u"""SRW execution template.

:copyright: Copyright (c) 2015 RadiaSoft LLC.  All Rights Reserved.
:license: http://www.apache.org/licenses/LICENSE-2.0.html
"""
from __future__ import absolute_import, division, print_function

import glob
import json
import math
import numpy as np
import os
import py.path
import re
import shutil
import sirepo.importer
import traceback
import zipfile

from pykern import pkio
from pykern.pkdebug import pkdc, pkdp
from pykern import pkjinja
from pykern import pkresource
from sirepo import simulation_db
from sirepo.template import template_common
import bnlcrl.pkcli.simulate
import uti_plot_com

WANT_BROWSER_FRAME_CACHE = False

_MULTI_ELECTRON_FILENAME_FOR_MODEL = {
    'fluxAnimation': 'res_spec_me.dat',
    'multiElectronAnimation': 'res_int_pr_me.dat',
}

_EXAMPLE_FOLDERS = {
    'Bending Magnet Radiation': '/SR Calculator',
    'Diffraction by an Aperture': '/Wavefront Propagation',
    'Ellipsoidal Undulator Example': '/Examples',
    'Focusing Bending Magnet Radiation': '/Examples',
    'Gaussian X-ray Beam Through Perfect CRL': '/Examples',
    'Gaussian X-ray beam through a Beamline containing Imperfect Mirrors': '/Examples',
    'Idealized Free Electron Laser Pulse': '/SR Calculator',
    'LCLS SXR beamline - Simplified': '/Light Source Facilities/LCLS',
    'LCLS SXR beamline': '/Light Source Facilities/LCLS',
    'NSLS-II CHX beamline': '/Light Source Facilities/NSLS-II',
    'Polarization of Bending Magnet Radiation': '/Examples',
    'Soft X-Ray Undulator Radiation Containing VLS Grating': '/Examples',
    'Tabulated Undulator Example': '/Examples',
    'Undulator Radiation': '/SR Calculator',
    'Young\'s Double Slit Experiment (green laser)': '/Wavefront Propagation',
    'Young\'s Double Slit Experiment (green laser, no lens)': '/Wavefront Propagation',
    'Young\'s Double Slit Experiment': '/Wavefront Propagation',
}

_PREDEFINED_MAGNETIC_ZIP_FILE = 'magnetic_measurements.zip'

#: Where server files and static files are found
_STATIC_FOLDER = py.path.local(pkresource.filename('static'))

with open(str(_STATIC_FOLDER.join('json/beams.json'))) as f:
    _PREDEFINED_BEAMS = json.load(f)

with open(str(_STATIC_FOLDER.join('json/mirrors.json'))) as f:
    _PREDEFINED_MIRRORS = json.load(f)

with open(str(_STATIC_FOLDER.join('json/srw-schema.json'))) as f:
    _SCHEMA = json.load(f)


def background_percent_complete(report, run_dir, is_running, schema):
    filename = str(run_dir.join(_MULTI_ELECTRON_FILENAME_FOR_MODEL[report]))
    if os.path.isfile(filename):
        data = simulation_db.read_json(run_dir.join(template_common.INPUT_BASE_NAME))
        return {
            'percent_complete': 100,
            'frame_count': 1,
            'total_frames': 1,
            'last_update_time': os.path.getmtime(filename),
            'start_time': data['models']['simulationStatus'][report]['startTime'] if report in data['models']['simulationStatus'] else None,
        }
    return {
        'percent_complete': 0,
        'frame_count': 0,
        'total_frames': 0,
    }


def copy_related_files(data, source_path, target_path):
    pass


def extract_report_data(filename, model_data):
    flux_type = 1
    if 'report' in model_data and model_data['report'] == 'fluxAnimation':
        flux_type = int(model_data['animationArgs'])
    elif 'models' in model_data:
        flux_type = model_data['models']['fluxReport']['fluxType']
    sValShort = 'Flux'; sValType = 'Flux through Finite Aperture'; sValUnit = 'ph/s/.1%bw'
    if flux_type == 2:
        sValShort = 'Intensity'
        sValUnit = 'ph/s/.1%bw/mm^2'
    is_gaussian = False
    if 'models' in model_data and model_data['models']['simulation']['sourceType'] == 'g':
        is_gaussian = True
    files_3d = ['res_pow.dat', 'res_int_se.dat', 'res_int_pr_se.dat', 'res_mirror.dat', 'res_int_pr_me.dat']
    if model_data['report'] == 'initialIntensityReport':
        before_propagation_name = 'Before Propagation (E={photonEnergy} eV)'
    else:
        before_propagation_name = 'E={photonEnergy} eV'
    file_info = {
        'res_trj.dat': [['Longitudinal Position', 'Position', 'Electron Trajectory'], ['m', 'm']],
        'res_spec_se.dat': [['Photon Energy', 'Intensity', 'On-Axis Spectrum from Filament Electron Beam'], ['eV', _intensity_units(is_gaussian, model_data)]],
        'res_spec_me.dat': [['Photon Energy', sValShort, sValType], ['eV', sValUnit]],
        'res_pow.dat': [['Horizontal Position', 'Vertical Position', 'Power Density', 'Power Density'], ['m', 'm', 'W/mm^2']],
        'res_int_se.dat': [['Horizontal Position', 'Vertical Position', before_propagation_name, 'Intensity'], ['m', 'm', _intensity_units(is_gaussian, model_data)]],
        #TODO(pjm): improve multi-electron label
        'res_int_pr_me.dat': [['Horizontal Position', 'Vertical Position', 'After Propagation', 'Intensity'], ['m', 'm', _intensity_units(is_gaussian, model_data)]],
        'res_int_pr_se.dat': [['Horizontal Position', 'Vertical Position', 'After Propagation (E={photonEnergy} eV)', 'Intensity'], ['m', 'm', _intensity_units(is_gaussian, model_data)]],
        'res_mirror.dat': [['Horizontal Position', 'Vertical Position', 'Optical Path Difference', 'Optical Path Difference'], ['m', 'm', 'm']],
    }

    if model_data['report'] == 'trajectoryReport':
        assert model_data['models']['trajectoryReport']['plotAxis'] in ['x', 'y']
        if model_data['models']['trajectoryReport']['plotAxis'] == 'x':
            axis_name = 'Horizontal'
        else:
            axis_name = 'Vertical'
        file_info['res_trj.dat'][0][1] = '{} {}'.format(axis_name, file_info['res_trj.dat'][0][1])
        data, mode, allrange, arLabels, arUnits = uti_plot_com.file_load(
            filename,
            traj_report=True,
            traj_axis=model_data['models']['trajectoryReport']['plotAxis'],
        )
    else:
        data, mode, allrange, arLabels, arUnits = uti_plot_com.file_load(filename)
    filename = os.path.basename(filename)

    title = file_info[filename][0][2]
    if '{photonEnergy}' in title:
        title = title.format(photonEnergy=model_data['models']['simulation']['photonEnergy'])
    info = {
        'title': title,
        'x_range': [allrange[0], allrange[1]],
        'y_label': _superscript(file_info[filename][0][1] + ' [' + file_info[filename][1][1] + ']'),
        'x_label': file_info[filename][0][0] + ' [' + file_info[filename][1][0] + ']',
        'x_units': file_info[filename][1][0],
        'y_units': file_info[filename][1][1],
        'points': data,
    }
    if filename in files_3d:
        info = _remap_3d(info, allrange, file_info[filename][0][3], file_info[filename][1][2])
    return info
{'fraction_non_alphanumeric': 0.14639113055283204, 'fraction_numerical': 0.007164944565955201, 'mean_word_length': 4.169590643274854, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1269784', 'n_tokens_mistral': 4506, 'n_tokens_neox': 4347, 'n_words': 992}