:material-link: View in Colab    :octicons-octoface: GitHub source

from your import Your, Writer
import os
import logging
logger = logging.getLogger()
logger = logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(threadName)s - %(levelname)s -'
                                                        ' %(message)s')

We can write + convert files using the Writer class in Your

Let's read a Psrfits file and write out a Psrfits and a Filterbank file

fits_file = '../tests/data/28.fits'
y = Your(fits_file)

While writing files you can also select the required channel and spectra range. Just give the required ranges as input to the Writer class.

w = Writer(y, nstart=0, nsamp=10, c_min=10, c_max = 100, outdir='.', outname='filfromfits_28')
w.to_fil()
2020-08-20 15:29:28,188 - your.writer - MainThread - INFO - Writing data from spectra 0-10 in the frequency channel range 10-100 to filterbank
2020-08-20 15:29:28,189 - your.formats.filwriter - MainThread - INFO - Trying to write data to filterbank file: ./filfromfits_28.fil
2020-08-20 15:29:28,190 - your.formats.filwriter - MainThread - WARNING - Filterbank file already exists at ./filfromfits_28.fil
2020-08-20 15:29:28,190 - your.formats.filwriter - MainThread - INFO - Size of the Filerbank file is too small. Overwriting it.
2020-08-20 15:29:28,194 - your.formats.filwriter - MainThread - INFO - Writing 10 spectra to file: ./filfromfits_28.fil
2020-08-20 15:29:28,195 - your.formats.filwriter - MainThread - INFO - Successfully written data to Filterbank file: ./filfromfits_28.fil

and we can just read this Filterbank using Your to make sure that the header parameters and the data are correctly written. Take a look at Getting Started for more details on using Your.

yt = Your('filfromfits_28.fil')
yt.your_header
Unified Header:
  "basename": "filfromfits_28"
  "bw": 90.0
  "center_freq": 1184.5
  "dec_deg": 13.964475555555554
  "dtype": "uint8"
  "fch1": 1139.5
  "filelist": [
    "filfromfits_28.fil"
  ]
  "filename": "filfromfits_28.fil"
  "foff": 1.0
  "frequency_decimation_factor": 1
  "gb": 75.61933243565039
  "gl": 97.96004079061271
  "isfil": true
  "isfits": false
  "native_foff": 1.0
  "native_nchans": 90
  "native_nspectra": 10.0
  "native_tsamp": 0.00126646875
  "nbits": 8
  "nchans": 90
  "npol": 1
  "nspectra": 10.0
  "ra_deg": 186.65681708333335
  "source_name": "src1"
  "time_decimation_factor": 1
  "tsamp": 0.00126646875
  "tstart": 58682.620316710374
  "tstart_utc": "2019-07-18T14:53:15.364"
dt = yt.get_data(0, 10)
d = y.get_data(0, 10)
(dt-d[:, 10:100]).sum()
0

Looks good. Let's write a PSRFITS file now.

All the inputs defined while declaring Writer are still gonna be used. Let's change the outname for clarity and select some channels

w = Writer(y, nstart=0, nsamp=10, c_min=0, c_max = 100, outdir='.', outname='fitsfromfits_28')
w.outname = 'fitsfromfits_28'
w.c_min = 0
w.c_max = 100
w.to_fits()
2020-08-20 15:29:28,254 - root - MainThread - INFO - ObsInfo updated with relevant parameters
2020-08-20 15:29:28,255 - your.formats.fitswriter - MainThread - INFO - Setting the following info to be written in ./fitsfromfits_28.fits 
 {
    "ant_x": -1601185.63,
    "ant_y": -5041978.15,
    "ant_z": 3554876.43,
    "bmaj_deg": 0.0,
    "bmin_deg": 0.0,
    "bpa_deg": 0.0,
    "bw": 100.0,
    "chan_bw": 1.0,
    "dec_str": "13:57:52.1120",
    "dt": 0.00126646875,
    "fcenter": 1179.5,
    "file_date": "2020-08-20T19:29:28",
    "longitude": -107.61833443381258,
    "nbits": 8,
    "nchan": 100,
    "nsblk": 10,
    "nsuboffs": 0.0,
    "obs_date": "2019-07-18T14:53:15",
    "observer": "Human",
    "proj_id": "Awesome_Project",
    "ra_str": "12:26:37.6361",
    "scan_len": 0.0126646875,
    "src_name": "src1",
    "stt_imjd": 58682,
    "stt_lst": 12438.193191755068,
    "stt_offs": 0.3637763159349561,
    "stt_smjd": 53595,
    "telescope": "VLA"
}
2020-08-20 15:29:28,265 - root - MainThread - INFO - Building the PSRFITS table
2020-08-20 15:29:28,288 - root - MainThread - INFO - Writing PSRFITS table to file: ./fitsfromfits_28.fits
2020-08-20 15:29:28,530 - root - MainThread - INFO - Header information written in ./fitsfromfits_28.fits
2020-08-20 15:29:28,531 - your.writer - MainThread - INFO - Filling PSRFITS file with data
2020-08-20 15:29:28,551 - your.writer - MainThread - INFO - Number of subints to write 1
2020-08-20 15:29:28,552 - your.writer - MainThread - INFO - Writing data to ./fitsfromfits_28.fits from subint = 0 to 1.
2020-08-20 15:29:29,137 - your.writer - MainThread - INFO - All spectra written to ./fitsfromfits_28.fits

Let's now read a Filterbank file and write out a Psrfits and a Filterbank file

fil_file = '../tests/data/28.fil'
y = Your(fil_file)
w = Writer(y, nstart=0, nsamp=10, outdir='.', outname='filfromfil_28')
w.to_fil()
2020-08-20 15:29:29,167 - your.writer - MainThread - INFO - Writing data from spectra 0-10 in the frequency channel range 0-336 to filterbank
2020-08-20 15:29:29,167 - your.formats.filwriter - MainThread - INFO - Trying to write data to filterbank file: ./filfromfil_28.fil
2020-08-20 15:29:29,168 - your.formats.filwriter - MainThread - INFO - Output file does not already exist. Creating a new Filterbank file.
2020-08-20 15:29:29,274 - your.formats.filwriter - MainThread - INFO - Writing 10 spectra to file: ./filfromfil_28.fil
2020-08-20 15:29:29,275 - your.formats.filwriter - MainThread - INFO - Successfully written data to Filterbank file: ./filfromfil_28.fil
w.nstart = 2
w.nsamp = 100
w.outname = 'fitsfromfil_28'
w.to_fits()
2020-08-20 15:29:29,282 - root - MainThread - INFO - ObsInfo updated with relevant parameters
2020-08-20 15:29:29,283 - your.formats.fitswriter - MainThread - INFO - Setting the following info to be written in ./fitsfromfil_28.fits 
 {
    "ant_x": -1601185.63,
    "ant_y": -5041978.15,
    "ant_z": 3554876.43,
    "bmaj_deg": 0.0,
    "bmin_deg": 0.0,
    "bpa_deg": 0.0,
    "bw": -336.0,
    "chan_bw": -1.0,
    "dec_str": "13:57:52.1120",
    "dt": 0.00126646875,
    "fcenter": 1297.0,
    "file_date": "2020-08-20T19:29:29",
    "longitude": -107.61833443381258,
    "nbits": 8,
    "nchan": 336,
    "nsblk": 100,
    "nsuboffs": 0.0,
    "obs_date": "2019-07-18T14:53:15",
    "observer": "Human",
    "proj_id": "Awesome_Project",
    "ra_str": "12:26:37.6361",
    "scan_len": 0.126646875,
    "src_name": "src1",
    "stt_imjd": 58682,
    "stt_lst": 12438.195731491169,
    "stt_offs": 0.36630911752581596,
    "stt_smjd": 53595,
    "telescope": "VLA"
}
2020-08-20 15:29:29,292 - root - MainThread - INFO - Building the PSRFITS table
2020-08-20 15:29:29,315 - root - MainThread - INFO - Writing PSRFITS table to file: ./fitsfromfil_28.fits
2020-08-20 15:29:29,329 - root - MainThread - INFO - Header information written in ./fitsfromfil_28.fits
2020-08-20 15:29:29,331 - your.writer - MainThread - INFO - Filling PSRFITS file with data
2020-08-20 15:29:29,351 - your.writer - MainThread - INFO - Number of subints to write 1
2020-08-20 15:29:29,351 - your.writer - MainThread - INFO - Writing data to ./fitsfromfil_28.fits from subint = 0 to 1.
2020-08-20 15:29:29,968 - your.writer - MainThread - INFO - All spectra written to ./fitsfromfil_28.fits