-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated the log system to carry on the same version in all scripts
- Loading branch information
Bruno Quint
committed
Sep 13, 2017
1 parent
0c2d121
commit 9bf601c
Showing
10 changed files
with
187 additions
and
284 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,7 @@ | |
""" | ||
|
||
from __future__ import division, print_function | ||
from __future__ import absolute_import, division, print_function | ||
|
||
import astropy.io.fits as pyfits | ||
import argparse | ||
|
@@ -21,13 +21,15 @@ | |
import numpy as np | ||
import pandas as pd | ||
|
||
__author__ = 'Bruno Quint' | ||
from .tools import io, version | ||
|
||
log = io.MyLogger(__name__) | ||
|
||
log.basicConfig(format='%(levelname)s: %(name)s(%(funcName)s): %(message)s', | ||
level=log.INFO) | ||
__author__ = 'Bruno Quint' | ||
|
||
|
||
def main(): | ||
|
||
# Parsing Arguments ------------------------------------------------------- | ||
parser = argparse.ArgumentParser( | ||
description="Build a data-cube from image files.") | ||
|
@@ -54,10 +56,15 @@ def main(): | |
|
||
parsed_args = parser.parse_args() | ||
|
||
if parsed_args.quiet: | ||
log.basicConfig(level=log.ERROR) | ||
if parsed_args.debug: | ||
log.basicConfig(level=log.DEBUG) | ||
log.set_verbose(verbose=not parsed_args.quiet) | ||
log.set_debug(debug=parsed_args.debug) | ||
|
||
log.info("") | ||
log.info("SAM-FP Tools: mkcube") | ||
log.info("by Bruno Quint ([email protected])") | ||
log.info("version {:s}".format(version.__str__)) | ||
log.info("Starting program.") | ||
log.info("") | ||
|
||
make_cube(parsed_args.files, | ||
output=parsed_args.output, | ||
|
@@ -193,10 +200,10 @@ def make_cube(list_of_files, z_key='FAPEROTZ', combine_algorithm='average', | |
hdr.add_blank('--- Channels and Files ---', before='CHAN_001') | ||
|
||
filename = output | ||
output = safesave(output, verbose=True) | ||
output = io.safe_save(output, verbose=True) | ||
|
||
log.info('Writing file to %s' % output) | ||
pyfits.writeto(output, cube, hdr, clobber=True) | ||
log.info('Writing file to {:s}'.format(output)) | ||
pyfits.writeto(output, cube, hdr, overwrite=True) | ||
|
||
log.debug(pd.DataFrame(data={'x': z, | ||
'y': z_array, | ||
|
@@ -207,52 +214,5 @@ def make_cube(list_of_files, z_key='FAPEROTZ', combine_algorithm='average', | |
return | ||
|
||
|
||
def safesave(name, overwrite=False, verbose=False): | ||
""" | ||
This is a generic method used to check if a file called 'name' | ||
already exists. If so, it starts some interaction with the user. | ||
Parameters: | ||
name : str | ||
The name of the file that will be written in the future. | ||
overwrite : bool | ||
If False, this method will interact with the user to ask if 'name' | ||
file shall be overwritten or if a new name will be given. If True, | ||
'name' file is automatically overwritten. | ||
verbose : bool | ||
force verbose mode on even when overwrite is automatic. | ||
""" | ||
import os | ||
import sys | ||
|
||
v = False if (overwrite is True) else True | ||
if v: | ||
print("\n Writing to output file %s" % name) | ||
|
||
while os.path.exists(name): | ||
|
||
if overwrite in ['y', 'Y', True]: | ||
if v or verbose: | ||
print(" Overwriting %s file." % name) | ||
os.remove(name) | ||
|
||
elif overwrite in ['', 'n', 'N', False]: | ||
name = input(" Please, enter a new filename:\n > ") | ||
|
||
elif overwrite in ['q']: | ||
if v: | ||
print(" Exiting program.") | ||
sys.exit() | ||
|
||
else: | ||
overwrite = input(" '%s' file exist. Overwrite? (y/[n])" % name) | ||
if v: | ||
print(" Writing data-cube to %s" % name) | ||
|
||
return name | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,8 +9,6 @@ | |
|
||
from __future__ import division, print_function | ||
|
||
from .tools import io | ||
|
||
import argparse | ||
import os | ||
import sys | ||
|
@@ -20,10 +18,13 @@ | |
import numpy as np | ||
from scipy.interpolate import UnivariateSpline | ||
|
||
from .tools import io, version | ||
|
||
log = io.MyLogger(__name__) | ||
|
||
|
||
def main(): | ||
|
||
# Setting Options --------------------------------------------------------- | ||
parser = argparse.ArgumentParser( | ||
description="Apply a phase-map on a data-cube." | ||
) | ||
|
@@ -61,14 +62,16 @@ def main(): | |
) | ||
|
||
args = parser.parse_args() | ||
v = not args.quiet | ||
log.set_verbose(verbose=not args.quiet) | ||
|
||
# Printing program header -------------------------------------------------- | ||
if v: | ||
start = time.time() | ||
print("\n Phase-Map Apply") | ||
print(" by Bruno Quint & Fabricio Ferrari") | ||
print(" version 0.0 - Feb 2014") | ||
start = time.time() | ||
log.info("") | ||
log.info("SAM-FP Tools: PHase-Map Apply") | ||
log.info("by Bruno Quint ([email protected])") | ||
log.info("version {:s}".format(version.__str__)) | ||
log.info("Starting program.") | ||
log.info("") | ||
|
||
root_dir = os.path.dirname(args.cube_file) | ||
cube_file = args.cube_file | ||
|
@@ -79,50 +82,45 @@ def main(): | |
else: | ||
out_file = args.output | ||
|
||
if v: | ||
print(" \n Root dir: %s" % root_dir) | ||
print(" Cube to be corrected: %s" % cube_file) | ||
print(" Phase-map to be applied: %s" % map_file) | ||
print(" Output corrected cube: %s" % out_file) | ||
log.info("Root dir: %s" % root_dir) | ||
log.info("Cube to be corrected: %s" % cube_file) | ||
log.info("Phase-map to be applied: %s" % map_file) | ||
log.info("Output corrected cube: %s" % out_file) | ||
|
||
# Systemic wavelength ------------------------------------------------------ | ||
vel = args.speed | ||
c = 299792 # km/s | ||
wavelength = args.wavelength * np.sqrt((1 + vel / c) / (1 - vel / c)) | ||
if v: | ||
print(" Emitted/systemic wavelength: %.2f A" % args.wavelength) | ||
print(" Systemic velocity: %.2f km/s" % vel) | ||
print(" Observed wavelength: %.2f A" % wavelength) | ||
|
||
# Reading input data ------------------------------------------------------ | ||
if v: | ||
print("\n Reading cube to be corrected.") | ||
log.info("") | ||
log.info("Emitted/systemic wavelength: %.2f A" % args.wavelength) | ||
log.info("Systemic velocity: %.2f km/s" % vel) | ||
log.info("Observed wavelength: %.2f A" % wavelength) | ||
|
||
# Reading input data ------------------------------------------------------ | ||
log.info("") | ||
log.info("Reading cube to be corrected.") | ||
data_cube = pyfits.open(cube_file)[0] | ||
log.info("Done.") | ||
|
||
if v: | ||
print(" Done.") | ||
print("\n Reading phase-map to be applied.") | ||
|
||
log.info("Reading phase-map to be applied.") | ||
phase_map = pyfits.open(map_file)[0] | ||
|
||
if v: | ||
print(" Done.") | ||
log.info("Done.") | ||
|
||
# Checking data ----------------------------------------------------------- | ||
if data_cube.data[0].shape != phase_map.shape: | ||
print("[!] Cube and map does not have matching width and height.") | ||
print("[!] Leaving now.\n") | ||
log.error("Cube and map does not have matching width and height.") | ||
log.error("[!] Leaving now.\n") | ||
sys.exit() | ||
|
||
if data_cube.data.ndim != 3: | ||
print("[!] Cube file is not really a cube.") | ||
print("[!] Leaving now.\n") | ||
log.error("[!] Cube file is not really a cube.") | ||
log.error("[!] Leaving now.\n") | ||
sys.exit() | ||
|
||
if phase_map.data.ndim != 2: | ||
print("[!] Map file is not really an image.") | ||
print("[!] Leaving now.\n") | ||
log.error("[!] Map file is not really an image.") | ||
log.error("[!] Leaving now.\n") | ||
sys.exit() | ||
|
||
m = data_cube.header['NAXIS1'] | ||
|
@@ -137,36 +135,33 @@ def main(): | |
|
||
# Reading the Free-Spectral-Range -------------------------------------- | ||
try: | ||
if v: | ||
print(" Reading free-spectral-range from cube header.") | ||
log.info("") | ||
log.info("Reading free-spectral-range from cube header.") | ||
# TODO add an option to use the FSR found while extracting | ||
# TODO the phase-map or while fitting it. | ||
# TODO or even to give the option for the user to enter it. | ||
cal_fsr = phase_map.header['PHM_FSR'] | ||
cal_wavelength = phase_map.header['PHMWCAL'] | ||
f_s_r = cal_fsr / cal_wavelength * wavelength | ||
if v: | ||
print(" Free Spectral Range = %.2f %s" % (f_s_r, units)) | ||
log.info(" Free Spectral Range = %.2f %s" % (f_s_r, units)) | ||
|
||
except KeyError: | ||
print(" Please, enter the free-spectral-range in %s units" % units) | ||
f_s_r = io.input(" > ") | ||
log.info("Please, enter the free-spectral-range in %s units" % units) | ||
f_s_r = io.input(" >") | ||
|
||
f_s_r = round(f_s_r / abs(sample)) # From BCV to Channels | ||
if v: | ||
print(" Free-Spectral-Range is %d channels" % f_s_r) | ||
log.info("Free-Spectral-Range is %d channels" % f_s_r) | ||
|
||
fsr = f_s_r * args.npoints # From Channels to nPoints | ||
fsr = int(round(fsr)) | ||
if v: | ||
print(" Free-Spectral-Range is %d points" % fsr) | ||
log.info("Free-Spectral-Range is %d points" % fsr) | ||
|
||
# Assure that the reference spectrum will not be moved ---------------- | ||
try: | ||
phase_map.data = phase_map.data - phase_map.data[ref_y, ref_x] | ||
except IndexError: | ||
print("[!] Reference pixel out of field.") | ||
print("[!] Skipping reference pixel map subtraction.") | ||
log.warn("Reference pixel out of field.") | ||
log.warn("Skipping reference pixel map subtraction.") | ||
pass | ||
phase_map.data *= -1 | ||
|
||
|
@@ -177,8 +172,8 @@ def main(): | |
phase_map.data = phase_map.data * args.npoints | ||
|
||
# Applying phase-map -------------------------------------------------- | ||
if v: | ||
print("\n Applying phase-map:") | ||
log.info("") | ||
log.info("Applying phase-map:") | ||
|
||
n_channels = data_cube.header['NAXIS3'] | ||
z = np.arange(3 * n_channels) - n_channels | ||
|
@@ -211,15 +206,14 @@ def main(): | |
data_cube.data[:, j, i] = spec[n_channels:2 * n_channels] | ||
|
||
# Giving a feedback to the user | ||
if v: | ||
if not args.quiet: | ||
temp = ((i + 1) * 100.00 / m) | ||
sys.stdout.write('\r %2.2f%% ' % temp) | ||
sys.stdout.write('\r %2.2f%% ' % temp) | ||
sys.stdout.flush() | ||
|
||
end_of_cube = min(int(round(f_s_r)), data_cube.data.shape[0]) | ||
data_cube.data = data_cube.data[0:end_of_cube, :, :] | ||
if v: | ||
print(" Done.") | ||
log.info(" Done.") | ||
|
||
if args.center: | ||
collapsed_cube = data_cube.data.sum(axis=2).sum(axis=1) | ||
|
@@ -252,21 +246,20 @@ def main(): | |
# data_cube.header.add_blank(after='PHMFIT_C') | ||
|
||
# Saving corrected data-cube ---------------------------------------------- | ||
if v: | ||
print("\n Writing output to file %s." % out_file) | ||
log.info("Writing output to file %s." % out_file) | ||
|
||
try: | ||
data_cube.writeto(out_file, overwrite=True) | ||
except TypeError: | ||
data_cube.writeto(out_file, clobber=True) | ||
|
||
if v: | ||
print(" Done.") | ||
# noinspection PyUnboundLocalVariable | ||
end = time.time() - start | ||
print("\n Total time ellapsed: {0:02d}:{1:02d}:{2:02d}".format( | ||
log.info("Done.") | ||
|
||
end = time.time() - start | ||
log.info("") | ||
log.info("Total time ellapsed: {0:02d}:{1:02d}:{2:02d}".format( | ||
int(end // 3600), int(end % 3600 // 60), int(end % 60))) | ||
print(" All done!\n") | ||
log.info(" All done!\n") | ||
|
||
|
||
# Method shift_spectrum ======================================================== | ||
|
@@ -318,35 +311,3 @@ def shift_spectrum(spec, dz, fsr=-1, sample=1.0, n_points=100): | |
spec = spline(z) | ||
|
||
return spec | ||
|
||
|
||
def error(my_string): | ||
s = BColors.FAIL + '[ERROR] ' + BColors.ENDC | ||
s = s + my_string | ||
print(s) | ||
return | ||
|
||
|
||
def warning(my_string): | ||
s = BColors.WARNING + '[WARNING] ' + BColors.ENDC | ||
s = s + my_string | ||
print(s) | ||
return | ||
|
||
|
||
# noinspection PyClassHasNoInit | ||
class BColors: | ||
HEADER = '\033[95m' | ||
OKBLUE = '\033[94m' | ||
OKGREEN = '\033[92m' | ||
WARNING = '\033[93m' | ||
FAIL = '\033[91m' | ||
ENDC = '\033[0m' | ||
|
||
def disable(self): | ||
self.HEADER = '' | ||
self.OKBLUE = '' | ||
self.OKGREEN = '' | ||
self.WARNING = '' | ||
self.FAIL = '' | ||
self.ENDC = '' |
Oops, something went wrong.