0

I am working in the field of astronomy, and the process that I use to unzip the images that I get from the telescopes can be very tedious. The format that the images come in is 'fits.fz' which stands for fits.fits-zipped. I want to decompress these into just '.fits'. I have already I'm working on a program that simplifies this process of decompressing. I have created a graphical interface with two buttons through Python and Tkinter. The first button creates a text file named 'list.txt' and then executes a pre-existing .bat file which dumps the names of every file in a specific directory that ends with 'fits.fz' into 'list.txt'. The first button is also supposed to copy the specific names of the files into a very specific place in another bat file. The other .bat file is called 'Decompress.bat' and is supposed to use the following command for each file in 'list.txt':

 C:\ds9\ds9.exe 
 C:\directory\FITS FILE HERE 
-savefits

I would like for the python program to be able to copy specific sections from a line of code and paste them where 'FITS FILE HERE' is.

The following is the function that is executed when the first button is pressed.

 f = open('C:/jah/list.txt')
 f1 = open('C:/jah/decompress.bat', 'a')

 def begin_wombocombo(): #Is function for first button
     open('C:/jah/list.txt', 'w').close() #Clears 'list.txt'
     open('C:/jah/decompress.bat', 'w').close() #Clears 'decompress.bat'
     subprocess.call([r'C:/jah/newbat.bat']) #Dumps directory into 'list.txt'

     doIHaveToCopyTheLine=False #Bool for whether or not the program has to copy line
     for line in f.readlines(): #loops through all instances to find fz files and then pastes them into decompress.bat
       if 'fits.fz' in line:
         doIHaveToCopyTheLine=True
       if doIHaveToCopyTheLine:
         f1.write(line)
     f1.close()
     f.close()

The issue with this is that it only copies the lines of text that has the fits.fz files. This means that it copies everything else on the line such as when the file was created. Is there any way to simply copy and paste the fits.fz file alone? How would I go about working these strings into the .bat file?

Thank you for your time, and btw the second button just executes 'decompress.bat' which is the file with the commands to unzip the images.

13
  • Maybe you just need to use os.listdir() instead of subprocess.call([r'C:/jah/newbat.bat']) (don't forget to import os in first line) Commented Jun 19, 2019 at 23:28
  • @CrafterKolyan If I use os.listdir(), how would I go about pasting it into the decompress.bat file? Edit: formatting Commented Jun 19, 2019 at 23:30
  • Exactly as you do it now. With f1.write(line) Commented Jun 19, 2019 at 23:31
  • Seems like you could simplify this process by directly calling ds9.exe from python, rather than writing out bat files - or is the process more complex than this? Commented Jun 19, 2019 at 23:32
  • @Blorgbeard I am fairly new to python so I am not sure how to go about doing this. Could you point me in the right direction? Commented Jun 19, 2019 at 23:34

1 Answer 1

0

I think in Python, something like this would do the trick, without writing out batch files etc.

import os
import subprocess

target_directory = 'C:\\directory\\' # change this as required
zipped_files = [x for x in os.listdir(target_directory) 
                   if x.lower().endswith('.fits.fz')]
for filename in zipped_files:
    subprocess.call([r'C:\ds9\ds9.exe', os.path.abspath(filename), '-savefits'])
Sign up to request clarification or add additional context in comments.

18 Comments

I'm trying to implement it right now. I don't see anything happening though. Do I need to replace "filename" or "os.path.abspath"?
That's odd. I'm currently on linux, and I obviously don't have ds9.exe, but from my testing it should work. Do you see no output at all?
Oh, it might be the backslashes in the string, try adding the r that I just edited in.
Ah, and this is working on the current directory - you might have to pass the directory with the files in it to listdir().
Yes that was it. I also just needed to edit the file path. "C:/SAOImageDS9/ds9.exe"
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.