-
Notifications
You must be signed in to change notification settings - Fork 0
/
arrange_images.py
executable file
·91 lines (88 loc) · 2.83 KB
/
arrange_images.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/python
'''
Program:
The code is used to arrange images into proper folders
Usage:
arrange_images.py
Editor:
Jacob975
20180621
#################################
update log
20180621 version alpha 1
1. The code works
'''
import os
from astropy.io import fits as pyfits
import numpy as np
import glob
import time
import TAT_env
def check_sources_info(name_image):
# load header of images
header=pyfits.getheader(name_image)
try:
source_name = header['TARGET']
return 0, source_name
except:
pass
try:
RA=header['RA'].split(':')
DEC=header['DEC'].split(':')
except:
print "Warning!\nHeader of {0} is broken".format(name_image)
command = "mv {0} X_{0}_X".format(name_image)
os.system(command)
return 1, ""
# load header of library, and check if the sources be found in this image.
for source in sources_list:
source_RA = source['RA'].split(":")
source_DEC = source['DEC'].split(":")
if (float(RA[0]) == float(source_RA[0])) & (float(DEC[0]) == float(source_DEC[0])):
return 0, source['name']
print "In {0}, source not found".format(name_image)
return 1, ""
def check_bands_info(name_image):
darkh = pyfits.getheader(name_image)
try:
band = darkh['FILTER']
exptime = darkh['EXPTIME']
except:
print "Warning!\nHeader of {0} is broken".format(name_image)
command = "mv {0} X_{0}_X".format(name_image)
os.system(command)
return 1, "", 0
return 0, band, int(exptime)
#--------------------------------------------
# main code
if __name__ == "__main__":
# Measure time
start_time = time.time()
#---------------------------------------
# Initialize
global sources_list
sources_list = TAT_env.object_list
name_image_list = glob.glob('*.fit')
#---------------------------------------
# Arrange image to folders
print "### arrange_image.py ###"
print "Arrange images into proper directory"
for name_image in name_image_list:
# arrange image according to filters and sources type
failure, source = check_sources_info(name_image)
if failure:
continue
failure, band, exptime = check_bands_info(name_image)
if failure:
continue
new_dir = "{0}/{1}_{2}".format(source, band, exptime)
# move image to seleted dir, if dir not exist, create a new one.
command = "mkdir -p {0}".format(new_dir)
os.system(command)
command = "mv {0} {1}".format(name_image, new_dir)
os.system(command)
print "{0} ---> {1}".format(name_image, new_dir)
#---------------------------------------
# Measure time
elapsed_time = time.time() - start_time
print "Exiting Main Program, spending ", elapsed_time, "seconds."