Conditions | 6 |
Total Lines | 81 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
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 |
||
133 | def init_spiral_arms(ifile='ne_arms_log_mod.inp'): |
||
134 | armsinp = os.path.join(DATA_PATH, ifile) |
||
135 | # logarms = DATA_PATH + 'log_arms.out' |
||
136 | |||
137 | narms = 5 |
||
138 | # integer armmap(5) ! for remapping from Wainscoat |
||
139 | # data armmap/1, 3, 4, 2, 5/ ! order to TC93 order, which is |
||
140 | # ! from GC outwards toward Sun. |
||
141 | armmap = [1, 3, 4, 2, 5] |
||
142 | NNj = [20, 20, 20, 20, 20] |
||
143 | narmpoints = 500 |
||
144 | ncoord = 2 |
||
145 | NNmax = 20 |
||
146 | rad = 180/np.pi |
||
147 | # Arms |
||
148 | arms_tbl = Table.read(armsinp, format='ascii') # a, rmin, thmin, extent |
||
149 | assert len(arms_tbl) == narms |
||
150 | |||
151 | r1 = np.zeros((NNmax, narms)) |
||
152 | th1 = np.zeros((NNmax, narms)) |
||
153 | kmax = np.zeros(narms).astype(int) |
||
154 | arm = np.zeros((narms, narmpoints, ncoord)) |
||
155 | |||
156 | for j, row in enumerate(arms_tbl): |
||
157 | th1[0:NNj[j], j] = (row['thmin'] + |
||
158 | np.arange(NNj[j])*row['extent']/(NNj[j]-1.)) # rad |
||
159 | r1[:, j] = row['rmin'] * np.exp((th1[:, j]-row['thmin'])/row['a']) |
||
160 | th1[:, j] *= rad # ! deg |
||
161 | # c *** begin sculpting spiral arm 2 == TC arm 3*** |
||
162 | if armmap[j] == 3: |
||
163 | cut1 = (th1[:, j] > 370.) & (th1[:, j] <= 410.) |
||
164 | r1[cut1, j] *= (1. + 0.04 * np.cos((th1[cut1, j]-390.)*180 / |
||
165 | (40.*rad))) |
||
166 | # c . (1. + 0.01*cos((th1(n,j)-390.)*180./(40.*rad))) |
||
167 | cut2 = (th1[:, j] > 315.) & (th1[:, j] <= 370.) |
||
168 | r1[cut2, j] *= (1. - 0.07 * np.cos((th1[cut2, j]-345.)*180 / |
||
169 | (55.*rad))) |
||
170 | # c . (1.0 - 0.08*cos((th1(n,j)-345.)*180./(55.*rad))) |
||
171 | cut3 = (th1[:, j] > 180.) & (th1[:, j] <= 315.) |
||
172 | r1[cut3, j] *= (1 + 0.16 * np.cos((th1[cut3, j]-260.)*180 / |
||
173 | (135.*rad))) |
||
174 | # (1 + 0.13* np.cos((th1[cut3,j]-260.)*180./(135.*rad))) |
||
175 | # c *** begin sculpting spiral arm 4 == TC arm 2*** |
||
176 | if armmap[j] == 2: |
||
177 | cut1 = (th1[:, j] > 290.) & (th1[:, j] <= 395.) |
||
178 | r1[cut1, j] *= (1. - 0.11 * np.cos((th1[cut1, j]-350.)*180 / |
||
179 | (105.*rad))) |
||
180 | # c *** end arm sculpting *** |
||
181 | |||
182 | """ |
||
183 | open(11,file=logarms, status='unknown') |
||
184 | write(11,*) 'arm n xa ya' |
||
185 | """ |
||
186 | # do 21 j=1,narms |
||
187 | for j in range(narms): |
||
188 | dth = 5.0/r1[0, j] # Python indexing |
||
189 | th = th1[0, j]-0.999*dth |
||
190 | # Generate spline |
||
191 | cspline = CubicSpline(th1[:NNj[j], j], r1[:NNj[j], j]) |
||
192 | # call cspline(th1(1,j),r1(1,j),-NNj(j),th,r) |
||
193 | # for k in range(narmpoints): |
||
194 | # do 10 k=1,narmpoints-1 |
||
195 | th = th + dth * np.arange(narmpoints) |
||
196 | gd_th = np.where(th <= th1[NNj[j]-1, j])[0] |
||
197 | kmax[j] = np.max(gd_th) + 1 # Python indexing (we will use arange) |
||
198 | r = cspline(th[gd_th]) |
||
199 | # x,y of each arm |
||
200 | arm[j, gd_th, 0] = -r*np.sin(th[gd_th]/rad) # Python indexing |
||
201 | arm[j, gd_th, 1] = r*np.cos(th[gd_th]/rad) |
||
202 | |||
203 | # Wrap into a dict |
||
204 | arms_dict = {} |
||
205 | arms_dict['table'] = arms_tbl |
||
206 | arms_dict['r1'] = r1 |
||
207 | arms_dict['th1'] = r1 |
||
208 | arms_dict['kmax'] = kmax |
||
209 | arms_dict['narms'] = narms |
||
210 | arms_dict['narmpoints'] = narmpoints |
||
211 | arms_dict['armmap'] = armmap |
||
212 | arms_dict['arm'] = arm |
||
213 | return arms_dict |
||
214 |