utils.py 16.1 KB
Newer Older
jonas's avatar
jonas committed
1
2
from astropy.io import fits
import numpy as np
jonas's avatar
jonas committed
3
import os
jonas's avatar
jonas committed
4
5
import time
import subprocess
jonas's avatar
jonas committed
6
import math
jonas's avatar
jonas committed
7

jonas's avatar
jonas committed
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

class bcolors:
  HEADER = '\033[95m'
  OKBLUE = '\033[94m'
  OKGREEN = '\033[92m'
  WARNING = '\033[93m'
  FAIL = '\033[91m'
  ENDC = '\033[0m'
  BOLD = '\033[1m'
  UNDERLINE = '\033[4m'
  RESET = '\u001b[0m'

def printc(*args, color = bcolors.RESET, **kwargs):
  """My custom print() function."""
  print(u"\u001b"+f"{color}", end='\r')
  print(*args, **kwargs)
  print(u"\u001b"+f"{bcolors.RESET}", end='\r')
  return 

def load_fits(path):
  """
  load the fits file
  
  Parameters
  ----------
  path: string, location of the fits file
  
  Output
  ------
  data: numpy array, of stokes images in (row, col, wv, pol) 
  header: hdul header object, header of the fits file
  """

  hdul_tmp = fits.open(f'{path}')
  
  data = np.asarray(hdul_tmp[0].data, dtype = np.float32)

  header = hdul_tmp[0].header
  
jonas's avatar
jonas committed
47
48
49
50
51
52
53
54
55
56
57
  return data, header 


def fits_get_sampling(file,verbose = False):
    '''
    wave_axis,voltagesData,tunning_constant,cpos = fits_get_sampling(file)
    No S/C velocity corrected!!!
    cpos = 0 if continuum is at first wavelength and = 5 if continuum is at the end

    From SPGPylibs PHITools
    '''
jonas's avatar
jonas committed
58
    #print('-- Obtaining voltages......')
jonas's avatar
jonas committed
59
    fg_head = 3
jonas's avatar
jonas committed
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
    #try:
    with fits.open(file) as hdu_list:
        header = hdu_list[fg_head].data
        j = 0
        dummy = 0
        voltagesData = np.zeros((6))
        tunning_constant = 0.0
        ref_wavelength = 0.0
        for v in header:
            #print(v)
            if (j < 6):
                if tunning_constant == 0:
                    tunning_constant = float(v[4])/1e9
                if ref_wavelength == 0:
                    ref_wavelength = float(v[5])/1e3
75
76
                #print(dummy, v[2], type(dummy), type(v[2]))
                if np.abs(np.abs(float(v[2])) - np.abs(dummy)) > 5: #check that the next voltage is more than 5 from the previous, as voltages change slightly
jonas's avatar
jonas committed
77
78
79
80
                    #print(dummy, v[2])
                    voltagesData[j] = float(v[2])
                    dummy = voltagesData[j] 
                    j += 1
jonas's avatar
jonas committed
81

jonas's avatar
jonas committed
82
83
    #except Exception:
    #   print("Unable to open fits file: {}",file)     
jonas's avatar
jonas committed
84
85
86
87
88
89
90
91
92
93
94
95
    #print(voltagesData)
    d1 = voltagesData[0] - voltagesData[1]
    d2 = voltagesData[4] - voltagesData[5]
    if np.abs(d1) > np.abs(d2):
        cpos = 0
    else:
        cpos = 5
    if verbose:
        print('Continuum position at wave: ', cpos)
    wave_axis = voltagesData*tunning_constant + ref_wavelength  #6173.3356
    #print(wave_axis)
    return wave_axis,voltagesData,tunning_constant,cpos
jonas's avatar
jonas committed
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115

def fix_path(path,dir='forward',verbose=False):
    path = repr(path)
    if dir == 'forward':
        path = path.replace(")", "\)")
        path = path.replace("(", "\(")
        path = path.replace(" ", "\ ")
        path = os.path.abspath(path).split("'")[1]
        if verbose == True:
            print('forward')
            print(path)
        return path
    elif dir == 'backward':
        path = path.replace("\\\\", "")
        path = path.split("'")[1]
        if verbose == True:
            print('backward')
            print(path)
        return path
    else:
jonas's avatar
jonas committed
116
117
        pass   

jonas's avatar
jonas committed
118
def cmilos(data_f, hdr_arr, wve_axis_arr, data_shape, cpos_arr, data, rte, field_stop, start_row, start_col, out_rte_filename, out_dir):
jonas's avatar
jonas committed
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
    print(" ")
    printc('-->>>>>>> RUNNING CMILOS ',color=bcolors.OKGREEN)
    
    try:
        CMILOS_LOC = os.path.realpath(__file__)

        CMILOS_LOC = CMILOS_LOC[:-8] + 'cmilos/' #-11 as hrt_pipe.py is 11 characters

        if os.path.isfile(CMILOS_LOC+'milos'):
            printc("Cmilos executable located at:", CMILOS_LOC,color=bcolors.WARNING)

        else:
            raise ValueError('Cannot find cmilos:', CMILOS_LOC)

    except ValueError as err:
        printc(err.args[0],color=bcolors.FAIL)
        printc(err.args[1],color=bcolors.FAIL)
        return        

    wavelength = 6173.3356

    for scan in range(int(data_shape[-1])):

        start_time = time.time()

        file_path = data_f[scan]
        wave_axis = wve_axis_arr[scan]
jonas's avatar
jonas committed
146
        hdr = hdr_arr[scan]
jonas's avatar
jonas committed
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

        #must invert each scan independently, as cmilos only takes in one dataset at a time

        #get wave_axis from the header information of the science scans
        if cpos_arr[0] == 0:
            shift_w =  wave_axis[3] - wavelength
        elif cpos_arr[0] == 5:
            shift_w =  wave_axis[2] - wavelength

        wave_axis = wave_axis - shift_w

        print('It is assumed the wavelength array is given by the header')
        #print(wave_axis,color = bcolors.WARNING)
        print("Wave axis is: ", (wave_axis - wavelength)*1000.)
        print('Saving data into dummy_in.txt for RTE input')

        sdata = data[:,:,:,:,scan]
        y,x,p,l = sdata.shape
        #print(y,x,p,l)

        filename = 'dummy_in.txt'
        with open(filename,"w") as f:
            for i in range(x):
                for j in range(y):
                    for k in range(l):
                        f.write('%e %e %e %e %e \n' % (wave_axis[k],sdata[j,i,0,k],sdata[j,i,1,k],sdata[j,i,2,k],sdata[j,i,3,k])) #wv, I, Q, U, V
        del sdata

        printc(f'  ---- >>>>> Inverting data scan number: {scan} .... ',color=bcolors.OKGREEN)

        cmd = CMILOS_LOC+"./milos"
        cmd = fix_path(cmd)

        if rte == 'RTE':
            rte_on = subprocess.call(cmd+" 6 15 0 0 dummy_in.txt  >  dummy_out.txt",shell=True)
        if rte == 'CE':
            rte_on = subprocess.call(cmd+" 6 15 2 0 dummy_in.txt  >  dummy_out.txt",shell=True)
        if rte == 'CE+RTE':
            rte_on = subprocess.call(cmd+" 6 15 1 0 dummy_in.txt  >  dummy_out.txt",shell=True)

        #print(rte_on)

        printc('  ---- >>>>> Reading results.... ',color=bcolors.OKGREEN)
        del_dummy = subprocess.call("rm dummy_in.txt",shell=True)
        #print(del_dummy)

        res = np.loadtxt('dummy_out.txt')
        npixels = res.shape[0]/12.
        #print(npixels)
        #print(npixels/x)
        result = np.zeros((12,y*x)).astype(float)
        rte_invs = np.zeros((12,y,x)).astype(float)
        for i in range(y*x):
            result[:,i] = res[i*12:(i+1)*12]
        result = result.reshape(12,y,x)
        result = np.einsum('ijk->ikj', result)
        rte_invs = result
        del result
        rte_invs_noth = np.copy(rte_invs)

        """
        From 0 to 11
        Counter (PX Id)
        Iterations
        Strength
        Inclination
        Azimuth
        Eta0 parameter
        Doppler width
        Damping
        Los velocity
        Constant source function
        Slope source function
        Minimum chisqr value
        """

        noise_in_V =  np.mean(data[:,:,3,cpos_arr[0],:])
        low_values_flags = np.max(np.abs(data[:,:,3,:,scan]),axis=-1) < noise_in_V  # Where values are low
        
        rte_invs[2,low_values_flags] = 0
        rte_invs[3,low_values_flags] = 0
        rte_invs[4,low_values_flags] = 0

        #np.savez_compressed(out_dir+'_RTE', rte_invs=rte_invs, rte_invs_noth=rte_invs_noth)
        
        del_dummy = subprocess.call("rm dummy_out.txt",shell=True)
        #print(del_dummy)

jonas's avatar
jonas committed
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
        """
        #vlos S/C vorrection
        v_x, v_y, v_z = hdr['HCIX_VOB']/1000, hdr['HCIY_VOB']/1000, hdr['HCIZ_VOB']/1000

        #need line of sight velocity, should be total HCI velocity in km/s, with sun at origin. 
        #need to take care for velocities moving towards the sun, (ie negative) #could use continuum position as if towards or away
    
        if cpos_arr[scan] == 5: #moving away, redshifted
            dir_factor = 1
        
        elif cpos_arr[scan] == 0: #moving towards, blueshifted
            dir_factor == -1
        
        v_tot = dir_factor*math.sqrt(v_x**2 + v_y**2+v_z**2) #in km/s

        rte_invs_noth[8,:,:] = rte_invs_noth[8,:,:] - v_tot
        """

jonas's avatar
jonas committed
253
254
255
256
257
258
259
260
261
262
263
264
265
266
        rte_data_products = np.zeros((6,rte_invs_noth.shape[1],rte_invs_noth.shape[2]))

        rte_data_products[0,:,:] = rte_invs_noth[9,:,:] + rte_invs_noth[10,:,:] #continuum
        rte_data_products[1,:,:] = rte_invs_noth[2,:,:] #b mag strength
        rte_data_products[2,:,:] = rte_invs_noth[3,:,:] #inclination
        rte_data_products[3,:,:] = rte_invs_noth[4,:,:] #azimuth
        rte_data_products[4,:,:] = rte_invs_noth[8,:,:] #vlos
        rte_data_products[5,:,:] = rte_invs_noth[2,:,:]*np.cos(rte_invs_noth[3,:,:]*np.pi/180.) #blos

        rte_data_products *= field_stop[np.newaxis,start_row:start_row + data.shape[0],start_col:start_col + data.shape[1]] #field stop, set outside to 0

        if out_rte_filename is None:
            filename_root = str(file_path.split('.fits')[0][-10:])
        else:
jonas's avatar
jonas committed
267
268
269
270
271
272
273
274
275
            if isinstance(out_rte_filename, list):
                filename_root = out_rte_filename[scan]

            elif isinstance(out_rte_filename, str):
                filename_root = out_rte_filename

            else:
                filename_root = str(file_path.split('.fits')[0][-10:])
                print(f"out_rte_filename neither string nor list, reverting to default: {filename_root}")
jonas's avatar
jonas committed
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342

        with fits.open(file_path) as hdu_list:
            hdu_list[0].data = rte_data_products
            hdu_list.writeto(out_dir+filename_root+'_rte_data_products.fits', overwrite=True)

        with fits.open(file_path) as hdu_list:
            hdu_list[0].data = rte_data_products[5,:,:]
            hdu_list.writeto(out_dir+filename_root+'_blos_rte.fits', overwrite=True)

        with fits.open(file_path) as hdu_list:
            hdu_list[0].data = rte_data_products[4,:,:]
            hdu_list.writeto(out_dir+filename_root+'_vlos_rte.fits', overwrite=True)

        with fits.open(file_path) as hdu_list:
            hdu_list[0].data = rte_data_products[0,:,:]
            hdu_list.writeto(out_dir+filename_root+'_Icont_rte.fits', overwrite=True)

        printc('--------------------------------------------------------------',bcolors.OKGREEN)
        printc(f"------------- CMILOS RTE Run Time: {np.round(time.time() - start_time,3)} seconds ",bcolors.OKGREEN)
        printc('--------------------------------------------------------------',bcolors.OKGREEN)

def pmilos(data_f, wve_axis_arr, data_shape, cpos_arr, data, rte, field_stop, start_row, start_col, out_rte_filename, out_dir):
    print(" ")
    printc('-->>>>>>> RUNNING PMILOS ',color=bcolors.OKGREEN)
    
    try:
        PMILOS_LOC = os.path.realpath(__file__)

        PMILOS_LOC = PMILOS_LOC[:-8] + 'P-MILOS/' #11 as hrt_pipe.py is 11 characters -8 if in utils.py

        if os.path.isfile(PMILOS_LOC+'pmilos.x'):
            printc("Pmilos executable located at:", PMILOS_LOC,color=bcolors.WARNING)

        else:
            raise ValueError('Cannot find pmilos:', PMILOS_LOC)

    except ValueError as err:
        printc(err.args[0],color=bcolors.FAIL)
        printc(err.args[1],color=bcolors.FAIL)
        return  
    
    wavelength = 6173.3356

    for scan in range(int(data_shape[-1])):

        start_time = time.time()

        file_path = data_f[scan]
        wave_axis = wve_axis_arr[scan]

        #must invert each scan independently, as cmilos only takes in one dataset at a time

        #get wave_axis from the header information of the science scans
        if cpos_arr[0] == 0:
            shift_w =  wave_axis[3] - wavelength
        elif cpos_arr[0] == 5:
            shift_w =  wave_axis[2] - wavelength

        wave_axis = wave_axis - shift_w

        print('It is assumed the wavelength array is given by the header')
        #print(wave_axis,color = bcolors.WARNING)
        print("Wave axis is: ", (wave_axis - wavelength)*1000.)
        print('Saving data into ./P-MILOS/run/data/input_tmp.fits for pmilos RTE input')

        #write wavelengths to wavelength.fits file for the settings

343
344
345
346
347
        wave_input = np.zeros((2,6)) #cfitsio reads dimensions in opposite order
        wave_input[0,:] = 1
        wave_input[1,:] = wave_axis

        print(wave_axis)
jonas's avatar
jonas committed
348
349
350

        hdr = fits.Header()

351
        primary_hdu = fits.PrimaryHDU(wave_input, header = hdr)
jonas's avatar
jonas committed
352
353
354
355
        hdul = fits.HDUList([primary_hdu])
        hdul.writeto(f'./P-MILOS/run/wavelength_tmp.fits', overwrite=True)

        sdata = data[:,:,:,:,scan]
jonas's avatar
jonas committed
356
357

        #create input fits file for pmilos
jonas's avatar
jonas committed
358
359
360
361
362
363
364
365
366
367
368
369
        hdr = fits.Header() 
        
        hdr['CTYPE1'] = 'HPLT-TAN'
        hdr['CTYPE2'] = 'HPLN-TAN'
        hdr['CTYPE3'] = 'STOKES' #check order of stokes
        hdr['CTYPE4'] = 'WAVE-GRI' 
    
        primary_hdu = fits.PrimaryHDU(sdata, header = hdr)
        hdul = fits.HDUList([primary_hdu])
        hdul.writeto(f'./P-MILOS/run/data/input_tmp.fits', overwrite=True)

        if rte == 'RTE':
370
            cmd = "mpiexec -np 10 ../pmilos.x pmilos.minit"
jonas's avatar
jonas committed
371
        
jonas's avatar
jonas committed
372
        if rte == 'CE':
373
            cmd = "mpiexec -np 10 ../pmilos.x pmilos_ce.minit"
jonas's avatar
jonas committed
374
375
376

        if rte == 'CE+RTE':
            print("CE+RTE not possible on PMILOS, performing RTE instead")
377
            cmd = "mpiexec -np 10 ../pmilos.x pmilos.minit"
jonas's avatar
jonas committed
378

jonas's avatar
jonas committed
379
380
381
382
383
       
        del sdata
        #need to change settings for CE or CE+RTE in the pmilos.minit file here
        
        printc(f'  ---- >>>>> Inverting data scan number: {scan} .... ',color=bcolors.OKGREEN)
jonas's avatar
jonas committed
384

jonas's avatar
jonas committed
385
386
387
        cwd = os.getcwd()
        os.chdir("./P-MILOS/run/")
        rte_on = subprocess.call(cmd,shell=True)
jonas's avatar
jonas committed
388
389
        os.chdir(cwd)

390
391
392
393
394
395
396
        if rte == 'CE':
            out_file = 'inv_input_tmp_mod_ce.fits'

        else:
            out_file = 'inv_input_tmp_mod.fits'

        with fits.open(f'./P-MILOS/run/results/{out_file}') as hdu_list:
jonas's avatar
jonas committed
397
398
            result = hdu_list[0].data

jonas's avatar
jonas committed
399
        #del_dummy = subprocess.call(f"rm ./P-MILOS/run/results/{out_file}.fits",shell=True) 
400
        del_dummy = subprocess.call(f"rm ./P-MILOS/run/results/{out_file}.fits",shell=True) #must delete the output file
jonas's avatar
jonas committed
401
402
      
        #result has dimensions [rows,cols,13]
jonas's avatar
jonas committed
403
        print(result.shape)
jonas's avatar
jonas committed
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
        """
        PMILOS Output 13 columns
        0. eta0 = line-to-continuum absorption coefficient ratio 
        1. B = magnetic field strength [Gauss] 
        2. vlos = line-of-sight velocity [km/s] 
        3. dopp = Doppler width [Angstroms] 
        4. aa = damping parameter 
        5. gm = magnetic field inclination [deg] 
        6. az = magnetic field azimuth [deg] 
        7. S0 = source function constant 
        8. S1 = source function gradient 
        9. mac = macroturbulent velocity [km/s] 
        10. filling factor of the magnetic component [0-1]  
        11. Number of iterations performed 
        12. Chisqr value
        """

        rte_data_products = np.zeros((6,result.shape[0],result.shape[1]))

        rte_data_products[0,:,:] = result[:,:,7] + result[:,:,8] #continuum
jonas's avatar
jonas committed
424
425
426
427
428
        rte_data_products[1,:,:] = result[:,:,1] #b mag strength
        rte_data_products[2,:,:] = result[:,:,5] #inclination
        rte_data_products[3,:,:] = result[:,:,6] #azimuth
        rte_data_products[4,:,:] = result[:,:,2] #vlos
        rte_data_products[5,:,:] = result[:,:,1]*np.cos(result[:,:,5]*np.pi/180.) #blos
jonas's avatar
jonas committed
429
430
431
432

        rte_data_products *= field_stop[np.newaxis,start_row:start_row + data.shape[0],start_col:start_col + data.shape[1]] #field stop, set outside to 0

        #flipping taken care of for the field stop in the hrt_pipe 
jonas's avatar
jonas committed
433

jonas's avatar
jonas committed
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
        if out_rte_filename is None:
            filename_root = str(file_path.split('.fits')[0][-10:])
        else:
            filename_root = out_rte_filename

        with fits.open(file_path) as hdu_list:
            hdu_list[0].data = rte_data_products
            hdu_list.writeto(out_dir+filename_root+'_rte_data_products.fits', overwrite=True)

        with fits.open(file_path) as hdu_list:
            hdu_list[0].data = rte_data_products[5,:,:]
            hdu_list.writeto(out_dir+filename_root+'_blos_rte.fits', overwrite=True)

        with fits.open(file_path) as hdu_list:
            hdu_list[0].data = rte_data_products[4,:,:]
            hdu_list.writeto(out_dir+filename_root+'_vlos_rte.fits', overwrite=True)

        with fits.open(file_path) as hdu_list:
            hdu_list[0].data = rte_data_products[0,:,:]
            hdu_list.writeto(out_dir+filename_root+'_Icont_rte.fits', overwrite=True)


    printc('--------------------------------------------------------------',bcolors.OKGREEN)
    printc(f"------------- PMILOS RTE Run Time: {np.round(time.time() - start_time,3)} seconds ",bcolors.OKGREEN)
    printc('--------------------------------------------------------------',bcolors.OKGREEN)