imaseries.py
10.7 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# -*- coding: utf-8 -*-
"""
@author: aklotz@irap.omp.eu
"""
import numpy as np
import os
#import inspect
from astropy.wcs import WCS
import multiprocessing
import psutil
try:
from .filenames import FileNames
except:
from filenames import FileNames
try:
from .ima import Ima
except:
from ima import Ima
try:
from .guitastrotools import GuitastroException
except:
from guitastrotools import GuitastroException
# #####################################################################
# #####################################################################
# #####################################################################
# Class ImaSeries
# #####################################################################
# #####################################################################
# This class provides an Astropy wrapper
# #####################################################################
class ImaSeriesException(GuitastroException):
"""Exception raised for errors in the ImaSeries class.
"""
SHAPES_NOT_SAME = 0
FILE_NOT_FOUND = 1
errors = [""]*2
errors[SHAPES_NOT_SAME] = "Shapes of images are not the same"
errors[FILE_NOT_FOUND] = "File not found"
class ImaSeries(ImaSeriesException, FileNames):
"""Image processing applied to a series of images.
This class process a series of images to generate another series of images.
The class ImaSeries imports the following methods from the class FileNames. See the FileNames class documentation for these methods:
* longitude
* namings
* naming
* naming_rules
* naming_ident
* naming_get
* naming_set
* path
* extension
* get_night
* fullfilename
* basename
* itername
* enumname
* genename
* genenames
* innames
* inoutnames
* outfilename
* indexname
* indexnames
* deletenames
"""
VERBOSE_NONE = 0
VERBOSE_SPECIFIC = 1
VERBOSE_DEBUG = 2
VERBOSE_ESSENTIAL = 4
VERBOSE_ALL = 8
def __init__(self, *args, **kwargs):
super().__init__()
self._ima = Ima()
self._verbose_level = self.VERBOSE_NONE
self._longiau_deg = 0.0
self._outfilename = os.path.join(self._ima.path(), "noname"+self._ima.extension())
self._ima.do_multiprocessing = False
self.do_multiprocessing = True
def execute_process_type1(self, method_process, process_args, genename, outfilegene, outfile_extension):
"""Generic method for multiprocesses
"""
# --- Total list of files
fnames = []
k = 1
for index in genename["indexes"]:
fnames.append( self.indexname(genename, outfilegene, outfile_extension, index, k) )
k += 1
nitot = k - 1
# --- Total list of files
effective_cpu_count = self._ima.cpu_count - 2
if effective_cpu_count < 1:
effective_cpu_count = 1
if self.do_multiprocessing == False:
effective_cpu_count = 1
# ---
proc = psutil.Process()
affinity = proc.cpu_affinity()
# --- Jobs attribution
ni = int(np.ceil(nitot / effective_cpu_count))
i2 = 0
jobs = []
p = [None]*effective_cpu_count
for i in range(0, effective_cpu_count):
i1 = i2
i2 = i1 + ni + 1
if i2 > nitot:
i2 = nitot
subfnames = fnames[i1:i2].copy()
# affinity = [i]
p[i] = ImaSeries()
p[i] = self.copy()
process = multiprocessing.Process(target=p[i].method_process, args=(affinity, subfnames, process_args))
jobs.append(process)
# --- Jobs start
for j in jobs:
j.start()
# --- Jobs join
for j in jobs:
j.join()
# --- Jobs close
for j in jobs:
j.terminate()
j.close()
# =============================================
# Print filters
# =============================================
def print_level(self, level:int=""):
if isinstance(level,type(int)) == True:
self._verbose_level = level
return self._verbose_level
def print_msg(self, level:int, message:str):
if message == "":
self._verbose_level = level
return
else:
if self._verbose_level >= level:
print(message)
# =============================================
# Image processing (pixels are modified)
# =============================================
def calibwcs(self, fitsname, outfilename, method="duplicate", **kwargs):
# ---
genename, outfilegene, outfile_extension = self.inoutnames(fitsname, outfilename, **kwargs)
# ---
if method.upper()=="DUPLICATE":
# --
k = 1
for index in genename["indexes"]:
# ---
filename_short, basename, filename, outfname = self.indexname(genename, outfilegene, outfile_extension, index, k)
# ---
self._ima.load(filename)
if k == 1:
wcs = WCS(self._ima._hdu.header, naxis = 2)
else:
self._ima._hdu.header.extend(wcs.to_header())
self._ima.save(outfname)
k += 1
return k-1
def div(self, fitsname, outfilename, filename_to_div, mult:float, **kwargs):
# ---
genename, outfilegene, outfile_extension = self.inoutnames(fitsname, outfilename, **kwargs)
# ---
filename_to_div = self._ima.fullfilename(filename_to_div)
if os.path.exists(filename_to_div) == False:
raise ImaSeriesException("FILE_NOT_FOUND",filename_to_div)
# --
mult = float(mult)
# --
k = 1
for index in genename["indexes"]:
# ---
filename_short, basename, filename, outfname = self.indexname(genename, outfilegene, outfile_extension, index, k)
# ---
self._ima.load(filename)
self._ima.div(filename_to_div, mult)
self._ima.save(outfname)
k += 1
return k-1
def ngain(self, fitsname: str, outfilename: str, norm:float, **kwargs) -> int:
# ---
genename, outfilegene, outfile_extension = self.inoutnames(fitsname, outfilename, **kwargs)
# ---
norm = float(norm)
# ---
method_process = self.ngain_process
process_args = (norm, )
# ---
nitot = self.execute_process_type1(method_process, process_args, genename, outfilegene, outfile_extension)
return nitot
def ngain_process(self, affinity: list, fnames: list, process_args: tuple):
norm, = process_args
proc = psutil.Process()
proc.cpu_affinity(affinity)
for fname in fnames:
filename_short, basename, filename, outfname = fname
# ---
self._ima.load(filename)
stat = self._ima.stat()
mult = norm / stat["mean"]
self._ima.mult(mult)
self._ima.save(outfname)
def register(self, fitsname, outfilename, **kwargs):
# ---
genename, outfilegene, outfile_extension = self.inoutnames(fitsname, outfilename, **kwargs)
# --
k = 1
for index in genename["indexes"]:
# ---
filename_short, basename, filename, outfname = self.indexname(genename, outfilegene, outfile_extension, index, k)
# ---
self._ima.load(filename)
if k == 1:
filename_ref = filename
translation = np.array([0, 0])
else:
transf = self._ima.register(filename_ref)
translation = transf.translation
print(f"{k} trans = {translation}")
self._ima.save(outfname)
k += 1
return k-1
def sub(self, fitsname, outfilename, filename_to_sub, **kwargs):
# ---
genename, outfilegene, outfile_extension = self.inoutnames(fitsname, outfilename, **kwargs)
# ---
filename_to_sub = self._ima.fullfilename(filename_to_sub)
if os.path.exists(filename_to_sub) == False:
raise ImaSeriesException("FILE_NOT_FOUND",filename_to_sub)
# --
#name = inspect.stack()
k = 1
for index in genename["indexes"]:
# ---
filename_short, basename, filename, outfname = self.indexname(genename, outfilegene, outfile_extension, index, k)
# ---
self._ima.load(filename)
self._ima.sub(filename_to_sub)
self._ima.save(outfname)
k += 1
return k-1
#return name
def uncosmic(self, fitsname, outfilename, **kwargs):
# ---
genename, outfilegene, outfile_extension = self.inoutnames(fitsname, outfilename, **kwargs)
# --
k = 1
for index in genename["indexes"]:
# ---
filename_short, basename, filename, outfname = self.indexname(genename, outfilegene, outfile_extension, index, k)
# ---
self._ima.load(filename)
self._ima.uncosmic()
self._ima.save(outfname)
k += 1
return k-1
# #####################################################################
# #####################################################################
# #####################################################################
# Main
# #####################################################################
# #####################################################################
# #####################################################################
if __name__ == "__main__":
default = 3
example = input(f"Select the example (0 to 3) ({default}) ")
try:
example = int(example)
except:
example = default
print("Example = {}".format(example))
# --- Get path_images for examples
ima = Ima()
path_images = ima.conf_guitastro()['path_data']
path_products = ima.conf_guitastro()['path_products']
if example == 1:
imaseries = ImaSeries()
path = r"C:\d\mp-g2a\photometrie_pulsante\2017-11-07"
f = os.path.join(path,"Dypeg flat-001.fit")
res = imaseries.sub(f, "i", "Dypeg offset")
if example == 2:
imaseries = ImaSeries()
path = r"C:\d\mp-g2a\photometrie_pulsante\2017-11-07"
imaseries.path(path)
res = imaseries.ngain("i", "j", 10000)
if example == 3:
imaseries = ImaSeries()
path = r"C:\d\mp-g2a\photometrie_pulsante\2017-11-07"
imaseries.path(path)
res = imaseries.ngain("bb", "j", 10000)