Skip to content

Commit 57b6512

Browse files
authored
Comments updated. (#31)
Updated doc strings.
1 parent 8787abb commit 57b6512

File tree

5 files changed

+54
-10
lines changed

5 files changed

+54
-10
lines changed

calc/shval3.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@ package calc
22

33
import "math"
44

5-
// Calculates field components from spherical harmonic (sh) models.
6-
// x - northward component
7-
// y - eastward component
8-
// z - vertically-downward component
5+
// Computes field components from spherical harmonic (sh) models.
6+
// The calculation is performed for two sets of coeffs for a single location,
7+
// thus it returns two sets of X, Y, Z.
8+
//
9+
// X - northward component
10+
//
11+
// Y - eastward component
12+
//
13+
// Z - vertically-downward component
914
func Shval3(flat, flon, elev float64, nmax int, gha, ghb *[]float64) (float64, float64, float64, float64, float64, float64) {
1015
// similar to shval3 from C implementation
1116
var earths_radius float64 = 6371.2
@@ -150,10 +155,14 @@ func Shval3(flat, flon, elev float64, nmax int, gha, ghb *[]float64) (float64, f
150155
return x, y, z, xtemp, ytemp, ztemp
151156
}
152157

153-
// Computes the geomagnetic d, i, h, and f from x, y, and z.
158+
// Computes the geomagnetic D, I, H, and F from X, Y, and Z.
159+
//
154160
// D - declination
161+
//
155162
// I - inclination
163+
//
156164
// H - horizontal intensity
165+
//
157166
// F - total intensity
158167
func Dihf(x, y, z float64) (float64, float64, float64, float64) {
159168
var d, i, h, f float64

coeffs/read.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ type epochData struct {
3333
coeffs *[]float64
3434
}
3535

36-
// NewCoeffsData - returns an initialized IGRF SHC data structure.
36+
// Returns an initialized IGRF SHC data structure.
3737
func NewCoeffsData() (*IGRFcoeffs, error) {
3838
igrf := IGRFcoeffs{data: &map[string]*epochData{}}
3939
if err := igrf.readCoeffs(); err != nil {
@@ -42,7 +42,8 @@ func NewCoeffsData() (*IGRFcoeffs, error) {
4242
return &igrf, nil
4343
}
4444

45-
// Coeffs - returns two sets of SHC coeffs for the given `date`, as well as for `date` plus one year. Also returns the maximal spherical harmonic degree.
45+
// Returns two sets of SH coeffs for the given `date`,
46+
// as well as for `date` plus one year. Also returns the maximal spherical harmonic degree.
4647
func (igrf *IGRFcoeffs) Coeffs(date float64) (*[]float64, *[]float64, int, error) {
4748
max_column := len(*igrf.epochs)
4849
min_epoch := (*igrf.epochs)[0]
@@ -65,6 +66,10 @@ func (igrf *IGRFcoeffs) Coeffs(date float64) (*[]float64, *[]float64, int, error
6566
return coeffs_start, coeffs_end, nmax, nil
6667
}
6768

69+
// Computes a set of SH coeffs and the maximal spherical harmonic degree
70+
// for a given `date` and `start_epoch`, `end_epoch`.
71+
//
72+
// `date` must be less than the maximal possible epoch.
6873
func (igrf *IGRFcoeffs) interpolateCoeffs(start_epoch, end_epoch string, date float64) (*[]float64, int) {
6974
factor, err := findDateFactor(start_epoch, end_epoch, date)
7075
if err != nil {
@@ -116,6 +121,10 @@ func (igrf *IGRFcoeffs) interpolateCoeffs(start_epoch, end_epoch string, date fl
116121
return &values, nmax
117122
}
118123

124+
// Computes a set of SH coeffs and the maximal spherical harmonic degree
125+
// for a given `date` and `start_epoch`, `end_epoch`.
126+
//
127+
// `date` is beyond than the maximal possible epoch.
119128
func (igrf *IGRFcoeffs) extrapolateCoeffs(start_epoch, end_epoch string, date float64) *[]float64 {
120129
dte1, _ := strconv.ParseFloat(start_epoch, 32)
121130
factor := date - dte1
@@ -145,6 +154,8 @@ func (igrf *IGRFcoeffs) extrapolateCoeffs(start_epoch, end_epoch string, date fl
145154
return &values
146155
}
147156

157+
// Calculates start and end epochs for a given date,
158+
// default `interval` is used for borders and for multiplicity
148159
func (igrf *IGRFcoeffs) findEpochs(date float64) (string, string) {
149160
max_column := len(*igrf.epochs)
150161
min_epoch := (*igrf.epochs)[0]
@@ -162,6 +173,7 @@ func (igrf *IGRFcoeffs) findEpochs(date float64) (string, string) {
162173
return start_epoch, end_epoch
163174
}
164175

176+
// The main function that populates the existing `IGRFcoeffs` structure.
165177
func (igrf *IGRFcoeffs) readCoeffs() error {
166178
line_provider := coeffsLineProvider()
167179

@@ -189,6 +201,7 @@ func (igrf *IGRFcoeffs) readCoeffs() error {
189201
return nil
190202
}
191203

204+
// Finds lines with epochs, parses these lines and returns arrays of epochs: names and floats.
192205
func getEpochs(reader <-chan string) (*[]string, *[]float64, error) {
193206
cs_re := regexp.MustCompile(`^c/s.*`)
194207
for line := range reader {
@@ -202,6 +215,7 @@ func getEpochs(reader <-chan string) (*[]string, *[]float64, error) {
202215
return nil, nil, errors.New("Unable to get epochs.")
203216
}
204217

218+
// Parses the header of the coeffs. Usually it's the first two non-comment lines.
205219
func parseHeader(line1, line2 string) ([]string, []float64) {
206220
line1_data := space_re.Split(line1, -1)
207221
line2_data := space_re.Split(line2, -1)
@@ -236,6 +250,7 @@ func parseHeader(line1, line2 string) ([]string, []float64) {
236250
return names, epochs[shift:]
237251
}
238252

253+
// Parses lines with coefficients and populates the `IGRFcoeffs` structure.
239254
func (igrf *IGRFcoeffs) getCoeffsForEpochs(provider <-chan string) error {
240255
var i int = 0
241256
for line := range provider {
@@ -250,6 +265,7 @@ func (igrf *IGRFcoeffs) getCoeffsForEpochs(provider <-chan string) error {
250265
return nil
251266
}
252267

268+
// Populates the corresponding fields inside the `IGRFcoeffs` structure with actual coeffs.
253269
func (igrf *IGRFcoeffs) loadCoeffs(line_num int, line_coeffs *[]float64) {
254270
for index, coeff := range *line_coeffs {
255271
epoch := (*igrf.epochs)[index]
@@ -258,7 +274,7 @@ func (igrf *IGRFcoeffs) loadCoeffs(line_num int, line_coeffs *[]float64) {
258274
}
259275
}
260276

261-
// nMaxForEpoch - returns max spherical harmonic degree for a certain epoch
277+
// Returns max spherical harmonic degree for a certain epoch.
262278
func nMaxForEpoch(epoch string) (int, error) {
263279
// this is hardcoded
264280
var nmax int

coeffs/utils.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func findDateFactor(start_epoch, end_epoch string, date float64) (float64, error
6868
return factor, nil
6969
}
7070

71-
// coeffsLineProvider - reads lines from raw coeffs data, omits comments
71+
// Reads lines from raw coeffs data and writes a srting into a channel, drops comments.
7272
func coeffsLineProvider() <-chan string {
7373
ch := make(chan string)
7474
coeffs_reader := strings.NewReader(igrf13coeffs)
@@ -92,6 +92,7 @@ func epoch2string(epoch float64) string {
9292
return fmt.Sprintf("%.1f", epoch)
9393
}
9494

95+
// Parses an array of strings into an array of floats.
9596
func parseArrayToFloat(raw_data []string) (*[]float64, error) {
9697
data := make([]float64, len(raw_data))
9798
for index, token := range raw_data {

igrf/igrf.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,14 @@ import (
1010
"github.com/proway2/go-igrf/coeffs"
1111
)
1212

13-
// IGRF computes values for the geomagnetic field and secular variation for a given set of coordinates and date.
13+
// IGRF computes values for the geomagnetic field and secular variation for a given set of coordinates and date
14+
// and returns a populated `IGRFresults` structure.
15+
//
1416
// lat, lon - geodetic latitude and longitude (WGS84 latitude and altitude above mean sea level).
1517
// Valid values -90.0 < lat < 90.0, -180.0 < lon < 180.0.
18+
//
1619
// alt - geodetic altitude above mean sea level in km (-1.00 to 600.00).
20+
//
1721
// date - decimal date (1900.00 to 2025).
1822
func IGRF(lat, lon, alt, date float64) (IGRFresults, error) {
1923
if err := checkInitialConditions(lat, lon, alt); err != nil {

igrf/result.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,33 @@ package igrf
33
// IGRFresults represents the result of IGRF calculation
44
//
55
// Fields:
6+
//
67
// Declination (D): 14.175 ° (+ve east)
8+
//
79
// DeclinationSV (D): -8.92 arcmin/yr
10+
//
811
// Inclination (I): 74.229 ° (+ve down)
12+
//
913
// InclinationSV (I): -2.59 arcmin/yr
14+
//
1015
// HzIntensity (Horizontal intensity (H)): 14626.6 nT
16+
//
1117
// HorizontalSV (H): -16.8 nT/yr
18+
//
1219
// NorthComponent (X): 14181.2 nT
20+
//
1321
// NorthSV (X): -28.2 nT/yr
22+
//
1423
// EastComponent (Y): 3581.8 nT
24+
//
1525
// EastSV (Y): 32.0 nT/yr
26+
//
1627
// VerticalComponent (Z): 51788.4 nT
28+
//
1729
// VerticalSV (Z): 80.1 nT/yr
30+
//
1831
// TotalIntensity (F): 53814.3 nT
32+
//
1933
// TotalSV (F): 71.8 nT/yr
2034
type IGRFresults struct {
2135
Declination float64

0 commit comments

Comments
 (0)