# -*- coding: utf-8 -*-
"""
Created on Mon Nov  7 20:44:14 2022

data from: MODTRAN_wl_785.0-850.0-5.0nm_h1_500.0km_h0_0.0km_elevation_data.csv

@author: Duncan McArthur
"""

import numpy as np

__all__ = ['get_atm_data']

###############################################################################

wvls = np.array([785,790,795,800,805,810,815,820,825,830,835,840,845,850])
elev = np.arange(0,90+1,1)
loss = np.zeros((91,14))

#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

# 785 nm
loss[:,0] = np.array([0.000469318,0.0148069,0.0595679,0.122834,0.190048,
                      0.253819,0.311518,0.36271,0.407817,0.447536,
                      0.482591,0.513648,0.541283,0.565985,0.588166,
                      0.60817,0.626284,0.642753,0.65778,0.671538,
                      0.684172,0.69581,0.70656,0.716515,0.725756,
                      0.734353,0.742367,0.749852,0.756856,0.763422,
                      0.769584,0.775378,0.780833,0.785975,0.790827,
                      0.795412,0.799749,0.803854,0.807745,0.811435,
                      0.814938,0.818265,0.821428,0.824434,0.827295,
                      0.830019,0.832613,0.835086,0.837443,0.83969,
                      0.841833,0.843877,0.845827,0.847687,0.849462,
                      0.851155,0.85277,0.854311,0.85578,0.857181,
                      0.858516,0.859787,0.860997,0.862149,0.863244,
                      0.864284,0.865271,0.866207,0.867092,0.86793,
                      0.86872,0.869464,0.870165,0.870821,0.871435,
                      0.872008,0.872539,0.873031,0.873483,0.873897,
                      0.874273,0.874612,0.874914,0.87518,0.875408,
                      0.875602,0.87576,0.875882,0.87597,0.876022,
                      0.87604])

#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

# 790 nm
loss[:,1] = np.array([0.000387954,0.0129725,0.0537359,0.112725,0.176399,
                      0.237527,0.293351,0.343262,0.387532,0.426737,
                      0.461514,0.492467,0.520123,0.544936,0.567295,
                      0.587524,0.605896,0.622644,0.637966,0.652026,
                      0.664968,0.676913,0.68797,0.698227,0.707765,
                      0.716653,0.724952,0.732714,0.739987,0.746814,
                      0.75323,0.759269,0.764961,0.770333,0.775408,
                      0.780207,0.784752,0.789058,0.793142,0.797018,
                      0.800701,0.804201,0.80753,0.810699,0.813715,
                      0.816588,0.819328,0.821939,0.824429,0.826805,
                      0.829072,0.831236,0.833301,0.835272,0.837153,
                      0.838948,0.840661,0.842296,0.843856,0.845343,
                      0.84676,0.848112,0.849398,0.850623,0.851788,
                      0.852894,0.853944,0.854939,0.855882,0.856773,
                      0.857615,0.858407,0.859153,0.859853,0.860507,
                      0.861117,0.861684,0.862209,0.86269,0.863132,
                      0.863533,0.863895,0.864217,0.864499,0.864744,
                      0.86495,0.865119,0.865249,0.865343,0.865399,
                      0.865418])

#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

# 795 nm
loss[:,2] = np.array([0.000349167,0.0120797,0.0508815,0.107776,0.169734,
                      0.229601,0.284551,0.333886,0.377797,0.416802,
                      0.451494,0.482441,0.510149,0.535055,0.557535,
                      0.577904,0.596429,0.613338,0.628824,0.64305,
                      0.656158,0.668268,0.679484,0.689899,0.699591,
                      0.708628,0.71707,0.724973,0.732381,0.739338,
                      0.74588,0.75204,0.757849,0.763334,0.768517,
                      0.773421,0.778066,0.782469,0.786646,0.790612,
                      0.794381,0.797964,0.801373,0.804618,0.807708,
                      0.810653,0.81346,0.816136,0.818689,0.821125,
                      0.82345,0.825669,0.827788,0.82981,0.831741,
                      0.833584,0.835343,0.837021,0.838623,0.84015,
                      0.841607,0.842994,0.844316,0.845574,0.846771,
                      0.847908,0.848987,0.85001,0.850978,0.851894,
                      0.852759,0.853575,0.854341,0.85506,0.855733,
                      0.856359,0.856942,0.857481,0.857978,0.858432,
                      0.858845,0.859216,0.859547,0.859838,0.86009,
                      0.860302,0.860475,0.86061,0.860706,0.860763,
                      0.860783])

#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

# 800 nm
loss[:,3] = np.array([0.000331926,0.0116485,0.0494999,0.105438,0.166688,
                      0.226106,0.28081,0.330039,0.373939,0.412993,
                      0.447774,0.478832,0.506664,0.5317,0.55431,
                      0.574806,0.593457,0.610486,0.626087,0.640421,
                      0.653633,0.66584,0.67715,0.687651,0.697425,
                      0.706539,0.715055,0.723025,0.730498,0.737517,
                      0.744116,0.750332,0.756191,0.761725,0.766954,
                      0.7719,0.776585,0.781027,0.785241,0.789242,
                      0.793043,0.796657,0.800096,0.803369,0.806486,
                      0.809456,0.812287,0.814987,0.817561,0.820018,
                      0.822363,0.8246,0.826736,0.828775,0.830722,
                      0.832581,0.834354,0.836046,0.837662,0.839201,
                      0.84067,0.842068,0.843401,0.844669,0.845875,
                      0.847022,0.84811,0.84914,0.850116,0.85104,
                      0.851912,0.852734,0.853506,0.85423,0.854908,
                      0.85554,0.856127,0.85667,0.857171,0.857628,
                      0.858044,0.858419,0.858753,0.859046,0.859299,
                      0.859513,0.859688,0.859824,0.85992,0.859978,
                      0.859997])

#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

# 805 nm
loss[:,4] = np.array([0.000318661,0.0112419,0.0480336,0.102719,0.162865,
                      0.221422,0.275491,0.324268,0.367856,0.406703,
                      0.441354,0.472341,0.500142,0.52518,0.547813,
                      0.56835,0.587053,0.604143,0.619811,0.634217,
                      0.647503,0.659787,0.671174,0.681752,0.691602,
                      0.700791,0.70938,0.717423,0.724967,0.732055,
                      0.738721,0.745003,0.750926,0.756521,0.76181,
                      0.766815,0.771557,0.776053,0.780321,0.784372,
                      0.788221,0.791883,0.795368,0.798685,0.801846,
                      0.804859,0.80773,0.810469,0.813081,0.815575,
                      0.817954,0.820224,0.822394,0.824463,0.826441,
                      0.828328,0.830129,0.831849,0.833489,0.835055,
                      0.836547,0.837968,0.839322,0.840611,0.841838,
                      0.843003,0.844109,0.845157,0.846149,0.847089,
                      0.847976,0.848812,0.849598,0.850335,0.851024,
                      0.851667,0.852265,0.852818,0.853327,0.853792,
                      0.854216,0.854597,0.854936,0.855234,0.855492,
                      0.85571,0.855887,0.856026,0.856124,0.856182,
                      0.856202])

#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

# 810 nm
loss[:,5] = np.array([0.00023142,0.00865913,0.038471,0.0844643,0.13648,
                      0.188229,0.23686,0.28138,0.321671,0.357982,
                      0.390693,0.420206,0.446903,0.471127,0.493178,
                      0.513317,0.53177,0.548728,0.564359,0.578806,
                      0.592194,0.604631,0.61621,0.627013,0.637113,
                      0.646572,0.655447,0.663788,0.671639,0.679038,
                      0.686023,0.692623,0.698867,0.704781,0.710389,
                      0.715709,0.720764,0.725568,0.730139,0.734489,
                      0.738633,0.742583,0.746351,0.749945,0.753377,
                      0.756654,0.759784,0.762775,0.765634,0.768367,
                      0.770978,0.773476,0.775865,0.778149,0.780333,
                      0.782421,0.784418,0.786326,0.788148,0.789889,
                      0.791551,0.793136,0.794647,0.796088,0.797459,
                      0.798765,0.800004,0.80118,0.802295,0.803351,
                      0.804349,0.80529,0.806175,0.807006,0.807784,
                      0.808509,0.809184,0.809809,0.810384,0.810911,
                      0.811389,0.811821,0.812204,0.812543,0.812834,
                      0.81308,0.813281,0.813438,0.81355,0.813617,
                      0.813639])

#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

# 815 nm
loss[:,6] = np.array([9.89327e-05,0.00465839,0.0235371,0.0558382,0.0949768,
                      0.135879,0.175777,0.213403,0.248299,0.280407,
                      0.309855,0.336847,0.36161,0.384366,0.405321,
                      0.424663,0.44256,0.459157,0.474585,0.488957,
                      0.502376,0.514929,0.526694,0.537739,0.548129,
                      0.557914,0.567145,0.575866,0.584116,0.591929,
                      0.599337,0.606369,0.613049,0.619402,0.625449,
                      0.631209,0.6367,0.641937,0.646936,0.651711,
                      0.656274,0.660636,0.664808,0.668801,0.672623,
                      0.676282,0.679785,0.683142,0.686358,0.68944,
                      0.692392,0.695221,0.697932,0.70053,0.703018,
                      0.705401,0.707684,0.709868,0.71196,0.713961,
                      0.715873,0.717701,0.719446,0.721111,0.722699,
                      0.724211,0.725649,0.727016,0.728313,0.729542,
                      0.730705,0.731802,0.732836,0.733807,0.734716,
                      0.735566,0.736356,0.737087,0.737762,0.73838,
                      0.738941,0.739448,0.739898,0.740296,0.740639,
                      0.740929,0.741165,0.741349,0.741481,0.741559,
                      0.741585])

#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

# 820 nm
loss[:,7] = np.array([5.24803e-05,0.00336115,0.0189346,0.0472597,0.082724,
                      0.120541,0.157943,0.193576,0.226889,0.257739,
                      0.286187,0.312383,0.336514,0.358766,0.379324,
                      0.398355,0.416008,0.432419,0.447708,0.461979,
                      0.475329,0.487839,0.499584,0.510629,0.521033,
                      0.530846,0.540115,0.548883,0.557188,0.565061,
                      0.572535,0.579637,0.58639,0.592819,0.598945,
                      0.604785,0.610357,0.615676,0.620757,0.625614,
                      0.630259,0.634704,0.638958,0.643031,0.646931,
                      0.650669,0.65425,0.657683,0.660974,0.664128,
                      0.667152,0.670053,0.672832,0.675497,0.678052,
                      0.680499,0.682844,0.685089,0.687239,0.689297,
                      0.691265,0.693146,0.694943,0.696658,0.698293,
                      0.699851,0.701335,0.702744,0.704082,0.70535,
                      0.70655,0.707682,0.708749,0.709753,0.710692,
                      0.711569,0.712385,0.713141,0.713838,0.714477,
                      0.715057,0.715581,0.716047,0.716458,0.716813,
                      0.717113,0.717358,0.717548,0.717683,0.717765,
                      0.717792])

#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

# 825 nm
loss[:,8] = np.array([7.11956e-05,0.00427084,0.0229835,0.0556892,0.095513,
                      0.137125,0.177645,0.215775,0.25106,0.283457,
                      0.313113,0.340247,0.365098,0.3879,0.408867,
                      0.428195,0.446057,0.462603,0.477967,0.492264,
                      0.505601,0.518066,0.529739,0.540691,0.550983,
                      0.560671,0.569804,0.578428,0.58658,0.594297,
                      0.601609,0.608545,0.615133,0.621395,0.627354,
                      0.633027,0.638434,0.643589,0.648507,0.653204,
                      0.65769,0.661979,0.666079,0.670002,0.673755,
                      0.677348,0.680788,0.684083,0.687239,0.690263,
                      0.693159,0.695934,0.698594,0.70114,0.703581,
                      0.705918,0.708155,0.710297,0.712347,0.714307,
                      0.716182,0.717972,0.719682,0.721313,0.722867,
                      0.724348,0.725757,0.727095,0.728365,0.729569,
                      0.730707,0.731782,0.732794,0.733745,0.734635,
                      0.735466,0.736239,0.736956,0.737616,0.738221,
                      0.738771,0.739267,0.739708,0.740097,0.740433,
                      0.740717,0.740949,0.741129,0.741257,0.741333,
                      0.741359])

#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

# 830 nm
loss[:,9] = np.array([0.000137994,0.00628473,0.0304505,0.0698772,0.115912,
                      0.162671,0.207271,0.248574,0.286306,0.32058,
                      0.351669,0.379891,0.40556,0.428967,0.450372,
                      0.470005,0.488064,0.504725,0.520134,0.534424,
                      0.547708,0.560084,0.57164,0.582452,0.592585,
                      0.602101,0.611052,0.619483,0.627437,0.634951,
                      0.642056,0.648785,0.655164,0.661217,0.666968,
                      0.672434,0.677635,0.682588,0.687307,0.691807,
                      0.696099,0.700199,0.704111,0.707851,0.711426,
                      0.714844,0.718113,0.721241,0.724235,0.7271,
                      0.729843,0.732469,0.734983,0.737387,0.73969,
                      0.741893,0.744002,0.74602,0.747949,0.749792,
                      0.751554,0.753235,0.754841,0.756371,0.757829,
                      0.759217,0.760537,0.76179,0.762979,0.764105,
                      0.76517,0.766174,0.767121,0.768009,0.76884,
                      0.769617,0.770339,0.771008,0.771624,0.772188,
                      0.772701,0.773163,0.773575,0.773937,0.774251,
                      0.774516,0.774731,0.774899,0.775019,0.775089,
                      0.775114])

#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

# 835 nm
loss[:,10] = np.array([0.000332523,0.0107408,0.0449329,0.0953722,0.150776,
                       0.204794,0.254821,0.300114,0.340753,0.377125,
                       0.409708,0.438969,0.465334,0.489178,0.510821,
                       0.53054,0.548567,0.565104,0.580322,0.594366,
                       0.607363,0.61942,0.630636,0.641091,0.650855,
                       0.659994,0.668564,0.676612,0.684183,0.691315,
                       0.698043,0.704398,0.710407,0.716097,0.721491,
                       0.726606,0.731465,0.736082,0.740472,0.744651,
                       0.74863,0.752423,0.756039,0.759489,0.762781,
                       0.765925,0.768926,0.771795,0.774537,0.777157,
                       0.779662,0.782057,0.784348,0.786536,0.788629,
                       0.79063,0.792543,0.794371,0.796117,0.797785,
                       0.799377,0.800896,0.802345,0.803724,0.805038,
                       0.806287,0.807474,0.808601,0.809668,0.810679,
                       0.811634,0.812536,0.813383,0.814179,0.814924,
                       0.815619,0.816265,0.816863,0.817414,0.817918,
                       0.818376,0.818789,0.819156,0.81948,0.81976,
                       0.819996,0.820188,0.820338,0.820445,0.820509,
                       0.82053])

#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

# 840 nm
loss[:,11] = np.array([0.000662687,0.017023,0.0637298,0.126956,0.192659,
                       0.254283,0.309719,0.358782,0.401994,0.440073,
                       0.473734,0.503615,0.530263,0.554143,0.57564,
                       0.595077,0.612723,0.628806,0.643518,0.657019,
                       0.669448,0.680923,0.691548,0.701407,0.710579,
                       0.719128,0.727114,0.734587,0.741594,0.748173,
                       0.754359,0.760184,0.765678,0.770865,0.775768,
                       0.780406,0.7848,0.788966,0.792918,0.796672,
                       0.800238,0.803631,0.80686,0.809933,0.812861,
                       0.815651,0.818311,0.820849,0.82327,0.82558,
                       0.827785,0.82989,0.831901,0.833819,0.835652,
                       0.837401,0.839072,0.840667,0.842188,0.843639,
                       0.845023,0.846342,0.847598,0.848795,0.849932,
                       0.851013,0.852038,0.853012,0.853934,0.854805,
                       0.855628,0.856404,0.857134,0.857819,0.858459,
                       0.859056,0.859612,0.860125,0.860598,0.86103,
                       0.861423,0.861776,0.862091,0.862369,0.862608,
                       0.862811,0.862975,0.863103,0.863195,0.86325,
                       0.863268])

#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

# 845 nm
loss[:,12] = np.array([0.00101424,0.0226808,0.0793551,0.151998,0.22481,
                       0.291353,0.350035,0.401154,0.445586,0.484304,
                       0.5182,0.548031,0.574435,0.597932,0.618954,
                       0.637853,0.654919,0.670399,0.684495,0.697375,
                       0.709186,0.72005,0.730073,0.739344,0.747939,
                       0.755929,0.76337,0.770314,0.776809,0.782892,
                       0.788599,0.79396,0.799006,0.80376,0.808245,
                       0.812478,0.816483,0.820273,0.823862,0.827265,
                       0.830495,0.833562,0.836476,0.839247,0.841882,
                       0.844391,0.846779,0.849056,0.851225,0.85329,
                       0.855261,0.857141,0.858934,0.860645,0.862277,
                       0.863832,0.865317,0.866734,0.868084,0.869371,
                       0.870598,0.871766,0.872877,0.873935,0.874941,
                       0.875895,0.876801,0.87766,0.878472,0.879241,
                       0.879966,0.88065,0.881292,0.881895,0.882458,
                       0.882984,0.883472,0.883922,0.884338,0.884718,
                       0.885063,0.885374,0.88565,0.885893,0.886103,
                       0.886281,0.886426,0.886538,0.886618,0.886667,
                       0.886683])

#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

# 850 nm
loss[:,13] = np.array([0.00128165,0.0263227,0.0885456,0.165901,0.241937,
                       0.310483,0.370314,0.42201,0.466648,0.505329,
                       0.539028,0.568563,0.594607,0.617707,0.638313,
                       0.656788,0.67343,0.688491,0.702176,0.714658,
                       0.726081,0.736573,0.746237,0.755162,0.763426,
                       0.771097,0.778233,0.784886,0.7911,0.796915,
                       0.802365,0.80748,0.812291,0.816819,0.821087,
                       0.825114,0.82892,0.832519,0.835926,0.839153,
                       0.842215,0.845121,0.84788,0.850502,0.852994,
                       0.855366,0.857623,0.859773,0.86182,0.86377,
                       0.86563,0.867403,0.869094,0.870707,0.872244,
                       0.87371,0.875107,0.87644,0.877711,0.878923,
                       0.880076,0.881175,0.88222,0.883214,0.88416,
                       0.885057,0.885907,0.886713,0.887477,0.888198,
                       0.888879,0.889522,0.890125,0.89069,0.891219,
                       0.891712,0.89217,0.892592,0.892982,0.893338,
                       0.893662,0.893954,0.894213,0.894441,0.894638,
                       0.894805,0.894941,0.895046,0.895121,0.895166,
                       0.895181])

#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

def get_atm_data(w):
    """
    Get the atmospheric transmissivity as a function of elevation for a
    specified wavelength.

    Parameters
    ----------
    w : float
        Vacuum wavelength (nm). 

    Returns
    -------
    elev : float, array-like
        Elevation angles for transmissivity (deg).
    loss : float, array-like
        Atmospheric transmissivity.

    """
    if (w in wvls):
        # Find wavelength data column from array
        iwl  = np.where(wvls == w)[0][0]
    else:
        raise ValueError('Wavelength not recognised for predefined' +
                         ' atmospheric transmissivity function:',w)
    return elev, loss[:,iwl]

#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

def get_atm_810():
    """
    Return the atmospheric transmission function and associated elvations for
    a wavelength of 810 nm.

    Returns
    -------
    elev : float, array-like (91,)
        Elevation angles (deg).
    Tatm : float, array-like (91,)
        Atmospheric transmission efficiency at 810 nm.

    """
    # Generate associated elevation data points
    elev = np.arange(0,90+1,1) # Elevation (deg)
    # Atmospheric transmission is stored for 810 nm
    Tatm = np.array([0.00023142,0.00865913,0.038471,0.0844643,0.13648,
                     0.188229,0.23686,0.28138,0.321671,0.357982,
                     0.390693,0.420206,0.446903,0.471127,0.493178,
                     0.513317,0.53177,0.548728,0.564359,0.578806,
                     0.592194,0.604631,0.61621,0.627013,0.637113,
                     0.646572,0.655447,0.663788,0.671639,0.679038,
                     0.686023,0.692623,0.698867,0.704781,0.710389,
                     0.715709,0.720764,0.725568,0.730139,0.734489,
                     0.738633,0.742583,0.746351,0.749945,0.753377,
                     0.756654,0.759784,0.762775,0.765634,0.768367,
                     0.770978,0.773476,0.775865,0.778149,0.780333,
                     0.782421,0.784418,0.786326,0.788148,0.789889,
                     0.791551,0.793136,0.794647,0.796088,0.797459,
                     0.798765,0.800004,0.80118,0.802295,0.803351,
                     0.804349,0.80529,0.806175,0.807006,0.807784,
                     0.808509,0.809184,0.809809,0.810384,0.810911,
                     0.811389,0.811821,0.812204,0.812543,0.812834,
                     0.81308,0.813281,0.813438,0.81355,0.813617,
                     0.813639])
    return elev, Tatm