# Startup.  The Montage modules are pretty much self-contained
# but this script needs a few extra utilities.

import os
import sys
import shutil

from MontagePy.main    import *
from MontagePy.archive import *

from IPython.display import Image

# TODO: format with yapf

# These are the parameters defining the mosaic we want to make.

# location  = "M 17"

#TODO: feedback, it would be nice to be able to pass an astropy skycoord
# object as location.

location = "162.64744 35.81169 Equatorial"
size      = 1.0
dataset   = "2MASS J"
workdir   = "mosaic_ps1"

# We create and move into subdirectories in this notebook
# but we want to come back to the original startup directory
# whenever we restart the processing.

try:
    home
except:
    home = os.getcwd()

os.chdir(home)

#TODO: format strings with .format()
print("Startup folder: " + home)


# Clean out any old copy of the work tree, then remake it
# and the set of the subdirectories we will need.

# try:
#     shutil.rmtree(workdir)
# except:
#     print("                Can't delete work tree; probably doesn't exist yet", flush=True)
#
# print("Work directory: " + workdir, flush=True)
#
# os.makedirs(workdir)

os.chdir(workdir)

#TODO: jacobic - already create dirs before downloading/uncompressing images)
# os.makedirs("raw")
os.makedirs("projected")
os.makedirs("diffs")
os.makedirs("corrected")

# Create the FITS header for the mosaic.

#TODO: use paramter keywords in tutorials. reference zen of python
#TODO: also autocomepletion does not work when you use **arg_dict and **kwargs
rtn = mHdr(locstr=location, width=size, height=size, outfile="region.hdr")

print("mHdr:             " + str(rtn), flush=True)


# Retrieve archive images covering the region then scan
# the images for their coverage metadata.

# rtn = mArchiveDownload(dataset, location, size, "raw")

# print("mArchiveDownload: " + str(rtn), flush=True)

rtn = mImgtbl("raw", "rimages.tbl")

#TODO: why not enable logging? or verbose mode?

print("mImgtbl (raw):    " + str(rtn), flush=True)

# Reproject the original images to the  frame of the
# output FITS header we created

#TODO: why is stuff in camelcase?
#TODO: kwargs default values are not shown?
#TODO: i was able to pass in an int without a value error?
rtn = mProjExec("raw", "rimages.tbl", "region.hdr", projdir="projected", quickMode=True)

print("mProjExec:           " + str(rtn), flush=True)

mImgtbl("projected", "pimages.tbl")

print("mImgtbl (projected): " + str(rtn), flush=True)

# Coadd the projected images without backgound correction.
# This step is just to illustrate the need for background correction
# and can be omitted.

rtn = mAdd("projected", "pimages.tbl", "region.hdr", "uncorrected.fits")

print("mAdd:    " + str(rtn), flush=True)

# Make a PNG rendering of the data and display it.

rtn = mViewer("-ct 1 -gray uncorrected.fits -2s max gaussian-log -out uncorrected.png", "", mode=2)

print("mViewer: " + str(rtn), flush=True)

Image(filename='uncorrected.png')

# Determine the overlaps between images (for background modeling).

rtn = mOverlaps("pimages.tbl", "diffs.tbl")

print("mOverlaps:    " + str(rtn), flush=True)


# Generate difference images and fit them.

rtn = mDiffFitExec("projected", "diffs.tbl", "region.hdr", "diffs", "fits.tbl")

print("mDiffFitExec: " + str(rtn), flush=True)


# Model the background corrections.

rtn = mBgModel("pimages.tbl", "fits.tbl", "corrections.tbl")

print("mBgModel:     " + str(rtn), flush=True)

# Background correct the projected images.

rtn = mBgExec("projected", "pimages.tbl", "corrections.tbl", "corrected")

print("mBgExec:             " + str(rtn), flush=True)

rtn = mImgtbl("corrected", "cimages.tbl")

print("mImgtbl (corrected): " + str(rtn), flush=True)


# Coadd the background-corrected, projected images.

rtn = mAdd("corrected", "cimages.tbl", "region.hdr", "mosaic.fits")

print("mAdd:                " + str(rtn), flush=True)

# Make a PNG rendering of the data and display it.

rtn = mViewer("-ct 1 -gray mosaic.fits -2s max gaussian-log -out mosaic.png", "", mode=2)

print("mViewer: " + str(rtn), flush=True)

Image(filename='mosaic.png')

print('Done')
# Startup.  The Montage modules are pretty much self-contained
# but this script needs a few extra utilities.

import os
import sys
import shutil

from MontagePy.main    import *
from MontagePy.archive import *

from IPython.display import Image

# TODO: format with yapf

# These are the parameters defining the mosaic we want to make.

# location  = "M 17"

#TODO: feedback, it would be nice to be able to pass an astropy skycoord
# object as location.

location = "162.64744 35.81169 Equatorial"
size      = 1.0
dataset   = "2MASS J"
workdir   = "mosaic_ps1"

# We create and move into subdirectories in this notebook
# but we want to come back to the original startup directory
# whenever we restart the processing.

try:
    home
except:
    home = os.getcwd()

os.chdir(home)

#TODO: format strings with .format()
print("Startup folder: " + home)


# Clean out any old copy of the work tree, then remake it
# and the set of the subdirectories we will need.

# try:
#     shutil.rmtree(workdir)
# except:
#     print("                Can't delete work tree; probably doesn't exist yet", flush=True)
#
# print("Work directory: " + workdir, flush=True)
#
# os.makedirs(workdir)

os.chdir(workdir)

#TODO: jacobic - already create dirs before downloading/uncompressing images)
# os.makedirs("raw")
os.makedirs("projected")
os.makedirs("diffs")
os.makedirs("corrected")

# Create the FITS header for the mosaic.

#TODO: use paramter keywords in tutorials. reference zen of python
#TODO: also autocomepletion does not work when you use **arg_dict and **kwargs
rtn = mHdr(locstr=location, width=size, height=size, outfile="region.hdr")

print("mHdr:             " + str(rtn), flush=True)


# Retrieve archive images covering the region then scan
# the images for their coverage metadata.

# rtn = mArchiveDownload(dataset, location, size, "raw")

# print("mArchiveDownload: " + str(rtn), flush=True)

rtn = mImgtbl("raw", "rimages.tbl")

#TODO: why not enable logging? or verbose mode?

print("mImgtbl (raw):    " + str(rtn), flush=True)

# Reproject the original images to the  frame of the
# output FITS header we created

#TODO: why is stuff in camelcase?
#TODO: kwargs default values are not shown?
#TODO: i was able to pass in an int without a value error?
rtn = mProjExec("raw", "rimages.tbl", "region.hdr", projdir="projected", quickMode=True)

print("mProjExec:           " + str(rtn), flush=True)

mImgtbl("projected", "pimages.tbl")

print("mImgtbl (projected): " + str(rtn), flush=True)

# Coadd the projected images without backgound correction.
# This step is just to illustrate the need for background correction
# and can be omitted.

rtn = mAdd("projected", "pimages.tbl", "region.hdr", "uncorrected.fits")

print("mAdd:    " + str(rtn), flush=True)

# Make a PNG rendering of the data and display it.

rtn = mViewer("-ct 1 -gray uncorrected.fits -2s max gaussian-log -out uncorrected.png", "", mode=2)

print("mViewer: " + str(rtn), flush=True)

Image(filename='uncorrected.png')

# Determine the overlaps between images (for background modeling).

rtn = mOverlaps("pimages.tbl", "diffs.tbl")

print("mOverlaps:    " + str(rtn), flush=True)


# Generate difference images and fit them.

rtn = mDiffFitExec("projected", "diffs.tbl", "region.hdr", "diffs", "fits.tbl")

print("mDiffFitExec: " + str(rtn), flush=True)


# Model the background corrections.

rtn = mBgModel("pimages.tbl", "fits.tbl", "corrections.tbl")

print("mBgModel:     " + str(rtn), flush=True)

# Background correct the projected images.

rtn = mBgExec("projected", "pimages.tbl", "corrections.tbl", "corrected")

print("mBgExec:             " + str(rtn), flush=True)

rtn = mImgtbl("corrected", "cimages.tbl")

print("mImgtbl (corrected): " + str(rtn), flush=True)


# Coadd the background-corrected, projected images.

rtn = mAdd("corrected", "cimages.tbl", "region.hdr", "mosaic.fits")

print("mAdd:                " + str(rtn), flush=True)

# Make a PNG rendering of the data and display it.

rtn = mViewer("-ct 1 -gray mosaic.fits -2s max gaussian-log -out mosaic.png", "", mode=2)

print("mViewer: " + str(rtn), flush=True)

Image(filename='mosaic.png')

print('Done')
# Startup.  The Montage modules are pretty much self-contained # but this script needs a few extra utilities. # TODO: format with yapf # These are the parameters defining the mosaic we want to make. # location  = "M 17" #TODO: feedback, it would be nice to be able to pass an astropy skycoord # object as location. # We create and move into subdirectories in this notebook # but we want to come back to the original startup directory # whenever we restart the processing. #TODO: format strings with .format() # Clean out any old copy of the work tree, then remake it # and the set of the subdirectories we will need. # try: #     shutil.rmtree(workdir) # except: #     print("                Can't delete work tree; probably doesn't exist yet", flush=True) # # print("Work directory: " + workdir, flush=True) # # os.makedirs(workdir) #TODO: jacobic - already create dirs before downloading/uncompressing images) # os.makedirs("raw") # Create the FITS header for the mosaic. #TODO: use paramter keywords in tutorials. reference zen of python #TODO: also autocomepletion does not work when you use **arg_dict and **kwargs # Retrieve archive images covering the region then scan # the images for their coverage metadata. # rtn = mArchiveDownload(dataset, location, size, "raw") # print("mArchiveDownload: " + str(rtn), flush=True) #TODO: why not enable logging? or verbose mode? # Reproject the original images to the  frame of the # output FITS header we created #TODO: why is stuff in camelcase? #TODO: kwargs default values are not shown? #TODO: i was able to pass in an int without a value error? # Coadd the projected images without backgound correction. # This step is just to illustrate the need for background correction # and can be omitted. # Make a PNG rendering of the data and display it. # Determine the overlaps between images (for background modeling). # Generate difference images and fit them. # Model the background corrections. # Background correct the projected images. # Coadd the background-corrected, projected images. # Make a PNG rendering of the data and display it.