[Python] Translating an IDL binary data reader to Python

Discussão em 'Python' iniciado por Stack, Maio 11, 2025.

  1. Stack

    Stack Membro Participativo

    I’m trying to read a binary data file and have its data reader. The reader is about 10 lines and is written in IDL (which I am not familiar with), and I have no way to compile it to recover the binary data from the file. I’m hoping that an expert in IDL and Python can help me translate this IDL data reader into Python.

    There are two channels (called "WE" and "NS") of 24-bit data in the binary data file. Appended to each 24-bit data word is an 8-bit control and status word. Thus the binary data file comprises a WE 32-bit word followed by a NS 32-bit word, then another WE 32-bit word and NS 32-bit word, and so on. The control bits are 8 lowest bits and the data are the 24 upper bits in every word. I just need to read the 24-bit data, not the control bits.

    An actual data file is over 2 GB, but it appears from the IDL reader that you can just read a portion of the file, for example a few thousand data words, which would be great for testing data processing algorithms.

    The annotated IDL file below looks fairly well documented, but my looking up IDL commands and then trying to figure out how it does the data parsing to separate the 24-bit data from the 8-bit control words, or how it forms negative numbers, remain mysteries. The actual IDL code is only about 10 lines. Any suggestions for equivalent Python statements would be very greatly appreciated.

    pro NC_vlf, file,t0m,t0s,dus, t,B_NS_nT,B_WE_nT
    ;+
    ;IDL reader for the binary data files
    ; input
    ; file ... file name (containing 1 hour of data from both channels)
    ; example: file='CH1_2_20170103_060000'
    ; t0m ... start time minutes since the entire hour written in the file name.
    ; example: t0m= 42.
    ; t0s ... start time seconds
    ; example: t0s=3.547
    ; dus ... duration in seconds
    ; example: dus=0.008
    ; output
    ; B_NS_nT, B_WE_nT ...time series of 24-bit data samples
    ;
    ; t...time axis in seconds from the entire hour
    ;
    fs=78.125d3 ;(10MHz/128) sampling frequency in Hz
    ;
    ; __ file reading
    np=(file_info(file)).size/8L ;...number of 8-byte blocks in the file
    d=lonarr(2,np) ;... array of 32bit integers for two channels
    ; 2 x (aux_byte, 24bit sample)
    openr,1,file, /Swap_If_Big_Endian ;...data reading, little endian
    readu,1,d
    close,1
    ;
    ; _ data selection
    ;...indices to the selected interval
    i0=long(fs*(t0m*60d0+t0s))
    i1=i0+ceil(fs*dus)
    ;...time axis in seconds from the entire hour
    t=findgen(i1-i0+1)/fs+t0s
    ;...time series of 24-bit data samples from NS and WE channels in nanoTesla
    B_NS_nT=(d[nswe[0],i0:i1]/256L) /1e7 ; quantization level at 1e-7 nT (100 attoTesla)
    B_WE_nT=(d[nswe[1],i0:i1]/256L) /1e7

    end

    Continue reading...

Compartilhe esta Página