IR Formatter

by richardingham

HTML

<h1>Format IR Data</h1>

<form id="irform">
<table>
<tr>
<th>Baseline</th>
<td><input type="text" id="baseline" value="100"></td>
</tr>
<tr>
<th>Weak</th>
<td>0 to <input type="text" id="weak" value="40" size="16"></td>
</tr>
<tr>
<th>Strong</th>
<td>100 to <input type="text" id="strong" value="75" size="13"></td>
</tr>
<tr>
<th>Threshold</th>
<td><input type="text" id="threshold" value="5">%</td>
</tr>
<tr>
<th>Decimal Places</th>
<td><input type="text" id="dp" value="1"></td>
</tr>
<tr>
<th>Peak&nbsp;Data</th>
<td><textarea id="data" rows="10" cols="100"></textarea></td>
</tr>
<tr>
<th></th>
<td>
<input id="process" type="button" value="Reformat">
<input id="clear" type="reset" value="Clear">
</td>
</tr>
<tr id="resultsrow">
<th>Formatted</th>
<td id="results"></td>
</tr>
</table>
</form>

CSS

th { 
    text-align: right; 
    padding-right: 10px;
}

JavaScript

jQuery(function($) {
    $("#process").click(function(e) {
        var weak = $("#weak").val(),
            strong = $("#strong").val(),
            baseline = $("#baseline").val(),
            data = $("#data").val().replace(/\s+/gm, " ").replace(/\s+$/,'').split(" "),
            threshold = $("#threshold").val(),
            dp = $("#dp").val(),
            num = data.length,
            min = Math.min.apply(Math, data),
            processed = [],
            wl, percent, label;

        if (num == 0) return;

        for (var i = 0; i < num; i += 2) {
            wl = parseFloat(data[i]);
            percent = (baseline - data[i + 1]) * 100 / (baseline - min);

            label = "m";
            if (percent <= weak) label = "w";
            if (percent >= strong) label = "s";
            if (percent <= threshold) continue;

            processed.push(wl.toFixed(dp) + " (" + label + ")");
        }

        $("#results").html("IR (thin film) &#957; /cm<sup>-1</sup> = " + processed.join(", ") + ".");
        $("#resultsrow").show();
        e.preventDefault();
    });

    $("#clear").click(function() {
        $("#results").html("");
        $("#resultsrow").hide();
    });

    $("#resultsrow").hide();
});