| Conditions | 6 |
| Total Lines | 82 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | """ Module for I/O |
||
| 85 | def init_spiral_arms(): |
||
| 86 | |||
| 87 | from astropy.table import Table |
||
| 88 | from scipy.interpolate import CubicSpline |
||
| 89 | armsinp= data_path + 'ne_arms_log_mod.inp' |
||
| 90 | logarms= data_path + 'log_arms.out' |
||
| 91 | |||
| 92 | narms=5 |
||
| 93 | #integer armmap(5) ! for remapping from Wainscoat |
||
| 94 | #data armmap/1, 3, 4, 2, 5/ ! order to TC93 order, which is |
||
| 95 | # ! from GC outwards toward Sun. |
||
| 96 | armmap = [1,3,4,2,5] |
||
| 97 | NNj = [20, 20, 20, 20, 20] |
||
| 98 | narmpoints=500 |
||
| 99 | ncoord=2 |
||
| 100 | NNmax=20 |
||
| 101 | rad = 180/np.pi |
||
| 102 | # Arms |
||
| 103 | arms_tbl = Table.read(armsinp, format='ascii') # a, rmin, thmin, extent |
||
| 104 | assert len(arms_tbl) == narms |
||
| 105 | |||
| 106 | r1 = np.zeros((NNmax, narms)) |
||
| 107 | th1 = np.zeros((NNmax, narms)) |
||
| 108 | kmax = np.zeros(narms).astype(int) |
||
| 109 | arm = np.zeros((narms, narmpoints, ncoord)) |
||
| 110 | |||
| 111 | for j, row in enumerate(arms_tbl): |
||
| 112 | th1[0:NNj[j],j] = row['thmin'] + np.arange(NNj[j])*row['extent']/(NNj[j]-1.) #! rad |
||
| 113 | r1[:,j] = row['rmin'] * np.exp((th1[:,j]-row['thmin'])/row['a']) |
||
| 114 | th1[:,j] *= rad # ! deg |
||
| 115 | #c *** begin sculpting spiral arm 2 == TC arm 3*** |
||
| 116 | if armmap[j] == 3: |
||
| 117 | cut1 = (th1[:,j] > 370.) & (th1[:,j] <= 410.) |
||
| 118 | r1[cut1,j] *= (1. + 0.04* np.cos((th1[cut1,j]-390.)*180./(40.*rad))) |
||
| 119 | #c . (1. + 0.01*cos((th1(n,j)-390.)*180./(40.*rad))) |
||
| 120 | cut2 = (th1[:,j] > 315.) & (th1[:,j] <= 370.) |
||
| 121 | r1[cut2,j] *= (1. - 0.07* np.cos((th1[cut2,j]-345.)*180./(55.*rad))) |
||
| 122 | #c . (1.0 - 0.08*cos((th1(n,j)-345.)*180./(55.*rad))) |
||
| 123 | cut3 = (th1[:,j] > 180.) & (th1[:,j] <= 315.) |
||
| 124 | r1[cut3,j] *= (1 + 0.16* np.cos((th1[cut3,j]-260.)*180./(135.*rad))) |
||
| 125 | # (1 + 0.13* np.cos((th1[cut3,j]-260.)*180./(135.*rad))) |
||
| 126 | #c *** begin sculpting spiral arm 4 == TC arm 2*** |
||
| 127 | if armmap[j] == 2: |
||
| 128 | cut1 = (th1[:,j] > 290.) & (th1[:,j] <= 395.) |
||
| 129 | r1[cut1,j] *= (1. - 0.11* np.cos((th1[cut1,j]-350.)*180./(105.*rad))) |
||
| 130 | #c *** end arm sculpting *** |
||
| 131 | |||
| 132 | |||
| 133 | |||
| 134 | """ |
||
| 135 | open(11,file=logarms, status='unknown') |
||
| 136 | write(11,*) 'arm n xa ya' |
||
| 137 | """ |
||
| 138 | from xastropy.xutils import xdebug as xdb |
||
| 139 | #do 21 j=1,narms |
||
| 140 | for j in range(narms): |
||
| 141 | dth = 5.0/r1[0,j] # Python indexing |
||
| 142 | th = th1[0,j]-0.999*dth |
||
| 143 | # Generate spline |
||
| 144 | cspline = CubicSpline(th1[:NNj[j],j],r1[:NNj[j],j]) |
||
| 145 | #call cspline(th1(1,j),r1(1,j),-NNj(j),th,r) |
||
| 146 | #for k in range(narmpoints): |
||
| 147 | #do 10 k=1,narmpoints-1 |
||
| 148 | th = th + dth * np.arange(narmpoints) |
||
| 149 | gd_th = np.where(th <= th1[NNj[j]-1, j])[0] |
||
| 150 | kmax[j] = np.max(gd_th) + 1 # Python indexing (we will use arange) |
||
| 151 | r = cspline(th[gd_th]) |
||
| 152 | # x,y of each arm |
||
| 153 | arm[j,gd_th,0] = -r*np.sin(th[gd_th]/rad) # Python indexing |
||
| 154 | arm[j,gd_th,1] = r*np.cos(th[gd_th]/rad) |
||
| 155 | |||
| 156 | # Wrap into a dict |
||
| 157 | arms_dict = {} |
||
| 158 | arms_dict['table'] = arms_tbl |
||
| 159 | arms_dict['r1'] = r1 |
||
| 160 | arms_dict['th1'] = r1 |
||
| 161 | arms_dict['kmax'] = kmax |
||
| 162 | arms_dict['narms'] = narms |
||
| 163 | arms_dict['narmpoints'] = narmpoints |
||
| 164 | arms_dict['armmap'] = armmap |
||
| 165 | arms_dict['arm'] = arm |
||
| 166 | return arms_dict |
||
| 167 |