Conditions | 4 |
Total Lines | 97 |
Code Lines | 27 |
Lines | 0 |
Ratio | 0 % |
Changes | 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 | # Copyright (C) 2019 NRL |
||
44 | def subsol(year, doy, utime): |
||
45 | """Finds subsolar geocentric longitude and latitude. |
||
46 | |||
47 | Parameters |
||
48 | ---------- |
||
49 | year : (int) |
||
50 | Calendar year between 1601 and 2100 |
||
51 | doy : (int) |
||
52 | Day of year between 1-365/366 |
||
53 | utime : (float) |
||
54 | Seconds since midnight on the specified day |
||
55 | |||
56 | Returns |
||
57 | ------- |
||
58 | sbsllon : (float) |
||
59 | Subsolar longitude in degrees E for the given date/time |
||
60 | sbsllat : (float) |
||
61 | Subsolar latitude in degrees N for the given date/time |
||
62 | |||
63 | Raises |
||
64 | ------ |
||
65 | ValueError if year is out of range |
||
66 | |||
67 | Notes |
||
68 | ----- |
||
69 | Based on formulas in Astronomical Almanac for the year 1996, p. C24. |
||
70 | (U.S. Government Printing Office, 1994). Usable for years 1601-2100, |
||
71 | inclusive. According to the Almanac, results are good to at least 0.01 |
||
72 | degree latitude and 0.025 degrees longitude between years 1950 and 2050. |
||
73 | Accuracy for other years has not been tested. Every day is assumed to have |
||
74 | exactly 86400 seconds; thus leap seconds that sometimes occur on December |
||
75 | 31 are ignored (their effect is below the accuracy threshold of the |
||
76 | algorithm). |
||
77 | |||
78 | References |
||
79 | ---------- |
||
80 | After Fortran code by A. D. Richmond, NCAR. Translated from IDL |
||
81 | by K. Laundal. |
||
82 | |||
83 | """ |
||
84 | |||
85 | # Convert from 4 digit year to 2 digit year |
||
86 | yr2 = year - 2000 |
||
87 | |||
88 | if year >= 2101 or year <= 1600: |
||
89 | raise ValueError('subsol valid between 1601-2100. Input year is:', year) |
||
90 | |||
91 | # Determine if this year is a leap year |
||
92 | nleap = np.floor((year - 1601) / 4) |
||
93 | nleap = nleap - 99 |
||
94 | if year <= 1900: |
||
95 | ncent = np.floor((year - 1601) / 100) |
||
96 | ncent = 3 - ncent |
||
97 | nleap = nleap + ncent |
||
98 | |||
99 | # Calculate some of the coefficients needed to deterimine the mean longitude |
||
100 | # of the sun and the mean anomaly |
||
101 | l_0 = -79.549 + (-0.238699 * (yr2 - 4 * nleap) + 3.08514e-2 * nleap) |
||
102 | g_0 = -2.472 + (-0.2558905 * (yr2 - 4 * nleap) - 3.79617e-2 * nleap) |
||
103 | |||
104 | # Days (including fraction) since 12 UT on January 1 of IYR2: |
||
105 | dfrac = (utime / 86400 - 1.5) + doy |
||
106 | |||
107 | # Mean longitude of Sun: |
||
108 | l_sun = l_0 + 0.9856474 * dfrac |
||
109 | |||
110 | # Mean anomaly: |
||
111 | grad = np.radians(g_0 + 0.9856003 * dfrac) |
||
112 | |||
113 | # Ecliptic longitude: |
||
114 | lmrad = np.radians(l_sun + 1.915 * np.sin(grad) + 0.020 * np.sin(2 * grad)) |
||
115 | sinlm = np.sin(lmrad) |
||
116 | |||
117 | # Days (including fraction) since 12 UT on January 1 of 2000: |
||
118 | epoch_day = dfrac + 365.0 * yr2 + nleap |
||
119 | |||
120 | # Obliquity of ecliptic: |
||
121 | epsrad = np.radians(23.439 - 4.0e-7 * epoch_day) |
||
122 | |||
123 | # Right ascension: |
||
124 | alpha = np.degrees(np.arctan2(np.cos(epsrad) * sinlm, np.cos(lmrad))) |
||
125 | |||
126 | # Declination, which is the subsolar latitude: |
||
127 | sbsllat = np.degrees(np.arcsin(np.sin(epsrad) * sinlm)) |
||
128 | |||
129 | # Equation of time (degrees): |
||
130 | etdeg = l_sun - alpha |
||
131 | etdeg = etdeg - 360.0 * np.round(etdeg / 360.0) |
||
132 | |||
133 | # Apparent time (degrees): |
||
134 | aptime = utime / 240.0 + etdeg # Earth rotates one degree every 240 s. |
||
135 | |||
136 | # Subsolar longitude: |
||
137 | sbsllon = 180.0 - aptime |
||
138 | sbsllon = sbsllon - 360.0 * np.round(sbsllon / 360.0) |
||
139 | |||
140 | return sbsllon, sbsllat |
||
141 | |||
213 |