Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9200941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:53:05+00:00 2026-06-17T22:53:05+00:00

I am rather new to python programming so please be a big simple with

  • 0

I am rather new to python programming so please be a big simple with your answer.

I have a .raw file which is 2b/2b complex short int format. Its actually a 2-D raster file. I want to read and seperate both real and complex parts. Lets say the raster is [MxN] size.

Please let me know if question is not clear.

Cheers
N

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T22:53:07+00:00Added an answer on June 17, 2026 at 10:53 pm

    You could do it with the struct module. Here’s a simple example based on the file formatting information you mentioned in a comment:

    import struct
    
    def read_complex_array(filename, M, N):
        row_fmt = '={}h'.format(N)  # "=" prefix means integers in native byte-order
        row_len = struct.calcsize(row_fmt)
        result = []
        with open(filename, "rb" ) as input:
            for col in xrange(M):
                reals = struct.unpack(row_fmt, input.read(row_len))
                imags = struct.unpack(row_fmt, input.read(row_len))
                cmplx = [complex(r,i) for r,i in zip(reals, imags)]
                result.append(cmplx)
        return result
    

    This will return a list of complex-number lists, as can be seen in this output from a trivial test I ran:

    [
      [  0.0+  1.0j    1.0+  2.0j    2.0+  3.0j    3.0+  4.0j],
      [256.0+257.0j  257.0+258.0j  258.0+259.0j  259.0+260.0j],
      [512.0+513.0j  513.0+514.0j  514.0+515.0j  515.0+516.0j]
    ]
    

    Both the real and imaginary parts of complex numbers in Python are usually represented as a pair of machine-level double precision floating point numbers.

    You could also use the array module. Here’s the same thing using it:

    import array
    
    def read_complex_array2(filename, M, N):
        result = []
        with open(filename, "rb" ) as input:
            for col in xrange(M):
                reals = array.array('h')
                reals.fromfile(input, N)
                # reals.byteswap()  # if necessary
                imags = array.array('h')
                imags.fromfile(input, N)
                # imags.byteswap()  # if necessary
                cmplx = [complex(r,i) for r,i in zip(reals, imags)]
                result.append(cmplx)
        return result
    

    As you can see, they’re very similar, so it’s not clear there’s a big advantage to using one over the other. I suspect the array based version might be faster, but that would have to be determined by actually timing it with some real data to be able to say with any certainty.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to network programming but old to Python. I have a simple
I am rather new to wx/python so please excuse if this is stupid or
I'm using Python 2.7. I'm rather new to the python langauge. I have two
I'm rather new to python and programming ;-), and I'm writting a programm for
Im rather new to python but I have been attemping to learn the basics.
I'm rather new to Backbone.js development, and have run into a bit of a
I'm rather new to Powershell and am working on setting up my profile.ps1 file.
I am relatively new in Python so facing difficulties in basics. I have following
I'm new to Lua programming, having come over from python to basically make a
I'm writing a library to process gaze tracking in Python, and I'm rather new

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.