<gh_stars>1-10
#<NAME>
#24 OCT 2019
#wmmtestgen.py
#This is a simple script to generate c++ catch2 tests.
import pymap3d as pm
import numpy as np


def main(header_to_test,modelnames,dates,heights,lats,lons,bns,bes,bds,margin):
    """create and write catch2 tests as a c++ source file to test header_to_test
        defining the WMM coefficents and model

     The file created has the name header_to_test[:-4]+'_test.cpp'

    Args:
        header_to_test(str ending in .hpp): the c++ header file with the model and coefficents
        modelnames(list of str): The list of model names for each test
        dates(numpy array): decimal test years
        heights(numpy array): test heights (km) with respect to wgs 84 ellipsoid
        lats(numpy array): test lat (deg)
        lons(numpy array): test lon (deg)
        bns(numpy array): test X(local north) magnetic field (nT)
        bes(numpy array): test Y(local east) magnetic field (nT)
        bds(numpy array): test Z(local down) magnetic field (nT)
        margin(float or int): Acceptable error in each component (nT)"""
    xs,ys,zs=pm.geodetic2ecef(lats,lons,heights*1000)
    bxs,bys,bzs=pm.enu2uvw(bes*1E-9,bns*1E-9,-bds*1E-9,lats,lons)
    file_name=header_to_test[:-4]+'_test.cpp'
    with open(file_name,'w') as f:
        outstr="""
// %s Generated by python script wmmtestgen.py
/** \\file
 * \\author <NAME> (<EMAIL>)
 * \\date 24 OCT 2019
 * \\brief c++ catch2 tests for magnetic field header only library.
 * \\details Compile with g++ %s -std=c++1z
 */
#define CATCH_CONFIG_MAIN  // This tells Catch to provide a main() - only do this in one cpp file
#include "catch.hpp"
#include "../%s"

"""%(file_name,file_name,header_to_test)

        for i in range(len(xs)):
            testcase="""
TEST_CASE( "geomag test %s of %s model", "[GeoMag]" ) {
    geomag::Vector in;
    in.x= %s;
    in.y= %s;
    in.z= %s;
    geomag::Vector out;
    geomag::Vector truth;
    truth.x= %s;
    truth.y= %s;
    truth.z= %s;
    out= geomag::GeoMag(%s,in,geomag::%s);
    CHECK( out.x*1E9 == Approx(truth.x*1E9).margin(%s) );
    CHECK( out.y*1E9 == Approx(truth.y*1E9).margin(%s) );
    CHECK( out.z*1E9 == Approx(truth.z*1E9).margin(%s) );
}


        """%(i,modelnames[i],repr(xs[i]),repr(ys[i]),repr(zs[i]),repr(bxs[i]),repr(bys[i]),repr(bzs[i]),dates[i],modelnames[i],margin,margin,margin)
            outstr=outstr+testcase
        f.write(outstr)

if __name__ == '__main__':
    header_to_test='geomag.hpp'
    dates=([2015]*6+[2017.5]*6)*2 + [2020]*6 + [2022.5]*6
    modelnames= ['WMM2015']*12 + ['WMM2015v2']*12 + ['WMM2020']*12
    heights=([0]*3+[100]*3+[0]*3+[100]*3)*3
    lats=[80,0,-80]*4*3
    lons=[0,120,240]*4*3
    bns=[6627.1,39518.2,5797.3,6314.3,37535.6,5613.1,6599.4,39571.4,5873.8,6290.5,37585.5,5683.5]
    bes=[-445.9,392.9,15761.1,-471.6,364.4,14791.5,-317.1,222.5,15781.4,-348.5,209.5,14808.8]
    bds=[54432.3,-11252.4,-52919.1,52269.8,-10773.4,-50378.6,54459.2,-11030.1,-52687.9,52292.7,-10564.2,-50163.0]
    bns+=[6636.6,39521.1,5796.3,6323.4,37538.1,5612.2,6605.2,39569.4,5864.6,6294.3,37584.4,5674.9]
    bes+=[-451.9,377.7,15759.1,-477.6,351.1,14789.3,-298.7,252.3,15764.1,-331.1,235.7,14793.1]
    bds+=[54408.9,-11228.8,-52927.1,52249.1,-10751.1,-50385.8,54506.3,-11067.9,-52706.1,52337.8,-10600.5,-50179.5]

    bns+=[6570.4,39624.3,5940.6,6261.8,37636.7,5744.9,6529.9,39684.7,6016.5,6224.0,37694.0,5815.0]
    bes+=[-146.3,109.9,15772.1,-185.5,104.9,14799.5,1.1,-42.2,15776.7,-44.5,-35.3,14803.0]
    bds+=[54606.0,-10932.5,-52480.8,52429.1,-10474.8,-49969.4,54713.4,-10809.5,-52251.6,52527.0,-10362.0,-49755.3]

    margin= 0.1#nT error acceptable
    main(header_to_test,modelnames,np.array(dates),np.array(heights),np.array(lats),np.array(lons),np.array(bns),np.array(bes),np.array(bds),margin)

#<NAME>
#24 OCT 2019
#wmmtestgen.py
#This is a simple script to generate c++ catch2 tests.
import pymap3d as pm
import numpy as np


def main(header_to_test,modelnames,dates,heights,lats,lons,bns,bes,bds,margin):
    """create and write catch2 tests as a c++ source file to test header_to_test
        defining the WMM coefficents and model

     The file created has the name header_to_test[:-4]+'_test.cpp'

    Args:
        header_to_test(str ending in .hpp): the c++ header file with the model and coefficents
        modelnames(list of str): The list of model names for each test
        dates(numpy array): decimal test years
        heights(numpy array): test heights (km) with respect to wgs 84 ellipsoid
        lats(numpy array): test lat (deg)
        lons(numpy array): test lon (deg)
        bns(numpy array): test X(local north) magnetic field (nT)
        bes(numpy array): test Y(local east) magnetic field (nT)
        bds(numpy array): test Z(local down) magnetic field (nT)
        margin(float or int): Acceptable error in each component (nT)"""
    xs,ys,zs=pm.geodetic2ecef(lats,lons,heights*1000)
    bxs,bys,bzs=pm.enu2uvw(bes*1E-9,bns*1E-9,-bds*1E-9,lats,lons)
    file_name=header_to_test[:-4]+'_test.cpp'
    with open(file_name,'w') as f:
        outstr="""
// %s Generated by python script wmmtestgen.py
/** \\file
 * \\author <NAME> (<EMAIL>)
 * \\date 24 OCT 2019
 * \\brief c++ catch2 tests for magnetic field header only library.
 * \\details Compile with g++ %s -std=c++1z
 */
#define CATCH_CONFIG_MAIN  // This tells Catch to provide a main() - only do this in one cpp file
#include "catch.hpp"
#include "../%s"

"""%(file_name,file_name,header_to_test)

        for i in range(len(xs)):
            testcase="""
TEST_CASE( "geomag test %s of %s model", "[GeoMag]" ) {
    geomag::Vector in;
    in.x= %s;
    in.y= %s;
    in.z= %s;
    geomag::Vector out;
    geomag::Vector truth;
    truth.x= %s;
    truth.y= %s;
    truth.z= %s;
    out= geomag::GeoMag(%s,in,geomag::%s);
    CHECK( out.x*1E9 == Approx(truth.x*1E9).margin(%s) );
    CHECK( out.y*1E9 == Approx(truth.y*1E9).margin(%s) );
    CHECK( out.z*1E9 == Approx(truth.z*1E9).margin(%s) );
}


        """%(i,modelnames[i],repr(xs[i]),repr(ys[i]),repr(zs[i]),repr(bxs[i]),repr(bys[i]),repr(bzs[i]),dates[i],modelnames[i],margin,margin,margin)
            outstr=outstr+testcase
        f.write(outstr)

if __name__ == '__main__':
    header_to_test='geomag.hpp'
    dates=([2015]*6+[2017.5]*6)*2 + [2020]*6 + [2022.5]*6
    modelnames= ['WMM2015']*12 + ['WMM2015v2']*12 + ['WMM2020']*12
    heights=([0]*3+[100]*3+[0]*3+[100]*3)*3
    lats=[80,0,-80]*4*3
    lons=[0,120,240]*4*3
    bns=[6627.1,39518.2,5797.3,6314.3,37535.6,5613.1,6599.4,39571.4,5873.8,6290.5,37585.5,5683.5]
    bes=[-445.9,392.9,15761.1,-471.6,364.4,14791.5,-317.1,222.5,15781.4,-348.5,209.5,14808.8]
    bds=[54432.3,-11252.4,-52919.1,52269.8,-10773.4,-50378.6,54459.2,-11030.1,-52687.9,52292.7,-10564.2,-50163.0]
    bns+=[6636.6,39521.1,5796.3,6323.4,37538.1,5612.2,6605.2,39569.4,5864.6,6294.3,37584.4,5674.9]
    bes+=[-451.9,377.7,15759.1,-477.6,351.1,14789.3,-298.7,252.3,15764.1,-331.1,235.7,14793.1]
    bds+=[54408.9,-11228.8,-52927.1,52249.1,-10751.1,-50385.8,54506.3,-11067.9,-52706.1,52337.8,-10600.5,-50179.5]

    bns+=[6570.4,39624.3,5940.6,6261.8,37636.7,5744.9,6529.9,39684.7,6016.5,6224.0,37694.0,5815.0]
    bes+=[-146.3,109.9,15772.1,-185.5,104.9,14799.5,1.1,-42.2,15776.7,-44.5,-35.3,14803.0]
    bds+=[54606.0,-10932.5,-52480.8,52429.1,-10474.8,-49969.4,54713.4,-10809.5,-52251.6,52527.0,-10362.0,-49755.3]

    margin= 0.1#nT error acceptable
    main(header_to_test,modelnames,np.array(dates),np.array(heights),np.array(lats),np.array(lons),np.array(bns),np.array(bes),np.array(bds),margin)
#<NAME> #24 OCT 2019 #wmmtestgen.py #This is a simple script to generate c++ catch2 tests. create and write catch2 tests as a c++ source file to test header_to_test         defining the WMM coefficents and model       The file created has the name header_to_test[:-4]+'_test.cpp'      Args:         header_to_test(str ending in .hpp): the c++ header file with the model and coefficents         modelnames(list of str): The list of model names for each test         dates(numpy array): decimal test years         heights(numpy array): test heights (km) with respect to wgs 84 ellipsoid         lats(numpy array): test lat (deg)         lons(numpy array): test lon (deg)         bns(numpy array): test X(local north) magnetic field (nT)         bes(numpy array): test Y(local east) magnetic field (nT)         bds(numpy array): test Z(local down) magnetic field (nT)         margin(float or int): Acceptable error in each component (nT) // %s Generated by python script wmmtestgen.py /** \\file  * \\author <NAME> (<EMAIL>)  * \\date 24 OCT 2019  * \\brief c++ catch2 tests for magnetic field header only library.  * \\details Compile with g++ %s -std=c++1z  */ #define CATCH_CONFIG_MAIN  // This tells Catch to provide a main() - only do this in one cpp file #include "catch.hpp" #include "../%s" TEST_CASE( "geomag test %s of %s model", "[GeoMag]" ) {     geomag::Vector in;     in.x= %s;     in.y= %s;     in.z= %s;     geomag::Vector out;     geomag::Vector truth;     truth.x= %s;     truth.y= %s;     truth.z= %s;     out= geomag::GeoMag(%s,in,geomag::%s);     CHECK( out.x*1E9 == Approx(truth.x*1E9).margin(%s) );     CHECK( out.y*1E9 == Approx(truth.y*1E9).margin(%s) );     CHECK( out.z*1E9 == Approx(truth.z*1E9).margin(%s) ); } #nT error acceptable