Skip to content

Commit

Permalink
Merge branch '100-write-unit-test-for-astrohackextract_holog' into 13…
Browse files Browse the repository at this point in the history
…2-generate_holog_obs_dict-ingnoring-point_name-input
  • Loading branch information
jrhosk committed Aug 8, 2023
2 parents 3434e2e + a3b6289 commit 12814bd
Show file tree
Hide file tree
Showing 4 changed files with 229 additions and 48 deletions.
24 changes: 12 additions & 12 deletions src/astrohack/_utils/_extract_holog.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ def _create_holog_file(
)


def _create_holog_obs_dict(pnt_dict,baseline_average_distance,baseline_average_nearest,ant_names,ant_pos,ant_names_main):
def _create_holog_obs_dict(pnt_dict, baseline_average_distance, baseline_average_nearest, ant_names, ant_pos, ant_names_main):
'''
Generate holog_obs_dict.
'''
Expand Down Expand Up @@ -524,33 +524,33 @@ def _create_holog_meta_data(holog_file, holog_dict, input_params):

for ddi, map_dict in holog_dict.items():
if "ddi_" in ddi:
for map, ant_dict in map_dict.items():
if "map_" in map:
for mapping, ant_dict in map_dict.items():
if "map_" in mapping:
for ant, xds in ant_dict.items():
if "ant_" in ant:
if ant not in ant_holog_dict:
ant_holog_dict[ant] = {ddi:{map:{}}}
ant_holog_dict[ant] = {ddi:{mapping:{}}}
elif ddi not in ant_holog_dict[ant]:
ant_holog_dict[ant][ddi] = {map:{}}
ant_holog_dict[ant][ddi] = {mapping:{}}

ant_holog_dict[ant][ddi][map] = xds.to_dict(data=False)
ant_holog_dict[ant][ddi][mapping] = xds.to_dict(data=False)
cell_sizes.append(xds.attrs["grid_parms"]["cell_size"])
n_pixs.append(xds.attrs["grid_parms"]["n_pix"])
telescope_names.append(xds.attrs['telescope_name'])

cell_sizes_sigfigs = _significant_digits(cell_sizes, digits=3)

if not (len(set(cell_sizes_sigfigs)) == 1):
logger.error('Cell size not consistant: ' + str(cell_sizes))
raise
logger.error('Cell size not consistent: ' + str(cell_sizes))
raise Exception('Cell size not consistent: ' + str(cell_sizes))

if not (len(set(n_pixs)) == 1):
logger.error('Number of pixels not consistant: ' + str(n_pixs))
raise
logger.error('Number of pixels not consistent: ' + str(n_pixs))
#raise Exception('Number of pixels not consistent: ' + str(n_pixs))

if not (len(set(telescope_names)) == 1):
logger.error('Telescope name not consistant: ' + str(telescope_names))
raise
logger.error('Telescope name not consistent: ' + str(telescope_names))
raise Exception('Telescope name not consistent: ' + str(telescope_names))

output_meta_file = "{name}/{ext}".format(name=holog_file, ext=".holog_json")

Expand Down
50 changes: 17 additions & 33 deletions src/astrohack/extract_holog.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@
from astrohack._utils._tools import _remove_suffix
from astrohack._utils._tools import _jsonify

from astrohack._utils._extract_holog import _create_holog_obs_dict

from astrohack.mds import AstrohackHologFile
from astrohack.mds import AstrohackPointFile

from astrohack.extract_pointing import extract_pointing

Expand All @@ -53,7 +56,7 @@ def extract_holog(
:param ms_name: Name of input measurement file name.
:type ms_name: str
:param point_name: Name of *<point_name>.point.zarr* file to use.
:param point_name: Name of *<point_name>.point.zarr* file to use. This is must be provided.
:type holog_name: str
:param holog_name: Name of *<holog_name>.holog.zarr* file to create. Defaults to measurement set name with *holog.zarr* extension.
Expand Down Expand Up @@ -439,7 +442,7 @@ def _check_extract_holog_params(function_name, extract_holog_params):
if not parm_check:
logger.error(f'[{function_name}]: Parameter holog_obs_dict must be of type {str(dict)}.')

parms_passed = parms_passed and _check_parms(function_name, extract_holog_params, 'baseline_average_distance', [float, int, str], default='all')
parms_passed = parms_passed and _check_parms(function_name, extract_holog_params, 'baseline_average_distance', [int, float, str], default='all')

parms_passed = parms_passed and _check_parms(function_name, extract_holog_params, 'baseline_average_nearest', [int, str], default='all')

Expand All @@ -460,15 +463,14 @@ def _check_extract_holog_params(function_name, extract_holog_params):

def generate_holog_obs_dict(
ms_name,
point_name=None,
point_name,
ddi='all',
baseline_average_distance='all',
baseline_average_nearest='all',
overwrite=False,
parallel=False
):
"""
Extract holography and optionally pointing data, from measurement set. Creates holography output file.
Generate holography observation dictionary, from measurement set..
:param ms_name: Name of input measurement file name.
:type ms_name: str
Expand All @@ -482,12 +484,9 @@ def generate_holog_obs_dict(
:param baseline_average_nearest: To increase the signal to noise for a mapping antenna mutiple reference antennas can be used. The baseline_average_nearest is the number of nearest reference antennas to use. The baseline_average_nearest is only used if the holog_obs_dict is not specified. baseline_average_distance and baseline_average_nearest can not be used together.
:type holog_obs_dict: int, optional
:param point_name: Name of *<point_name>.point.zarr* file to create. Defaults to measurement set name with *point.zarr* extension.
:param point_name: Name of *<point_name>.point.zarr* file to use.
:type point_name: str, optional
:param overwrite: Boolean for whether to overwrite current holog.zarr and point.zarr files, defaults to False.
:type overwrite: bool, optional
:param parallel: Boolean for whether to process in parallel. Defaults to False
:type parallel: bool, optional
Expand Down Expand Up @@ -558,26 +557,8 @@ def generate_holog_obs_dict(

function_name = inspect.stack()[CURRENT_FUNCTION].function

_check_if_file_exists(extract_holog_params['ms_name'])

if os.path.exists(point_name) is False or point_name==None:

logger.debug('[{caller}]: File {file} does not exists. Extracting ...'.format(caller=function_name, file=point_name))

from astrohack._utils._tools import _remove_suffix

point_name = _remove_suffix(ms_name, '.ms') + '.point.zarr'
extract_holog_params['point_name'] = point_name

logger.debug('[{caller}]: Extracting pointing to {output}'.format(caller=function_name, output=point_name))


pnt_dict = extract_pointing(
ms_name=extract_holog_params['ms_name'],
point_name=extract_holog_params['point_name'],
parallel=extract_holog_params['parallel'],
overwrite=extract_holog_params['overwrite']
)
_check_if_file_exists(ms_name)
_check_if_file_exists(point_name)

######## Get Spectral Windows ########
ctb = ctables.table(
Expand All @@ -586,9 +567,11 @@ def generate_holog_obs_dict(
lockoptions={"option": "usernoread"},
ack=False,
)

ddi_spw = ctb.getcol("SPECTRAL_WINDOW_ID")
ddpol_indexol = ctb.getcol("POLARIZATION_ID")
ms_ddi = np.arange(len(ddi_spw))

ctb.close()

######## Get Antenna IDs and Names ########
Expand Down Expand Up @@ -622,19 +605,20 @@ def generate_holog_obs_dict(

# Create holog_obs_dict or modify user supplied holog_obs_dict.
ddi = extract_holog_params['ddi']


from astrohack._utils._extract_holog import _create_holog_obs_dict
pnt_mds = AstrohackPointFile(extract_holog_params['point_name'])
pnt_mds._open()

holog_obs_dict = _create_holog_obs_dict(
pnt_dict,
pnt_mds,
extract_holog_params['baseline_average_distance'],
extract_holog_params['baseline_average_nearest'],
ant_names,
ant_pos,
ant_names_main
)

#From the generated holog_obs_dict subselect user supplied ddis.
#From the generated holog_obs_dict subselect user supplied ddis.
if ddi != 'all':
holog_obs_dict_keys = list(holog_obs_dict.keys())
for ddi_key in holog_obs_dict_keys:
Expand Down
22 changes: 19 additions & 3 deletions src/astrohack/extract_pointing.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,30 @@ def extract_pointing(
function_name = inspect.stack()[CURRENT_FUNCTION].function

######### Parameter Checking #########
extract_pointing_params = _check_extract_pointing_params(function_name=function_name, extract_point_params=extract_pointing_params)
extract_pointing_params = _check_extract_pointing_params(
function_name=function_name,
extract_point_params=extract_pointing_params
)

input_params = extract_pointing_params.copy()

try:
_check_if_file_exists(extract_pointing_params['ms_name'])
_check_if_file_will_be_overwritten(extract_pointing_params['point_name'], extract_pointing_params['overwrite'])


# Until check params is changed, comment this out.
'''
if point_name==None:
logger.debug('[{caller}]: File {file} does not exists. Extracting ...'.format(caller=function_name, file=point_name))
from astrohack._utils._tools import _remove_suffix
point_name = _remove_suffix(ms_name, '.ms') + '.point.zarr'
extract_holog_params['point_name'] = point_name
logger.debug('[{caller}]: Extracting pointing to {output}'.format(caller=function_name, output=point_name))
'''

pnt_dict = _extract_pointing(
ms_name=extract_pointing_params['ms_name'],
Expand All @@ -110,7 +126,7 @@ def extract_pointing(
except Exception as exception:
logger.error("There was an error, exiting:: Exception: {exception}".format(exception=exception))

return
return None

def _check_extract_pointing_params(function_name, extract_point_params):

Expand Down
Loading

0 comments on commit 12814bd

Please sign in to comment.