pymakeplots 0.2.3__tar.gz → 0.2.4__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: pymakeplots
3
- Version: 0.2.3
3
+ Version: 0.2.4
4
4
  Home-page: https://github.com/TimothyADavis/pymakeplots
5
5
  Author: Timothy A. Davis
6
6
  Author-email: DavisT@cardiff.ac.uk
@@ -9,11 +9,11 @@ import matplotlib.pyplot as plt
9
9
  import matplotlib
10
10
  from pymakeplots.sauron_colormap import sauron
11
11
  from mpl_toolkits.axes_grid1 import make_axes_locatable
12
- from matplotlib.patches import Ellipse,Rectangle
12
+ from matplotlib.patches import Ellipse,Rectangle, Arrow
13
13
  from matplotlib import cm
14
14
  from matplotlib.colors import ListedColormap, LinearSegmentedColormap
15
15
  from matplotlib.offsetbox import AnchoredText,AuxTransformBox, AnchoredOffsetbox
16
- from mpl_toolkits.axes_grid1.anchored_artists import AnchoredSizeBar
16
+ from mpl_toolkits.axes_grid1.anchored_artists import AnchoredSizeBar,AnchoredDirectionArrows
17
17
  from astropy.coordinates import ICRS
18
18
  import matplotlib.gridspec as gridspec
19
19
  from astropy.table import Table
@@ -40,7 +40,7 @@ def rotateImage(img, angle, pivot):
40
40
 
41
41
 
42
42
  class pymakeplots:
43
- def __init__(self,cube_flat=None,pb=None,cube=None,rest_value=None,velocity_convention='radio'):
43
+ def __init__(self,cube_flat=None,pb=None,cube=None,rest_value=None,velocity_convention='radio',rotate=None):
44
44
  self.galname=None
45
45
  self.gal_distance=None
46
46
  self.posang=None
@@ -58,6 +58,7 @@ class pymakeplots:
58
58
  self.pbcorr_cube_trim=None
59
59
  self.mask_trim=None
60
60
  self.bmaj=None
61
+ self.rotate=rotate
61
62
  self.smoothmask_spatial=1.5
62
63
  self.bmin=None
63
64
  self.bpa=None
@@ -93,6 +94,8 @@ class pymakeplots:
93
94
  self.make_square=True
94
95
  self.useallpixels = False
95
96
  self.suppress_subbeam_artifacts=False
97
+ self.mom1_cmap=sauron
98
+ self.mom2_cmap=sauron
96
99
  #self.wcs=None
97
100
 
98
101
  if (cube != None)&(pb==None)&(cube_flat==None):
@@ -322,22 +325,44 @@ class pymakeplots:
322
325
  def get_header_coord_arrays(self,hdr):
323
326
 
324
327
 
325
- cd1=self.spectralcube.wcs.pixel_scale_matrix[0,0]*3600
326
- cd2=self.spectralcube.wcs.pixel_scale_matrix[1,1]*3600
327
- x1=((np.arange(1,hdr['NAXIS1']+1)-(hdr['NAXIS1']//2))*cd1)# + hdr['CRVAL1']
328
+ cd1=self.spectralcube.header['CDELT1']*3600.
329
+ cd2=self.spectralcube.header['CDELT2']*3600.
330
+ x1=((np.arange(1,hdr['NAXIS1']+1)-(hdr['NAXIS1']//2))*(cd1))# + hdr['CRVAL1']
328
331
  y1=((np.arange(1,hdr['NAXIS2']+1)-(hdr['NAXIS1']//2))*cd2)# + hdr['CRVAL2']
329
332
 
330
333
  v1=self.spectralcube.spectral_axis.value
331
334
 
332
335
  cd3= np.median(np.diff(v1))
333
-
336
+ #breakpoint()
334
337
 
335
338
  return x1,y1,v1,np.abs(cd1),cd3
336
339
 
337
340
  def read_in_a_cube(self,path,rest_value=None,primary=False):
338
341
 
342
+ if self.rotate !=None:
343
+ try:
344
+ h=fits.getheader(path.split('.fits')[0]+'_rotated.fits')
345
+ if float(h['ROTANGLE']) == self.rotate:
346
+ path=path.split('.fits')[0]+'_rotated.fits'
347
+ alreadyrotated=True
348
+ else:
349
+ alreadyrotated=False
350
+ except:
351
+ alreadyrotated=False
352
+
353
+
339
354
  scube=SpectralCube.read(path).with_spectral_unit(u.km/u.s, velocity_convention=self.velocity_convention,rest_value=rest_value)
340
355
 
356
+ hdr=scube.header
357
+
358
+ if self.rotate !=None:
359
+ if alreadyrotated == False:
360
+ hdr['CROTA2']=self.rotate
361
+ hdr['CROTA3']=0
362
+ scube=scube.reproject(hdr)
363
+ scube.meta['ROTANGLE']=self.rotate
364
+ scube.write(path.split('.fits')[0]+'_rotated.fits',overwrite=True)
365
+
341
366
  hdr=scube.header
342
367
  cube = np.squeeze(scube.filled_data[:,:,:].T).value #squeeze to remove singular stokes axis if present
343
368
  cube[np.isfinite(cube) == False] = 0.0
@@ -396,7 +421,6 @@ class pymakeplots:
396
421
  return xpix,ypix,xoffsetarc,yoffsetarc
397
422
 
398
423
  def prepare_cubes(self):
399
-
400
424
  self.centskycoord=self.spectralcube.wcs.celestial.pixel_to_world(self.xcoord.size//2,self.ycoord.size//2).transform_to('icrs')
401
425
  self.x_skycent=self.centskycoord.ra.value
402
426
  self.y_skycent=self.centskycoord.dec.value
@@ -415,10 +439,10 @@ class pymakeplots:
415
439
  pointingposes=SkyCoord(self.pointingsra,self.pointingsdec,frame='icrs',unit=(u.hourangle,u.deg))
416
440
  _,_,pointoffsetx,pointoffsety=self.calc_offset(pointingposes.ra,pointingposes.dec)
417
441
  self.points2plot=np.zeros((len(pointoffsetx),3))
418
- self.points2plot[:,0]=pointoffsetx-xoffsetarc
442
+ self.points2plot[:,0]=-pointoffsetx+xoffsetarc
419
443
  self.points2plot[:,1]=pointoffsety-yoffsetarc
420
444
  self.points2plot[:,2]=self.pointingsdiam.value
421
-
445
+ #breakpoint()
422
446
  self.clip_cube(xoffsetarc,yoffsetarc)
423
447
 
424
448
  self.mask_trim=self.smooth_mask(self.flat_cube_trim)
@@ -563,7 +587,7 @@ class pymakeplots:
563
587
  plt.show()
564
588
  plt.close()
565
589
 
566
- def scalebar(self,ax,loc='lower right'):
590
+ def scalebarlength(self):
567
591
  barlength_pc = np.ceil((np.abs(self.xc[-1]-self.xc[0])*4.84*self.gal_distance)/1000.)*100
568
592
  barlength_arc= barlength_pc/(4.84*self.gal_distance)
569
593
 
@@ -576,7 +600,11 @@ class pymakeplots:
576
600
  barlength_arc= barlength_pc/(4.84*self.gal_distance)
577
601
 
578
602
 
603
+ return barlength_arc,barlength_pc
604
+
605
+ def scalebar(self,ax,loc='lower right'):
579
606
 
607
+ barlength_arc,barlength_pc=self.scalebarlength()
580
608
  if np.log10(barlength_pc) > 3:
581
609
  label=(barlength_pc/1e3).astype(str)+ " kpc"
582
610
  else:
@@ -591,9 +619,6 @@ class pymakeplots:
591
619
 
592
620
  def clip_cube(self,xoffsetarc,yoffsetarc):
593
621
 
594
- #
595
-
596
-
597
622
 
598
623
  if np.any(self.chans2do == None):
599
624
 
@@ -621,7 +646,6 @@ class pymakeplots:
621
646
  if np.array(self.imagesize).size == 1:
622
647
  self.imagesize=[self.imagesize,self.imagesize]
623
648
 
624
-
625
649
  wx,=np.where(np.abs(self.xcoord-xoffsetarc) <= self.imagesize[0])
626
650
  wy,=np.where(np.abs(self.ycoord-yoffsetarc) <= self.imagesize[1])
627
651
  self.spatial_trim=[np.min(wx),np.max(wx),np.min(wy),np.max(wy)]
@@ -690,8 +714,67 @@ class pymakeplots:
690
714
  ax.add_artist(box)
691
715
 
692
716
 
693
-
717
+ def add_axis_labels(self,ax1,first=True):
718
+ if self.rotate !=None:
719
+ self._xlab='Offset'
720
+ self._ylab='Offset'
721
+ else:
722
+ self._xlab='RA Offset'
723
+ self._ylab='Dec Offset'
724
+
725
+ if self.all_axes_physical:
726
+ ax1.set_xlabel(self._xlab+' (kpc)')
727
+ if first: ax1.set_ylabel(self._ylab+' (kpc)')
728
+ else:
729
+ ax1.set_xlabel(self._xlab+' (")')
730
+ if first: ax1.set_ylabel(self._ylab+' (")')
731
+
732
+ def add_arrow(self,ax,first=True,last=True):
733
+ if first and last:
734
+ loc='upper right'
735
+ else:
736
+ loc="lower right"
737
+ # if self.all_axes_physical:
738
+ # length2use=self.ang2kpctrans(self.bmaj)
739
+ # else:
740
+ # length2use=self.bmaj
741
+ #
742
+ # rotated_arrow = AnchoredDirectionArrows(
743
+ # ax.transData,
744
+ # 'Dec', 'RA',
745
+ # loc=loc,
746
+ # length=-0.1,#length2use*2,
747
+ # aspect_ratio=-1,
748
+ # color='k',
749
+ # text_props={'ec': 'k', 'fc': 'k'},
750
+ # angle=self.rotate,
751
+ # )
752
+ # ax.add_artist(rotated_arrow)
753
+
694
754
 
755
+ aux_tr_box = AuxTransformBox(ax.transData)
756
+ barlength_arc,barlength_pc=self.scalebarlength()
757
+ if self.all_axes_physical:
758
+ length2use=self.ang2kpctrans(barlength_arc)
759
+ else:
760
+ length2use=barlength_arc
761
+
762
+ rotmat=np.array([[np.cos(np.deg2rad(self.rotate)),-np.sin(np.deg2rad(self.rotate))],[np.sin(np.deg2rad(self.rotate)),np.cos(np.deg2rad(self.rotate))]])
763
+ dx1,dx2=np.dot(rotmat,np.array([0,length2use]))
764
+ aux_tr_box.add_artist(Arrow(0, 0,dx1,dx2,color='k'))
765
+ box = AnchoredOffsetbox(child=aux_tr_box, loc=loc, frameon=False)
766
+ ax.add_artist(box)
767
+ #ax.text(3, y + 0.05, bracketstyle, ha="center", va="bottom", fontsize=14)
768
+
769
+ def add_second_axis(self,ax1):
770
+ if np.log10(self.ang2pctrans(np.max([np.max(self.xc),np.max(self.yc)]))) > 3:
771
+ secax = ax1.secondary_yaxis('right', functions=(self.ang2kpctrans, self.ang2kpctrans_inv))
772
+ secax.set_ylabel(self._ylab+' (kpc)')
773
+ else:
774
+ secax = ax1.secondary_yaxis('right', functions=(self.ang2pctrans, self.ang2pctrans_inv))
775
+ secax.set_ylabel(self._ylab+' (pc)')
776
+
777
+
695
778
  def mom0(self,ax1,first=True,last=True):
696
779
  mom0=(self.pbcorr_cube_trim*self.mask_trim).sum(axis=2)*self.dv
697
780
 
@@ -714,17 +797,10 @@ class pymakeplots:
714
797
  #breakpoint()
715
798
  im1=ax1.contourf(self.xc,self.yc,mom0.T,levels=levs,cmap=newcmp)
716
799
  #im1=ax1.pcolormesh(self.xc,self.yc,mom0.T,cmap=newcmp,vmin=minmom0,vmax=maxmom0)
717
-
718
- if self.all_axes_physical:
719
- ax1.set_xlabel('RA offset (kpc)')
720
- if first: ax1.set_ylabel('Dec offset (kpc)')
721
- else:
722
- ax1.set_xlabel('RA offset (")')
723
- if first: ax1.set_ylabel('Dec offset (")')
724
-
725
800
 
801
+
802
+ self.add_axis_labels(ax1,first=first)
726
803
 
727
-
728
804
 
729
805
  if maxmom0 > 0:
730
806
  vticks=np.linspace(0,(np.round((maxmom0 / 10**np.floor(np.log10(maxmom0))))*10**np.floor(np.log10(maxmom0))),4)
@@ -740,6 +816,8 @@ class pymakeplots:
740
816
 
741
817
 
742
818
  self.add_beam(ax1)
819
+ if self.rotate != None and first:
820
+ self.add_arrow(ax1,first=first,last=last)
743
821
 
744
822
  if np.any(self.points2plot != None):
745
823
  naca=0
@@ -766,15 +844,14 @@ class pymakeplots:
766
844
  if self.make_square:
767
845
  ax1.set_xlim(np.min([self.xc[0],self.yc[0]]),np.max([self.xc[-1],self.yc[-1]]))
768
846
  ax1.set_ylim(np.min([self.xc[0],self.yc[0]]),np.max([self.xc[-1],self.yc[-1]]))
847
+ else:
848
+ ax1.set_xlim(self.xc[0],self.xc[-1])
849
+ ax1.set_ylim(self.yc[0],self.yc[-1])
769
850
  ax1.set_aspect('equal')
770
851
 
852
+
771
853
  if last and not self.all_axes_physical:
772
- if np.log10(self.ang2pctrans(np.max([np.max(self.xc),np.max(self.yc)]))) > 3:
773
- secax = ax1.secondary_yaxis('right', functions=(self.ang2kpctrans, self.ang2kpctrans_inv))
774
- secax.set_ylabel(r'Dec offset (kpc)')
775
- else:
776
- secax = ax1.secondary_yaxis('right', functions=(self.ang2pctrans, self.ang2pctrans_inv))
777
- secax.set_ylabel(r'Dec offset (pc)')
854
+ self.add_second_axis(ax1)
778
855
 
779
856
 
780
857
  if self.fits:
@@ -790,14 +867,9 @@ class pymakeplots:
790
867
 
791
868
  vticks=np.linspace((-1)*np.ceil(np.max(np.abs(self.vcoord_trim-self.vsys))/10.)*10.,np.ceil(np.max(np.abs(self.vcoord_trim-self.vsys))/10.)*10.,5)
792
869
 
793
- im1=ax1.contourf(self.xc,self.yc,mom1.T-self.vsys,levels=self.vcoord_trim-self.vsys,cmap=sauron,vmin=vticks[0],vmax=vticks[-1])
870
+ im1=ax1.contourf(self.xc,self.yc,mom1.T-self.vsys,levels=self.vcoord_trim-self.vsys,cmap=self.mom1_cmap,vmin=vticks[0],vmax=vticks[-1])
794
871
 
795
- if self.all_axes_physical:
796
- ax1.set_xlabel('RA offset (kpc)')
797
- if first: ax1.set_ylabel('Dec offset (kpc)')
798
- else:
799
- ax1.set_xlabel('RA offset (")')
800
- if first: ax1.set_ylabel('Dec offset (")')
872
+ self.add_axis_labels(ax1,first=first)
801
873
 
802
874
 
803
875
  cb=self.colorbar(im1,ticks=vticks)
@@ -808,14 +880,15 @@ class pymakeplots:
808
880
  if self.make_square:
809
881
  ax1.set_xlim(np.min([self.xc[0],self.yc[0]]),np.max([self.xc[-1],self.yc[-1]]))
810
882
  ax1.set_ylim(np.min([self.xc[0],self.yc[0]]),np.max([self.xc[-1],self.yc[-1]]))
811
-
883
+ else:
884
+ ax1.set_xlim(self.xc[0],self.xc[-1])
885
+ ax1.set_ylim(self.yc[0],self.yc[-1])
886
+
887
+ if self.rotate != None and first:
888
+ self.add_arrow(ax1,first=first,last=last)
889
+
812
890
  if last and not self.all_axes_physical:
813
- if np.log10(self.ang2pctrans(np.max([np.max(self.xc),np.max(self.yc)]))) > 3:
814
- secax = ax1.secondary_yaxis('right', functions=(self.ang2kpctrans, self.ang2kpctrans_inv))
815
- secax.set_ylabel(r'Dec offset (kpc)')
816
- else:
817
- secax = ax1.secondary_yaxis('right', functions=(self.ang2pctrans, self.ang2pctrans_inv))
818
- secax.set_ylabel(r'Dec offset (pc)')
891
+ self.add_second_axis(ax1)
819
892
 
820
893
  if self.fits:
821
894
  self.write_fits(mom1.T,1)
@@ -840,14 +913,9 @@ class pymakeplots:
840
913
  self.maxvdisp=50.
841
914
  #breakpoint()
842
915
  mom2levs=np.linspace(0,self.maxvdisp,10)
843
- im1=ax1.contourf(self.xc,self.yc,mom2.T,levels=mom2levs,cmap=sauron,vmax=self.maxvdisp)
916
+ im1=ax1.contourf(self.xc,self.yc,mom2.T,levels=mom2levs,cmap=self.mom2_cmap,vmax=self.maxvdisp)
844
917
 
845
- if self.all_axes_physical:
846
- ax1.set_xlabel('RA offset (kpc)')
847
- if first: ax1.set_ylabel('Dec offset (kpc)')
848
- else:
849
- ax1.set_xlabel('RA offset (")')
850
- if first: ax1.set_ylabel('Dec offset (")')
918
+ self.add_axis_labels(ax1,first=first)
851
919
 
852
920
  if self.maxvdisp < 50:
853
921
  dvticks=10
@@ -868,14 +936,15 @@ class pymakeplots:
868
936
  if self.make_square:
869
937
  ax1.set_xlim(np.min([self.xc[0],self.yc[0]]),np.max([self.xc[-1],self.yc[-1]]))
870
938
  ax1.set_ylim(np.min([self.xc[0],self.yc[0]]),np.max([self.xc[-1],self.yc[-1]]))
939
+ else:
940
+ ax1.set_xlim(self.xc[0],self.xc[-1])
941
+ ax1.set_ylim(self.yc[0],self.yc[-1])
871
942
 
943
+ if self.rotate != None and first:
944
+ self.add_arrow(ax1,first=first,last=last)
945
+
872
946
  if last and not self.all_axes_physical:
873
- if np.log10(self.ang2pctrans(np.max([np.max(self.xc),np.max(self.yc)]))) > 3.3:
874
- secax = ax1.secondary_yaxis('right', functions=(self.ang2kpctrans, self.ang2kpctrans_inv))
875
- secax.set_ylabel(r'Dec offset (kpc)')
876
- else:
877
- secax = ax1.secondary_yaxis('right', functions=(self.ang2pctrans, self.ang2pctrans_inv))
878
- secax.set_ylabel(r'Dec offset (pc)',rotation=270,labelpad=10)
947
+ self.add_second_axis(ax1)
879
948
 
880
949
 
881
950
  if self.fits:
@@ -899,6 +968,12 @@ class pymakeplots:
899
968
  newhdu.header['CRPIX2']=self.spectralcube.header['CRPIX2']
900
969
  newhdu.header['CRVAL2']=self.spectralcube.header['CRVAL2']
901
970
  newhdu.header['CDELT2']=self.spectralcube.header['CDELT2']
971
+ if self.rotate!=None:
972
+ newhdu.header['PC1_1']=self.spectralcube.header['PC1_1']
973
+ newhdu.header['PC1_2']=self.spectralcube.header['PC1_2']
974
+ newhdu.header['PC2_1']=self.spectralcube.header['PC2_1']
975
+ newhdu.header['PC2_2']=self.spectralcube.header['PC2_2']
976
+
902
977
  try:
903
978
  newhdu.header['PV2_1']=self.spectralcube.header['PV2_1']
904
979
  newhdu.header['PV2_2']=self.spectralcube.header['PV2_2']
@@ -1125,8 +1200,8 @@ class pymakeplots:
1125
1200
  anchored_text = AnchoredText("PA: "+str(round(self.posang,1))+'$^{\\circ}$', loc=loc1,frameon=False)
1126
1201
  axes.add_artist(anchored_text)
1127
1202
 
1128
- if self.gal_distance != None and not self.all_axes_physical:
1129
- self.scalebar(axes,loc=loc2)
1203
+ #if self.gal_distance != None and not self.all_axes_physical:
1204
+ # self.scalebar(axes,loc=loc2)
1130
1205
 
1131
1206
  if self.fits:
1132
1207
  self.write_pvd_fits(pvdaxis,vaxis,pvd.T)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: pymakeplots
3
- Version: 0.2.3
3
+ Version: 0.2.4
4
4
  Home-page: https://github.com/TimothyADavis/pymakeplots
5
5
  Author: Timothy A. Davis
6
6
  Author-email: DavisT@cardiff.ac.uk
@@ -5,7 +5,7 @@ with open("README.md", "r") as fh:
5
5
 
6
6
 
7
7
  setup(name='pymakeplots',
8
- version='0.2.3',
8
+ version='0.2.4',
9
9
  description='',
10
10
  url='https://github.com/TimothyADavis/pymakeplots',
11
11
  author='Timothy A. Davis',
File without changes
File without changes
File without changes