Skip to content

Commit

Permalink
Merge pull request #129 from casangi/127-generate-holog-obs-and-extra…
Browse files Browse the repository at this point in the history
…ct-holog-fix

127 generate holog obs and extract holog fix
  • Loading branch information
jrhosk authored Aug 1, 2023
2 parents 5e6dc30 + 19ccc44 commit 980a94d
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 1,193 deletions.
4 changes: 3 additions & 1 deletion src/astrohack/_utils/_dio.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,14 +222,16 @@ def _write_fits(header, imagetype, data, filename, unit, origin):
return


def _create_destination_folder(caller, destination):
def _create_destination_folder(destination):
"""
Try to create a folder if it already exists raise a warning
Args:
caller: Calling function
destination: the folder to be created
"""
logger = _get_astrohack_logger()
caller=inspect.stack()[CALLING_FUNCTION].function

try:
os.mkdir(destination)
except FileExistsError:
Expand Down
5 changes: 4 additions & 1 deletion src/astrohack/_utils/_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,13 @@ def _remove_suffix(input_string, suffix):

return input_string


# DEPRECATED
def _jsonify(holog_obj):
""" Convert holog_obs_description dictionay to json format. This just means converting numpy.ndarry
entries to string lists.
# DEPRECATED
:param holog_obj: holog_obs_description dictionary.
:type holog_obj: dict
:param holog_obj: holog_obs_description dictionary.
Expand Down Expand Up @@ -153,6 +155,7 @@ def _parm_to_list(caller, parm, data_dict, prefix):
"""
logger = _get_astrohack_logger()

if parm == 'all':
oulist = list(data_dict.keys())
elif isinstance(parm, str):
Expand Down
34 changes: 11 additions & 23 deletions src/astrohack/extract_holog.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

from casacore import tables as ctables

from astrohack._utils._tools import NumpyEncoder

from astrohack._utils._constants import pol_str
from astrohack._utils._conversion import _convert_ant_name_to_id
from astrohack._utils._extract_holog import _create_holog_meta_data
Expand Down Expand Up @@ -57,7 +59,7 @@ def extract_holog(
:param holog_name: Name of *<holog_name>.holog.zarr* file to create. Defaults to measurement set name with *holog.zarr* extension.
:type holog_name: str, optional
:param holog_obs_dict: The *holog_obs_dict* describes which scan and antenna data to extract from the measurement set. As detailed below, this compound dictionary also includes important meta data needed for preprocessing and extraction of the holography data from the measurement set. If not specified holog_obs_dict will be generated. For auto generation of the holog_obs_dict the assumtion is made that the same antanna beam is not mapped twice in a row (alternating sets of antennas is fine).
:param holog_obs_dict: The *holog_obs_dict* describes which scan and antenna data to extract from the measurement set. As detailed below, this compound dictionary also includes important meta data needed for preprocessing and extraction of the holography data from the measurement set. If not specified holog_obs_dict will be generated. For auto generation of the holog_obs_dict the assumtion is made that the same antanna beam is not mapped twice in a row (alternating sets of antennas is fine). If the holog_obs_dict is specified, the ddi input is ignored.
:type holog_obs_dict: dict, optional
:param ddi: DDI(s) that should be extracted from the measurement set. Defaults to all DDI's in the ms.
Expand Down Expand Up @@ -214,7 +216,8 @@ def extract_holog(
# Create holog_obs_dict or modify user supplied holog_obs_dict.
ddi = extract_holog_params['ddi']

if holog_obs_dict is None: #Automatically create holog_obs_dict
# Create holog_obs_dict if not specified
if holog_obs_dict is None:
from astrohack._utils._extract_holog import _create_holog_obs_dict
holog_obs_dict = _create_holog_obs_dict(
pnt_dict,
Expand All @@ -234,27 +237,15 @@ def extract_holog(
if ddi_id not in ddi:
del holog_obs_dict[ddi_key]

else:
#If a user defines a holog_obs_dict it needs to be duplicated for each ddi.
holog_obs_dict_with_ddi = {}
if ddi == 'all':
for ddi_id in ms_ddi:
holog_obs_dict_with_ddi['ddi_' + str(ddi_id)] = holog_obs_dict
else:
for ddi_id in ddi:
holog_obs_dict_with_ddi['ddi_' + str(ddi_id)] = holog_obs_dict

holog_obs_dict = holog_obs_dict_with_ddi

logger.info(f"[{function_name}]: holog_obs_dict: \n%s", pformat(list(holog_obs_dict.values())[0], indent=2, width=2))


outfile_obj = copy.deepcopy(holog_obs_dict)

_jsonify(outfile_obj)
encoded_obj = json.dumps(holog_obs_dict, cls=NumpyEncoder)

with open(".holog_obs_dict.json", "w") as outfile:
json.dump(outfile_obj, outfile)
json.dump(encoded_obj, outfile)


######## Get Scan and Subscan IDs ########
# SDM Tables Short Description (https://drive.google.com/file/d/16a3g0GQxgcO7N_ZabfdtexQ8r2jRbYIS/view)
Expand Down Expand Up @@ -651,12 +642,9 @@ def generate_holog_obs_dict(
if ddi_id not in ddi:
del holog_obs_dict[ddi_key]


outfile_obj = copy.deepcopy(holog_obs_dict)

_jsonify(outfile_obj)
encoded_obj = json.dumps(holog_obs_dict, cls=NumpyEncoder)

with open(".holog_obs_dict.json", "w") as outfile:
json.dump(outfile_obj, outfile)
json.dump(encoded_obj, outfile)

return outfile_obj
return json.loads(encoded_obj)
14 changes: 7 additions & 7 deletions src/astrohack/mds.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ def export_to_fits(self, destination, complex_split='cartesian', ant_id=None, dd
parms_passed = parms_passed and _check_parms(fname, parm_dict, 'parallel', [bool], default=True)

_parm_check_passed(fname, parms_passed)
_create_destination_folder(fname, parm_dict['destination'])
_create_destination_folder(parm_dict['destination'])
parm_dict['metadata'] = self._meta_data
_dask_general_compute(fname, self, _export_to_fits_holog_chunk, parm_dict, ['ant', 'ddi'], parallel=parallel)

Expand Down Expand Up @@ -280,7 +280,7 @@ def plot_apertures(self, destination, ant_id=None, ddi=None, plot_screws=False,
parms_passed = parms_passed and _check_parms(fname, parm_dict, 'dpi', [int], default=300)

_parm_check_passed(fname, parms_passed)
_create_destination_folder(fname, parm_dict['destination'])
_create_destination_folder(parm_dict['destination'])
_dask_general_compute(fname, self, _plot_aperture_chunk, parm_dict, ['ant', 'ddi'], parallel=parallel)

def plot_beams(self, destination, ant_id=None, ddi=None, complex_split='polar', display=True, colormap='viridis',
Expand Down Expand Up @@ -338,7 +338,7 @@ def plot_beams(self, destination, ant_id=None, ddi=None, complex_split='polar',
parms_passed = parms_passed and _check_parms(fname, parm_dict, 'dpi', [int], default=300)

_parm_check_passed(fname, parms_passed)
_create_destination_folder(fname, parm_dict['destination'])
_create_destination_folder(parm_dict['destination'])
_dask_general_compute(fname, self, _plot_beam_chunk, parm_dict, ['ant', 'ddi'], parallel=parallel)


Expand Down Expand Up @@ -533,7 +533,7 @@ def plot_diagnostics(self, destination, delta=0.01, ant_id=None, ddi=None, map_i
parms_passed = parms_passed and _check_parms(fname, parm_dict, 'parallel', [bool], default=False)

_parm_check_passed(fname, parms_passed)
_create_destination_folder(fname, parm_dict['destination'])
_create_destination_folder(parm_dict['destination'])
key_order = ["ddi", "map", "ant"]
_dask_general_compute(fname, self, _calibration_plot_chunk, parm_dict, key_order, parallel)

Expand Down Expand Up @@ -678,7 +678,7 @@ def export_screws(self, destination, ant_id=None, ddi=None, unit='mm', threshold
parms_passed = parms_passed and _check_parms(fname, parm_dict, 'dpi', [int], default=300)

_parm_check_passed(fname, parms_passed)
_create_destination_folder(fname, parm_dict['destination'])
_create_destination_folder(parm_dict['destination'])
_dask_general_compute(fname, self, _export_screws_chunk, parm_dict, ['ant', 'ddi'], parallel=False)

def plot_antennae(self, destination, ant_id=None, ddi=None, plot_type='deviation', plot_screws=False, unit=None,
Expand Down Expand Up @@ -769,7 +769,7 @@ def plot_antennae(self, destination, ant_id=None, ddi=None, plot_type='deviation
parms_passed = parms_passed and _check_parms(fname, parm_dict, 'dpi', [int], default=300)

_parm_check_passed(fname, parms_passed)
_create_destination_folder(fname, parm_dict['destination'])
_create_destination_folder(parm_dict['destination'])
_dask_general_compute(fname, self, _plot_antenna_chunk, parm_dict, ['ant', 'ddi'], parallel=parallel)

def export_to_fits(self, destination, ant_id=None, ddi=None, parallel=False):
Expand Down Expand Up @@ -806,7 +806,7 @@ def export_to_fits(self, destination, ant_id=None, ddi=None, parallel=False):
parms_passed = parms_passed and _check_parms(fname, parm_dict, 'parallel', [bool], default=True)

_parm_check_passed(fname, parms_passed)
_create_destination_folder(fname, parm_dict['destination'])
_create_destination_folder(parm_dict['destination'])
_dask_general_compute(fname, self, _export_to_fits_panel_chunk, parm_dict, ['ant', 'ddi'], parallel=parallel)


Expand Down
Loading

0 comments on commit 980a94d

Please sign in to comment.