Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates to conversion/schema/spreadsheet to sync them to the latest version #255

Merged
merged 50 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
515a7ce
conversion: add processor_info dict, #247
FedeMPouzols Sep 11, 2024
f958b04
conversion: add observation_info dict, make it mandatory in schema ch…
FedeMPouzols Sep 11, 2024
edc4c78
observation_info: leave fields empty when empty OBSERVATION, #247
FedeMPouzols Sep 11, 2024
8579e5b
add create_info_dicts, #247
FedeMPouzols Sep 11, 2024
2fcfb6b
docs for create_info_dicts, move out to msv4_info_dics, #247
FedeMPouzols Sep 11, 2024
0ad1043
small rearrangement of steps in conversion, #247
FedeMPouzols Sep 11, 2024
47053a8
Merge remote-tracking branch 'origin/main' into 247-xradio-check-sche…
FedeMPouzols Sep 11, 2024
814217a
fix missing imports, date str format, #247
FedeMPouzols Sep 11, 2024
40aec0c
update vis_tutorial with new info_dicts, #247
FedeMPouzols Sep 11, 2024
53b1a6e
get (optional) exec_block info when available in ASDM_EXECBLOCK, #247
FedeMPouzols Sep 11, 2024
9c8b008
fix autodoc of antenna_name class, #247
FedeMPouzols Sep 12, 2024
525ce58
add time_cal (using TimeCoordArrayBase) and use it in sys_cal_xds spe…
FedeMPouzols Sep 12, 2024
05d2cad
rename ephemris->ephemeris, #247
FedeMPouzols Sep 12, 2024
b990877
comments and formatting minor things, #247
FedeMPouzols Sep 12, 2024
a4a7ce3
add create_system_calibration_xds(), #247
FedeMPouzols Sep 12, 2024
d784635
convert, read/load sys_cal_xds, propagate sys_cal_interp option, #247
FedeMPouzols Sep 12, 2024
ba6797b
reorganize create_system_calibration_xds(), split prepare_generic_sys…
FedeMPouzols Sep 12, 2024
79e21f9
do not enable phase_cal/sys_cal interp for now, #247
FedeMPouzols Sep 12, 2024
542a2ad
Merge remote-tracking branch 'origin/main' into 247-xradio-check-sche…
FedeMPouzols Sep 12, 2024
b17ca8a
fix import toolviper logger, #247
FedeMPouzols Sep 12, 2024
baa051d
partition_info: sync conversion+schema with spreadsheet, #247
FedeMPouzols Sep 12, 2024
48e4ba1
add TimeEphemeris coord in schema, sync field_and_source_xds with spr…
FedeMPouzols Sep 12, 2024
c8e15de
Use time_ephemeris when not interpolating to main time, #247
FedeMPouzols Sep 12, 2024
468ed01
Merge remote-tracking branch 'origin/main' into 247-xradio-check-sche…
FedeMPouzols Sep 13, 2024
14b0193
station_id->station_name in weather_xds, handle ids when loading subt…
FedeMPouzols Sep 13, 2024
fe1ac9f
add frequency_cal and its metadata, use FrequencyCalArray in schema, …
FedeMPouzols Sep 13, 2024
89222a8
pointing schema: add time_pointing and local_sky_dir_label (+ arrays)…
FedeMPouzols Sep 13, 2024
9a4c0a5
sync pointing_xds with spreadsheet, #247
FedeMPouzols Sep 13, 2024
892546e
remove scan_number from poniting_xds, #247
FedeMPouzols Sep 13, 2024
c1b8135
pointing: remove old code for removed vars/attrs, #247
FedeMPouzols Sep 13, 2024
fcdea5f
split gain_curve_xds schema out of antenna_xds, #247
FedeMPouzols Sep 13, 2024
487b815
turn extract_gain_curve_info=>create_gain_curve_xds, #247
FedeMPouzols Sep 13, 2024
4f841e7
reduce indentation levels, #247
FedeMPouzols Sep 13, 2024
239f8a9
add gain_curve_xds to conversion and read_ps, #247
FedeMPouzols Sep 13, 2024
693390f
sync GAIN_CURVE dims, do not transpose poly_term, #247
FedeMPouzols Sep 13, 2024
cbb46aa
split phase_calibration_xds schema out of antenna_xds, adjust optiona…
FedeMPouzols Sep 13, 2024
a37884f
turn extract_phase_cal_info=>create_phase_calibration_xds, #247
FedeMPouzols Sep 13, 2024
e36fb9e
simplify indentation, more checks, #247
FedeMPouzols Sep 13, 2024
373f4f1
checks for opt vars and add coords from ant_xds, #247
FedeMPouzols Sep 13, 2024
91d901a
ad phase_calibration_xds in conversion and read_ps, #247
FedeMPouzols Sep 13, 2024
0d79bb1
add gain_curve_xds and phase_calibration_xds to vis_schema page, #247
FedeMPouzols Sep 13, 2024
5dd7dad
fix some sphinx warnings, #247
FedeMPouzols Sep 13, 2024
6b6a23a
add newer attributes in main_xds, add phase_cal and gain_curve xds in…
FedeMPouzols Sep 13, 2024
29d204b
add newer attrs (versions/date) in main_xds/main_sd_xds, #247
FedeMPouzols Sep 13, 2024
a1eedc3
Merge remote-tracking branch 'origin/main' into 247-xradio-check-sche…
FedeMPouzols Sep 13, 2024
b3b2f57
gain_curve/phase_cal: update optionality of vars, gain_curve: add mea…
FedeMPouzols Sep 13, 2024
78ff50b
add descriptions of gain_cal and phase_cal xds vars/coords, 247
FedeMPouzols Sep 13, 2024
d57401c
circumvent problems with ufunc and string types/lengths, #247
FedeMPouzols Sep 13, 2024
7cf0a45
fix toolviper imports, update guides to latest schema/conversion, #247
FedeMPouzols Sep 13, 2024
0026917
replace graphviper/toolviper import, update tutorial to latest schema…
FedeMPouzols Sep 13, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions doc/source/image_data/tutorials/index.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Tutorials
======
=========

.. toctree::
:maxdepth: 1

image
image
245 changes: 142 additions & 103 deletions doc/source/observation_data/guides/ALMA_ephemeris.ipynb

Large diffs are not rendered by default.

243 changes: 185 additions & 58 deletions doc/source/observation_data/guides/ALMA_single_dish.ipynb

Large diffs are not rendered by default.

185 changes: 120 additions & 65 deletions doc/source/observation_data/guides/LOFAR.ipynb

Large diffs are not rendered by default.

113 changes: 85 additions & 28 deletions doc/source/observation_data/guides/MeerKAT.ipynb

Large diffs are not rendered by default.

115 changes: 86 additions & 29 deletions doc/source/observation_data/guides/SKA_mid.ipynb

Large diffs are not rendered by default.

4,557 changes: 3,645 additions & 912 deletions doc/source/observation_data/guides/VLBA.ipynb

Large diffs are not rendered by default.

1,941 changes: 1,610 additions & 331 deletions doc/source/observation_data/guides/VLBI.ipynb

Large diffs are not rendered by default.

176 changes: 122 additions & 54 deletions doc/source/observation_data/guides/ngEHT.ipynb

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions doc/source/observation_data/schema_and_api/index.rst
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Schema and API
======
==============

.. toctree::
:maxdepth: 1

vis_api
vis_schema
vis_schema
46 changes: 29 additions & 17 deletions doc/source/observation_data/schema_and_api/vis_schema.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,43 +23,55 @@ visibility or spectrum arrays have a :ref:`field_and_source_xds` sub-dataset.
Sub-datasets
------------

.. _field_and_source_xds:

field_and_source_xds
~~~~~~~~~~~~~~~~~~~~
.. autoclass:: xradio.vis.schema.FieldSourceXds()

.. xradio_dataset_schema_table:: xradio.vis.schema.FieldSourceXds

antenna_xds
~~~~~~~~~~~
.. autoclass:: xradio.vis.schema.AntennaXds()

.. xradio_dataset_schema_table:: xradio.vis.schema.AntennaXds

weather_xds
~~~~~~~~~~~
.. autoclass:: xradio.vis.schema.WeatherXds()

.. xradio_dataset_schema_table:: xradio.vis.schema.WeatherXds

pointing_xds
~~~~~~~~~~~~
.. autoclass:: xradio.vis.schema.PointingXds()

.. xradio_dataset_schema_table:: xradio.vis.schema.PointingXds

phased_array_xds
~~~~~~~~~~~~~~~~
.. autoclass:: xradio.vis.schema.PhasedArrayXds()
weather_xds
~~~~~~~~~~~
.. autoclass:: xradio.vis.schema.WeatherXds()

.. xradio_dataset_schema_table:: xradio.vis.schema.PhasedArrayXds
.. xradio_dataset_schema_table:: xradio.vis.schema.WeatherXds

system_calibration_xds
~~~~~~~~~~~~~~~~~~~~~~
.. autoclass:: xradio.vis.schema.SystemCalibrationXds()

.. xradio_dataset_schema_table:: xradio.vis.schema.SystemCalibrationXds

.. _field_and_source_xds:
gain_curve_xds
~~~~~~~~~~~~~~
.. autoclass:: xradio.vis.schema.GainCurveXds()

field_and_source_xds
~~~~~~~~~~~~~~~~~~~~
.. autoclass:: xradio.vis.schema.FieldSourceXds()
.. xradio_dataset_schema_table:: xradio.vis.schema.GainCurveXds

.. xradio_dataset_schema_table:: xradio.vis.schema.FieldSourceXds
phase_calibration_xds
~~~~~~~~~~~~~~~~~~~~~
.. autoclass:: xradio.vis.schema.PhaseCalibrationXds()

.. xradio_dataset_schema_table:: xradio.vis.schema.PhaseCalibrationXds

phased_array_xds
~~~~~~~~~~~~~~~~
.. autoclass:: xradio.vis.schema.PhasedArrayXds()

.. xradio_dataset_schema_table:: xradio.vis.schema.PhasedArrayXds

.. _info dictionaries:

Expand Down Expand Up @@ -148,9 +160,9 @@ as frequencies or timestamps.

.. xradio_array_schema_table:: xradio.vis.schema.BaselineArray

.. autoclass:: xradio.vis.schema.BaselineAntennaArray()
.. autoclass:: xradio.vis.schema.BaselineAntennaNameArray()

.. xradio_array_schema_table:: xradio.vis.schema.BaselineAntennaArray
.. xradio_array_schema_table:: xradio.vis.schema.BaselineAntennaNameArray

.. autoclass:: xradio.vis.schema.FrequencyArray()

Expand Down
4 changes: 2 additions & 2 deletions doc/source/observation_data/tutorials/index.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Tutorials
======
=========

.. toctree::
:maxdepth: 1

ps_vis
ps_vis
1,005 changes: 612 additions & 393 deletions doc/source/observation_data/tutorials/ps_vis.ipynb

Large diffs are not rendered by default.

1,112 changes: 608 additions & 504 deletions doc/source/observation_data/tutorials/ps_vis_full_dataset.ipynb

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions src/xradio/vis/_vis_utils/_ms/_tables/read.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,23 @@


def table_exists(path: str) -> bool:
"""
Whether a casacore table exists on disk (in the casacore.tables.tableexists sense)
"""
return tables.tableexists(path)


def table_has_column(path: str, column_name: str) -> bool:
"""
Whether a column is present in a casacore table
"""
with open_table_ro(path) as tb_tool:
if column_name in tb_tool.colnames():
return True
else:
return False


def convert_casacore_time(
rawtimes: np.ndarray, convert_to_datetime: bool = True
) -> np.ndarray:
Expand Down
132 changes: 91 additions & 41 deletions src/xradio/vis/_vis_utils/_ms/conversion.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
import datetime
import importlib
import numcodecs
import time
from .._zarr.encoding import add_encoding
from typing import Dict, Union
import toolviper.utils.logger as logger
import os
import pathlib
import time
from typing import Dict, Union

import numpy as np
import xarray as xr

import toolviper.utils.logger as logger
from casacore import tables
from xradio.vis._vis_utils._ms.msv4_sub_xdss import (
create_pointing_xds,
create_system_calibration_xds,
create_weather_xds,
)
from xradio.vis._vis_utils._ms.create_antenna_xds import create_antenna_xds
from .msv4_info_dicts import create_info_dicts
from xradio.vis._vis_utils._ms.create_antenna_xds import (
create_antenna_xds,
create_gain_curve_xds,
create_phase_calibration_xds,
)
from xradio.vis._vis_utils._ms.create_field_and_source_xds import (
create_field_and_source_xds,
)
Expand All @@ -25,6 +32,7 @@
col_dims,
)

from .._zarr.encoding import add_encoding
from .subtables import subt_rename_ids
from ._tables.table_query import open_table_ro, open_query
from ._tables.read import (
Expand Down Expand Up @@ -693,6 +701,7 @@ def convert_and_write_partition(
pointing_interpolate: bool = False,
ephemeris_interpolate: bool = False,
phase_cal_interpolate: bool = False,
sys_cal_interpolate: bool = False,
compressor: numcodecs.abc.Codec = numcodecs.Zstd(level=2),
storage_backend="zarr",
overwrite: bool = False,
Expand Down Expand Up @@ -723,6 +732,10 @@ def convert_and_write_partition(
_description_, by default None
ephemeris_interpolate : bool, optional
_description_, by default None
phase_cal_interpolate : bool, optional
_description_, by default None
sys_cal_interpolate : bool, optional
_description_, by default None
compressor : numcodecs.abc.Codec, optional
_description_, by default numcodecs.Zstd(level=2)
storage_backend : str, optional
Expand Down Expand Up @@ -783,7 +796,15 @@ def get_observation_info(in_file, observation_id, obs_mode):
)

start = time.time()
xds = xr.Dataset()
xds = xr.Dataset(
attrs={
"creation_date": datetime.datetime.now().isoformat(),
"xradio_version": importlib.metadata.version("xradio"),
"schema_version": "4.0.-9999",
"type": "visibility",
}
)

# interval = check_if_consistent(tb_tool.getcol("INTERVAL"), "INTERVAL")
interval = tb_tool.getcol("INTERVAL")

Expand Down Expand Up @@ -874,22 +895,51 @@ def get_observation_info(in_file, observation_id, obs_mode):
antenna_id,
feed_id,
telescope_name,
)

logger.debug("Time antenna xds " + str(time.time() - start))

start = time.time()
gain_curve_xds = create_gain_curve_xds(
in_file, xds.frequency.attrs["spectral_window_id"], ant_xds
)
logger.debug("Time gain_curve xds " + str(time.time() - start))

start = time.time()
phase_calibration_xds = create_phase_calibration_xds(
in_file,
xds.frequency.attrs["spectral_window_id"],
ant_xds,
time_min_max,
phase_cal_interp_time,
)
logger.debug("Time phase_calibration xds " + str(time.time() - start))

# Change antenna_ids to antenna_names
xds = antenna_ids_to_names(xds, ant_xds, is_single_dish)
# but before, keep the name-id arrays, we need them for the pointing and weather xds
ant_xds_name_ids = ant_xds["antenna_name"].set_xindex("antenna_id")
ant_xds = ant_xds.drop_vars(
"antenna_id"
) # No longer needed after converting to name.
ant_xds_station_name_ids = ant_xds["station"].set_xindex("antenna_id")
# No longer needed after converting to name.
ant_xds = ant_xds.drop_vars("antenna_id")

logger.debug("Time ant xds " + str(time.time() - start))
# Create system_calibration_xds
start = time.time()
if sys_cal_interpolate:
sys_cal_interp_time = xds.time.values
else:
sys_cal_interp_time = None
system_calibration_xds = create_system_calibration_xds(
in_file,
xds.frequency,
ant_xds_name_ids,
sys_cal_interp_time,
)
logger.debug("Time system_calibation " + str(time.time() - start))

# Create weather_xds
start = time.time()
weather_xds = create_weather_xds(in_file)
weather_xds = create_weather_xds(in_file, ant_xds_station_name_ids)
logger.debug("Time weather " + str(time.time() - start))

# Create pointing_xds
Expand All @@ -914,7 +964,6 @@ def get_observation_info(in_file, observation_id, obs_mode):
)

start = time.time()
xds.attrs["type"] = "visibility"

# Time and frequency should always be increasing
if len(xds.frequency) > 1 and xds.frequency[1] - xds.frequency[0] < 0:
Expand Down Expand Up @@ -943,7 +992,7 @@ def get_observation_info(in_file, observation_id, obs_mode):
# assert len(col_unique) == 1, col_name + " is not consistent."
# return col_unique[0]

field_and_source_xds, source_id, num_lines = create_field_and_source_xds(
field_and_source_xds, source_id, _num_lines = create_field_and_source_xds(
in_file,
field_id,
xds.frequency.attrs["spectral_window_id"],
Expand All @@ -965,11 +1014,18 @@ def get_observation_info(in_file, observation_id, obs_mode):
"FIELD_PHASE_CENTER"
].attrs["frame"]

if overwrite:
mode = "w"
else:
mode = "w-"
partition_info_misc_fields = {
"scan_id": scan_id,
"obs_mode": obs_mode,
"taql_where": taql_where,
}
info_dicts = create_info_dicts(
in_file, xds, field_and_source_xds, partition_info_misc_fields, tb_tool
)
xds.attrs.update(info_dicts)

# xds ready, prepare to write
start = time.time()
main_chunksize = parse_chunksize(main_chunksize, "main", xds)
add_encoding(xds, compressor=compressor, chunks=main_chunksize)
logger.debug("Time add compressor and chunk " + str(time.time() - start))
Expand All @@ -979,31 +1035,10 @@ def get_observation_info(in_file, observation_id, obs_mode):
pathlib.Path(in_file).name.replace(".ms", "") + "_" + str(ms_v4_id),
)

if "line_name" in field_and_source_xds.coords:
line_name = to_list(
unique_1d(np.ravel(field_and_source_xds.line_name.values))
)
if overwrite:
mode = "w"
else:
line_name = []

xds.attrs["partition_info"] = {
# "spectral_window_id": xds.frequency.attrs["spectral_window_id"],
"spectral_window_name": xds.frequency.attrs["spectral_window_name"],
# "field_id": to_list(unique_1d(field_id)),
"field_name": to_list(
np.unique(field_and_source_xds.field_name.values)
),
# "source_id": to_list(unique_1d(source_id)),
"line_name": line_name,
"scan_number": to_list(np.unique(scan_id)),
"source_name": to_list(
np.unique(field_and_source_xds.source_name.values)
),
"polarization_setup": to_list(xds.polarization.values),
"num_lines": num_lines,
"obs_mode": obs_mode.split(","),
"taql": taql_where,
}
mode = "w-"

start = time.time()
if storage_backend == "zarr":
Expand All @@ -1022,6 +1057,21 @@ def get_observation_info(in_file, observation_id, obs_mode):
store=os.path.join(file_name, "POINTING"), mode=mode
)

if system_calibration_xds:
system_calibration_xds.to_zarr(
store=os.path.join(file_name, "SYSCAL"), mode=mode
)

if gain_curve_xds:
gain_curve_xds.to_zarr(
store=os.path.join(file_name, "GAIN_CURVE"), mode=mode
)

if phase_calibration_xds:
phase_calibration_xds.to_zarr(
store=os.path.join(file_name, "PHASE_CAL"), mode=mode
)

if weather_xds:
weather_xds.to_zarr(
store=os.path.join(file_name, "WEATHER"), mode=mode
Expand Down
Loading
Loading