text:
package org.apache.hadoop.hbase;

import java.nio.ByteBuffer;
import java.util.Comparator;
import org.apache.hadoop.hbase.util.ByteBufferUtils;
import org.apache.yetus.audience.InterfaceAudience;
import org.apache.yetus.audience.InterfaceStability;

/**
 * Comparator for comparing cells and has some specialized methods that allows comparing individual
 * cell components like row, family, qualifier and timestamp
 */
@InterfaceAudience.Public
@InterfaceStability.Evolving
public interface CellComparator extends Comparator<Cell> {
  /**
   * A comparator for ordering cells in user-space tables. Useful when writing cells in sorted
   * order as necessary for bulk import (i.e. via MapReduce).
   * <p>
   * CAUTION: This comparator may provide inaccurate ordering for cells from system tables,
   * and should not be relied upon in that case.
   */
  // For internal use, see CellComparatorImpl utility methods.
  static CellComparator getInstance() {
    return CellComparatorImpl.COMPARATOR;
  }

  /**
   * Lexographically compares two cells. The key part of the cell is taken for comparison which
   * includes row, family, qualifier, timestamp and type
   * @param leftCell the left hand side cell
   * @param rightCell the right hand side cell
   * @return greater than 0 if leftCell is bigger, less than 0 if rightCell is bigger, 0 if both
   *         cells are equal
   */
  @Override
  int compare(Cell leftCell, Cell rightCell);

  /**
   * Compare cells.
   * @param ignoreSequenceid True if we are to compare the key portion only and ignore
   *    the sequenceid. Set to false to compare key and consider sequenceid.
   * @return 0 if equal, -1 if a < b, and +1 if a > b.
   */
  int compare(Cell leftCell, Cell rightCell, boolean ignoreSequenceid);

  /**
   * Lexographically compares the rows of two cells.
   * @param leftCell the left hand side cell
   * @param rightCell the right hand side cell
   * @return greater than 0 if leftCell is bigger, less than 0 if rightCell is bigger, 0 if both
   *         cells are equal
   */
  int compareRows(Cell leftCell, Cell rightCell);

  /**
   * Compares the row part of the cell with a simple plain byte[] like the
   * stopRow in Scan.
   * @param cell the cell
   * @param bytes the byte[] representing the row to be compared with
   * @param offset the offset of the byte[]
   * @param length the length of the byte[]
   * @return greater than 0 if leftCell is bigger, less than 0 if rightCell is bigger, 0 if both
   *         cells are equal
   */
  int compareRows(Cell cell, byte[] bytes, int offset, int length);

  /**
   * @param row ByteBuffer that wraps a row; will read from current position and will reading all
   *            remaining; will not disturb the ByteBuffer internal state.
   * @return greater than 0 if leftCell is bigger, less than 0 if rightCell is bigger, 0 if both
   *         cells are equal
   */
  default int compareRows(ByteBuffer row, Cell cell) {
    if (cell instanceof ByteBufferExtendedCell) {
      return ByteBufferUtils.compareTo(row, row.position(), row.remaining(),
        ((ByteBufferExtendedCell) cell).getRowByteBuffer(),
        ((ByteBufferExtendedCell) cell).getRowPosition(),
        cell.getRowLength());
    }
    return ByteBufferUtils.compareTo(row, row.position(), row.remaining(),
      cell.getRowArray(), cell.getRowOffset(),
      cell.getRowLength());
  }

  /**
   * Lexographically compares the two cells excluding the row part. It compares family, qualifier,
   * timestamp and the type
   * @param leftCell the left hand side cell
   * @param rightCell the right hand side cell
   * @return greater than 0 if leftCell is bigger, less than 0 if rightCell is bigger, 0 if both
   *         cells are equal
   */
  int compareWithoutRow(Cell leftCell, Cell rightCell);

  /**
   * Lexographically compares the families of the two cells
   * @param leftCell the left hand side cell
   * @param rightCell the right hand side cell
   * @return greater than 0 if leftCell is bigger, less than 0 if rightCell is bigger, 0 if both
   *         cells are equal
   */
  int compareFamilies(Cell leftCell, Cell rightCell);

  /**
   * Lexographically compares the qualifiers of the two cells
   * @param leftCell the left hand side cell
   * @param rightCell the right hand side cell
   * @return greater than 0 if leftCell is bigger, less than 0 if rightCell is bigger, 0 if both
   *         cells are equal
   */
  int compareQualifiers(Cell leftCell, Cell rightCell);

  /**
   * Compares cell's timestamps in DESCENDING order. The below older timestamps sorting ahead of
   * newer timestamps looks wrong but it is intentional. This way, newer timestamps are first found
   * when we iterate over a memstore and newer versions are the first we trip over when reading from
   * a store file.
   * @param leftCell the left hand side cell
   * @param rightCell the right hand side cell
   * @return 1 if left's timestamp < right's timestamp -1 if left's timestamp > right's
   *         timestamp 0 if both timestamps are equal
   */
  int compareTimestamps(Cell leftCell, Cell rightCell);

  /**
   * Compares cell's timestamps in DESCENDING order. The below older timestamps sorting ahead of
   * newer timestamps looks wrong but it is intentional. This way, newer timestamps are first found
   * when we iterate over a memstore and newer versions are the first we trip over when reading from
   * a store file.
   * @param leftCellts the left cell's timestamp
   * @param rightCellts the right cell's timestamp
   * @return 1 if left's timestamp < right's timestamp -1 if left's timestamp > right's
   *         timestamp 0 if both timestamps are equal
   */
  int compareTimestamps(long leftCellts, long rightCellts);

  /**
   * @return A dumbed-down, fast comparator for hbase2 base-type, the {@link ByteBufferKeyValue}.
   *   Create an instance when you make a new memstore, when you know only BBKVs will be passed.
   *   Do not pollute with types other than BBKV if can be helped; the Comparator will slow.
   */
  Comparator getSimpleComparator();
}


text:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JARVIS4
{
    public static class JARVISMultiTasking
    {
        public static void ExecuteFunction(Func<string, StatusObject> function_prototype, string function_arguments)
        {
            try
            {
                function_prototype.Invoke(function_arguments);
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
        public static StatusObject AddThread(string thread_name)
        {
            StatusObject SO = new StatusObject();
            try
            {

            }
            catch(Exception e)
            {

            }
            return SO;
        }
    }
}

redpajama_set_name: RedPajamaGithub


text:
module Spec
  module Runner
    module Formatter
      class RdocFormatter < BaseTextFormatter
        def add_behaviour(name)
          @output.puts "# #{name}"
        end

        def example_passed(example)
          @output.puts "# * #{example.description}"
          @output.flush
        end

        def example_failed(example, counter, failure)
          @output.puts "# * #{example.description} [#{counter} - FAILED]"
        end

        def example_pending(behaviour_name, example_name, message)
          @output.puts "# * #{behaviour_name} #{example_name} [PENDING: #{message}]"
        end
      end
    end
  end
end


text:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Rope
{
    public static class CharRope
    {
        /// <summary>
        /// Creates a new rope from the specified text.
        /// </summary>
        public static Rope<char> Create(string text)
        {
            if (text == null)
                throw new ArgumentNullException("text");
            return new Rope<char>(InitFromString(text));
        }

        /// <summary>
        /// Retrieves the text for a portion of the rope.
        /// Runs in O(lg N + M), where M=<paramref name="length"/>.
        /// </summary>
        /// <exception cref="ArgumentOutOfRangeException">offset or length is outside the valid range.</exception>
        /// <remarks>
        /// This method counts as a read access and may be called concurrently to other read accesses.
        /// </remarks>
        public static string ToString(this Rope<char> rope, int startIndex, int length)
        {
            if (rope == null)
                throw new ArgumentNullException("rope");
#if DEBUG
            if (length < 0)
                throw new ArgumentOutOfRangeException("length", length, "Value must be >= 0");
#endif
            if (length == 0)
                return string.Empty;
            char[] buffer = new char[length];
            rope.CopyTo(startIndex, buffer, 0, length);
            return new string(buffer);
        }

        /// <summary>
        /// Retrieves the text for a portion of the rope and writes it to the specified text writer.
        /// Runs in O(lg N + M), where M=<paramref name="length"/>.
        /// </summary>
        /// <exception cref="ArgumentOutOfRangeException">offset or length is outside the valid range.</exception>
        /// <remarks>
        /// This method counts as a read access and may be called concurrently to other read accesses.
        /// </remarks>
        public static void WriteTo(this Rope<char> rope, TextWriter output, int startIndex, int length)
        {
            if (rope == null)
                throw new ArgumentNullException("rope");
            if (output == null)
                throw new ArgumentNullException("output");
            rope.VerifyRange(startIndex, length);
            rope.root.WriteTo(startIndex, output, length);
        }

        /// <summary>
        /// Appends text to this rope.
        /// Runs in O(lg N + M).
        /// </summary>
        /// <exception cref="ArgumentNullException">newElements is null.</exception>
        public static void AddText(this Rope<char> rope, string text)
        {
            InsertText(rope, rope.Length, text);
        }

        /// <summary>
        /// Inserts text into this rope.
        /// Runs in O(lg N + M).
        /// </summary>
        /// <exception cref="ArgumentNullException">newElements is null.</exception>
        /// <exception cref="ArgumentOutOfRangeException">index or length is outside the valid range.</exception>
        public static void InsertText(this Rope<char> rope, int index, string text)
        {
            if (rope == null)
                throw new ArgumentNullException("rope");
            rope.InsertRange(index, text.ToCharArray(), 0, text.Length);
            /*if (index < 0 || index > rope.Length) {
				throw new ArgumentOutOfRangeException("index", index, "0 <= index <= " + rope.Length.ToString(CultureInfo.InvariantCulture));
			}
			if (text == null)
				throw new ArgumentNullException("text");
			if (text.Length == 0)
				return;
			rope.root = rope.root.Insert(index, text);
			rope.OnChanged();*/
        }

        internal static RopeNode<char> InitFromString(string text)
        {
            if (text.Length == 0)
            {
                return RopeNode<char>.emptyRopeNode;
            }
            RopeNode<char> node = RopeNode<char>.CreateNodes(text.Length);
            FillNode(node, text, 0);
            return node;
        }

        static void FillNode(RopeNode<char> node, string text, int start)
        {
            if (node.contents != null)
            {
                text.CopyTo(start, node.contents, 0, node.length);
            }
            else
            {
                FillNode(node.left, text, start);
                FillNode(node.right, text, start + node.left.length);
            }
        }

        internal static void WriteTo(this RopeNode<char> node, int index, TextWriter output, int count)
        {
            if (node.height == 0)
            {
                if (node.contents == null)
                {
                    // function node
                    node.GetContentNode().WriteTo(index, output, count);
                }
                else
                {
                    // leaf node: append data
                    output.Write(node.contents, index, count);
                }
            }
            else
            {
                // concat node: do recursive calls
                if (index + count <= node.left.length)
                {
                    node.left.WriteTo(index, output, count);
                }
                else if (index >= node.left.length)
                {
                    node.right.WriteTo(index - node.left.length, output, count);
                }
                else
                {
                    int amountInLeft = node.left.length - index;
                    node.left.WriteTo(index, output, amountInLeft);
                    node.right.WriteTo(0, output, count - amountInLeft);
                }
            }
        }

        /// <summary>
        /// Gets the index of the first occurrence of any element in the specified array.
        /// </summary>
        /// <param name="rope">The target rope.</param>
        /// <param name="anyOf">Array of characters being searched.</param>
        /// <param name="startIndex">Start index of the search.</param>
        /// <param name="length">Length of the area to search.</param>
        /// <returns>The first index where any character was found; or -1 if no occurrence was found.</returns>
        public static int IndexOfAny(this Rope<char> rope, char[] anyOf, int startIndex, int length)
        {
            if (rope == null)
                throw new ArgumentNullException("rope");
            if (anyOf == null)
                throw new ArgumentNullException("anyOf");
            rope.VerifyRange(startIndex, length);

            while (length > 0)
            {
                var entry = rope.FindNodeUsingCache(startIndex).PeekOrDefault();
                char[] contents = entry.node.contents;
                int startWithinNode = startIndex - entry.nodeStartIndex;
                int nodeLength = Math.Min(entry.node.length, startWithinNode + length);
                for (int i = startIndex - entry.nodeStartIndex; i < nodeLength; i++)
                {
                    char element = contents[i];
                    foreach (char needle in anyOf)
                    {
                        if (element == needle)
                            return entry.nodeStartIndex + i;
                    }
                }
                length -= nodeLength - startWithinNode;
                startIndex = entry.nodeStartIndex + nodeLength;
            }
            return -1;
        }

        /// <summary>
        /// Gets the index of the first occurrence of the search text.
        /// </summary>
        public static int IndexOf(this Rope<char> rope, string searchText, int startIndex, int length, StringComparison comparisonType)
        {
            if (rope == null)
                throw new ArgumentNullException("rope");
            if (searchText == null)
                throw new ArgumentNullException("searchText");
            rope.VerifyRange(startIndex, length);
            int pos = rope.ToString(startIndex, length).IndexOf(searchText, comparisonType);
            if (pos < 0)
                return -1;
            else
                return pos + startIndex;
        }

        /// <summary>
        /// Gets the index of the last occurrence of the search text.
        /// </summary>
        public static int LastIndexOf(this Rope<char> rope, string searchText, int startIndex, int length, StringComparison comparisonType)
        {
            if (rope == null)
                throw new ArgumentNullException("rope");
            if (searchText == null)
                throw new ArgumentNullException("searchText");
            rope.VerifyRange(startIndex, length);
            int pos = rope.ToString(startIndex, length).LastIndexOf(searchText, comparisonType);
            if (pos < 0)
                return -1;
            else
                return pos + startIndex;
        }
    }
}


text:
import {Component} from "angular2/core";
import {RouteConfig, ROUTER_DIRECTIVES} from "angular2/router";
import {SocketService} from "./services/socket.service";
import {InitiativeControlComponent} from "./components/initiative-control/initiative-control.component";
import {InitiativeService} from "./services/initiative.service";
import {DisplayInitiativeComponent} from "./components/display-initiative/display-initiative.component";

/**
 * Main app component for the character sheet app.
 */
@Component({
  selector: 'body[dnd-display], dnd-display',
  templateUrl: 'app/app.component.html',
  directives: [
    ROUTER_DIRECTIVES,
  ],
  providers: [
    SocketService,
    InitiativeService
  ]
})
@RouteConfig([
  {path: '/',        name: 'Display', component: DisplayInitiativeComponent, useAsDefault: true},
  {path: '/control', name: 'Control', component: InitiativeControlComponent},
])
export class AppComponent {
  constructor(
  ) {
  }
}


text:
<?xml version="1.0" encoding="UTF-8"?>
<site>
   <description name="Domino REST Servlet UpdateSite">
      (c) 2017 Sven Hasselbach
http://www.hasselba.ch
   </description>
   <feature url="features/dominorestservlet.feature_1.0.0.201702281305.jar" id="dominorestservlet.feature" version="1.0.0.201702281305">
      <category name="Domino REST Servlet"/>
   </feature>
   <category-def name="Domino REST Servlet" label="Domino REST Servlet"/>
</site>


text:
package rendezvous.federator.executor.cache.impl;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.apache.log4j.Logger;

import rendezvous.federator.core.Entity;
import rendezvous.federator.core.Field;
import rendezvous.federator.core.Value;
import rendezvous.federator.executor.cache.Cache;
import rendezvous.federator.executor.cache.CachedValue;

public class BloomFilterImpl implements Cache {

	private final static int maxSize = 10000;
	private static long currentSize = 0;
	private static double evictionLevel = 0.999;
	private static double cleaningLevel = 0.9;

	//private static BloomFilter<Field>  bf = new FilterBuilder(maxSize, 0.1).buildBloomFilter();
	//private static BloomFilter<Entity>  bfEntities = new FilterBuilder(maxSize, 0.1).buildBloomFilter();
	private static Map<Field,CachedValue> values = new HashMap<Field,CachedValue>();

	private final static Logger logger = Logger.getLogger(BloomFilterImpl.class);

	@Override
	public Set<Value> get(Entity entity) {

		Set<Value> values = new HashSet<Value>();

		for (Value value : entity.getValues()) {
			values.add(this.get(value.getField()));

			logger.debug("Getting value from cache "+value.getField().getFieldName());
		}

		return values;
	}

	@Override
	public Value get(Field field) {

		/*
		if(bf.contains(field)){
			return values.get(field).getValue();
		}*/

		return null;
	}

	@Override
	public void add(Entity entity) {

		logger.info("Adding entity <"+entity.getName()+"> to cache ");
		/*
		if(entity.isComplete()) bfEntities.add(entity);

		for (Value value : entity.getValues()) {
			add(value);
		}*/
	}

	private void evict() {

		logger.info("Starting eviction");
		/*
		BloomFilter<Field>  newBF = new FilterBuilder(maxSize, 0.1).buildBloomFilter();
		Map<Field,CachedValue> newValues = new HashMap<Field,CachedValue>();

		Long newCurrentSize = (long) 0;
		for(CachedValue value : values.values()){

			if(value.getCachedSequence()>(currentSize-(maxSize*cleaningLevel))){
				newBF.add(value.getValue().getField());
				newValues.put(value.getValue().getField(), value);
				newCurrentSize++;
			}
		}

		bf=newBF;
		values=newValues;
		currentSize=newCurrentSize;

		System.gc();
		logger.info("Finished eviction");
		*/
	}

	@Override
	public void add(Value value) {

		if (currentSize > Math.round(maxSize*evictionLevel)) evict();

		Long cachedSequence = (long) values.size()+1;

		CachedValue cachedValue = new CachedValue(value, cachedSequence);
		/*
		bf.add(value.getField());
		values.put(value.getField(), cachedValue);

		currentSize++;
		logger.info("Current size "+ currentSize);
		*/
	}

	@Override
	public void evictAll() {
		//bf.clear();
	}

	@Override
	public boolean contains(Entity entity) {
		/*
		if(entity.isComplete() && bfEntities.contains(entity)){
			return true;
		}
		*/
		return false;
	}
}


text:
namespace _01.Person
{
    using System;

    public class Child : Person
    {
        public Child(string name, int age)
            : base(name, age)
        {
        }

        public override int Age
        {
            get => base.Age;

            protected set
            {
                if (value > 15)
                {
                    throw new ArgumentException("Child's age must be less than 15!");
                }

                base.Age = value;
            }
        }
    }
}


text:
"""
===========================================
Reconstruct with Diffusion Spectrum Imaging
===========================================

We show how to apply Diffusion Spectrum Imaging (Wedeen et al. Science 2012) to
diffusion MRI datasets of Cartesian keyhole diffusion gradients.

First import the necessary modules:
"""

import nibabel as nib
from dipy.data import fetch_taiwan_ntu_dsi, read_taiwan_ntu_dsi, get_sphere
from dipy.align.aniso2iso import resample
from dipy.reconst.dsi import DiffusionSpectrumModel

"""
Download and read the data for this tutorial.
"""

fetch_taiwan_ntu_dsi()
img, gtab = read_taiwan_ntu_dsi()

"""
img contains a nibabel Nifti1Image object (data) and gtab contains a GradientTable
object (gradient information e.g. b-values). For example to read the b-values
it is possible to write print(gtab.bvals).

Load the raw diffusion data and the affine.
"""

data = img.get_data()
print('data.shape (%d, %d, %d, %d)' % data.shape)

"""
data.shape ``(96, 96, 60, 203)``

This dataset has anisotropic voxel sizes, therefore reslicing is necessary.
"""

affine = img.get_affine()

"""
Read the voxel size from the image header.
"""

voxel_size = img.get_header().get_zooms()[:3]

"""
Instantiate the Model and apply it to the data.
"""

dsmodel = DiffusionSpectrumModel(gtab)

"""
Lets just use one slice only from the data.
"""

dataslice = data[:, :, data.shape[2] / 2]

dsfit = dsmodel.fit(dataslice)

"""
Load an odf reconstruction sphere
"""

sphere = get_sphere('symmetric724')

"""
Calculate the ODFs with this specific sphere
"""

ODF = dsfit.odf(sphere)

print('ODF.shape (%d, %d, %d)' % ODF.shape)

"""
ODF.shape ``(96, 96, 724)``

In a similar fashion it is possible to calculate the PDFs of all voxels
in one call with the following way
"""

PDF = dsfit.pdf()

print('PDF.shape (%d, %d, %d, %d, %d)' % PDF.shape)

"""
PDF.shape ``(96, 96, 17, 17, 17)``

We see that even for a single slice this PDF array is close to 345 MBytes so we
really have to be careful with memory usage when use this function with a full
dataset.

The simple solution is to generate/analyze the ODFs/PDFs by iterating through
each voxel and not store them in memory if that is not necessary.
"""

from dipy.core.ndindex import ndindex

for index in ndindex(dataslice.shape[:2]):
    pdf = dsmodel.fit(dataslice[index]).pdf()

"""
If you really want to save the PDFs of a full dataset on the disc we recommend
using memory maps (numpy.memmap) but still have in mind that if you do that for
example for a dataset of volume size ``(96, 96, 60)`` you will need about 20
which can take less space when reasonable spheres (with < 1000 vertices) are.

.. include:: ../links_names.inc

"""


text:
from sympy.core import S, symbols
from sympy.matrices import eye, Matrix, ShapeError
from sympy.matrices.expressions import (
    Identity, MatrixExpr, MatrixSymbol, Determinant,
    det, ZeroMatrix, Transpose
)
from sympy.utilities.pytest import raises
from sympy import refine, Q

n = symbols('n', integer=True)
A = MatrixSymbol('A', n, n)
B = MatrixSymbol('B', n, n)
C = MatrixSymbol('C', 3, 4)

def test_det():
    assert isinstance(Determinant(A), Determinant)
    assert not isinstance(Determinant(A), MatrixExpr)
    raises(ShapeError, lambda: Determinant(C))
    assert det(eye(3)) == 1
    assert det(Matrix(3, 3, [1, 3, 2, 4, 1, 3, 2, 5, 2])) == 17
    A / det(A)  # Make sure this is possible

    raises(TypeError, lambda: Determinant(S.One))

    assert Determinant(A).arg is A

def test_eval_determinant():
    assert det(Identity(n)) == 1
    assert det(ZeroMatrix(n, n)) == 0
    assert det(Transpose(A)) == det(A)

def test_refine():
    assert refine(det(A), Q.orthogonal(A)) == 1
    assert refine(det(A), Q.singular(A)) == 0


text:
include ../include/compiler.inc

NAME = vec_mat_multi

all: F_serial F_OMP

include ../include/exe_options.inc


text:
1. Prompt means to ask the user for input.
2. The `>>` or "get from" operator reads a value into a variable.
3. After prompting the user for an integer value you can:
```c++
int number;
cin >> number;
```
4. \n is called the "newline" character and it is like typing in a carriage return.
5. An input is terminated into a string by white space.
6. An integer is also terminated by white space.
7. Like this: `cout << "Hello, " << first_name << "!\n";`
8. An object is some memory that holds a value of a specific type.
9. A literal string is delimited by double quotes. A variable may refer to, or contain, a literal string. `string string_variable = "Literal string";`
10. Strings and chars?
11. A variable is a named object.
12. A char is typically a single byte, an int is 4 bytes and a double is 8 bytes.
13. We use bytes which are 8 bits.
14. The `=` is an assignment operator and the `==` is used to compare.
15. A definition is a statement that introduces a new name into a program and sets aside memory for a variable.
16. An initialization states the type of a variable, gives it a name, and may fill it with a value. Assignment, on the other hand, is used to fill an initialized variable with a new value.
17. String concatenation is the joining of two strings where two strings `"Howdy"` and `"partner"` are combined with the `+` operator. The resulting string becomes `"Howdypartner"`. Concatenation will combine the strings as is, it will not add whitespace between the joined strings so that must be planned for. The strings may be literal strings or the contents of a string type variable.
18. Legal variable names: They're all suspect for the following reasons.
```c++
This_little_pig     // Leading capital reserved for defined types only
This_1_is fine      // Spaces are no good
2_For_1_special     // Leading numbers are bad 'mkay?
latest thing        // Sneaky space
the_$12_method      // $ is not a valid name char
_this_is_ok         // Leading underscores are reserved for system entities
MiniMineMine        // Tough to read, potentially confusing
number              // actually this is apparently ok..
correct?            // Question mark ees no good, senior
```
19. Examples of legal names that may cause confusion are: origin0, little1, variations on a name such as call, call1, CallAll.
20. Meaningful names are good choices, names that indicate what type the value might be, and its potential use.
21. Type safety is using objects only according to the rules for their type. By not being type-safe we may produce unpredictable values and results.
22. Conversion from a double to an int may cause overflow of the int for large numbers, or loss of accuracy due to truncation of any decimal portions.
23. We can convert types to larger memory types but must be careful when down converting to a smaller memory type. We must avoid narrowing.


text:
define(['shared/gw_common'], function (GW) {
    return {
        visible: function(params) { return true; },
        describe: function(params) {
            return '!LOC:Naval Ammunition Tech increases the damage of all naval vessels by 25%';
        },
        summarize: function(params) {
            return '!LOC:Naval Ammunition Tech';
        },
        icon: function(params) {
            return 'coui://ui/main/game/galactic_war/gw_play/img/tech/gwc_naval.png';
        },
        audio: function (parms) {
            return {
                found: '/VO/Computer/gw/board_tech_available_ammunition'
            }
        },
        getContext: function (galaxy) {
            return {
                totalSize: galaxy.stars().length
            };
        },
        deal: function (system, context, inventory) {
            var chance = 0;
            var dist = system.distance();
            chance = 35;

            return { chance: chance };
        },
        buff: function(inventory, params) {
            var units = [
                '/pa/units/sea/frigate/frigate_ammo_shell.json',
                '/pa/units/sea/frigate/frigate_ammo_aa.json',
                '/pa/units/sea/destroyer/destroyer_ammo.json',
                '/pa/units/sea/destroyer/destroyer_torpedo_ammo.json',
                '/pa/units/sea/sea_scout/sea_scout_ammo.json',
                '/pa/units/sea/attack_sub/attack_sub_ammo.json',
                '/pa/units/sea/missile_ship/missile_ship_aa_ammo.json',
                '/pa/units/sea/missile_ship/missile_ship_ammo.json',
                '/pa/units/sea/battleship/battleship_ammo.json',
                '/pa/units/sea/nuclear_sub/nuclear_sub_ammo.json',
                '/pa/units/sea/nuclear_sub/nuclear_sub_ammo_missile.json',
                '/pa/units/sea/battleship/battleship_ammo.json',
                '/pa/units/sea/hover_ship/hover_ship_ammo.json',
                '/pa/units/sea/hover_ship/hover_ship_ammo_side.json',
                '/pa/units/sea/drone_carrier/drone/drone_ammo.json'
            ];
            var mods = [];
            var modUnit = function(unit) {
                mods.push({
                    file: unit,
                    path: 'damage',
                    op: 'multiply',
                    value: 1.25
                });
            };
            _.forEach(units, modUnit);
            inventory.addMods(mods);
        },
        dull: function(inventory) {
        }
    };
});


text:
package org.apache.commons.collections4.set;

import java.util.Comparator;
import java.util.SortedSet;
import org.apache.commons.collections4.Transformer;
import org.apache.commons.collections4.set.TransformedSet;

public class TransformedSortedSet<E> extends TransformedSet<E> implements SortedSet<E>
{
    protected TransformedSortedSet() {}
    protected SortedSet<E> getSortedSet(){ return null; }
    protected TransformedSortedSet(SortedSet<E> p0, Transformer<? super E, ? extends E> p1){}
    public Comparator<? super E> comparator(){ return null; }
    public E first(){ return null; }
    public E last(){ return null; }
    public SortedSet<E> headSet(E p0){ return null; }
    public SortedSet<E> subSet(E p0, E p1){ return null; }
    public SortedSet<E> tailSet(E p0){ return null; }
    public static <E> TransformedSortedSet<E> transformedSortedSet(SortedSet<E> p0, Transformer<? super E, ? extends E> p1){ return null; }
    public static <E> TransformedSortedSet<E> transformingSortedSet(SortedSet<E> p0, Transformer<? super E, ? extends E> p1){ return null; }
}


text:
'use strict';

import { core, object, Disposable } from 'metal';
import dataAttributes from './dataAttributes';
import globals from '../globals/globals';
import App from './App';
import HtmlScreen from '../screen/HtmlScreen';
import Route from '../route/Route';

class AppDataAttributeHandler extends Disposable {

	/**
	 * Initilizes App, register surfaces and routes from data attributes.
	 * @constructor
	 */
	constructor() {
		super();

		/**
		 * Holds the app reference initialized by data attributes.
		 * @type {App}
		 * @default null
		 */
		this.app = null;

		/**
		 * Holds the base element to search initialization data attributes. This
		 * element is the container used to enable initialization based on the
		 * presence of `data-senna` attribute.
		 * @type {Element}
		 * @default null
		 */
		this.baseElement = null;
	}

	/**
	 * Inits application based on information scanned from document.
	 */
	handle() {
		if (!core.isElement(this.baseElement)) {
			throw new Error('Senna data attribute handler base element ' +
				'not set or invalid, try setting a valid element that ' +
				'contains a `data-senna` attribute.');
		}

		if (!this.baseElement.hasAttribute(dataAttributes.senna)) {
			console.log('Senna was not initialized from data attributes. ' +
				'In order to enable its usage from data attributes try setting ' +
				'in the base element, e.g. `<body data-senna>`.');
			return;
		}

		if (this.app) {
			throw new Error('Senna app was already initialized.');
		}

		console.log('Senna initialized from data attribute.');

		this.app = new App();
		this.maybeAddRoutes_();
		this.maybeAddSurfaces_();
		this.maybeSetBasePath_();
		this.maybeSetLinkSelector_();
		this.maybeSetLoadingCssClass_();
		this.maybeSetUpdateScrollPosition_();
		this.maybeDispatch_();
	}

	/**
	 * Disposes of this instance's object references.
	 * @override
	 */
	disposeInternal() {
		if (this.app) {
			this.app.dispose();
		}
	}

	/**
	 * Gets the app reference.
	 * @return {App}
	 */
	getApp() {
		return this.app;
	}

	/**
	 * Gets the base element.
	 * @return {Element} baseElement
	 */
	getBaseElement() {
		return this.baseElement;
	}

	/**
	 * Maybe adds app routes from link elements that are `senna-route`.
	 */
	maybeAddRoutes_() {
		var routesSelector = 'link[rel="senna-route"]';
		this.querySelectorAllAsArray_(routesSelector).forEach((link) => this.maybeParseLinkRoute_(link));
		if (!this.app.hasRoutes()) {
			this.app.addRoutes(new Route(/.*/, HtmlScreen));
			console.log('Senna can\'t find route elements, adding default.');
		}
	}

	/**
	 * Maybe adds app surfaces by scanning `data-senna-surface` data attribute.
	 */
	maybeAddSurfaces_() {
		var surfacesSelector = '[' + dataAttributes.surface + ']';
		this.querySelectorAllAsArray_(surfacesSelector).forEach((surfaceElement) => {
			this.updateElementIdIfSpecialSurface_(surfaceElement);
			this.app.addSurfaces(surfaceElement.id);
		});
	}

	/**
	 * Dispatches app navigation to the current path when initializes.
	 */
	maybeDispatch_() {
		if (this.baseElement.hasAttribute(dataAttributes.dispatch)) {
			this.app.dispatch();
		}
	}

	/**
	 * Adds app route by parsing valid link elements. A valid link element is of
	 * the kind `rel="senna-route"`.
	 * @param {Element} link
	 */
	maybeParseLinkRoute_(link) {
		var route = new Route(this.maybeParseLinkRoutePath_(link), this.maybeParseLinkRouteHandler_(link));
		this.app.addRoutes(route);
		console.log('Senna scanned route ' + route.getPath());
	}

	/**
	 * Maybe parse link route handler.
	 * @param {Element} link
	 * @return {?string}
	 */
	maybeParseLinkRouteHandler_(link) {
		var handler = link.getAttribute('type');
		if (core.isDefAndNotNull(handler)) {
			handler = object.getObjectByName(handler);
		}
		return handler;
	}

	/**
	 * Maybe parse link route path.
	 * @param {Element} link
	 * @return {?string}
	 */
	maybeParseLinkRoutePath_(link) {
		var path = link.getAttribute('href');
		if (core.isDefAndNotNull(path)) {
			if (path.indexOf('regex:') === 0) {
				path = new RegExp(path.substring(6));
			}
		}
		return path;
	}

	/**
	 * Maybe sets app base path from `data-senna-base-path` data attribute.
	 */
	maybeSetBasePath_() {
		var basePath = this.baseElement.getAttribute(dataAttributes.basePath);
		if (core.isDefAndNotNull(basePath)) {
			this.app.setBasePath(basePath);
			console.log('Senna scanned base path ' + basePath);
		}
	}

	/**
	 * Maybe sets app link selector from `data-senna-link-selector` data
	 * attribute.
	 */
	maybeSetLinkSelector_() {
		var linkSelector = this.baseElement.getAttribute(dataAttributes.linkSelector);
		if (core.isDefAndNotNull(linkSelector)) {
			this.app.setLinkSelector(linkSelector);
			console.log('Senna scanned link selector ' + linkSelector);
		}
	}

	/**
	 * Maybe sets app link loading css class from `data-senna-loading-css-class`
	 * data attribute.
	 */
	maybeSetLoadingCssClass_() {
		var loadingCssClass = this.baseElement.getAttribute(dataAttributes.loadingCssClass);
		if (core.isDefAndNotNull(loadingCssClass)) {
			this.app.setLoadingCssClass(loadingCssClass);
			console.log('Senna scanned loading css class ' + loadingCssClass);
		}
	}

	/**
	 * Maybe sets app update scroll position from
	 * `data-senna-update-scroll-position` data attribute.
	 */
	maybeSetUpdateScrollPosition_() {
		var updateScrollPosition = this.baseElement.getAttribute(dataAttributes.updateScrollPosition);
		if (core.isDefAndNotNull(updateScrollPosition)) {
			if (updateScrollPosition === 'false') {
				this.app.setUpdateScrollPosition(false);
			} else {
				this.app.setUpdateScrollPosition(true);
			}
			console.log('Senna scanned update scroll position ' + updateScrollPosition);
		}
	}

	/**
	 * Queries elements from document and returns an array of elements.
	 * @param {!string} selector
	 * @return {array.<Element>}
	 */
	querySelectorAllAsArray_(selector) {
		return Array.prototype.slice.call(globals.document.querySelectorAll(selector));
	}

	/**
	 * Updates element id if handled as special surface element. Some surfaces
	 * are slightly different from others, like when threating body
	 * as surface.
	 * @param {Element} element
	 */
	updateElementIdIfSpecialSurface_(element) {
		if (!element.id && element === globals.document.body) {
			element.id = 'senna_surface_' + core.getUid();
		}
	}

	/**
	 * Sets the base element.
	 * @param {Element} baseElement
	 */
	setBaseElement(baseElement) {
		this.baseElement = baseElement;
	}

}

export default AppDataAttributeHandler;


text:
"""
   compute_water_level.py
   ~~~~~~~~

   This script is used  to:
   1- calculate water depth above pressure sensor from pressure reading and coincident barometric pressure measurements.
   2- calculate total water depth
   3- calculate water surface elevation
   4- plot data.

"""
import platform
import time
import os
import copy
import glob
import datetime
import numpy as np
import sonde
import matplotlib.pyplot as plt
import pandas

if platform.system()=='Windows':
    base_dir = 'T:\\BaysEstuaries\\Data\\WQData'
    project_dir = os.path.join('T:\\BaysEstuaries\\PROJECTS\\NUECES',
                               'Nueces Delta Ecol. Modeling_USACE',
                               '2011/Data2012-2013')

else:
    base_dir = '/T/BaysEstuaries/Data/WQData'
    project_dir = os.path.join('/T/BaysEstuaries/PROJECTS/NUECES',
                                'Nueces Delta Ecol. Modeling_USACE',
                                '2011/Data2012-2013')

def drop_seconds(time_series):
    """this is to realign water level and barometer timseries for adjustment"""
    return [pandas.datetime(dt.year, dt.month, dt.day,dt.hour, dt.minute) for dt in
                            time_series.index]
def string_cleaner(string):
    if type(string) is str:
        return string.strip().lower()
    else:
        return string

 #the pressure in meters at which

site_name = raw_input('Enter Site Name: ').lower()
write_file = raw_input("write output file? [yes/no]: ")

barologger_sitename = raw_input('Enter Barologger Site Name: ').lower()

if barologger_sitename == 'nude3_baro':
    barologger_file_name = 'lower_delta_filled_barometer_data.csv'
if barologger_sitename == 'nueces5_baro':
    barologger_file_name = 'upper_delta_filled_barometer_data.csv'

log_file_dir = os.path.join('deployment_logs','original_files')
log_file_name = 'NUECES_DELTA_Deployment_Log_2012'
dep_log_file = os.path.join(base_dir, log_file_dir, log_file_name +
                                    '_deployment_temp.csv')
cal_log_file = os.path.join(base_dir, log_file_dir, log_file_name +
                                     '_calibration.csv')
site_dep_log_file = os.path.join(base_dir, log_file_dir, site_name+'_dep_log.csv')
austin_baro_file = os.path.join(project_dir,
                                'mabry_station_and_sea_level_pressure.csv')
sensor_to_gps_height_file = os.path.join(project_dir, 'site_coordinates.csv')

sonde_site_dir = os.path.join(base_dir, 'sites', site_name)
barologger_site_dir = os.path.join(base_dir, 'sites', barologger_sitename)
sonde_data_file = os.path.join(sonde_site_dir, 'twdb_wq_' + site_name \
                                + '.csv')
barologger_data_file = os.path.join(barologger_site_dir, barologger_file_name)

deployment_data = pandas.read_csv(dep_log_file, header=0, delimiter=',',
                                      parse_dates={'dep_datetime':[1,5],
                                                   'ret_datetime':[1,2]},
                                    na_values=['nd', '', ' ','n/a'])
calibration_data = pandas.read_csv(cal_log_file, header=0, delimiter=',',
                                   parse_dates=[[0,2]], index_col=0,
                                    na_values=['nd', '', ' ','n/a'])
deployment_data = deployment_data.applymap(string_cleaner)
deployment_data.index = deployment_data.dep_datetime
deployment_data.rename(columns={'Height from Base of Instrument to Base of GPS Receiver          (m)':
                        'sonde_bottom_to_gps'},inplace=True)
deployment_data.rename(columns=
                        {'Depth from bottom of instrument to water surface (m) ':
                            'sonde_bottom_to_surface'}, inplace=True)
deployment_data.rename(columns={'Total Water Depth          (m) ':
                                    'total_depth'}, inplace=True)
site_deployment_data = deployment_data[deployment_data['SITE ID'] == site_name]
site_deployment_data.index = site_deployment_data.dep_datetime
site_deployment_data['spotcheck_sensor_depth'] = np.nan

calibration_data = calibration_data.applymap(string_cleaner)

sonde_data = sonde.Sonde(sonde_data_file)
sonde_dates = [pandas.datetime.strptime(dt.strftime('%m-%d-%y %H:%M:%S'),
                                  '%m-%d-%y %H:%M:%S') for dt in
                                  sonde_data.dates]
sensor_to_gps_height = pandas.read_csv(sensor_to_gps_height_file,
                                       sep=',', index_col=[0],
                                        na_values=['nd','\s*'])

sonde_series = pandas.DataFrame(sonde_data.data,index=sonde_dates)
sonde_series['file_name'] = [f.lower().strip().split('.')[0] for f
                            in sonde_data.data_file]
sonde_series['sonde_id'] = 'unknown'
deploy_filename_list = np.unique(sonde_series.file_name)

baro_series = pandas.read_csv(barologger_data_file, sep=',', parse_dates=[0],
                              index_col=0)
austin_lab_baro_series = pandas.read_csv(austin_baro_file, sep=',',
                                         parse_dates=[0], index_col=0)

sonde_series.index = [pandas.datetime(dt.year,
                                      dt.month, dt.day,dt.hour, dt.minute)
                                      for dt in sonde_series.index]
baro_series.index = [pandas.datetime(dt.year, dt.month, dt.day,dt.hour,
                                     dt.minute) for dt in baro_series.index]
sonde_series = sonde_series.groupby(level=0).first()
sonde_baro_series = pandas.concat((sonde_series, baro_series),
                                  axis=1)
sonde_baro_series.water_depth_non_vented\
                [sonde_baro_series.water_depth_non_vented < -900.] = np.nan
sonde_baro_series.air_pressure[sonde_baro_series.air_pressure < -900] = np.nan
sonde_baro_series['corrected_sonde_depth'] = np.nan
sonde_baro_series['water_surface_elevation_one'] = np.nan
sonde_baro_series['water_surface_elevation_two'] = np.nan
sonde_baro_series['water_surface_elevation_three'] = np.nan
sonde_baro_series['water_surface_elevation_med'] = np.nan
sonde_baro_series['total_water_depth'] = np.nan
sonde_baro_series = sonde_baro_series.resample('15min', how='first')
ltc_zero_pressure = 9.5
ll_zero_pressure = 0.
ysi_bottom_to_sensor = .102
solinst_bottom_to_sensor = 0.01
horizontal_orientation = ['nueces2', 'nueces5', 'nueces6', 'nueces11']
water_depth_detection_limit = 0.03

site_gps_elevation_one = sensor_to_gps_height.ix[site_name,
                                             'survey_one_elevation']
site_gps_elevation_two = sensor_to_gps_height.ix[site_name,
                                             'survey_two_elevation']
site_gps_elevation_three = sensor_to_gps_height.ix[site_name,
                                             'survey_three_elevation']
site_gps_elevation_med = sensor_to_gps_height.ix[site_name,['survey_one_elevation',
                                             'survey_two_elevation',
                                             'survey_three_elevation']].quantile(
                                             0.5)
last_ret_start = pandas.datetime(2013,11,10)
last_ret_end = pandas.datetime(2013,11,15)

plt.figure()
for sonde_file in deploy_filename_list:
    deployment_specific_data = site_deployment_data[site_deployment_data\
                                ['DEPLOYED FILENAME']==sonde_file]
    sonde_type = deployment_specific_data['DEPLOYED SONDE ID']
    dep_file_mask = sonde_baro_series['file_name'] == sonde_file
    site_sonde_baro_series = sonde_baro_series[dep_file_mask]
    sonde_baro_series['sonde_id'][dep_file_mask] = sonde_type[0]
    if 'ltc' in sonde_type.values[0]:
        print "correcting deployment file ", sonde_file
        sonde_baro_series['corrected_sonde_depth'][dep_file_mask] = \
        sonde_baro_series.water_depth_non_vented[dep_file_mask] - \
        sonde_baro_series.air_pressure[dep_file_mask] + \
        ltc_zero_pressure
        if site_name == 'nueces13':
            if sonde_file == '0712nu13':
                survey_solinst_sonde_to_gps = 1.48
                print "solinst_to_gps,", sonde_file, survey_solinst_sonde_to_gps
            else:
                survey_solinst_sonde_to_gps = 0.905
                print "solinst_to_gps,", sonde_file, survey_solinst_sonde_to_gps

        else:
            survey_solinst_sonde_to_gps = sensor_to_gps_height.ix[site_name,
                                        'median_solinst_sonde_bottom_to_gps']

        if site_name in horizontal_orientation:
            site_deployment_data['spotcheck_sensor_depth'][site_deployment_data\
            ['DEPLOYED FILENAME']==sonde_file]= \
            deployment_specific_data.sonde_bottom_to_surface
            try:
                site_deployment_data.ix[last_ret_start:last_ret_end,
                    'spotcheck_sensor_depth'] = \
                site_deployment_data.sonde_bottom_to_surface.ix[last_ret_start:
                    last_ret_end][0]
            except TypeError:
                pass
            sonde_baro_series['water_surface_elevation_one'][dep_file_mask] =\
            site_gps_elevation_one  + sonde_baro_series['corrected_sonde_depth'][dep_file_mask]

            sonde_baro_series['water_surface_elevation_two'][dep_file_mask] =\
            site_gps_elevation_two  + sonde_baro_series['corrected_sonde_depth'][dep_file_mask]
            sonde_baro_series['water_surface_elevation_three'][dep_file_mask] = \
            site_gps_elevation_three  + sonde_baro_series['corrected_sonde_depth'][dep_file_mask]
            sonde_baro_series['water_surface_elevation_med'][dep_file_mask] = \
            site_gps_elevation_med  + sonde_baro_series['corrected_sonde_depth'][dep_file_mask]

            if np.isnan(sensor_to_gps_height.ix[site_name,
                                                'median_solinst_sensor_height']):
                solinst_sensor_height = sensor_to_gps_height.ix[site_name,
                                                'median_ysi_sensor_height']
            else:
                solinst_sensor_height = sensor_to_gps_height.ix[site_name,
                                                'median_solinst_sensor_height']

        else:
            site_deployment_data['spotcheck_sensor_depth'][site_deployment_data\
            ['DEPLOYED FILENAME']==sonde_file]= \
            deployment_specific_data.sonde_bottom_to_surface - \
            solinst_bottom_to_sensor

            try:
                site_deployment_data.ix[last_ret_start:last_ret_end,
                    'spotcheck_sensor_depth'] = \
                site_deployment_data.sonde_bottom_to_surface.ix[last_ret_start:
                    last_ret_end][0] - solinst_bottom_to_sensor
            except TypeError:
                pass

            sonde_baro_series['water_surface_elevation_one'][dep_file_mask] = \
            site_gps_elevation_one  + solinst_bottom_to_sensor + \
            sonde_baro_series['corrected_sonde_depth'][dep_file_mask]
            sonde_baro_series['water_surface_elevation_two'][dep_file_mask] = \
            site_gps_elevation_two + solinst_bottom_to_sensor + \
            sonde_baro_series['corrected_sonde_depth'][dep_file_mask]
            sonde_baro_series['water_surface_elevation_three'][dep_file_mask] = \
            site_gps_elevation_three + solinst_bottom_to_sensor + \
            sonde_baro_series['corrected_sonde_depth'][dep_file_mask]
            sonde_baro_series['water_surface_elevation_med'][dep_file_mask] = \
            site_gps_elevation_med + solinst_bottom_to_sensor + \
            sonde_baro_series['corrected_sonde_depth'][dep_file_mask]

            if site_name ==  'nueces13':
                if sonde_file == '0712nu13':
                    solinst_sensor_height = 0.29
                else: solinst_sensor_height = 0.925
            else:
                solinst_sensor_height = sensor_to_gps_height.ix[site_name,
                                        'median_solinst_sensor_height'] \
                                        + solinst_bottom_to_sensor
        sonde_baro_series['total_water_depth'][dep_file_mask] = \
            solinst_sensor_height + sonde_baro_series['corrected_sonde_depth']\
            [dep_file_mask]
#        water_depth_detection_mask = sonde_baro_series.corrected_sonde_depth\
#        < water_depth_detection_limit
#        sonde_baro_series['corrected_salinity'][dep_file_mask * \
#            water_depth_detection_mask] = np.nan
#        sonde_baro_series['total_water_depth'][dep_file_mask * \
#            water_depth_detection_mask] = -888.

    if 'ysi' in sonde_type.values[0]:
        print "correcting deployment file ", sonde_file
        site_calibration_data = calibration_data\
                             [calibration_data['SITE ID']== site_name]
        site_dep_data = pandas.read_csv(site_dep_log_file, header=0,
                                          sep=',',
                                          parse_dates={'dep_datetime':[1,5],
                                                   'ret_datetime':[1,2]},
                                    na_values=['nd', '', ' '])
        site_dep_data = site_dep_data.applymap(string_cleaner)
        site_dep_data.index = site_dep_data.dep_datetime

#        site_cal_baro_data = pandas.concat((site_calibration_data,
#                                           austin_lab_baro_series), axis=1)
#        site_cal_baro_data.station_pressure = site_cal_baro_data.\
#                        station_pressure.interpolate(method='linear')
#        ysi_zero_pressure_mabry = site_cal_baro_data.station_pressure.ix[site_calibration_data.index[site_calibration_data['DEPLOYED FILENAME']==sonde_file]]
        ysi_zero_pressure = site_dep_data.zero_pressure.ix[site_dep_data.index[site_dep_data['DEPLOYED FILENAME']==sonde_file]]
        sonde_baro_series['corrected_sonde_depth'][dep_file_mask] = \
        sonde_baro_series.water_depth_non_vented[dep_file_mask] - \
        sonde_baro_series.air_pressure[dep_file_mask] + ysi_zero_pressure[0]

        survey_ysi_sonde_to_gps = sensor_to_gps_height.ix[site_name,
                            'median_ysi_sonde_bottom_to_gps']  # this assumes that the solinst was placed at the same level of

        if site_name in horizontal_orientation:
            site_deployment_data['spotcheck_sensor_depth'][site_deployment_data\
            ['DEPLOYED FILENAME']==sonde_file]= \
            site_deployment_data.sonde_bottom_to_surface

            try:
                site_deployment_data.ix[last_ret_start:last_ret_end,
                    'spotcheck_sensor_depth'] = \
                site_deployment_data.sonde_bottom_to_surface.ix[last_ret_start:
                    last_ret_end][0]
            except TypeError:
                pass

            sonde_baro_series['water_surface_elevation_one'][dep_file_mask] = \
            site_gps_elevation_one  + sonde_baro_series['corrected_sonde_depth'][dep_file_mask]
            sonde_baro_series['water_surface_elevation_two'][dep_file_mask] \
            = site_gps_elevation_two + sonde_baro_series['corrected_sonde_depth'][dep_file_mask]
            sonde_baro_series['water_surface_elevation_three'][dep_file_mask] = \
            site_gps_elevation_three + sonde_baro_series['corrected_sonde_depth'][dep_file_mask]
            sonde_baro_series['water_surface_elevation_med'][dep_file_mask] = \
            site_gps_elevation_med + sonde_baro_series['corrected_sonde_depth'][dep_file_mask]

            if np.isnan(sensor_to_gps_height.ix[site_name,
                                                'median_ysi_sensor_height']):
                ysi_sensor_height = sensor_to_gps_height.ix[site_name,
                                        'median_solinst_sensor_height']
            else:
                ysi_sensor_height = sensor_to_gps_height.ix[site_name,
                                        'median_ysi_sensor_height']

        else:
            site_deployment_data['spotcheck_sensor_depth'][site_deployment_data\
            ['DEPLOYED FILENAME']==sonde_file]= \
            deployment_specific_data.sonde_bottom_to_surface - \
            ysi_bottom_to_sensor

            try:
                site_deployment_data.ix[last_ret_start:last_ret_end,
                    'spotcheck_sensor_depth'] = \
                site_deployment_data.sonde_bottom_to_surface.ix[last_ret_start:
                    last_ret_end][0] - ysi_bottom_to_sensor
            except TypeError:
                pass

            sonde_baro_series['water_surface_elevation_one'][dep_file_mask] = \
            site_gps_elevation_one + ysi_bottom_to_sensor +\
            sonde_baro_series['corrected_sonde_depth'][dep_file_mask]
            sonde_baro_series['water_surface_elevation_two'][dep_file_mask] = \
            site_gps_elevation_two + ysi_bottom_to_sensor + \
            sonde_baro_series['corrected_sonde_depth'][dep_file_mask]
            sonde_baro_series['water_surface_elevation_three'][dep_file_mask] = \
            site_gps_elevation_three + ysi_bottom_to_sensor + \
            sonde_baro_series['corrected_sonde_depth'][dep_file_mask]
            sonde_baro_series['water_surface_elevation_med'][dep_file_mask] = \
            site_gps_elevation_med + ysi_bottom_to_sensor + \
            sonde_baro_series['corrected_sonde_depth'][dep_file_mask]

            if np.isnan(sensor_to_gps_height.ix[site_name,
                                                'median_ysi_sensor_height']):
                ysi_sensor_height = sensor_to_gps_height.ix[site_name,
                                        'median_solinst_sensor_height']
            else:
                ysi_sensor_height = sensor_to_gps_height.ix[site_name,
                                                    'median_ysi_sensor_height'] \
                                                        + ysi_bottom_to_sensor
        sonde_baro_series['total_water_depth'][sonde_baro_series\
        ['file_name'] == sonde_file] = ysi_sensor_height + \
        sonde_baro_series\
        ['corrected_sonde_depth'][sonde_baro_series['file_name']== sonde_file]

    if 'll' in sonde_type.values[0]:
        sonde_baro_series['corrected_sonde_depth'][sonde_baro_series['file_name'] == sonde_file] = \
        sonde_baro_series.water_depth_non_vented[sonde_baro_series['file_name'] == sonde_file] - \
        sonde_baro_series.air_pressure[sonde_baro_series['file_name'] == sonde_file]

        if np.isnan(sensor_to_gps_height.ix[site_name,
                                'median_solinst_sonde_bottom_to_gps']):
            survey_solinst_sonde_to_gps = sensor_to_gps_height.ix[site_name,
                                            'median_ysi_sonde_bottom_to_gps'] - \
                                            .25
        else :
            survey_solinst_sonde_to_gps = sensor_to_gps_height.ix[site_name,
                                    'median_solinst_sonde_bottom_to_gps']
        if site_name in horizontal_orientation:
            site_deployment_data['spotcheck_sensor_depth'][site_deployment_data\
            ['DEPLOYED FILENAME']==sonde_file]= \
            site_deployment_data.sonde_bottom_to_surface

            try:
                site_deployment_data.ix[last_ret_start:last_ret_end,
                    'spotcheck_sensor_depth'] = \
                site_deployment_data.sonde_bottom_to_surface.ix[last_ret_start:
                    last_ret_end][0]
            except TypeError:
                pass

            sonde_baro_series['water_surface_elevation_one'][dep_file_mask] = \
            site_gps_elevation_one + sonde_baro_series['corrected_sonde_depth']\
            [dep_file_mask]
            sonde_baro_series['water_surface_elevation_two'][dep_file_mask] = \
            site_gps_elevation_two + sonde_baro_series['corrected_sonde_depth']\
            [dep_file_mask]
            sonde_baro_series['water_surface_elevation_three'][dep_file_mask] = \
            site_gps_elevation_three + sonde_baro_series['corrected_sonde_depth']\
            [dep_file_mask]
            sonde_baro_series['water_surface_elevation_med'][dep_file_mask] = \
            site_gps_elevation_med + sonde_baro_series['corrected_sonde_depth']\
            [dep_file_mask]

            if np.isnan(sensor_to_gps_height.ix[site_name,
                                                'median_solinst_sensor_height']):
                solinst_sensor_height = sensor_to_gps_height.ix[site_name,
                                                        'median_ysi_sensor_height']
            else:
                solinst_sensor_height = sensor_to_gps_height.ix[site_name,
                                                'median_solinst_sensor_height']
        else:
            site_deployment_data['spotcheck_sensor_depth'][site_deployment_data\
            ['DEPLOYED FILENAME']==sonde_file]= \
            deployment_specific_data.sonde_bottom_to_surface- \
            solinst_bottom_to_sensor

            try:
                site_deployment_data.ix[last_ret_start:last_ret_end,
                    'spotcheck_sensor_depth'] = \
                site_deployment_data.sonde_bottom_to_surface.ix[last_ret_start:
                    last_ret_end][0] - solinst_bottom_to_sensor
            except TypeError:
                pass

            sonde_baro_series['water_surface_elevation_one'][dep_file_mask] = \
            site_gps_elevation_one + solinst_bottom_to_sensor + \
            sonde_baro_series['corrected_sonde_depth'][dep_file_mask]
            sonde_baro_series['water_surface_elevation_two'][dep_file_mask] = \
            site_gps_elevation_two + solinst_bottom_to_sensor + \
            sonde_baro_series['corrected_sonde_depth'][dep_file_mask]
            sonde_baro_series['water_surface_elevation_three'][dep_file_mask] = \
            site_gps_elevation_three + solinst_bottom_to_sensor + \
            sonde_baro_series['corrected_sonde_depth'][dep_file_mask]
            sonde_baro_series['water_surface_elevation_med'][dep_file_mask] = \
            site_gps_elevation_med + solinst_bottom_to_sensor + \
            sonde_baro_series['corrected_sonde_depth'][dep_file_mask]

            if np.isnan(sensor_to_gps_height.ix[site_name,
                                                'median_solinst_sensor_height']):
                solinst_sensor_height = sensor_to_gps_height.ix[site_name,
                                        'median_ysi_sensor_height'] + .25
            else:
                solinst_sensor_height = sensor_to_gps_height.ix[site_name,
                                                'median_solinst_sensor_height'] \
                                                        + solinst_bottom_to_sensor
        sonde_baro_series['total_water_depth'][sonde_baro_series\
        ['file_name'] == sonde_file] = solinst_sensor_height + \
        sonde_baro_series\
        ['corrected_sonde_depth'][sonde_baro_series['file_name']== sonde_file]

    sonde_baro_series['corrected_sonde_depth']\
    [sonde_baro_series['file_name']==sonde_file].plot(style='.',label='sonde depth')

plt.ylim(0,2)

site_deployment_data[site_deployment_data.columns[-3]].dropna().plot(style='r.',
                                                             markersize=12)

water_depth_detection_mask = sonde_baro_series.corrected_sonde_depth\
                            < water_depth_detection_limit
sonde_baro_series['total_water_depth'][water_depth_detection_mask] =   -888.88
sonde_baro_series['water_surface_elevation_one'][water_depth_detection_mask] = -888.88
sonde_baro_series['water_surface_elevation_two'][water_depth_detection_mask] = -888.88
sonde_baro_series['water_surface_elevation_three'][water_depth_detection_mask] = -888.88
sonde_baro_series['water_surface_elevation_med'][water_depth_detection_mask] = -888.88
sonde_baro_series['water_temperature'][water_depth_detection_mask] = -888.88
#sonde_baro_series['water_electrical_conductivity'][water_depth_detection_mask] = -888.88

site_deployment_data['spotcheck_sensor_depth'].dropna().plot(style='r^',
                                                    markersize=10,
                                                    label='spot-check')
plt.title(site_name)
plt.ylabel('depth, m')
plt.legend().set_visible(False)
#plt.ylim(0,2)
sonde_baro_series.rename(columns={'water_depth_non_vented':
                                    'raw_pressure_reading'},inplace=True)

sonde_depth_ax = plt.figure().add_subplot(111)
sonde_baro_series.ix[:,'corrected_sonde_depth'].dropna().plot(style='b.',
    label='deployed sonde', ax=sonde_depth_ax)
sonde_baro_series.ix[:,'raw_pressure_reading'].dropna().plot(style='g.',
    label='raw pressure reading', ax=sonde_depth_ax)

site_deployment_data[site_deployment_data.columns[-3]].dropna().plot(style='r.',
                                                             markersize=12)
site_deployment_data['spotcheck_sensor_depth'].dropna().plot(style='r.',
    markersize=10, label='spot-check sonde', ax=sonde_depth_ax)

sonde_depth_ax.set_title(site_name)
#sonde_depth_ax.set_ylim(0, 2)
sonde_depth_ax.set_ylabel('water depth above sensor, m')
sonde_depth_ax.legend(loc='best')
#plt.savefig(os.path.join(sonde_site_dir,
#                         site_name + '_water_depth_above_sensor.png'))
total_depth_ax = plt.figure().add_subplot(111)
#sonde_baro_series.ix[:, 'corrected_sonde_depth'].plot(style='.', ax=ax,
#                                            label='water depth above sensor')
sonde_baro_series.ix[:, 'total_water_depth'].plot(style='.', ax=total_depth_ax,
                                            label='total water depth')
total_depth_ax.set_title(site_name)
total_depth_ax.set_ylim(0,2)
total_depth_ax.set_ylabel('water depth, m')
site_deployment_data.total_depth.plot(style='r.', markersize=12,ax=total_depth_ax,
                                      label= 'spot-check')
plt.legend(loc='best')
plt.savefig(os.path.join(sonde_site_dir, site_name + '_water_depth.png'))

site_deployment_data.total_depth = site_deployment_data.total_depth.apply(
                                    lambda d: np.float(d))
site_deployment_data.sonde_bottom_to_surface = site_deployment_data.\
                            sonde_bottom_to_surface.apply(
                                    lambda d: np.float(d))
'''
sonde_baro_series.ix[:, ['water_surface_elevation_one',
                         'water_surface_elevation_two',
                         'water_surface_elevation_three',
                         'water_surface_elevation_med']].plot(title=site_name,
                         style={'water_surface_elevation_one': '.',
                                'water_surace_elevation_two': '.',
                                'water_survace_elevation_three': '.',
                                'water_surface_elevation_med': '^'},mew=0)

sonde_baro_series.ix
plt.ylim(-2,2)
plt.ylabel('water surface elevation,m from NAVD88')
'''
plt.figure()
sonde_baro_series.water_surface_elevation_med[sonde_baro_series.water_surface_elevation_med < -800.] = np.nan
sonde_baro_series.water_surface_elevation_med.dropna().plot(style='.',
                                                      title=site_name)
plt.ylabel('water surface elevation,m from NAVD88')
#plt.xlabel('datetimes')
#plt.ylim(-1.5,-0.6)
plt.title(site_name)
plt.grid(b=None)
plt.savefig(os.path.join(sonde_site_dir, site_name + '_wse.png'))
#plt.xlim(sonde_baro_series.water_surface_elevation_med.index[0],
#         sonde_baro_series.water_surface_elevation_med.index[-1])
"""
ll_bool = site_deployment_data.sonde_type == 'll'
ltc_bool = site_deployment_data.sonde_type == 'ltc'
#solinst_bool = [ll or ltc for ll,ltc in zip(ll_bool, ltc_bool)]
#solinst_sensor_height = site_deployment_data.total_depth[solinst_bool] - \
                site_deployment_data.sonde_bottom_to_surface[solinst_bool]

ysi_sensor_height_series = site_deployment_data.total_depth\
                        [site_deployment_data.sonde_type == 'ysi'] - \
                site_deployment_data.sonde_bottom_to_surface\
                [site_deployment_data.sonde_type == 'ysi']

try:
    solinst_sensor_height.plot(style='.-', label='solinst')
except TypeError:
    pass
try:
    ysi_sensor_height.plot(style='.-', label='ysi')
except TypeError:
    pass
"""
#plt.title(site_name + ' sensor height')
#print site_name
#print "ysi:", ysi_sensor_height.quantile(.5)
#print "solinst:", solinst_sensor_height.quantile(.5)
plt.show()

metadata_file = os.path.join(base_dir,'swis_site_list_with_nueces_added.psv')
metadata = np.genfromtxt(metadata_file, delimiter='|', dtype='|S20,|S100,|S20,|S20',
                         usecols=(1,5,6,7),
                         names='site_name,site_desc,lat,lon',skip_header=1)

idx = np.where(metadata['site_name']==site_name.upper())

if write_file == 'yes':
    disclaimer_header = ''
    disclaimer_header += 'disclaimer: This data has been collected by a Texas Water Development Board datasonde.\n'
    disclaimer_header += 'disclaimer: Raw uncorrected data may contain errors. Provisional data has had anomalous \n'
    disclaimer_header += 'disclaimer: individual data points removed. Such data points typically are disconnected \n'
    disclaimer_header += 'disclaimer: from the recorded trend; nonetheless all removed data is retained in an associated \n'
    disclaimer_header += 'disclaimer: QA Rules file available upon request. However data that simply appear unusual are \n'
    disclaimer_header += 'disclaimer: not removed unless verifying information is obtained suggesting the data is not \n'
    disclaimer_header += 'disclaimer: representative of bay conditions. The Board makes no warranties (including no warranties \n'
    disclaimer_header += 'disclaimer: as to merchantability or fitness) either expressed or implied with respect to the data \n'
    disclaimer_header += 'disclaimer: or its fitness for any specific application. \n'

    disclaimer_header += 'fill_value for bad/missing data: -999.99. \n'
    try:
        disclaimer_header += 'fill_value for parameters when water level falls to or below sensor level above delta bed of ' + str(solinst_sensor_height)[:5] + ' m: -888.88. \n'
    except NameError:
        disclaimer_header += 'fill_value for parameters when water level falls to or below sensor level above delta bed of ' + str(ysi_sensor_height)[:5] + ' m: -888.88. \n'

    disclaimer_header += 'site_description: ' + metadata['site_desc'][idx][0] + '\n'
    disclaimer_header += 'site_name: ' + site_name + '\n'
    disclaimer_header += 'latitude: ' + metadata['lat'][idx][0] + '\n'
    disclaimer_header += 'longitude: ' + metadata['lon'][idx][0] + '\n'

    disclaimer_header += 'timezone: UTC-6 \n'

    columnorder = ['raw_pressure_reading', 'air_pressure', 'corrected_sonde_depth',
               'total_water_depth', 'water_surface_elevation_one',
               'water_surface_elevation_two', 'water_surface_elevation_three',
               'water_surface_elevation_med', 'water_temperature', 'sonde_id',
               'file_name']
#    units = 'mm/dd/yyyy hh:mm,mH2O,mH2O,mH2O,mH2O,m from NAVD88,m from NAVD88,\
#m from NAVD88,m from NAVD88,degC,n/a,n/a,n/a \n'
    processed_wq_file = os.path.join(sonde_site_dir, site_name + '_final_data_corrected.csv')
    sonde_baro_series = sonde_baro_series.ix[:, columnorder]
    fid = open(processed_wq_file, 'w')
    fid.write(disclaimer_header)
#    fid.write(units)
    sonde_baro_series.to_csv(fid, sep=',',
                             index_label='datetimes', na_rep = -999.99,
                             float_format='%5.2f')
    fid.close()