# Baysegal

## Installation

### Files

Download these packages:

- [Bayseagal source](http://minerva.ufsc.br/~andre/bayseagal/bayseagal-0.40.tar.gz)
- [JPLUS configuration file](http://minerva.ufsc.br/~andre/bayseagal/jplus-dr3.cfg)
- [Tables and data](http://minerva.ufsc.br/~andre/bayseagal/data.tar.gz)

Extract the sources, and enter its directory.

```
tar zxvf bayseagal-0.40.tar.gz
cd bayseagal-0.40
```

Put the config file in this directory. All paths are relative
to the source directory.

Then, extract the data package, containing the base, taylor matrix, filter curves,
and tables from JPLUS-DR3.

```
tar zxvf data.tar.gz
```

The contents of the source dir should match the listing below.

```
(base) Andres-MacBook-Air:bayseagal-0.4 andre$ ls
PKG-INFO
README.md
bayseagal
bayseagal.egg-info
data
jplus-dr3.cfg
requirements.txt
scripts
setup.cfg
setup.py
test
```

### Python environment

One can use [Mabaforge](https://github.com/conda-forge/miniforge#mambaforge)
or [Anaconda](https://www.anaconda.com/download/). These work roughly the same,
using the command `mamba` or `conda`, respectively. Create a new environment (named
bayseagal in the example), with the correct requirements:

```
mamba create -n baysegal --file requirements.txt
```

Add bayseagal-0.40 to python path:

```
mamba develop .
```

You are all set up.

## Usage

Before going into details, let's see the contents of the data directory.

```
├── Base.cb17.chab.7met.All.hdf5
├── JPLUS_filter_curves
│   ├── JPLUS_J0378.tab
│   ├── JPLUS_J0395.tab
│   ├── JPLUS_J0410.tab
│   ├── JPLUS_J0430.tab
│   ├── JPLUS_J0515.tab
│   ├── JPLUS_J0660.tab
│   ├── JPLUS_J0861.tab
│   ├── JPLUS_gSDSS.tab
│   ├── JPLUS_iSDSS.tab
│   ├── JPLUS_rSDSS.tab
│   ├── JPLUS_uJAVA.tab
│   └── JPLUS_zSDSS.tab
└── jplus-dr3
    ├── filter.fits
    ├── input
    ├── m_a_jplus-dr3.cb17.chab.7met_z1_fixed_H068.hdf5
    ├── magabdualobj
    │   └── magabdualobj_94193.fits
    ├── master
    │   └── master-94193.fits
    ├── mwextinction
    │   └── mwextinction_94193.fits
    ├── output
    │   └── jplus-dr3_psfcor_zsdss-100356_1-2.hdf5
    ├── photozlephare
    │   └── photozlephare_94193.fits
    ├── plots
    ├── stargalclass
    │   └── stargalclass_94193.fits
    ├── tileimage.fits
    └── xmatch_sdss_dr12
        └── xmatch_sdss_dr12_94193.fits
```

In this example, we have data from tile 94193. Of immediate interest
is the master table. A master table contain all pertinent data from a single tile. To create a master
table, use the script `scripts/create_master_table.py`.

### AEGIS field data

Mini J-PAS observed 4 tiles in the aegis field. In J-PLUS DR3, these tiles
correspond to tile 94193. The coordinates of the tiles in Mini J-PAS can be found
using the query:

```
select ref_tile_id, avg(ra) as mean_ra, mean_(dec) from minijpas.tileimage group by ref_tile_id
```

The coodinates are roughly r.a. = 214.0, dec = 52.8 (degrees). We find the J-PLUS tile
using the query

```
select * from jplus.tileimage where contains(point('', ra, dec), circle('', 214.0, 52.8, 1.0)) = 1
```


### Taylor matrix

The data package already contains this file, which is needed to compute
bandpass magnitudes using a Taylor expansion instead of time-consuming integrals.
This must match the config file, and the included one was created using `jplus-dr3.cfg`.

To create a new matrix, edit the config file (or create a new one) and run the command

```
python scripts/create_taylor_matrix.py --config=jplus-dr3.cfg
```

This takes around 20 minutes for 12 JPLUS filters.

### Creating an input table

There's an example script showing how to create an input table in `scripts/create_input_table.py`.
The paths are hardcoded, for now, so this may need some editing. Then, just run

```
python scripts/create_input_table.py
```

The results should be available, by default, in `data/jplus-dr3/input`.

### Running the fit

Finally, you are able to run `bayseagal_fit`. There are lots of command line options.
The options are

* `--config=jplus-dr3.cfg` sets the config file.
* `--input=data/jplus-dr3/input/jplus-dr3_psfcor_zsdss-100356.fits` overrides the input file.
* `--range=100:200` selects a range of objects to fit (from 100 to 199 in this example).
* `--output=data/jplus-dr3/output/jplus-dr3_psfcor_zsdss-100356_100-199.hdf5` overrides the output file.
* Use `--overwrite` to force re-fitting the objects.
* Use `--debug` to show additional messages, and plotting some diagnostics (default: `data/jplus-dr3/plots`).

Example usage:

```
python scripts/bayseagal_fit.py --config jplus-dr3.cfg \
--input data/jplus-dr3/input/jplus-dr3_psfcor_zsdss-94193.fits \
--range 0:10 --debug --overwrite \
--output=data/jplus-dr3/output/jplus-dr3_psfcor_zsdss-94193.hdf5
```

