In [2]:
%pylab inline
# combine_dustmaps.py: make a composite extinction map (Marshall,Green,Drimmel)
###############################################################################
import sys
import os, os.path
import pickle
import numpy
import h5py
import healpy
from galpy.util import save_pickles
import mwdust

_DEGTORAD= numpy.pi/180.
_ERASESTR= "                                                                                "
_DRYISHRUN= False

def dist2distmod(dist):
    """dist in kpc to distance modulus"""
    return 5.*numpy.log10(dist)+10.
def distmod2dist(distmod):
    """distance modulus to distance in kpc"""
    return 10.**(distmod/5.-2.)

_greendir= os.path.join(os.getenv('DUST_DIR'),'green19')
_GREEN15DISTMODS= numpy.linspace(4.,19.,31)
_GREEN15DISTS= distmod2dist(_GREEN15DISTMODS)


Populating the interactive namespace from numpy and matplotlib
/home/rybizki/anaconda3/lib/python3.6/importlib/_bootstrap.py:219: RuntimeWarning: numpy.dtype size changed, may indicate binary incompatibility. Expected 96, got 88
  return f(*args, **kwds)
/home/rybizki/anaconda3/lib/python3.6/site-packages/h5py/__init__.py:34: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
  from ._conv import register_converters as _register_converters

In [2]:
greendata = h5py.File(os.path.join(_greendir,'bayestar2019.h5'),'r')

In [3]:
greendata['/pixel_info'].attrs['DM_bin_edges']


Out[3]:
array([ 4.   ,  4.125,  4.25 ,  4.375,  4.5  ,  4.625,  4.75 ,  4.875,
        5.   ,  5.125,  5.25 ,  5.375,  5.5  ,  5.625,  5.75 ,  5.875,
        6.   ,  6.125,  6.25 ,  6.375,  6.5  ,  6.625,  6.75 ,  6.875,
        7.   ,  7.125,  7.25 ,  7.375,  7.5  ,  7.625,  7.75 ,  7.875,
        8.   ,  8.125,  8.25 ,  8.375,  8.5  ,  8.625,  8.75 ,  8.875,
        9.   ,  9.125,  9.25 ,  9.375,  9.5  ,  9.625,  9.75 ,  9.875,
       10.   , 10.125, 10.25 , 10.375, 10.5  , 10.625, 10.75 , 10.875,
       11.   , 11.125, 11.25 , 11.375, 11.5  , 11.625, 11.75 , 11.875,
       12.   , 12.125, 12.25 , 12.375, 12.5  , 12.625, 12.75 , 12.875,
       13.   , 13.125, 13.25 , 13.375, 13.5  , 13.625, 13.75 , 13.875,
       14.   , 14.125, 14.25 , 14.375, 14.5  , 14.625, 14.75 , 14.875,
       15.   , 15.125, 15.25 , 15.375, 15.5  , 15.625, 15.75 , 15.875,
       16.   , 16.125, 16.25 , 16.375, 16.5  , 16.625, 16.75 , 16.875,
       17.   , 17.125, 17.25 , 17.375, 17.5  , 17.625, 17.75 , 17.875,
       18.   , 18.125, 18.25 , 18.375, 18.5  , 18.625, 18.75 , 18.875])

In [12]:
numpy.linspace(4,18.875,120)


Out[12]:
array([ 4.   ,  4.125,  4.25 ,  4.375,  4.5  ,  4.625,  4.75 ,  4.875,
        5.   ,  5.125,  5.25 ,  5.375,  5.5  ,  5.625,  5.75 ,  5.875,
        6.   ,  6.125,  6.25 ,  6.375,  6.5  ,  6.625,  6.75 ,  6.875,
        7.   ,  7.125,  7.25 ,  7.375,  7.5  ,  7.625,  7.75 ,  7.875,
        8.   ,  8.125,  8.25 ,  8.375,  8.5  ,  8.625,  8.75 ,  8.875,
        9.   ,  9.125,  9.25 ,  9.375,  9.5  ,  9.625,  9.75 ,  9.875,
       10.   , 10.125, 10.25 , 10.375, 10.5  , 10.625, 10.75 , 10.875,
       11.   , 11.125, 11.25 , 11.375, 11.5  , 11.625, 11.75 , 11.875,
       12.   , 12.125, 12.25 , 12.375, 12.5  , 12.625, 12.75 , 12.875,
       13.   , 13.125, 13.25 , 13.375, 13.5  , 13.625, 13.75 , 13.875,
       14.   , 14.125, 14.25 , 14.375, 14.5  , 14.625, 14.75 , 14.875,
       15.   , 15.125, 15.25 , 15.375, 15.5  , 15.625, 15.75 , 15.875,
       16.   , 16.125, 16.25 , 16.375, 16.5  , 16.625, 16.75 , 16.875,
       17.   , 17.125, 17.25 , 17.375, 17.5  , 17.625, 17.75 , 17.875,
       18.   , 18.125, 18.25 , 18.375, 18.5  , 18.625, 18.75 , 18.875])

In [191]:
numpy.linspace(4,19,31)


Out[191]:
array([ 4. ,  4.5,  5. ,  5.5,  6. ,  6.5,  7. ,  7.5,  8. ,  8.5,  9. ,
        9.5, 10. , 10.5, 11. , 11.5, 12. , 12.5, 13. , 13.5, 14. , 14.5,
       15. , 15.5, 16. , 16.5, 17. , 17.5, 18. , 18.5, 19. ])

In [22]:
drimmel= mwdust.Drimmel03()

In [23]:
dists = _GREEN15DISTS

In [188]:
dists


Out[188]:
array([ 0.06309573,  0.07943282,  0.1       ,  0.12589254,  0.15848932,
        0.19952623,  0.25118864,  0.31622777,  0.39810717,  0.50118723,
        0.63095734,  0.79432823,  1.        ,  1.25892541,  1.58489319,
        1.99526231,  2.51188643,  3.16227766,  3.98107171,  5.01187234,
        6.30957344,  7.94328235, 10.        , 12.58925412, 15.84893192,
       19.95262315, 25.11886432, 31.6227766 , 39.81071706, 50.11872336,
       63.09573445])

In [7]:
dists = _GREEN15DISTS
plt.plot(dists,drimmel(270,0,dists))
plt.xlabel('dist in kpc')
plt.ylabel('E(B-V) in mag')
plt.xscale('log')
plt.yscale('linear')



In [8]:
healpy.pix2ang(256,0,nest = True, lonlat = True)


Out[8]:
(45.0, 0.14920792779581404)

In [145]:
def get_nside(healpix_number = 1):
    """
    returns the nside for specific healpix level
    """
    return(np.power(2,healpix_number))

def number_of_healpixels(healpix_number = 1):
    """
    returns the number of pixels for a specific level
    """
    return(np.power(4,healpix_number)*12)

In [147]:
healpy.pix2ang(256,0,nest = True, lonlat = True)


Out[147]:
(45.0, 0.14920792779581404)

In [10]:
hpx_level = 8
nside = get_nside(hpx_level)
print(nside)
nHpx = number_of_healpixels(hpx_level)
ext = np.zeros(shape = (nHpx,len(_GREEN15DISTS)))
for i in range(nHpx):
    if i%1000 == 0:
        print(i, nHpx)
    lb = healpy.pix2ang(nside,i,nest = True, lonlat = True)
    ext[i] = drimmel(lb[0],lb[1],_GREEN15DISTS)


256
0 786432
1000 786432
2000 786432
3000 786432
4000 786432
5000 786432
6000 786432
7000 786432
8000 786432
9000 786432
10000 786432
11000 786432
12000 786432
13000 786432
14000 786432
15000 786432
16000 786432
17000 786432
18000 786432
19000 786432
20000 786432
21000 786432
22000 786432
23000 786432
24000 786432
25000 786432
26000 786432
27000 786432
28000 786432
29000 786432
30000 786432
31000 786432
32000 786432
33000 786432
34000 786432
35000 786432
36000 786432
37000 786432
38000 786432
39000 786432
40000 786432
41000 786432
42000 786432
43000 786432
44000 786432
45000 786432
46000 786432
47000 786432
48000 786432
49000 786432
50000 786432
51000 786432
52000 786432
53000 786432
54000 786432
55000 786432
56000 786432
57000 786432
58000 786432
59000 786432
60000 786432
61000 786432
62000 786432
63000 786432
64000 786432
65000 786432
66000 786432
67000 786432
68000 786432
69000 786432
70000 786432
71000 786432
72000 786432
73000 786432
74000 786432
75000 786432
76000 786432
77000 786432
78000 786432
79000 786432
80000 786432
81000 786432
82000 786432
83000 786432
84000 786432
85000 786432
86000 786432
87000 786432
88000 786432
89000 786432
90000 786432
91000 786432
92000 786432
93000 786432
94000 786432
95000 786432
96000 786432
97000 786432
98000 786432
99000 786432
100000 786432
101000 786432
102000 786432
103000 786432
104000 786432
105000 786432
106000 786432
107000 786432
108000 786432
109000 786432
110000 786432
111000 786432
112000 786432
113000 786432
114000 786432
115000 786432
116000 786432
117000 786432
118000 786432
119000 786432
120000 786432
121000 786432
122000 786432
123000 786432
124000 786432
125000 786432
126000 786432
127000 786432
128000 786432
129000 786432
130000 786432
131000 786432
132000 786432
133000 786432
134000 786432
135000 786432
136000 786432
137000 786432
138000 786432
139000 786432
140000 786432
141000 786432
142000 786432
143000 786432
144000 786432
145000 786432
146000 786432
147000 786432
148000 786432
149000 786432
150000 786432
151000 786432
152000 786432
153000 786432
154000 786432
155000 786432
156000 786432
157000 786432
158000 786432
159000 786432
160000 786432
161000 786432
162000 786432
163000 786432
164000 786432
165000 786432
166000 786432
167000 786432
168000 786432
169000 786432
170000 786432
171000 786432
172000 786432
173000 786432
174000 786432
175000 786432
176000 786432
177000 786432
178000 786432
179000 786432
180000 786432
181000 786432
182000 786432
183000 786432
184000 786432
185000 786432
186000 786432
187000 786432
188000 786432
189000 786432
190000 786432
191000 786432
192000 786432
193000 786432
194000 786432
195000 786432
196000 786432
197000 786432
198000 786432
199000 786432
200000 786432
201000 786432
202000 786432
203000 786432
204000 786432
205000 786432
206000 786432
207000 786432
208000 786432
209000 786432
210000 786432
211000 786432
212000 786432
213000 786432
214000 786432
215000 786432
216000 786432
217000 786432
218000 786432
219000 786432
220000 786432
221000 786432
222000 786432
223000 786432
224000 786432
225000 786432
226000 786432
227000 786432
228000 786432
229000 786432
230000 786432
231000 786432
232000 786432
233000 786432
234000 786432
235000 786432
236000 786432
237000 786432
238000 786432
239000 786432
240000 786432
241000 786432
242000 786432
243000 786432
244000 786432
245000 786432
246000 786432
247000 786432
248000 786432
249000 786432
250000 786432
251000 786432
252000 786432
253000 786432
254000 786432
255000 786432
256000 786432
257000 786432
258000 786432
259000 786432
260000 786432
261000 786432
262000 786432
263000 786432
264000 786432
265000 786432
266000 786432
267000 786432
268000 786432
269000 786432
270000 786432
271000 786432
272000 786432
273000 786432
274000 786432
275000 786432
276000 786432
277000 786432
278000 786432
279000 786432
280000 786432
281000 786432
282000 786432
283000 786432
284000 786432
285000 786432
286000 786432
287000 786432
288000 786432
289000 786432
290000 786432
291000 786432
292000 786432
293000 786432
294000 786432
295000 786432
296000 786432
297000 786432
298000 786432
299000 786432
300000 786432
301000 786432
302000 786432
303000 786432
304000 786432
305000 786432
306000 786432
307000 786432
308000 786432
309000 786432
310000 786432
311000 786432
312000 786432
313000 786432
314000 786432
315000 786432
316000 786432
317000 786432
318000 786432
319000 786432
320000 786432
321000 786432
322000 786432
323000 786432
324000 786432
325000 786432
326000 786432
327000 786432
328000 786432
329000 786432
330000 786432
331000 786432
332000 786432
333000 786432
334000 786432
335000 786432
336000 786432
337000 786432
338000 786432
339000 786432
340000 786432
341000 786432
342000 786432
343000 786432
344000 786432
345000 786432
346000 786432
347000 786432
348000 786432
349000 786432
350000 786432
351000 786432
352000 786432
353000 786432
354000 786432
355000 786432
356000 786432
357000 786432
358000 786432
359000 786432
360000 786432
361000 786432
362000 786432
363000 786432
364000 786432
365000 786432
366000 786432
367000 786432
368000 786432
369000 786432
370000 786432
371000 786432
372000 786432
373000 786432
374000 786432
375000 786432
376000 786432
377000 786432
378000 786432
379000 786432
380000 786432
381000 786432
382000 786432
383000 786432
384000 786432
385000 786432
386000 786432
387000 786432
388000 786432
389000 786432
390000 786432
391000 786432
392000 786432
393000 786432
394000 786432
395000 786432
396000 786432
397000 786432
398000 786432
399000 786432
400000 786432
401000 786432
402000 786432
403000 786432
404000 786432
405000 786432
406000 786432
407000 786432
408000 786432
409000 786432
410000 786432
411000 786432
412000 786432
413000 786432
414000 786432
415000 786432
416000 786432
417000 786432
418000 786432
419000 786432
420000 786432
421000 786432
422000 786432
423000 786432
424000 786432
425000 786432
426000 786432
427000 786432
428000 786432
429000 786432
430000 786432
431000 786432
432000 786432
433000 786432
434000 786432
435000 786432
436000 786432
437000 786432
438000 786432
439000 786432
440000 786432
441000 786432
442000 786432
443000 786432
444000 786432
445000 786432
446000 786432
447000 786432
448000 786432
449000 786432
450000 786432
451000 786432
452000 786432
453000 786432
454000 786432
455000 786432
456000 786432
457000 786432
458000 786432
459000 786432
460000 786432
461000 786432
462000 786432
463000 786432
464000 786432
465000 786432
466000 786432
467000 786432
468000 786432
469000 786432
470000 786432
471000 786432
472000 786432
473000 786432
474000 786432
475000 786432
476000 786432
477000 786432
478000 786432
479000 786432
480000 786432
481000 786432
482000 786432
483000 786432
484000 786432
485000 786432
486000 786432
487000 786432
488000 786432
489000 786432
490000 786432
491000 786432
492000 786432
493000 786432
494000 786432
495000 786432
496000 786432
497000 786432
498000 786432
499000 786432
500000 786432
501000 786432
502000 786432
503000 786432
504000 786432
505000 786432
506000 786432
507000 786432
508000 786432
509000 786432
510000 786432
511000 786432
512000 786432
513000 786432
514000 786432
515000 786432
516000 786432
517000 786432
518000 786432
519000 786432
520000 786432
521000 786432
522000 786432
523000 786432
524000 786432
525000 786432
526000 786432
527000 786432
528000 786432
529000 786432
530000 786432
531000 786432
532000 786432
533000 786432
534000 786432
535000 786432
536000 786432
537000 786432
538000 786432
539000 786432
540000 786432
541000 786432
542000 786432
543000 786432
544000 786432
545000 786432
546000 786432
547000 786432
548000 786432
549000 786432
550000 786432
551000 786432
552000 786432
553000 786432
554000 786432
555000 786432
556000 786432
557000 786432
558000 786432
559000 786432
560000 786432
561000 786432
562000 786432
563000 786432
564000 786432
565000 786432
566000 786432
567000 786432
568000 786432
569000 786432
570000 786432
571000 786432
572000 786432
573000 786432
574000 786432
575000 786432
576000 786432
577000 786432
578000 786432
579000 786432
580000 786432
581000 786432
582000 786432
583000 786432
584000 786432
585000 786432
586000 786432
587000 786432
588000 786432
589000 786432
590000 786432
591000 786432
592000 786432
593000 786432
594000 786432
595000 786432
596000 786432
597000 786432
598000 786432
599000 786432
600000 786432
601000 786432
602000 786432
603000 786432
604000 786432
605000 786432
606000 786432
607000 786432
608000 786432
609000 786432
610000 786432
611000 786432
612000 786432
613000 786432
614000 786432
615000 786432
616000 786432
617000 786432
618000 786432
619000 786432
620000 786432
621000 786432
622000 786432
623000 786432
624000 786432
625000 786432
626000 786432
627000 786432
628000 786432
629000 786432
630000 786432
631000 786432
632000 786432
633000 786432
634000 786432
635000 786432
636000 786432
637000 786432
638000 786432
639000 786432
640000 786432
641000 786432
642000 786432
643000 786432
644000 786432
645000 786432
646000 786432
647000 786432
648000 786432
649000 786432
650000 786432
651000 786432
652000 786432
653000 786432
654000 786432
655000 786432
656000 786432
657000 786432
658000 786432
659000 786432
660000 786432
661000 786432
662000 786432
663000 786432
664000 786432
665000 786432
666000 786432
667000 786432
668000 786432
669000 786432
670000 786432
671000 786432
672000 786432
673000 786432
674000 786432
675000 786432
676000 786432
677000 786432
678000 786432
679000 786432
680000 786432
681000 786432
682000 786432
683000 786432
684000 786432
685000 786432
686000 786432
687000 786432
688000 786432
689000 786432
690000 786432
691000 786432
692000 786432
693000 786432
694000 786432
695000 786432
696000 786432
697000 786432
698000 786432
699000 786432
700000 786432
701000 786432
702000 786432
703000 786432
704000 786432
705000 786432
706000 786432
707000 786432
708000 786432
709000 786432
710000 786432
711000 786432
712000 786432
713000 786432
714000 786432
715000 786432
716000 786432
717000 786432
718000 786432
719000 786432
720000 786432
721000 786432
722000 786432
723000 786432
724000 786432
725000 786432
726000 786432
727000 786432
728000 786432
729000 786432
730000 786432
731000 786432
732000 786432
733000 786432
734000 786432
735000 786432
736000 786432
737000 786432
738000 786432
739000 786432
740000 786432
741000 786432
742000 786432
743000 786432
744000 786432
745000 786432
746000 786432
747000 786432
748000 786432
749000 786432
750000 786432
751000 786432
752000 786432
753000 786432
754000 786432
755000 786432
756000 786432
757000 786432
758000 786432
759000 786432
760000 786432
761000 786432
762000 786432
763000 786432
764000 786432
765000 786432
766000 786432
767000 786432
768000 786432
769000 786432
770000 786432
771000 786432
772000 786432
773000 786432
774000 786432
775000 786432
776000 786432
777000 786432
778000 786432
779000 786432
780000 786432
781000 786432
782000 786432
783000 786432
784000 786432
785000 786432
786000 786432

In [11]:
from astropy.io import fits
fits.writeto('drimmel_map',ext)

In [15]:
ext.shape


Out[15]:
(786432, 31)

In [17]:
pixinfo = numpy.recarray(len(ext),dtype = [("nside","uint32"),("healpix_index","uint64")])
pixinfo["nside"] = np.ones(len(ext))*256
pixinfo["healpix_index"] = np.arange(len(ext))

In [18]:
outfile = h5py.File("drimmel.h5","w")
outfile.create_dataset("pixel_info", data = pixinfo)
outfile.create_dataset("best_fit", data = ext)
outfile.close()

In [19]:
drimmel = h5py.File("drimmel.h5",'r')

In [4]:
import dustmaps

In [13]:
from dustmaps.drimmel import DrimmelQuery
from astropy.coordinates import SkyCoord
import astropy.units as u

In [ ]:
drimmel = DrimmelQuery(map_fname='/home/rybizki/programme/dust_green/drimmel/drimmel.h5')
dr = mwdust.Drimmel03()

In [177]:
from healpy import ang2pix

In [179]:
ang2pix(256, 0, 0, nest=True, lonlat=True)


Out[179]:
311296

In [180]:
healpy.pix2ang(256,311296,nest = True, lonlat = True)


Out[180]:
(0.0, 0.14920792779581404)

In [182]:
hpx_level = 8
nside = get_nside(hpx_level)
print(nside)
nHpx = number_of_healpixels(hpx_level)
i = 311297
lb = healpy.pix2ang(nside,i,nest = True, lonlat = True)
l = lb[0]
b = lb[1]
d = dists
coords = SkyCoord(l*np.ones_like(d)*u.deg, b*np.ones_like(d)*u.deg, d*u.kpc, frame='galactic')
ext_1 = drimmel(coords)
ext_2 = dr(l,b,d)
for i,item in enumerate(d):
    print(i,"dist = %.2f" %item, "green=%.2f" %ext_1[i], "bovy=%.2f" %ext_2[i]) #ext_1[i]-ext_2[i])


256
0 dist = 0.06 green=0.06 bovy=0.06
1 dist = 0.08 green=0.08 bovy=0.08
2 dist = 0.10 green=0.10 bovy=0.10
3 dist = 0.13 green=0.12 bovy=0.12
4 dist = 0.16 green=0.16 bovy=0.16
5 dist = 0.20 green=0.20 bovy=0.20
6 dist = 0.25 green=0.26 bovy=0.26
7 dist = 0.32 green=0.33 bovy=0.33
8 dist = 0.40 green=0.42 bovy=0.42
9 dist = 0.50 green=0.54 bovy=0.54
10 dist = 0.63 green=0.70 bovy=0.70
11 dist = 0.79 green=0.92 bovy=0.92
12 dist = 1.00 green=1.21 bovy=1.21
13 dist = 1.26 green=1.63 bovy=1.63
14 dist = 1.58 green=2.29 bovy=2.29
15 dist = 2.00 green=3.17 bovy=3.17
16 dist = 2.51 green=4.50 bovy=4.50
17 dist = 3.16 green=6.72 bovy=6.72
18 dist = 3.98 green=10.50 bovy=10.50
19 dist = 5.01 green=15.64 bovy=15.64
20 dist = 6.31 green=19.11 bovy=19.11
21 dist = 7.94 green=19.97 bovy=19.97
22 dist = 10.00 green=21.22 bovy=21.22
23 dist = 12.59 green=29.87 bovy=29.87
24 dist = 15.85 green=35.77 bovy=35.77
25 dist = 19.95 green=37.36 bovy=37.36
26 dist = 25.12 green=37.61 bovy=37.61
27 dist = 31.62 green=37.61 bovy=37.61
28 dist = 39.81 green=37.61 bovy=37.61
29 dist = 50.12 green=37.61 bovy=37.61
30 dist = 63.10 green=37.61 bovy=37.61

In [183]:
from dustmaps import bayestar

In [184]:
bs = bayestar.BayestarQuery()


---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
<ipython-input-184-57be3a5e56e9> in <module>()
----> 1 bs = bayestar.BayestarQuery()

~/anaconda3/lib/python3.6/site-packages/dustmaps/bayestar.py in __init__(self, map_fname, max_samples, version)
    103         t_start = time()
    104 
--> 105         with h5py.File(map_fname, 'r') as f:
    106             # Load pixel information
    107             print('Loading pixel_info ...')

~/anaconda3/lib/python3.6/site-packages/h5py/_hl/files.py in __init__(self, name, mode, driver, libver, userblock_size, swmr, **kwds)
    269 
    270                 fapl = make_fapl(driver, libver, **kwds)
--> 271                 fid = make_fid(name, mode, userblock_size, fapl, swmr=swmr)
    272 
    273                 if swmr_support:

~/anaconda3/lib/python3.6/site-packages/h5py/_hl/files.py in make_fid(name, mode, userblock_size, fapl, fcpl, swmr)
     99         if swmr and swmr_support:
    100             flags |= h5f.ACC_SWMR_READ
--> 101         fid = h5f.open(name, flags, fapl=fapl)
    102     elif mode == 'r+':
    103         fid = h5f.open(name, h5f.ACC_RDWR, fapl=fapl)

h5py/_objects.pyx in h5py._objects.with_phil.wrapper()

h5py/_objects.pyx in h5py._objects.with_phil.wrapper()

h5py/h5f.pyx in h5py.h5f.open()

OSError: Unable to open file (Unable to open file: name = '/home/rybizki/programme/dust_green/bayestar/bayestar2019.h5', errno = 2, error message = 'no such file or directory', flags = 0, o_flags = 0)

In [186]:
x = h5py.File("/home/rybizki/programme/dust_green/bayestar/bayestar2019.h5","r")

In [187]:



Object `x.open` not found.

In [ ]: