| Larch: Data Analysis Tools for X-ray Spectroscopy

# 5. Larch Tutorial

At its core, Larch is a language for processing of scientific data. This chapter describes the Larch language and introduces data processing using Larch. An important goal of Larch is to make writing and modifying data analysis scripts as simple as possible.

Although aimed at the novice programmer, this tutorial does make a few assumptions about the readers experience with scientific programming. For example, the reader is expected to have a technical background and some familiarity with using scientific data analysis programs. Some understanding of the concepts of how scientific data is stored on computers and of the basics of programming will greatly help the reader.

The Larch language is implemented in Python, and heavily based upon it. Knowledge of Python will greatly simplify learning Larch, and vice versa. This shared syntax is intentional, so that as you learn Larch, you will also be learning Python, which can be used to extend Larch. Alternatively, knowledge of Python will make Larch easy to learn. For further details on Python, including tutorials, see the Python documentation at http://python.org/

Contents:

- 5.1. Tutorial: Getting Started
  - 5.1.1. Larch as a Basic Calculator
- 5.2. Tutorial: Basic and Complex Data Types
  - 5.2.1. Basic Data Types
  - 5.2.2. Objects and Groups
  - 5.2.3. More Complex Data Structures: Lists, Arrays, Dictionaries
  - 5.2.4. Object identities, copying, and equality vs. identity
- 5.3. Tutorial: Basic Array Manipulation
  - 5.3.1. Functions for creating arrays
  - 5.3.2. The array dtype – datatype
  - 5.3.3. Basic array manipulation
  - 5.3.4. Slicing and extracting sub-arrays
  - 5.3.5. Array attributes and methods
  - 5.3.6. Mathematical functions for arrays
- 5.4. Tutorial: Conditional Execution and Flow Control
  - 5.4.1. Conditional Evaluation with if and else
  - 5.4.2. For loops
  - 5.4.3. While loops
- 5.5. Tutorial: Dealing With Errors
  - 5.5.1. Syntax Errors
  - 5.5.2. Exceptions
  - 5.5.3. Try and Except
  - 5.5.4. Raising your own exceptions
- 5.6. Tutorial: Writing Functions or Procedures
  - 5.6.1. Def statement
  - 5.6.2. Namespace and “Scope” inside a Procedure
  - 5.6.3. The return statement, and multiple Return values
  - 5.6.4. Keyword arguments
  - 5.6.5. Documentation Strings
- 5.7. Tutorial: Running Larch Scripts, and Modules
  - 5.7.1. Running a larch script with `run()`
  - 5.7.2. Importing a Larch Script as a Module
- 5.8. Tutorial: Builtin Functions
  - 5.8.1. Builtin data values
  - 5.8.2. Larch-Specific Builtin functions
  - 5.8.3. Builtin functions inherited from Python