Skip to content

Commit

Permalink
v1.0.0 API definition
Browse files Browse the repository at this point in the history
  • Loading branch information
tnakazato committed Dec 22, 2024
1 parent ef58285 commit 94ec05e
Show file tree
Hide file tree
Showing 20 changed files with 254 additions and 100 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ pip install .

## Usage

- Load NRO 45m PSW data as `astropy.io.fits.hdu.hdulist.HDUList` object.
```
import nro45data.reader as reader
hdulist = reader.read('nro-psw.nqm')
```
`nqm2fits` converts NRO 45m PSW data (.nqm) to FITS.

```python
import nro45data.psw as psw
psw.nqm2fits('mydata.nqm', 'mydata.fits')
```
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
author = "Takeshi Nakazato"

# The full version, including alpha/beta/rc tags
release = "0.0.2alpha"
release = "1.0.0alpha"


# -- General configuration ---------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ Welcome to nro45data's documentation!
==========================================

.. toctree::
:maxdepth: 3
:maxdepth: 4
:caption: Contents:

modules
nro45data


Indices and tables
Expand Down
7 changes: 0 additions & 7 deletions docs/modules.rst

This file was deleted.

35 changes: 35 additions & 0 deletions docs/nro45data.psw.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
nro45data.psw module
====================================

.. automodule:: nro45data.psw
:members:
:undoc-members:
:show-inheritance:

Submodules
----------

nro45data.psw.io submodule
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

.. automodule:: nro45data.psw.io
:members: _read_psw, _to_fits
:undoc-members:
:show-inheritance:

nro45data.psw.ms2 submodule
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

.. automodule:: nro45data.psw.ms2
:members:
:undoc-members:
:show-inheritance:

nro45data.psw.ms4 submodule
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

.. automodule:: nro45data.psw.ms4
:members:
:undoc-members:
:show-inheritance:

30 changes: 0 additions & 30 deletions docs/nro45data.reader.rst

This file was deleted.

2 changes: 1 addition & 1 deletion docs/nro45data.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ nro45data package
.. toctree::
:maxdepth: 4

nro45data.reader
nro45data.psw
28 changes: 28 additions & 0 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Configuration file for the Sphinx documentation builder.
#
# For the full list of built-in configuration values, see the documentation:
# https://www.sphinx-doc.org/en/master/usage/configuration.html

# -- Project information -----------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#project-information

project = 'nro45data'
copyright = '2024, Takeshi Nakazato'
author = 'Takeshi Nakazato'
release = '0.0.2alpha'

# -- General configuration ---------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration

extensions = []

templates_path = ['_templates']
exclude_patterns = []



# -- Options for HTML output -------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output

html_theme = 'alabaster'
html_static_path = ['_static']
17 changes: 17 additions & 0 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
.. nro45data documentation master file, created by

Check warning on line 1 in docs/source/index.rst

View workflow job for this annotation

GitHub Actions / pages

document isn't included in any toctree
sphinx-quickstart on Sat Dec 21 14:11:40 2024.
You can adapt this file completely to your liking, but it should at least
contain the root `toctree` directive.
nro45data documentation
=======================

Add your content using ``reStructuredText`` syntax. See the
`reStructuredText <https://www.sphinx-doc.org/en/master/usage/restructuredtext/index.html>`_
documentation for details.


.. toctree::
:maxdepth: 2
:caption: Contents:

2 changes: 1 addition & 1 deletion src/README.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
This directoy stores each Python Package.
**This** directoy stores each Python Package.
2 changes: 1 addition & 1 deletion src/nro45data/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from __future__ import annotations

__version__ = "0.0.2"
__version__ = "1.0.0alpha"
56 changes: 56 additions & 0 deletions src/nro45data/psw/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
from .io import _read_psw
from .io import _to_fits
from .ms2 import _to_ms2
from .ms4 import _to_ms4


def nqm2fits(nqmfile: str, fitsfile: str, overwrite: bool = False) -> bool:
"""Convert NRO45m PSW data (.nqm) to FITS.
Args:
nqmfile: Input NRO45m PSW file name
fitsfile: Output FITS file name
overwrite: Overwrite existing output file or not.
Default is False (not overwrite).
Returns:
Conversion status. True is successful.
"""
hdulist = _read_psw(nqmfile)
return _to_fits(hdulist, fitsfile, overwrite)


def nqm2ms2(nqmfile: str, msfile: str, overwrite: bool = False) -> bool:
"""Convert NRO45m PSW data (.nqm) to MeasurementSet v2.
Not implemented yet.
Args:
nqmfile: Input NRO45m PSW file name
msfile: Output MSv2 file name
overwrite: Overwrite existing output file or not.
Default is False (not overwrite).
Returns:
Conversion status. True is successful.
"""
hdulist = _read_psw(nqmfile)
return _to_ms2(hdulist, msfile, overwrite)


def nqm2ms4(nqmfile: str, psfile: str, overwrite: bool = False) -> bool:
"""Convert NRO45m PSW data (.nqm) to MeasurementSet v4 (ProcessingSet).
Not implemented yet.
Args:
nqmfile: Input NRO45m PSW file name
psfile: Output MSv4 (ProcessingSet) file name
overwrite: Overwrite existing output file or not.
Default is False (not overwrite).
Returns:
Conversion status. True is successful.
"""
hdulist = _read_psw(nqmfile)
return _to_ms4(hdulist, psfile, overwrite)
2 changes: 2 additions & 0 deletions src/nro45data/psw/io/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from .reader import _read_psw
from .fits import _to_fits
36 changes: 36 additions & 0 deletions src/nro45data/psw/io/fits.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from typing import List, Tuple, TYPE_CHECKING

import astropy.io.fits as fits

if TYPE_CHECKING:
from astropy.io.fits.hdu.hdulist import HDUList


def _to_fits(hdulist: 'HDUList', fitsfile: str, overwrite: bool = False) -> bool:
"""Export HDUList to FITS.
Args:
hdulist: HDUList generated from NRO 45m PSW file (.nqm)
fitsfile: Output FITS file name
overwrite: Overwrite existing output file or not.
Default is False (not overwrite).
Returns:
Export status. True is successful.
"""
status = True

primary_hdu = fits.PrimaryHDU()
output_hdulist = fits.HDUList([primary_hdu, hdulist[0]])

try:
output_hdulist.writeto(
fitsfile,
output_verify='fix+warn',
overwrite=overwrite
)
except Exception as e:
print(e)
status = False

return status
43 changes: 18 additions & 25 deletions src/nro45data/reader/psw.py → src/nro45data/psw/io/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
import tempfile
from typing import List, Tuple, TYPE_CHECKING

import astropy.io.fits as fits

if TYPE_CHECKING:
from astropy.io.fits.hdu.hdulist import HDUList
from astropy.io.fits.hdu.hdulist import HDUList

FITS_BLOCK_SIZE = 2880
FITS_RECORD_SIZE = 80
Expand Down Expand Up @@ -107,18 +104,19 @@ def _rename_duplicate_types(records: List[str]) -> List[str]:
if r.startswith('TTYPE'):
k = re.match(r".*= '([^']+)'.*", r)[1]
duplicate_rows[k].append(i)
print(f'duplicate rows: {duplicate_rows}')
fixed_records = records[::]
for k, rows in duplicate_rows.items():
for row in rows[1:]:
# print(f'key {k}, row {row}')
new_key = k[:-1] + chr(ord(k[-1]) + 1)
print(f'new key: {new_key}')
fixed_records[i] = fixed_records[i].replace(k, new_key)
# print(f'new key: {new_key}')
fixed_records[row] = fixed_records[row].replace(k, new_key)
# print(fixed_records[row])

return fixed_records


def _read_psw(filename: str) -> 'HDUList':
def _read_psw(filename: str) -> HDUList:
"""Read NRO 45m PSW data.
Args:
Expand All @@ -133,24 +131,19 @@ def _read_psw(filename: str) -> 'HDUList':

record_list, binary_data = _read_header_and_data(filename)

# tweak header to follow FITS standard
with tempfile.NamedTemporaryFile() as f:
# insert whitespace after '='
record_list = _follow_fits_standard(record_list)
record_list = _follow_fits_standard(record_list)

# rename duplicate TTYPE names
record_list = _rename_duplicate_types(record_list)
# rename duplicate TTYPE names
record_list = _rename_duplicate_types(record_list)
# for r in record_list:
# print(r)

header = ''.join(record_list).encode()
header = ''.join(record_list).encode()

f.seek(0, os.SEEK_SET)
f.write(header)
f.write(binary_data)
hdulist = HDUList.fromstring(
header + binary_data,
ignore_missing_simple=True,
lazy_load_hdus=False
)

# read data using astropy
hdulist = fits.open(
f.name,
ignore_missing_simple=True,
lazy_load_hdus=False
)
return hdulist
return hdulist
25 changes: 25 additions & 0 deletions src/nro45data/psw/ms2/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from astropy.io.fits.hdu.hdulist import HDUList


def _to_ms2(
hdulist: 'HDUList',
msfile: str,
overwrite: bool = False
) -> bool:
"""Export HDUList to MeasurementSet v2.
Not implemented yet.
Args:
hdulist: HDUList generated from NRO 45m PSW file (.nqm)
fitsfile: Output MSv2 file name
overwrite: Overwrite existing output file or not.
Default is False (not overwrite).
Returns:
Export status. True is successful.
"""
return False
25 changes: 25 additions & 0 deletions src/nro45data/psw/ms4/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from astropy.io.fits.hdu.hdulist import HDUList


def _to_ms4(
hdulist: 'HDUList',
psfile: str,
overwrite: bool = False
) -> bool:
"""Export HDUList to MeasurementSet v4 (ProcessingSet).
Not implemented yet.
Args:
hdulist: HDUList generated from NRO 45m PSW file (.nqm)
fitsfile: Output PS file name
overwrite: Overwrite existing output file or not.
Default is False (not overwrite).
Returns:
Export status. True is successful.
"""
return False
Loading

0 comments on commit 94ec05e

Please sign in to comment.