-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorder.py
executable file
·47 lines (37 loc) · 1.57 KB
/
order.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
import os
import re
##############################################################################
# This function sorts numerically the .fits images in a directory
##############################################################################
def directory_order(namedir):
name_array = os.listdir(namedir)
name_array.sort(key=natural_keys)
n_files = len(name_array)
for i in range(n_files):
sourcename = namedir + '/' + name_array[i]
mapname = namedir + '/' + "Tmp_Name_" + str(i+1) + '.fits'
os.rename(sourcename, mapname)
name_array = os.listdir(namedir)
name_array.sort(key=natural_keys)
for i in range(n_files):
sourcename = namedir + '/' + name_array[i]
mapname = namedir + '/' + "Map_" + str(i+1) + '.fits'
os.rename(sourcename, mapname)
name_array = os.listdir(namedir)
name_array.sort(key=natural_keys)
n_files = len(name_array)
return name_array, n_files
##############################################################################
# This function describes the natural sorting of a list (numerically). To be used as parameter for the list.sort() method
##############################################################################
def natural_keys(text):
return [ atof(c) for c in re.split(r'[+-]?([0-9]+(?:[.][0-9]*)?|[.][0-9]+)', text) ]
##############################################################################
# This function converts a text into a float
##############################################################################
def atof(text):
try:
retval = float(text)
except ValueError:
retval = text
return retval