This page was created by the IDL library routine `mk_html_help2`.

Last modified: Mon Aug 25 17:13:29 2025.

# Directory Listing of Routines

# projects/maven/lpw

- MVN_LPW_ANC_BOOM
- MVN_LPW_ANC_BOOM_GET_LUTS
- MVN_LPW_ANC_BOOM_MARS_SHADOW
- MVN_LPW_ANC_CK_SPK_CHECK
- MVN_LPW_ANC_CLEAR_SPICE_KERNELS
  Clears spice kernels, and unsets the 'kernel verified' flag in the
- MVN_LPW_ANC_COVTEST
- MVN_LPW_ANC_ENG
  Produce tplot variables of reaction wheel and thruster firing information when available.
- MVN_LPW_ANC_GET_SPICE_KERNELS
- MVN_LPW_ANC_RM_RW
- MVN_LPW_ANC_SPACECRAFT
  Routine to determine MAVEN pointing and position using the SPICE kernels.
- MVN_LPW_ANC_SPICE_TIME_CHECK
- MVN_LPW_CDF_CDF2TPLOT
- MVN_LPW_CDF_CHECK_VARS
- MVN_LPW_CDF_INFO_TO_TPLOT
- MVN_LPW_CDF_LATEST_FILE
- MVN_LPW_CDF_LOAD_VARS
- MVN_LPW_CDF_PRODUCE_L2
- MVN_LPW_CDF_READ
- MVN_LPW_CDF_READ_EXTRAS
- MVN_LPW_CDF_READ_FILE
- MVN_LPW_CDF_SAVE_VARS
- MVN_LPW_CDF_WRITE
- MVN_LPW_LOAD
  For a given UTC date, routine will check if the orbit data is present on the local machine. If not, it will download that data from the Berkeley server.
- MVN_LPW_LOAD_CDF
- MVN_LPW_LOAD_FILE
  Decomutater of the LPW telemetry data
- MVN_LPW_LOAD_FIND_CDF
  Routine to search for the CDF files MAVEN lpw produces, and load them into tplot memory. Routine is called upon by mvn-lpw-load.pro. Routine
- MVN_LPW_LOAD_L0[1]
- MVN_LPW_LOAD_L0[2]
- MVN_LPW_LOAD_L2
- MVN_LPW_PKT_CLOCKS_SPICE
- MVN_LPW_PRD_EUV
- MVN_LPW_PRD_IV_FIND_POINTS2
- MVN_LPW_PRD_IV_SWEEP_INFO
- MVN_LPW_PRD_LP_IV_LA
- MVN_LPW_PRD_LP_IV_N_T
- MVN_LPW_PRD_LP_GET_THIS_VERSION_NO
- MVN_LPW_PRD_LP_IV
- MVN_LPW_PRD_LP_IV_CAL
- MVN_LPW_PRD_LP_N_T_CLEAN_SWP_PP
- MVN_LPW_PRD_LP_N_T_COMPARE_WAVES[1]
- MVN_LPW_PRD_LP_N_T_COMPARE_WAVES[2]
- MVN_LPW_PRD_LP_N_T_COMPARE_WAVES[2]
- MVN_LPW_PRD_LP_N_T_COMPARE_WAVES[3]
- MVN_LPW_PRD_LP_N_T_COMPARE_WAVES[4]
- MVN_LPW_PRD_LP_N_T_COMPARE_WAVES[5]
- MVN_LPW_PRD_LP_N_T_COMPARE_WAVES[6]
- MVN_LPW_PRD_LP_N_T_COMPARE_WAVES[7]
- MVN_LPW_PRD_LP_N_T_COMPARE_WAVES[8]
- MVN_LPW_PRD_LP_N_T_FIT
- MVN_LPW_PRD_LP_N_T_LA
- MVN_LPW_PRD_LP_N_T_MM
- MVN_LPW_PRD_LP_N_T_PROXY
- MVN_LPW_PRD_LP_N_T_READCAL
- MVN_LPW_PRD_LP_SWP_SETUPPARAM
- MVN_LPW_PRD_LP_SWPPLOT
- MVN_LPW_PRD_MERGE_DLIMIT
- MVN_LPW_PRD_MRG_EXB
- MVN_LPW_PRD_MRG_SC_POT
- MVN_LPW_PRD_SWP_EXTRACT
- MVN_LPW_PRD_W_ACT_DENSITY
  Operates on loaded tplot_variable named "mvn_lpw_spec_hf_act"
- MVN_LPW_PRD_W_E12
- MVN_LPW_PRD_W_E12_BURST
- MVN_LPW_PRD_W_E12_CALIB
- MVN_LPW_PRD_W_N
- MVN_LPW_PRD_W_SPEC
- MVN_LPW_R_BLOCK16
  Decomutater for the compression for block=16 reading the datafile as WORD
- MVN_LPW_R_BLOCK16_BYTE
  Decomutater for the compression for block=16 reading the datafile as BYTE
- MVN_LPW_R_BLOCK32
  Decomutater for the compression for block=32 reading the datafile as WORD
- MVN_LPW_R_BLOCK32_BYTE
  Decomutater for the compression for block=32 reading the data file as BYTE
- MVN_LPW_R_CLOCK_CHECK
- MVN_LPW_R_GROUP_HSBM
  Group together the data from several HSBM packets into one hunk of data
- MVN_LPW_R_HEADER
  Decomutater of the LPW telemetry data, this is the old mvn_lpw_r_header THIS IS NOT WORKING ON L0-files
- MVN_LPW_R_HEADER_L0
  Decomutater of the LPW telemetry data, THIS IS FOR L0-files, splitted files and gourn data use mvn_lpw_r_heared.pro
- MVN_LPW_R_MASK
  This procedure only defines different masks
- MVN_LPW_SAVE_L0

# Routine Descriptions

## MVN_LPW_ANC_BOOM

[Next Routine] [List of Routines]

    PURPOSE: determine % of booms and sensors in shadow; sun angle on booms, distance sensors are from wake.

    INPUTS:
     - sun_x, sun_y, sun_z are the solar co-ordinates of the Sun (x,y,z) in the MAVEN s/c frame, in km.
                         They can be single numbers or arrays, at double precision for best results.

     - vx, vy, vz are MAVEN velocity vectors in km/s. Can be arrays (double precision required).

     - dlimit and limit are the dlimit and limit structures needed to create the tplot variables.

    OUTPUTS:
      Tplot variables:
      mvn_lpw_anc_boom_shadow_orient: 2 lines: the phi and theta angles (in degrees) of the Sun in the MAVEN s/c frame.
      mvn_lpw_anc_boom_shadow: 4 lines: boom1, sensor1, boom2, sensor2, % that is in shadow.
      mvn_lpw_anc_boom_shadow_desc: 2 lines: a number corresponding to the shadow 'form', as described below.
      mvn_lpw_anc_boom_angles: 2 lines: sun angle on the boom1/sensor1, boom2,sensor2, in degrees.

      mvn_lpw_anc_boom_wake_orient :2 lines: the phi and theta angles (in degrees) of the s/c velocity in the MAVEN s/c frame.
      mvn_lpw_anc_boom_wake_d_sc: distance in meters between sensor and geometric wake. (2 lines)
      mvn_lpw_anc_boom_wake: boom is in shadow or not, 1: in wake, 0: out of wake
      mvn_lpw_anc_boom_wake_d_perp: perp distance in meters from sensors to ram vector passing through s/c center.
      mvn_lpw_anc_boom_wake_d_para: para distance in meters from sensors, to ram vector passing through s/c center. +ve value
                                    means sensor is in front of s/c wrt motion; -ve values means sensor is behind.
      mvn_lpw_anc_boom_wake_ram_angles: angles between RAM velocity and sensors, in degrees. 2 lines, for per boom.

     KEYWORDS:
     - flow: optional keyword. This is a 3 element vector containing flow directions of any external flows to MAVEN (for example plasma flow).
                               Speeds are in km/s. If set, this flow is added to the MAVEN s/c vel to  give the overall ram vector. If omitted
                               just MAVEN velocity in MSO is used to determine the RAM velocity.

    KEY TO DESCRIPTION FOR SHADOW_DESC:
    This is output in the tplot variable mvn_lpw_anc_boom_shadow_desc:

    0 : whole boom and sensor in shadow
    1 : boom all in shadow, sensor all in sun
    2 : boom all in sun, sensor all in shadow
    3 : boom partly in shadow from base out, sensor all in sun
    4 : boom all in shadow, sensor partly in shadow from base out
    5 : boom partly in shadow, not from base out, sensor all in shadow
    6 : boom all in sun, sensor partly in shadow, not from base out
    7 : boom and sensor all both in sun

    For two or more blocks of shadow, there are many different arrangements, so group them all into one number (there aren't many).
    For this to happen, there have to be at least two up / down changes for the boom and sensor combined (note that the boom and sensor are
    separate so the shadow can change at the boom - to detect this we need to check the value of shadow at last boom point and first sensor point).

    8 : two or more blocks of shadow or sun on the boom, sensor in sun
    9 : two or more blocks of sun on the sensor, boom in sun
    10 : some other combination of multiple shadow spots on the boom and sensor, both neither fully in sun or shadow

    Written August 26th 2014 by CF: calculates shadow and wake properties of the two lpw booms.
    2014-09-08: CF: fixed bugs: Nans when position info not available now carry through and produce nans for all tplot variables.

(See projects/maven/lpw/mvn_lpw_anc_boom.pro)

## MVN_LPW_ANC_BOOM_GET_LUTS

[Previous Routine] [Next Routine] [List of Routines]

    Get shadow and wake luts, return them.

    KEYWORDS:
     - shadow: set /shadow to return the shaow LUT
     - wake: set /wake to return the wake LUT

    MODIFICATIONS:
    20150210:CF: modified mvn_lpw_anc_boom_latest_file to grab latest shadow LUT.

(See projects/maven/lpw/mvn_lpw_anc_boom_get_luts.pro)

## MVN_LPW_ANC_BOOM_MARS_SHADOW

[Previous Routine] [Next Routine] [List of Routines]

    INPUTS:

(See projects/maven/lpw/mvn_lpw_anc_boom_mars_shadow.pro)

## MVN_LPW_ANC_CK_SPK_CHECK

[Previous Routine] [Next Routine] [List of Routines]

    Give this routine an array of UNIX times; the routine uses SPICE to check whether there is ck and spk coverage for each timestamp.
    This function occurs in mvn-lpw-anc-spacecraft, but also occurs with several SPICE calculations. Only the ck/spk checking process
    is carried out here (which runs quicker), producing the same tplot variables as those in mvn-lpw-anc-spacecraft.

    The variables produced will have value zero if kernels are present, or 1 if flagged as not present. For 1, SPICE cannot be used to
    get spacecraft position / pointing, depending on which kernel is missing (ck = pointing, spk = position).

    INPUTS:
    unix_in: UNIX double precision array of timetamps. The routine will check SPICE coverage at each timestep.

    objcheck: the NAIF object code to be checked for spk (position kernels). -202 is MAVEN, for eg. See
               https://lasp.colorado.edu/maven/sdc/public/data/anc/spice/fk/maven_v05.tf
               for the full list of MAVEN codes.

    ckcheck: the NAIF object code to be checked for ck (pointing) kernels. These have three additional integers added to the end
             compared to objcheck. Eg, MAVEN is -202000.

    refcheck: the reference frame that objcheck should be checked in. Eg 'MAVEN_MSO'.

       NOTES ON INPUTS: the SPICE coverage routines will only work for frames and objects that are CK based (ie can change over time).
                        each MAVEN object is described in the above weblink, saying whether it's ck based or not. This routine may or may
                        not work for certain combinations of instruments and frames. CMF hasn't figured out whether this routine can be made
                        generic or not, however, it should work for MAVEN_SPACECRAFT and MAVEN_MSO.

    OUTPUTS:
    success: 0: routine failed to make the checkes - it should throw an error if this happens.
             1: checks completed.

    tplot variables: mvn_lpw_anc_ck_flag: flag for s/c pointing for each timestep, 1 = no data, 0 = data present
                     mvn_lpw_anc_spk_flag: flag for s/c position for each timestep, as above.

    OPTIONS:
    Set /loadspice to load the SPICE kernels found in 'mvn_lpw_load_kernel_files' to tplot memory. If not set, routine assumes they are already
       loaded.

    Set /unloadspice to remove SPICE kernels from IDL memory once checking is complete. If not set, routine will leave them in IDL memory.

    EGS:
    time = dindgen(86400)+time_double('2014-12-08')  ;make a UNIX time array, or use get_data to grab a time array...

    mvn_lpw_anc_get_spice_kernels, time, /notatlasp   ;find SPICE kernels and save into tplot variable 'mvn_lpw_load_kernel_files'
    mvn_lpw_anc_spacecraft, /only_load_spice    ;load SPICE kernels to IDL

    mvn_lpw_anc_ck_spk_check, time, ckcheck=-202000, objcheck=-202, refcheck='MAVEN_MSO'
                                                             ;check ck and spk coverage, for MAVENs position in the MSO frame.
                                                             ;As of 2019-07-31, there is ~an hour of missing ck coverage for this date, if
                                                             ;you want to check the code is working.

    NOTES:
    CMF worked out by hand how many MAVEN clock ticks represent one second: 65535.964843750000. This is hard coded below.

    VERSIONS:
    Created: 2019-07-31: Chris Fowler (cmfowler@berkeley.edu): code copied from mvn_lpw_anc_spacecraft.pro to be stand alone.

(See projects/maven/lpw/mvn_lpw_anc_ck_spk_check.pro)

## MVN_LPW_ANC_CLEAR_SPICE_KERNELS

[Previous Routine] [Next Routine] [List of Routines]

    NAME:
     mvn_lpw_anc_clear_spice_kernels
    PURPOSE:
     Clears spice kernels, and unsets the 'kernel verified' flag in the
     mvn_spc_met_to_unixtime so that mvn_spc_met_to_unixtime doesn't crash
    CALLING SEQUENCE:
     mvn_lpw_anc_clear_spice_kernels
     $LastChangedBy: cfowler2 $
     $LastChangedDate: 2016-07-26 07:47:14 -0700 (Tue, 26 Jul 2016) $
     $LastChangedRevision: 21525 $
     $URL: svn+ssh://thmsvn@ambrosia.ssl.berkeley.edu/repos/spdsoft/trunk/projects/maven/lpw/mvn_lpw_anc_clear_spice_kernels.pro $

(See projects/maven/lpw/mvn_lpw_anc_clear_spice_kernels.pro)

## MVN_LPW_ANC_COVTEST

[Previous Routine] [Next Routine] [List of Routines]

    Program written by CF on April 29th 2014 to check if give unix_times lie within the coverage of an SPK file. Multiple SPK files can be loaded
    and combined together to create larger coverage ranges.

    USAGE:
    coverage = mvn_lpw_anc_covtest(unix_in, kernels_to_check)

    INPUTS:
    unix_in: dblarr of unix times to check. ### ET times would be more accurate - check with Davin

    kernels_to_check: string or strarr containing the full paths to the spk kernels you want to look at.

    SPICE kernel files must already be loaded into IDL memory.

    object: for now, the NAIF ID code of the object to check; -202 is MAVEN, 1003228 is CSS.

    OUTPUTS:
    result: an array n_elements(unix_in) long. 1 means time is within coverage, 0 means it is outside of the kernel coverage.

     Version 1.0

    MODIFICATIONS:

     ;140718 clean up for check out L. Andersson
     20141208: modified by CF to check coverage for different objects - not just MAVEN. New input parameter 'object' allows this.

(See projects/maven/lpw/mvn_lpw_anc_covtest.pro)

## MVN_LPW_ANC_ENG

[Previous Routine] [Next Routine] [List of Routines]

    pro mvn_lpw_anc_eng, unix_in

    PROCEDURE:   mvn_lpw_anc_eng
    PURPOSE:
     Produce tplot variables of reaction wheel and thruster firing information when available.

    USAGE:
     mvn_lpw_anc_eng, unix_in

    INPUTS:

(See projects/maven/lpw/mvn_lpw_anc_eng.pro)

## MVN_LPW_ANC_GET_SPICE_KERNELS

[Previous Routine] [Next Routine] [List of Routines]

    Written 2014-10-20: CF: routine to get SPICE kernels for a certain time range using Davin's SPICE software. Routine places
    names of kernels into the tplot variable mvn_lpw_load_kernel_files, which is used by mvn_lpw_anc_spacecraft to get position etc.
    Added notatlasp keyword to disable server check, jmm, 2015-01-29

    INPUTS:
    utc_range: an array containing the times for which to search for SPICE kernels. Times can be double UNIX times, or string UTC times. The min and max values are fed into the SSL software
               to search for SPICE kernels covering this time range.

    OUTPUTS:
    tplot variable containing the kernels, used by mvn_lpw_anc_spacecraft.pro, mvn_lpw_load_kernel_files

    KEYWORDS
    Set /notatlasp if you are not using the LASP /spg/maven server to store kernels. This means the IDL environment variable ROOT_DATA_DIR is NOT set to /spg/maven at LASP.

    Set /load to load SPICE kernels into IDL memory.

    NOTE:

    EXAMPLE
    mvn_lpw_anc_get_spice_kernels, ['2014-10-10', '2014-10-11']  ;get SPICE kernels for the date '2014-10-10/00:00:00 up to 2014:10:10/23:59:59, ie 24 hours worth.
    mvn_lpw_anc_get_spice_kernels, [time_double('2014-10-10'), time_double('2014-10-11')]

    EDITS:
    2015-10-08: CMF added /load keyword.
    2015-11-09: CMF: modified routine to take an array of double or string times, and use the max/min values to send into the Berkeley routines.

(See projects/maven/lpw/mvn_lpw_anc_get_spice_kernels.pro)

## MVN_LPW_ANC_RM_RW

[Previous Routine] [Next Routine] [List of Routines]

     ;140718 clean up for check out L. Andersson
      Version 1.0

(See projects/maven/lpw/mvn_lpw_anc_rm_rw.pro)

## MVN_LPW_ANC_SPACECRAFT

[Previous Routine] [Next Routine] [List of Routines]

    pro mvn_lpw_anc_spacecraft, unix_in, not_quiet=not_quiet

    PROCEDURE:   mvn_lpw_anc_spacecraft
    PURPOSE:
     Routine to determine MAVEN pointing and position using the SPICE kernels.
     Routine determines angle between MAVEN x,y,z axes and the Sun.
     Routine gets MAVEN and Sun pointing directions in MAVEN spacecraft frame.
     SPICE is required to run this routine.
     Routine loads and unloads SPICE kernels for you.

     TO RUN:
     mvn_lpw_anc_get_spice_kernels, time    ;find required SPICE kernels for the UNIX time array 'time'. Kernels are saved into a tplot variable
     mvn_lpw_anc_spacecraft, time      ;calcualte pointing etc, using kernels stored previously in tplot.

    USAGE:
     mvn_lpw_cruciform, unix_in

    INPUTS:

(See projects/maven/lpw/mvn_lpw_anc_spacecraft.pro)

## MVN_LPW_ANC_SPICE_TIME_CHECK

[Previous Routine] [Next Routine] [List of Routines]

    Program written by CF on April 26th 2014. Routine takes an input ET time, and checks to see if it is in the predicted or reconstructed part of the MAVEN
    sclk kernel.

    USAGE:
    last_et_time_in_kernel = mvn_lpw_spice_time_check

    INPUTS:

(See projects/maven/lpw/mvn_lpw_anc_spice_time_check.pro)

## MVN_LPW_CDF_CDF2TPLOT

[Previous Routine] [Next Routine] [List of Routines]

    mvn_lpw_cdf_cdf2tplot, file, varnames=varnames, all=all

    Original file from SSL Berkeley, with original info below. Original file edited by Chris Fowler from Oct 2013 onwards for use
    with the MAVEN lpw software. Routine takes a single CDF file input and loads the data, limit and dlimit data into IDL memory as
    a tplot variable for plotting with the tplot software.

    Note: Capital letters are important for tplot variables and should be included in 'file'.
          The varformat='*' needs to be included otherwise not all of the cdf variables are loaded.

    INPUTS:
     - file: the full string directory and filename of the CDF file to be loaded into IDL memory.

    NOTE: I haven't used the other keywords, they're not needed for basic cdf file loading.

    OUTPUT:
     - a tplot variable in IDL memory containing the data, tplo limit and dlimit data for the specified CDF file. The tplot variable
       will have the same name as that for the variable saved within the CDF file, NOT the file name.

    EXAMPLE:
     mvn_lpw_cdf_cdf2tplot, '/Users/MAVEN_example/mvn_lpw_act_V1.cdf', varformat='*'  => produces a tplot variable with the name 'mvn_lpw_act_V1'

    EDITS:
     - Through till Jan 7 2014 (CF)

    ###########
     Original routine notes:

     Please note this routine is still in development
    CDF2TPLOT ,files,varnames=varnames,all=all

     record=record if only one record and not full cdf-file is requested

     $LastChangedBy: cfowler2 $
     $LastChangedDate: 2015-11-30 08:31:39 -0800 (Mon, 30 Nov 2015) $
     $LastChangedRevision: 19487 $
     $URL: svn+ssh://thmsvn@ambrosia.ssl.berkeley.edu/repos/spdsoft/trunk/projects/maven/lpw/mvn_lpw_cdf_cdf2tplot.pro $
    ##########

    Version 1.0
    ;140718 clean up for check out L. Andersson
    151130: CMF: added cdf_filename keyword in sub routines to append cdf filename to dlimit.L0_datafile structure.

(See projects/maven/lpw/mvn_lpw_cdf_cdf2tplot.pro)

## MVN_LPW_CDF_CHECK_VARS

[Previous Routine] [Next Routine] [List of Routines]

    Program written by CF on May 1st 2014. Routine checks dlimit and limit fields of the given tplot variable and ensures there are no
    blank string entries ' ' as these cause mvn_lpw_cdf_save_vars to crash.

    INPUTS:
     tplot_var: string, tplot variable name to be checked

    OUTPUTS:
     routine will replace any '' entries in dlimit and limit with 'N/A', and restore tplot variable.

      Version 1.0

    MODIFICATONS:

    ;140718 clean up for check out L. Andersson

(See projects/maven/lpw/mvn_lpw_cdf_check_vars.pro)

## MVN_LPW_CDF_INFO_TO_TPLOT

[Previous Routine] [Next Routine] [List of Routines]

    mvn_lpw_cdf_info_to_tplot, cdfi

    Original routine from SSL Berkeley was a .pro, with info below. Original routine edted by Chris Fowler from Oct 2013 onwards for use with the MAVEN
    lpw software. Routine is run from within mvn_lpw_cdf_cdf2tplot.pro. Routine takes data, tplot limit and dlimit data from a saved cdf file (input from
    mvn_lpw_cdf_cdf2tplot.pro) and creates a single tplot variable for this data. The name of the tplot variable is also returned so that we can check
    it has loaded into tplot.

    INPUTS:
     - cdfi: an IDL structure containing the tplot variable data. This structure comes directly out of mvn_lpw_cdf_load_vars.pro.

    OUTPUTS:
     - A single tplot variable. The variable name is that from within the CDF file, NOT the name of the CDF file.
     - The function also returns the tplot name of the variable loaded so that we can check if it loaded into tplot.

    EXAMPLE:
     mvn_lpw_cdf_info_to_tplot, cdfi

    EDITS:
     - Through till Jan 7 2014 (CF)

    ##############
     Original routine notes:

     This is a new routine that needs further testing, development, and enhancements.
     PROCEDURE:  cdf2tplot, cdfi
     Purpose:  Creates TPLOT variables from a CDF structure (obtained from "CDF_LOAD_VAR")
     This routine will only work well if the underlying CDF file follows the SPDF standard.

     Written by Davin Larson

     $LastChangedBy: cfowler2 $
     $LastChangedDate: 2016-10-31 10:58:33 -0700 (Mon, 31 Oct 2016) $
     $LastChangedRevision: 22234 $
     $URL: svn+ssh://thmsvn@ambrosia.ssl.berkeley.edu/repos/spdsoft/trunk/projects/maven/lpw/mvn_lpw_cdf_info_to_tplot.pro $
     #############

     Version 2.0
     ;140718 clean up for check out L. Andersson
     ;140918 CF: major changes to encompass SIS PDS requirements. Removed for loop over variables; deal with all variables at once.
                 now include MET and TT2000 time in the final tplot variable.
     151130: CMF: added cdf_filename keyword, to append cdf filename to dlimit.l0_datafile structure.

(See projects/maven/lpw/mvn_lpw_cdf_info_to_tplot.pro)

## MVN_LPW_CDF_LATEST_FILE

[Previous Routine] [Next Routine] [List of Routines]

    Routine to get the latest version and revision numbered file from an input directory and filename.

    INPUT:
    file: directory including start of filename upto but not including the version and revision part, for example the file test_v01_r02.sav
          would be entered as 'test'

          NOTE this routine assumes that the string format is as above, that the v and r numbers are two long, followed by four string characters (.cdf, .sav, etc)

    OUTPUT:
    function returns the directory and full filename to the latest file. If no files are found matching the input format, the string 'none_found' is returned.

    NOTE:
    This file assumes that the end format of the file is v##_r##.sav, and uses this to get version and revision numbers. If used on files of a different
    format this routine will crash.

(See projects/maven/lpw/mvn_lpw_cdf_latest_file.pro)

## MVN_LPW_CDF_LOAD_VARS

[Previous Routine] [Next Routine] [List of Routines]

    Function, cdfi = mvn_lpw_cdf_load_vars(file)

    Original routine from SSL Berkeley, with info below. Original routine edited by Chris Fowler from Oct 2013 onwards for use with
    the MAVEN lpw software. Routine is called from within mvn_lpw_cdf_cdf2tplot.pro, and is part of the software to load a tplot variable into
    IDL memory from a saved CDF file.

    INPUTS:
     - file: the full string directory and filename of the CDF file to be loaded into IDL memory. This is input in mvn_lpw_cdf_cdf2tplot.pro.

    OUTPUTS:
     - A structure containing the tplot variable data.

    KEYWORDS:
     NOTE: I'm not sure if these work yet as they may not have been written in by SSL. They don't seem necessary for basic file loading.

    EXAMPLE:
    cdfi = mvn_lpw_cdf_load_vars(file)

    #############
     Original routine notes:

     FUNCTION cdfi = cdf_load_vars(file)
     Keywords:
       VARFORMAT = string or string array:  a string or string array (which may contain wildcard
                             characters) that specifies the CDF variable names to load.  Use
                              'VARFORMAT='*' to load all variables.
       VARNAMES = named variable   ;output variable for variable names that were loaded.
       SPDF_DEPENDENCIES :   Set to 1 to have SPDF defined dependent variables also loaded.
       VAR_TYPE = string or string array;  Variables that have a VAR_TYPE matching these strings will
                             be loaded.
       CONVERT_INT1_TO_INT2  Set this keyword to convert signed one byte to signed 2 byte integers.
                             This is useful because IDL does not have the equivalent of INT1   (bytes are unsigned)
       RECORD: Specify the record index where you want to start reading.  By default, this option will read one record.
       NUMBER_RECORDS: Specify the number of records that you want to read.  By default, this option will begin at record zero.

     Note: Record & Number_Records can be used together to specify a range of records to be read.

     Author: Davin Larson - 2006

     $LastChangedBy: jimm $
     $LastChangedDate: 2015-02-10 11:41:54 -0800 (Tue, 10 Feb 2015) $
     $LastChangedRevision: 16942 $
     $URL: svn+ssh://thmsvn@ambrosia.ssl.berkeley.edu/repos/spdsoft/trunk/projects/maven/lpw/mvn_lpw_cdf_load_vars.pro $

     ###############
     ;
      Version 1.0
      ;140718 clean up for check out L. andersson

(See projects/maven/lpw/mvn_lpw_cdf_load_vars.pro)

## MVN_LPW_CDF_PRODUCE_L2

[Previous Routine] [Next Routine] [List of Routines]

    pro mvn_lpw_cdf_produce_l2, varlist,dir_cdf=dir_cdf,revision=revision

    This routine checks that the tplot variables given as inputs are in IDL memory. If so, they are turned into CDF files, and saved in the
    user specified directory.

    INPUTS:
     - varlist: a string or string array of tplot variables (already in IDL memory) to be saved as CDF files.

    KEYWORDS:
        dir_cdf: A string, of the full path to the CDF save directory.
        revision: A string, of the revision number. If not set, todays date is used as default for now.

    EXAMPLE:
     mvn_lpw_prd_w_E12
      mvn_lpw_cdf_produce_l2, 'mvn_lpw_calib_w_e12',revision='r2',dir_cdf=dir_cdf

    CREATED BY:   Laila Andersson  April 2014
    FILE:         mvn_lpw_cdf_produce_l2.pro
    VERSION:      1.0
    LAST MODIFICATION:
    05/02/2014 CF: added several checks for the keywords and inputs to avoid crashes. Tidied up routine.
    ;140718 clean up for check out L. Andersson

(See projects/maven/lpw/mvn_lpw_cdf_produce_l2.pro)

## MVN_LPW_CDF_READ

[Previous Routine] [Next Routine] [List of Routines]

    This routine should be used by most people to get LPW data. See below for examples.

    Edited on 2015-01-09 by CF: this routine takes a date, works out which files to load, and sends those to mvn_lpw_cdf_read_file.pro.

    Program written by Chris Fowler on Jan 6th 2014 as a wrapper for all the IDL routines needed to load cdf files into tplot memory
    for the lpw instrument.

     INPUTS:
     - date: a string variable with the date you want to load files for in the form: 'yyyy-mm-dd'

     OUTPUTS:
     - the tplot variables and corresponding limit and dlimit data are loaded into IDL tplot memory.

     KEYWORDS:
     - vars: variable(s) that you wish to load. Entered as a string, or string array if you want multiple variables loaded. Entries can be upper or lower case.
             The default (if not set) is to load all. There are twelve products LPW produces:
             wspecact       - waves active spectra
             wspecpas       - waves passive spectra
             we12burstlf    - electric field burst, low frequency    *** Burst mode data can take a long time to load and should be avoided if you don't want to use it.
             we12burstmf    - electric field burst, mid frequency    *** Burst mode data can take a long time to load and should be avoided if you don't want to use it.
             we12bursthf    - electric field burst, high frequency   *** Burst mode data can take a long time to load and should be avoided if you don't want to use it.
             wn             - density derived from waves
             lpiv           - IV curves from Langmuir Probe mode
             lpnt           - Density, Temperature, Vsc dervied from lpiv
             mrgexb         - Pointing flux
             mrgscpot       - Vsc (spacecraft potential)
             euv            - EUV data              *** NOTE that due to directory formats EUV must be loaded in a separate call; it cannot be loaded with other LPW variables. See examples below.
             we12           - 1D electric field

     - level: level of data to load, entered as a string, or string array for multiple levels. Entries can be uppder or lower case. The default (if not set) is just L2. There are four options:
       l1a, l1b, l2, l3 (euv only).

     - newdir: the default directory should be a mirror of SSL. Set this keyword if you want to look at files stored at another location. Note that sub folders within newdir are assumed to have the structure newdir/yyyy/mm/file.cdf. This hasn't been tested much and may break.

     - success: set to a named variable. This will return a string: '0': requested date and filetype was not loaded. '1': File load was successful.

     EXAMPLES:

     mvn_lpw_cdf_read, '2014-12-03', vars='lpiv', level='l2'                     ;Load L2 data, just the IV curves
     mvn_lpw_cdf_read, '2014-12-03'                                              ;Load all variables for L2 (default is L2)
     mvn_lpw_cdf_read, '2014-12-03', level=['l1a', 'l1b', 'l2'], vars = ['euv', 'lpnt']    ;load EUV and quantities derived from IV curves, for all levels (L1a, L1b, L2)

     ;To load EUV:
     mvn_lpw_cdf_read, '2015-01-02', vars='euv'     ;Load EUV, default is L2

     mvn_lpw_cdf_read, '2015-01-02', vars=['euv', 'lpnt', 'wn']   ;Load several LPW variables and EUV. This will load the LPW variables first, and produce a copy and pastable line in the terminal that will then load EUV. This line is identical to that shown below.
         mvn_lpw_cdf_read, '2015-01-02', vars='euv'               ;Identical copy and paste line to load EUV data.

     EDITS:
     - Througn till Jan 7 2014 (CF).
     - June 23 2014 CF: modified dir input to be either the same length as varlist (for multiple paths) or jsut one entry (the same path for
                        each cdf file)
     -140718 clean up for check out L. Andersson
     -2015-01-09: CF: routine changed to accept date. This routine calls upon mvn_lpw_cdf_read_file and provides the filenames to do the loading.
     -2015-04-30: CF: previous updates include ability to get EUV data, default is L2, checking input variables for errors.
     -2015-08-04: CMF: edited preamble.
     -2016-06-08: CMF: added success keyword.

     Version 2.0

(See projects/maven/lpw/mvn_lpw_cdf_read.pro)

## MVN_LPW_CDF_READ_EXTRAS

[Previous Routine] [Next Routine] [List of Routines]

    Routine to decompress the miscellaneous data produced from the LPW CDF file mvn_lpw_lp_n_t_L2.

    Currently, this will produce separate tplot variables for:

    S/C potential
    Electron density
    Electron temperature.

    INPUTS:
    Tplot variable stored in tplot memory: mvn_lpw_lp_n_t_L2

    OUTPUTS:
    New tplot variables:
    mvn_lpw_lp_Ne_L2: electron density (/cc)
    mvn_lpw_lp_Te_L2: electron temperature (K)
    mvn_lpw_lp_Usc_L2: spacecraft potential (V)

    MODIFICATIONS:
    Created: 2015-04-14: CMF.
    2016-12-20: CMF: bring in data.info information from data structure.

    NOTES: as of last edit, must check input tplot variable has dl.xFieldnam present. Check final outputs - check default value is NaN.

(See projects/maven/lpw/mvn_lpw_cdf_read_extras.pro)

## MVN_LPW_CDF_READ_FILE

[Previous Routine] [Next Routine] [List of Routines]

    Most users should not use this routine; you should instead use mvn_lpw_cdf_read.pro:

       mvn_lpw_cdf_read.pro:  Retrieves CDF files based on input date in format 'yyyy-mm-dd'. Routine finds latest version and revision from available files and calls upon mvn_lpw_cdf_read.pro to load them. Keywords available, see routine.

       mvn_lpw_cdf_read_file.pro (this file): Retrieves exact CDF files. User must give exact path and filename(s) to load. Primary use is calibration / file checking by LPW team.

    Routine renamed to mvn_lpw_cdf_read_file. This routine is given exact file names to load. mvn_lpw_cdf_read is given a date. That routine will call on this routine once it has figured out automatically which files to load, based on the date.

    Program written by Chris Fowler on Jan 6th 2014 as a wrapper for all the IDL routines needed to load cdf files into tplot memory
    for the lpw instrument.

     INPUTS:
     - dir: a string or string array containing the directory of the cdf files to be loaded into tplot memory (see example).
            dir can be one element if all cdf files lie in the same path, or it must be the same length as varlist if
            cdf files lie in different paths. In this latter case, each element of dir maps to each element of varlist.
     - varlist: a string, or string array, of cdf filenames to be loaded into tplot memory. ".cdf"
                must be included in the filename (see example).

     OUTPUTS:
     - the tplot variables and corresponding limit and dlimit data are loaded into IDL tplot memory.

     KEYWORDS:
     - See INPUTS. These are required.

     NOTE: tplot is required to run this routine.

     EXAMPLE: to load the following two CDF files:
     /Path1/test_file1.cdf
     /Path2/test_file2.cdf

     Run: If Path1 == Path2:     mvn_lpw_cdf_read_file, dir='/Path1/', varlist=['test_file1.cdf','test_file2.cdf']
          If Path1 =/= Path2:    mvn_lpw_cdf_read_file, dir=['/Path1/', '/Path2/'],  varlist=['test_file1.cdf','test_file2.cdf']

     EDITS:
     - Througn till Jan 7 2014 (CF).
     - June 23 2014 CF: modified dir input to be either the same length as varlist (for multiple paths) or jsut one entry (the same path for
                        each cdf file)
     -140718 clean up for check out L. Andersson
     - 2015-01-09: CF: changed routine to mvn_lpw_cdf_read_file. This is given file names manually. mvn_lpw_cdf_read is given a date, and calls upon this routine.
     - 2015-08-04: CMF: edited comments, cleaned up preamble.

     Version 2.0

(See projects/maven/lpw/mvn_lpw_cdf_read_file.pro)

## MVN_LPW_CDF_SAVE_VARS

[Previous Routine] [Next Routine] [List of Routines]

    Function dummy = mvn_lpw_cdf_save_vars(cdf_structure, new_cdf_name)

    Original file is from SSL Berkeley - original information shown below. Original file edited by Chris Fowler from Oct 2013 onwards
    for use with the MAVEN lpw software. Routine takes the output structure from mvn_lpw_cdf_dummy_struct.pro and saves it as a CDF file.

    This routine runs within mvn_lpw_cdf_write.pro. The tplot variables and save directory are inputs to mvn_lpw_cdf_write.pro.

    INPUTS:
     - cdf_structure: an IDL structure output from mvn_lpw_cdf_dummy_struct.pro containing data, tplot limit and dlimit data for a single tplot variable.
     - new_cdf_name: the full save directory for the CDF file ie /directory/filename.cdf. This is input in mvn_lpw_cdf_write.pro.

    OUTPUTS:
     - One CDF file for a single tplot variable at the specified directory.

    EXAMPLE:
     mvn_lpw_cdf_save_vars(tplot_structure, '/Users/MAVEN_example/mvn_lpw_pas_V1.cdf')

    ##############
     Original file notes:

     FUNCTION: dummy = cdf_save_vars(cdf_structure,new_cdf_name)
     PURPOSE:  To dump data and metadata from an IDL structure into a CDF file.
    		The structure format is the structure produced by cdf_load_vars.pro
     INPUTS:   cdf_structure : IDL structure defined by cdf_load_vars.pro
    	    new_cdf_name  : a string to name the new CDF file with
     OUTPUTS:  CDF file named by the new_cdf_name input
     EXAMPLE:  dummy = cdf_save_vars(cdfi,'newcdf.cdf')

     Written by: Matt Davis

    Note: To use this routine you must have the CDF_EPOCH/CDF_EPOCH16 bug patch on your IDL6.3
    and if you are using Solaris you need to be in 32-bit mode NOT 64-bit (ie, idl -32)

     Version 1.0
    ;140718 clean up for check out L. Andersson

(See projects/maven/lpw/mvn_lpw_cdf_save_vars.pro)

## MVN_LPW_CDF_WRITE

[Previous Routine] [Next Routine] [List of Routines]

    pro mvn_lpw_cdf_write, varlist, dir=dir

    Program written by Chris Fowler on Oct 15th 2013 to create a CDF file from one tplot variable.
    The required tplot variables must be read into IDL (using for example the r_header software) before
    running this routine.

    NOTE: caps are important in tplot names and must be included when present!

     INPUTS:
     - varlist is a string, or string array, from tplot_names, of tplot variables which have been loaded into IDL memory.
     - dir is a string and is the base directory where the CDF files will be saved. The individual file names
       are written within the routine and are the tplot variable names.
     - cdf_filename is an optional string. If set, the CDF filename will have the name 'cdf_filename'. If not set, the saved cdf file will
       be named using the tplot variable name only. The extension '.cdf' does not need to be included - it will be added automatically (the routine
       will still work even if it is included).

     OUTPUTS:
     - One CDF file per input tplot variable specified in varlist, containing the corresponding data, limit and dlimit information
       for each tplot variable. Save diretory is either the default or can be user specified in the keyword dir.

     KEYWORDS:
     - See INPUTS.

     EXAMPLES:
     - mvn_lpw_cdf_write, varlist='mvn_lpw_pas_V1', dir='/Users/MAVEN_example/' => produces the file /Users/MAVEN_example/mvn_lpw_pas_V1.cdf

     - mvn_lpw_cdf_write, varlist=['mvn_lpw_pas_V1', 'mvn_lpw_pas_V2'], dir='/Users/MAVEN_example/'  => produces the files
       /Users/MAVEN_example/mvn_lpw_pas_V1.cdf and /Users/MAVEN_example/mvn_lpw_pas_V1.cdf.

     - mvn_lpw_cdf_write, dir='/Users/MAVEN_example/', varlist='mvn_lpw_euv', cdf_filename='<yr><month><day><time>' => produces the file:
       /Users/MAVEN_example/mvn_lpw_euv_<yr><month><day><time>.cdf

       Version 1.0

     UPDATES:
     - Through till Jan 7th 2014.
     - May 1, 2014, CF: removed ability for varlist to accept numbers, must enter strings now. Added mvn_lpw_cdf_check_vars to ensure there are
       no blank '' fields in dlimit or limit, which causes mvn_lpw_cdf_save_vars to crash.
     ;140718 clean up for check out L. Andersson

(See projects/maven/lpw/mvn_lpw_cdf_write.pro)

## MVN_LPW_LOAD

[Previous Routine] [Next Routine] [List of Routines]

    PROCEDURE:   mvn_lpw_load
    PURPOSE:
     For a given UTC date, routine will check if the orbit data is present on the local machine. If not, it will download that data from the Berkeley server.
     Routine will then find the kernels required for that orbit on your machine. If they are not present, they will also be downloaded from the Berkeley server.
     For now, routine grabs all SPICE kernels.
     Directories of the data file and required SPICe kernels are saved into tplot variables so they can be accessed by other routines.
     Routine can only take one utc date, as our load routines only do one orbit at a time.

     The common block is for storing clock jump times for statistics.

    USAGE:
     mvn_lpw_load, '2014-02-02'
     mvn_lpw_load, filetype='GROUND'
     mvn_lpw_load, '2014-02-02', packet=['EUV','HSK'], tplot_var='all'

    INPUTS:

(See projects/maven/lpw/mvn_lpw_load.pro)

## MVN_LPW_LOAD_CDF

[Previous Routine] [Next Routine] [List of Routines]

    Program written by Chris Fowler on May 23rd 2014 as a wrapper for loading cdf files using mvn-lpw-load. Most users should use the routine mvn_lpw_cdf_read.pro to load LPW data. This routine
    cannot be used as a stand alone routine - it is called upon by mvn_lpw_load.pro.

     INPUTS:
     - cdf_files: a string or string array of full directory cdf files to load.

     OUTPUTS:
     - Loads the corresponding tplot variables into IDL memory.

     KEYWORDS:
     - NONE

     NOTE: tplot is required to run this routine.
           only one file directory can be specified - all cdf files to be loaded must be within the same folder and directory.

     Version 1.0

     EXAMPLE:
     mvn_lpw_cdf_load, ['/Path/to/CDF/file.cdf']

     EDITS:

    ;140718 clean up for check out L. Andersson

(See projects/maven/lpw/mvn_lpw_load_cdf.pro)

## MVN_LPW_LOAD_FILE

[Previous Routine] [Next Routine] [List of Routines]

    PROCEDURE:   mvn_lpw_load_file
    PURPOSE:
      Decomutater of the LPW telemetry data
      This call uses three different ways to get data: cdf, L0 and ground data (no sc header)
      This reads one file and creates the requested data products
      Presently cannot merge two files <--------

    USAGE:
      mvn_lpw_load,filename, tplot_var=tplot_var, filetype=filetype, packet=packet,board=board, compression=compression, use_spice=use_spice

    INPUTS:
           filename:      The full filename (including path) of a binary file containing zero or more LPW APID's.

    KEYWORDS:
           filetype:   'cdl' archive files (L2-data), 'L0' (L0 data, binary file with sc header),
                        or 'ground'/'ground_dir'a (file from ground testing, binary file without sc header)
                        Default is l0. Entry can be upper or lower case.

           packet:     Which packets to read into memeory, default all packets ['HSK','EUV','AVG','SPEC','HSBM','WPK'] . Entry can be
                       upper or lower case.

           board:      board_names=['EM1','EM2','EM3','FM']

           tplot_var  'all' or 'sci' Which tplot variables to produce. 'sci' produces tplot variables which have physical unit
                                     associated with them, and is the default. 'all' produces all tplot variables and includes
                                     master cycle information etc. Can be upper or lower case.

           use_compression: String: 'y' or 'n'. For EUV ground data only. Default is 'y'. Upper or lower case accepted.

           nospice: set /nospice to not use SPICE within the pkt routines. SPICE must still be installed to use SPICE even if this
                    keyword is not set.

    CREATED BY:   Laila Andersson  05-15-13
    FILE: mvn_lpw_load.pro
    VERSION:   1.0
    LAST MODIFICATION:
     2014, APril 17 CF: added kernel_dir keyword
     2014, April 15, CF: made default filetype='L0'; added SPICE y/n keyword; fixed bug with compression keyword
     2014, March 21, CF: added keyword spice to pkt routines
     2014, March 12, Chris Fowler - added keyword "compression"
     2014, Jannuary 5, Laila Andersson - added 'ground_dir'
     2013, July 11th, Chris Fowler - added keyword tplot_var=['all', 'sci']
                       05/15/13
    25/04/14 L. Andersson changed spice/kernerl_dir so onlu spice is used and contains kernel_dir
    29/04/14 CF: Have automated kernel finding into routine so kernel_dir is not required to be set now.
    30/04/14 CF: Reomved keyword kernel_dir. No longer needed as this is sorted in mvn_lpw_anc_wrapper. Edited /nospice keyword.
    13/05/14 CF: Changed name to mvn_lpw_load_file
    20/05/2014 CF: fixed bug with ground keyword.
    ;140718 clean up for check out L. Andersson
    20140807: CF: fixed case statement - not recognizing 'or' statement - filetype must now be lowercase.
    2014-10-06: CF: 'packet' is converted to all uppercase, so that user input can be any case. filetype converted to lower case to match existing code, but
                  ;user can now input as any case.

(See projects/maven/lpw/mvn_lpw_load_file.pro)

## MVN_LPW_LOAD_FIND_CDF

[Previous Routine] [Next Routine] [List of Routines]

    PROCEDURE:

    PURPOSE:
     Routine to search for the CDF files MAVEN lpw produces, and load them into tplot memory. Routine is called upon by mvn-lpw-load.pro. Routine
     will search for CDF files that lie within a UTC time given to mvn-lpw-load.

    USAGE:
      files = mvn_lpw_load_find_cdf('2014-02-02')

    INPUTS:
      data: a UTC string of the form 'yyyy-mm-dd'.

    OUTPUTS:
      output is a string array containing full directories to cdf files which lie on the day input.

    KEYWORDS:

      NONE
       Version 1.0

    MODIFICATIONS:
    2014-05-23  CF: finalized routine to work with mvn-lpw-load-file
    ;140718 clean up for check out L. Andersson

(See projects/maven/lpw/mvn_lpw_load_find_cdf.pro)

## MVN_LPW_LOAD_L0[1]

[Previous Routine] [Next Routine] [List of Routines]

    Routine to load multiple days of data using timespan and mvn_lpw_load (which is the L1 loader, not L2).

    mvn_lpw_load_append_l1:

    Sub routine to append tplot variables together on each iteration of mvn_lpw_load.

    INPUTS:
    ext: string: temporary extension added to end of tplot names when combining.

    .r /Users/cmfowler/IDL/STATIC_routines/LPW_loader/mvn_lpw_load_l0.pro

(See projects/maven/lpw/mvn_lpw_load_l0.pro)

## MVN_LPW_LOAD_L0[2]

[Previous Routine] [Next Routine] [List of Routines]

    Routine to load multiple days of data using timespan and mvn_lpw_load (which is the L1 loader, not L2).

    INPUTS:

    KEYWORDS:
    trange: [a,b], string start and stop times for the time range to load. These should be in the format 'yyyy-mm-dd' (no hr:mn:ss
            resolution). These follow the same format as timespan, days are loaded from 'a' up to but not including 'b' eg:
            trange = ['2018-01-01', '2018-01-03'] will load data for two dates, 01 and 02.
            trange = ['2018-01-01', '2018-01-04'] will load data for three dates, 01, 02 and 03.

            This routine will use date ranges set by timespan. If trange is set, this will overwrite the timespan settings.

    packet: select which packets to load. See mvn_lpw_load for all options. Suggested setting: packet='nohsbm'. This will load all data
            except burst data, which takes a long time to load.

    clearspice: The mvn_lpw_load routine automatically loads SPICE kernels (used for clock times). The default is to leave
                these kernels in memory in exit. Use this keyword to alter that behavior:
                string: '0': leave SPICE kernels in IDL on exit.
                        '1': remove SPICE kernels from IDL on exit (the default if not set).

    notatlasp: Set this keyword if you are not connected to the LASP SPG server, otherwise your IDL session will not be able to find
               L0 files.

(See projects/maven/lpw/mvn_lpw_load_l0.pro)

## MVN_LPW_LOAD_L2

[Previous Routine] [Next Routine] [List of Routines]

    mvn_lpw_load_l2

    Routine to load L2 LPW and EUV data into IDL tplot variables. This routine requires access to the SSL SVN library (tplot, MAVEN software, etc). You can set a time range using the SSL routine timespan outside of this routine. Or,
    you can give the routine a timespan using the trange keyword. The timespan can span multiple days, this routine grabs necessary L2 files and merges them into tplot variables.

    INPUTS:
     - vars: variable(s) that you wish to load. Entered as a string, or string array if you want multiple variables loaded. Entries can be upper or lower case.
             The default (if not set) is to load ['lpnt', 'wspecact', 'wspecpas']. There are twelve products LPW produces:
             wspecact       - waves active spectra
             wspecpas       - waves passive spectra
             we12burstlf    - electric field burst, low frequency    *** Burst mode data can take a long time to load and should be avoided if you don't want to use it.
             we12burstmf    - electric field burst, mid frequency    *** Burst mode data can take a long time to load and should be avoided if you don't want to use it.
             we12bursthf    - electric field burst, high frequency   *** Burst mode data can take a long time to load and should be avoided if you don't want to use it.
             wn             - density derived from waves
             lpiv           - IV curves from Langmuir Probe mode
             lpnt           - Density, Temperature, Vsc dervied from lpiv
             mrgexb         - Pointing flux (as of 2015-11-19 not yet available)
             mrgscpot       - Vsc (spacecraft potential)
             euv            - EUV data              *** NOTE that due to directory formats EUV must be loaded in a separate call; it cannot be loaded with other LPW variables. See examples below.
             we12            - 1D electric field

    OUTPUTS:
    Tplot variables as listed below. The left column is the var input here, the right column is the tplot variable name produced.
    'wspecact':     tvar = 'mvn_lpw_w_spec_act_l2'
    'wspecpas':     tvar = 'mvn_lpw_w_spec_pas_l2'
    'we12burstlf':  tvar = 'mvn_lpw_w_e12_burst_lf_l2'
    'we12burstmf':  tvar = 'mvn_lpw_w_e12_burst_mf_l2'
    'we12bursthf':  tvar = 'mvn_lpw_w_e12_burst_hf_l2'
    'wn':           tvar = 'mvn_lpw_w_n_l2'
    'lpiv':         tvar = 'mvn_lpw_lp_iv_l2'
    'lpnt_n':       tvar = 'mvn_lpw_lp_ne_l2'
    'lpnt_t':       tvar = 'mvn_lpw_lp_te_l2'
    'lpnt_v':       tvar = 'mvn_lpw_lp_vsc_l2'
    'mrgexb':       tvar = 'NA2'   ;THIS IS NOT AVAILABLE YET.
    'mrgscpot':     tvar = 'mvn_lpw_mrg_sc_pot_l2'
    'euv':          tvar = 'mvn_euv_calib_bands'
    'we12':         tvar = 'mvn_lpw_w_e12_l2'

    success: a float, set this to a variable to return upon exiting. Success will be an array, the same length as the number of variables requested in vars. Each element in success
             refers to the corresponding element in vars. For example, if vars=['wn', 'lpnt'], and success = [0., 1.], this means 'wn' failed to load, but 'lpnt' loaded successfully.
             Note that if you request multiple dates, and data for, say, 'wn', is found for some of those dates, success will be 1., despite failing for some dates where data was available.

             +1: routine successfully loaded requested variables. Note that currenty mrgexb is not available, and euv must be called separately from other LPW variables and will not be loaded
                 if other LPW variables are requested.
              0: no data were found
             -1: one or more input variables were not recognized. Check terminal output for which ones.
             -2: no timespan has been set. Set using timespan routine, or the trange keyword here.
             -3: trange was not entered as a string or float.

    tplotvars: set this to a variable to return a string array of the tplot variables loaded into tplot memory.

    KEYWORDS:
    trange: a double precision array of UNIX times, or string array of UTC times in the format 'yyyy-mm-dd/hr:mm:ss.ssss'. This routine will load data spanning the min and max values of trange. If trange is only one element long, this
            routine assumes a timespan of 1 day, ie trange : trange+1day. If this keyword is not set then the user must run timespan before hand to set the time range. If trange is set as a keyword, then timespan is used within this
            routine to set the time range. Be careful - this will overwrite any previous uses of timespan!

    set /noTPLOT to NOT tplot loaded data producets. If not set, default is to tplot loaded data.

    NOTES:
    I'm still working on this, so some dlimit fields will not be complete, such as time start and stop in various time frames. You will have to look at individual L2 CDF files one day at a time to get that information. For now I'm just getting the
    correct timespan of data to load.

    HSBM burst data and we12 data contains a lot of points. If you load in several days of data and try to tplot it, your machine may crash!

    If this routine encounters a problem, it will return (hopefully), not retall. Make sure your wrapper routine does not depend on any tplot variables produced by this routine.

    Due to folder locations, you will need to call for EUV data in a separate call with this routine if you also want LPW data. See examples below.

    This routine assumes you have the folder directory mirrored at SSL, with the environment variable 'ROOT_DATA_DIR' set. It will probably crash if you don't!

    EXAMPLES:
    timespan, '2015-04-01/02:33', 2.3    ;set timespan to be 2.3 days from set date.
    mvn_lpw_load_l2, ['lpnt', 'wspecact', 'wspecpas'], success=sc1, tplotvars=tvs   ;load in Ne, Te, Vsc, and active and passive spectra. Upon return, sc1 is a float array, 3 elements long, and tvs is a string array. See above for details.

    mvn_lpw_load_l2, ['lpnt', 'wn'], trange=['2015-03-01/13:43', '2015-03-06/12:00']  ;load in data and give routine a time range, which is then fed into the timespan routine here.
    mvn_lpw_load_l2, ['lpnt', 'wn'], trange=time_double(['2015-03-01/13:43', '2015-03-06/12:00'])  ;use UNIX times instead.

    timespan, '2015-04-01/02:33', 2.3
    mvn_lpw_load_l2, ['lpnt', 'wspecact', 'wspecpas', 'euv']  ;Because you request EUV as well as LPW data, routine will only get LPW data. You must get EUV separately, as below
    mvn_lpw_load_l2, ['euv']       ;When you only request EUV data, routine will get EUV data.

    Created by: Chris Fowler (christopher.fowler@lasp.colorado.edu)
    Creation Date: 2015-11-19.

    EDITS:
    2015-11-30: CMF: made all produced tplot variables lower case.
    2016-06-09: CMF: bug fixed where, if multiple vars were requested, and one was not found, code would exit before trying to find the remaining requested vars. Success keyword is now a float
                     array the same length as input vars, with a value for each requested variable.
    2016-12-20: CMF: added lines to bring through data.info field in the data structure.

(See projects/maven/lpw/mvn_lpw_load_l2.pro)

## MVN_LPW_PKT_CLOCKS_SPICE

[Previous Routine] [Next Routine] [List of Routines]

    Program written by Chris Fowler on Feb 19th 2014. Routine takes MAVEN s/c clock time in mvn_lpw_pkt routines and uses SPICE
    to determine correct start / end times of each dataset.

    INPUTS:
      Times must be in the correct MAVEN format ie 0123456789.12345. See MAVEN sclk SPICE kernels
      for more info (http://naif.jpl.nasa.gov/naif/data_mars.html).

(See projects/maven/lpw/mvn_lpw_pkt_clocks_spice.pro)

## MVN_LPW_PRD_EUV

[Previous Routine] [Next Routine] [List of Routines]

    PROCEDURE:   mvn_lpw_prd_euv

    Routine that takes EUV data and combines it with sc-attitude and produce one tplot variable for L2-production.
    Much of the calibration to get the data product into research quality product is made in this routine
    The error information and flag information is taking also into consideration information from other sources such as spacecraft atitude.
       The tplot variables mvn_lpw_euv must be loaded into tplot memory before running this routine.
       There are additional variables that need to be loaded
       Presently this routine do not go an grab them if they are missing.

    INPUTS:
     - None directly required by user.

    KEYWORDS:
     - make_cdf                                ;make one L2-cdf for the NASA DPS archive
        dir_cdf                                ;choose where the cdf will be produced

    EXAMPLE:
     mvn_lpw_prd_euv

    CREATED BY:   Laila Andersson  11-01-13
    FILE:         mvn_lpw_prd_euv.pro
    VERSION:      1.0
    LAST MODIFICATION:

(See projects/maven/lpw/mvn_lpw_prd_euv.pro)

## MVN_LPW_PRD_IV_FIND_POINTS2

[Previous Routine] [Next Routine] [List of Routines]

    Routine to find appropriate points.

    Inputs: For REE, 1 = boom1, 2 = boom2
    vswp1[*,128]   <= fitstruc.lpstruc.data.vswp / iswp
    iswp1[*,128]
    timeIn1[*]  <= fitstruc.lpstruc.time
    ErrA1[*]    <= fitstruc.lpstruc.ree.val.ErrA
    N1[*]       <= fitstruc.lpstruc.ree.val.N
    T1[*]       <= fitstruc.lpstruc.ree.val.Te
    valid1[*]   <= fitstruc.lpstruc.ree.val.valid
    vswp2
    iswp2
    timeIn2
    ErrA2
    N2
    T2
    valid2[*]

    Returns:
    indices of matching useable points for both booms in an array [*,2] long, or the string 'none_found'

(See projects/maven/lpw/mvn_lpw_prd_iv_find_points2.pro)

## MVN_LPW_PRD_IV_SWEEP_INFO

[Previous Routine] [Next Routine] [List of Routines]

    Routine to determine if sweep is one or two direction. Can also tell if sweep is bad.

    INPUTS:
    Vswp, Iswp: [128] length arrays containing the voltage and current data.

    RETURNS:
    structure containing:
    BiDir: 0: single; 1: double, 5: bad
    BadSweep: 0: good; 1: bad
    Max, min of vswp and Iswp

(See projects/maven/lpw/mvn_lpw_prd_iv_sweep_info.pro)

## MVN_LPW_PRD_LP_GET_THIS_VERSION_NO

[Previous Routine] [Next Routine] [List of Routines]

    FUNCTION:   mvn_lpw_prd_lp_get_this_version_no
     Read idl function headder to obtain the version information.

    INPUTS:
       this_pro: Give procedure/function filename as ascii

    KEYWORDS:

    EXAMPLE:
     ver = mvn_lpw_prd_lp_get_this_version_no, 'mvn_lpw_prd_lp_get_this_version_no.pro'
       gives back character 'version mvn_lpw_prd_lp_get_this_version_no: #.#'

    CREATED BY:   Michiko Morooka  10-21-14
    FILE:         mvn_lpw_prd_lp_get_this_version_no.pro
    VERSION:      2.0
    LAST MODIFICATION:
     2014-10-20   M. Morooka

(See projects/maven/lpw/mvn_lpw_prd_lp_get_this_version_no.pro)

## MVN_LPW_PRD_LP_IV

[Previous Routine] [Next Routine] [List of Routines]

    PROCEDURE:   mvn_lpw_prd_lp_IV

     Routine takes IV-cureves from both booms and combines them into one tplot variable for L2-production.
     The default swp1 and swp2 are from different subcycles.
     The sweep length can vary but the number of points in the sweep is fixed
     There will be error both in the current and the sweep potential
     The error information and flag information is taking also into consideration information from other sources such as spacecraft atitude.

    INPUTS:
       ext                                     ;'l1a' 'l1b' or 'l2'  what level of quality to produce ('l2' is full information to be archived)

    KEYWORDS:

    EXAMPLE:
     mvn_lpw_prd_lp_IV,'l1a'

    CREATED BY:   Laila Andersson  11-04-13
    FILE:         mvn_lpw_prd_lp_IV.pro
    VERSION:      2.0
    LAST MODIFICATION:
     2014-05-22   L. Andersson   sigificant update and working
     2015-05-26   L. Andersson   update for the L2 processing

(See projects/maven/lpw/mvn_lpw_prd_lp_iv.pro)

## MVN_LPW_PRD_LP_IV_CAL

[Previous Routine] [Next Routine] [List of Routines]

    PROCEDURE:   mvn_lpw_prd_lp_iv_cal

    INPUTS:
     - None directly required by user.
    KEYWORDS:
      data=data,limit=limit,dlimit=dlimit
    EXAMPLE:
     mvn_lpw_prd_lp_iv_cal,data=data,limit=limit,dlimit=dlimit

    CREATED BY:   Michiko Morooka  13-05-14
    FILE:         mvn_lpw_prd_lp_iv_cal.pro
    VERSION:      0.0
    LAST MODIFICATION:

(See projects/maven/lpw/mvn_lpw_prd_lp_iv_cal.pro)

## MVN_LPW_PRD_LP_IV_LA

[Previous Routine] [Next Routine] [List of Routines]

    PROCEDURE:   mvn_lpw_prd_lp_IV_la

     Routine takes IV-cureves from both booms and combines them into one tplot variable for L2-production.
     The default swp1 and swp2 are from different subcycles.
     The sweep length can vary but the number of points in the sweep is fixed
     There will be error both in the current and the sweep potential
     The error information and flag information is taking also into consideration information from other sources such as spacecraft atitude.

    INPUTS:
       ext                                     ;'l1a' 'l1b' or 'l2'  what level of quality to produce ('l2' is full information to be archived)

    KEYWORDS:

    EXAMPLE:
     mvn_lpw_prd_lp_IV,'l1a'

    CREATED BY:   Laila Andersson  11-04-13
    FILE:         mvn_lpw_prd_lp_IV.pro
    VERSION:      1.0
    LAST MODIFICATION:
     2014-05-22   L. Andersson   sigificant update and working

(See projects/maven/lpw/mvn_lpw_prd_lp_IV_la.pro)

## MVN_LPW_PRD_LP_IV_N_T

[Previous Routine] [Next Routine] [List of Routines]

    PROCEDURE:   mvn_lpw_prd_lp_IV_n_t_

     Routine takes IV-cureves from both booms and combines them into one tplot variable for L2-production.
     The default swp1 and swp2 are from different subcycles.
     The sweep length can vary but the number of points in the sweep is fixed
     There will be error both in the current and the sweep potential
     The error information and flag information is taking also into consideration information from other sources such as spacecraft atitude.

    INPUTS:
       ext                                     ;'l1a' 'l1b' or 'l2'  what level of quality to produce ('l2' is full information to be archived)

    KEYWORDS:

    EXAMPLE:
     mvn_lpw_prd_lp_IV,'l1a'

    CREATED BY:   Laila Andersson  11-04-13
    FILE:         mvn_lpw_prd_lp_IV.pro
    VERSION:      2.1
    LAST MODIFICATION:
     2014-05-22   L. Andersson   sigificant update and working
     2015-05-22   L. Andersson   update to get the L2 working

(See projects/maven/lpw/mvn_lpw_prd_lp_IV_n_t.pro)

## MVN_LPW_PRD_LP_N_T_CLEAN_SWP_PP

[Previous Routine] [Next Routine] [List of Routines]

    FUNCTION:   mvn_lpw_prd_lp_n_t_fit
     Clean up the sweep fit result. Take away uncertain data points.

    INPUTS:
       PP  :  Analysed LP sweep data set structure.

    EXAMPLE:
     PP = mvn_lpw_prd_lp_n_t_clean_swp_pp(PP)

    CREATED BY:   Michiko Morooka  03-23-15
    FILE:         mvn_lpw_prd_lp_n_t_clean_swp_pp.pro
    VERSION:      1.2
    LAST MODIFICATION:
     2015-03-23   M. Morooka
     2015-03-26   M. Morooka (1.0)
     2015-06-19   M. Morooka (1.1) Add ion side calcuration
     2015-07-02   M. Morooka (1.2) derive_io2

(See projects/maven/lpw/mvn_lpw_prd_lp_n_t_clean_swp_pp.pro)

## MVN_LPW_PRD_LP_N_T_COMPARE_WAVES[1]

[Previous Routine] [Next Routine] [List of Routines]

    THIS IS _V2: here I used oen set of params for Vsc > -1.5V. In this range, the corrections jump around, so the only way to do this is using  bulk stats for this region. Above this, corrections are a function of Vsc: this
    region is more stable and does not jump around. The corrections are also smaller. Compare_waves_lp4 is the major change in _V2 - I hard coded in the changes.

    Compare LPW waves densities with IV densities.
    .r /Users/chfo8135/IDL/MAVEN/Software/analysis_software/ImprovedFitRoutines/compare_waves_lp/mvn_lpw_prd_lp_n_t_compare_waves.pro

    compare_waves_lp1:
    Load in data for specified dates, save Ne, Te, Vsc, ErrA, NeWaves (NeW), altitude, position, UVI in a big save file, to be re-loaded in subsequent plotting routines.

    INPUTS:

    KEYWORDS:
    trange: string, two element array ['yyyy-mm-dd', 'yyyy-mm-dd'], the start and stop times to run the routine for. If not set the default range 2014-10-08 - 2015-11-31 will be used (when we have waves data).

    NOTES:
    The folder compare_data1 is for the default time range.

(See projects/maven/lpw/mvn_lpw_prd_lp_n_t_compare_waves.pro)

## MVN_LPW_PRD_LP_N_T_COMPARE_WAVES[2]

[Previous Routine] [Next Routine] [List of Routines]

    Analyze the data saved from above. Enter various restrictions on what to plot below.

    INPUTS:
    filename: string, full directory name of file to load.

    KEYWORDS:
    errA: the value below which values are plotted. Default if not set is <50.
    VscH, VscL: values of Vsc to use: VscL < Vsc < VscH. Default if not used is -6 < Vsc < 5. NOTE don't use 0. as it's a keyword - it won't work, use 0.00001 or something.
    angCORRM: +1. or -1., manually set as auto software doesn't work.
    result: set to a variable that will contain the fitted parameters.
    yesPLOT: set as a float: 1 to plot figures, 2 to NOT plot figures.

    compare_waves_lp2, '/Users/chfo8135/IDL/MAVEN/Software/analysis_software/ImprovedFitRoutines/compare_waves_lp/compare_data1/compare_data.sav'   ;full datase with waves.

(See projects/maven/lpw/mvn_lpw_prd_lp_n_t_compare_waves.pro)

## MVN_LPW_PRD_LP_N_T_COMPARE_WAVES[2]

[Previous Routine] [Next Routine] [List of Routines]

    Calculate median, and stddev (based on median) of some data. Give X,Y pairs as two arrays, binsize and min/max bin values. Returned are two arrays: the 'X axis' (the bins used), and the computed results as an Nx3 array: median, median+-stddev.
    Use the median instead of mean as this is much better for datasets that can have one or two large points the skew the mean.

    INPUTS:
    Xdata: array, X data points
    Ydata: array, Y data points
    binsize: size of bins
    low: lowest X value to consider when binning.
    upp: highest X value to consider when binning. Points outside of low and upp are not considered.

    KEYWORDS:

    RETURNS:
    structue containing two arrays:
    result.Xbins = bins used, for labeling the X axis.
    result.med = median value in each bin.
    result.medupp = median + stddev in each bin.
    result.medlow = median - stddev in each bin.

(See projects/maven/lpw/mvn_lpw_prd_lp_n_t_compare_waves.pro)

## MVN_LPW_PRD_LP_N_T_COMPARE_WAVES[3]

[Previous Routine] [Next Routine] [List of Routines]

    Things that need saving to work out correction to Nlp. These things are all a function of Vsc.
    Vsc middle - spacecraft potential: value at the middle of the bin used.
    Xp,Yp - point where median fit line (to data) crosses 1:1 line.
    Ar - angle between fit line and 1:1 line (radians)
    result[0], result[1]: A, B(*x) - constants for best fit line.

     = 6 parameters (1 indpendent (Vsc), 5 dependent).

    KEYWORDS:
    set /saveVALS to save the fitted parameters into an IDL save file structure.

(See projects/maven/lpw/mvn_lpw_prd_lp_n_t_compare_waves.pro)

## MVN_LPW_PRD_LP_N_T_COMPARE_WAVES[4]

[Previous Routine] [Next Routine] [List of Routines]

    This routine will correct LP densities as fn(Ne, Vsc). Give the routine Ne and Vsc from the LP fits. It will look up the correction parameters saved using the above routines. These parameters are then used to correct the data usign the formula within
    the code. This formula is also in the above routines.

    RESTRICTIONS:
    Currently empircal correction only works for -0.1 > Vsc > -7  V. There is not enough data statistically do this correction outside of these ranges.

    INPUTS:
    N: density for LP (in units /cc) (float).
    Vsc: spacecraft potential from LP (volts, sign is negative for 99% of the mission) (float).
    Nwaves: waves density (/cc) (float) to compare with LP density.

    KEYWORDS:
    paramfile: string: name of param file to load.
    output: set to a named variable that will contain the output structure. This structure will contain Nin, Vscin, and Nout, the corrected densities.
    VscR: Vsc range: set low to high, eg [-6., -1.], to only use points in that range. Default [-7., -0.1] used if not set. This is for plotting purposes only.
    Set /plotC to plot the corrected data. This requires you to specify the waves data as well. This will plot Nwaves vs Nlp. If you don't have Nwaves, the routine will still work, but cannot produce this plot.

    NOTES:
    Densities are converted to log10 space, corrected, and then converted back to linear. Correct units are essential!

    EXAMPLE:
    For Chris:
    d = compare_waves_lp6()
    compare_waves_lp5, d.nlp, d.vsc, d.nwa, paramfile='EMP_PARAMS_2016-03-01T17-40-21.sav', /plotC      ;plot the corrections as fn(Nwaves) - NOTE: you must give the routine Nwaves.

    For someone else:
    First set 'loadDIR' below to whereever you saved the params file.
    Get Nlp and Vsc in arrays, 'NinLP' and 'Vscin'.
    Use:

    compare_waves_lp5, NinLP, Vscin, paramfile='EMP_PARAMS_2016-02-09T19-10-41.sav', output=output ; OLD FILE ;don't give the routine Nwaves; just do the corrections, and return them in 'output'.
    compare_waves_lp5, NinLP, Vscin, paramfile='EMP_PARAMS_2016-02-16T16-15-15.sav', output=output  ; NEW FILE, params-2.
    compare_waves_lp5, NinLP, Vscin, paramfile='EMP_PARAMS_2016-02-29T21-56-46.sav', output=output  ;Latest params-1.
    compare_waves_lp5, NinLP, Vscin, Nwa, paramfile='EMP_PARAMS_2016-02-29T21-56-46.sav', output=output  ;Latest params-1., if you have waves
    compare_waves_lp5, NinLP, Vscin, Nwa, paramfile='EMP_PARAMS_2016-03-01T00-45-20.sav', output=output  ;Latest params, if you have waves
    compare_waves_lp5, NinLP, Vscin, Nwa, paramfile='EMP_PARAMS_2016-03-01T01-26-24.sav', output=output  ;Latest +1
    compare_waves_lp5, NinLP, Vscin, Nwa, paramfile='EMP_PARAMS_2016-03-01T17-40-21.sav',    ;latest+2

    compare_waves_lp5, d.NLP, d.Vsc, d.Nwa, paramfile='EMP_PARAMS_2016-03-03T19-34-50.sav'   ;latest+3 - NOTE: format change, I only save result0 and result1 now, so this file is different in compare_waves_lp_V2.pro.

    the array output will contain the input variables, and the corrected densities.
    This routine can be given any range of Vsc and density values - it will only correct those where -1>Vsc>-7 volts.

(See projects/maven/lpw/mvn_lpw_prd_lp_n_t_compare_waves.pro)

## MVN_LPW_PRD_LP_N_T_COMPARE_WAVES[5]

[Previous Routine] [Next Routine] [List of Routines]

    Routine to load in NE and Vsc from a save file. Output is filtered for ErrA and valid, and is a data structure.

    EXAMPLE:
    d = compare_waves_lp6()

(See projects/maven/lpw/mvn_lpw_prd_lp_n_t_compare_waves.pro)

## MVN_LPW_PRD_LP_N_T_COMPARE_WAVES[6]

[Previous Routine] [Next Routine] [List of Routines]

    Use compare_waves_lp2 to generate data arrays containing fit parameters. This routine uses a for loop to generate the values for many Vsc values quickly.
    The data are put into a save file.

(See projects/maven/lpw/mvn_lpw_prd_lp_n_t_compare_waves.pro)

## MVN_LPW_PRD_LP_N_T_COMPARE_WAVES[7]

[Previous Routine] [Next Routine] [List of Routines]

    Routine to plot the surface contour of the corrections used as fns(vsc, density). This routine makes a 2D array where one axis is Vsc, one is density. It uses
    compare_waves_lp7 to feed in these values, and calculates the correction to density. This is saves in the 2D array. The surface of this array is then plotted.

    KEYWORDS:
    set loadFILE to string - the filename to load in a datagrid instead of deriving. This is much quicker.
    set save='filename.sav' to save the datagrid into a save file, with this filename. Save directory is set below.

    EXAMPLES:
    compare_waves_lp8, loadFILE='LPcorrectionDATA_HiRes.sav'   ;hi res grid file
    compare_waves_lp8, loadFILE='LPcorrectionDATA_LowRes.sav'   ;low res grid file (for testing)

(See projects/maven/lpw/mvn_lpw_prd_lp_n_t_compare_waves.pro)

## MVN_LPW_PRD_LP_N_T_COMPARE_WAVES[8]

[Previous Routine] [Next Routine] [List of Routines]

    New way of calculating correction amount based on a 2D grid. Using Vsc and density, the  correct indices are calculated and the correction amount (%) identified. This is applied to the input density, to give the output.
    This method will be faster then compware_waves_lp5.

    Currently empircal correction only works for -0.1 > Vsc > -7  V. There is not enough data statistically do this correction outside of these ranges.

    INPUTS:
    NlpIN: density for LP (in units /cc) (float).
    VscIN: spacecraft potential from LP (volts, sign is negative for 99% of the mission) (float).
    NwaIN: waves density (/cc) (float) to compare with LP density. If not set, this is ignored. If set, a before and after plot will be produced.
    filename: string: the full directory and filename of the IDL save file containing the 2D surface information.

    OUTPUTS:
    NlpOUT: set to a variable to contain the corrected densities, in the same order as they were input.
    success: 0 means no success. 1 means successful
    corrected: an array of same length NlpIN: it will contain 1s and 0s: 1 means this indice was corrected, 0 means it was not (it lays outside of the correction ranges applicable).

    EXAMPLES:
    compare_waves_lp9, d.Nlp, d.Vsc, d.Nwa, filename='/Users/chfo8135/IDL/MAVEN/Software/analysis_software/ImprovedFitRoutines/compare_waves_lp/compare_data1/LPcorrectionDATA_HiRes.sav', NlpOUT=NlpOUT, corrected=corrected, success=success

(See projects/maven/lpw/mvn_lpw_prd_lp_n_t_compare_waves.pro)

## MVN_LPW_PRD_LP_N_T_FIT

[Previous Routine] [Next Routine] [List of Routines]

    FUNCTION:   mvn_lpw_prd_lp_n_t_fit
     Sweep fitting function set for MAVEN/LPW

    INPUTS:
       PP  :  LP sweep data set structure. (See  )
       type:

    KEYWORDS:
       win: use this keyword to see the every sweeep plot during the run

    EXAMPLE:
     mvn_lpw_prd_lp_n_t_fit, PP, 'l0'

    CREATED BY:   Michiko Morooka  10-21-14
    FILE:         mvn_lpw_prd_lp_n_t_fit.pro
    VERSION:      2.6
    LAST MODIFICATION:
     2014-10-20   M. Morooka
     2014-11-13   M. Morooka Update first version of fitting set.
     2014-11-17   M. Morooka Bug fixed for the electron fitting limit define. (fitswp_10V_00)
                             Outer region fitting fixed to two components. (fitswp_SW_02)
     2014-11-24   M. Morooka add givenU define algorithm (fitswp_10V_00)
     2014-12-19   Add two electron component fitting to the dense plasma resion.
                   fitswp_SW_03, fitswp_10V_01
     2015-01-31   Change fitting routine for the dense plasma resion.
                   fitswp_10V_02_e
     2015-02-02   Change fitting routine for the dense plasma resion. Two electron component fitting.
                   fitswp_10V_03_2e
     2015-03-23   Minor changes for fitting routine.
     2015-04-16   dTe is added dTe (2.7)
     2015-05-26   new gaussian fit (3.0)
     2015-07-02   revise gaussian fit (3.1)

(See projects/maven/lpw/mvn_lpw_prd_lp_n_t_fit.pro)

## MVN_LPW_PRD_LP_N_T_LA

[Previous Routine] [Next Routine] [List of Routines]

    PROCEDURE:   mvn_lpw_prd_lp_n_t_la

     Routine takes IV-cureves from both booms and combines them into one tplot variable for L2-production.
     The default swp1 and swp2 are from different subcycles.
     The sweep length can vary but the number of points in the sweep is fixed
     There will be error both in the current and the sweep potential
     The error information and flag information is taking also into consideration information from other sources such as spacecraft atitude.

    INPUTS:
       ext                                     ;'l1a' 'l1b' or 'l2'  what level of quality to produce ('l2' is full information to be archived)

    KEYWORDS:

    EXAMPLE:
     mvn_lpw_prd_lp_IV,'l1a'

    CREATED BY:   Laila Andersson  11-04-13
    FILE:         mvn_lpw_prd_lp_IV.pro
    VERSION:      1.0
    LAST MODIFICATION:
     2014-05-22   L. Andersson   sigificant update and working

(See projects/maven/lpw/mvn_lpw_prd_lp_n_t_la.pro)

## MVN_LPW_PRD_LP_N_T_MM

[Previous Routine] [Next Routine] [List of Routines]

    FUNCTION:   mvn_lpw_prd_lp_n_t_mm

     ROUTINE that use 'LPSTRUC' data struture and proceed the sweep curve fitting for MAVEN/LPW

    , prd_ver=prd_ver, lpstruc_filename=lpstruc_filename, pp_save=pp_save, pp_dir=pp_dir
    INPUTS:
       in1:  Data structure for LP sweep analysis.
             Specify prbe number if lp sweep data and ephemeris exsist in tplot var.

    KEYWORDS:
       I_offset: give specific number for negative biased current offset. (default NaN)

    EXAMPLE:
     swp_pp = mvn_lpw_prd_lp_n_t_mm(lpstruc)
     swp_pp = mvn_lpw_prd_lp_n_t_mm(probe_no)

    CREATED BY:   Michiko Morooka  01-14-15
    FILE:         mvn_lpw_prd_lp_n_t_mm.pro
    VERSION:      2.0
    LAST MODIFICATION:
       15-02-20/M.W.Morooka/ Two component fitting is activated for lpw mode-0.
                             Adding dNe dTe dU
       15-03-23 M.Morooka Fit result clean option added.
       15-04-15 M.Morooka bug fix for make_sweep_set (1.1)
       15-05-26 M.Morooka new gaussian fitting for ionosphere (2.0)
       15-07-02 M.Morooka Minor procedure change (2.1)

(See projects/maven/lpw/mvn_lpw_prd_lp_n_t_mm.pro)

## MVN_LPW_PRD_LP_N_T_PROXY

[Previous Routine] [Next Routine] [List of Routines]

    FUNCTION:   mvn_lpw_prd_lp_n_t_proxy

    INPUTS:
        PP:  Analysed LP sweep data set structure.

    EXAMPLE:
     PP = mvn_lpw_prd_lp_n_t_clean_swp_pp(PP)

    CREATED BY:   Michiko Morooka  09-18-15
    FILE:         mvn_lpw_prd_lp_n_t_proxy.pro
    VERSION:      0.0

(See projects/maven/lpw/mvn_lpw_prd_lp_n_t_proxy.pro)

## MVN_LPW_PRD_LP_N_T_READCAL

[Previous Routine] [Next Routine] [List of Routines]

    FUNCTION:   mvn_lpw_prd_lp_n_t_readcal
       Read calibration text file for maven lpw sweep analysis.

    INPUTS:
       input: information to identify the file name and output. 'input' can be
               % full path of the cal file (ex.'lpwdata_dir/data/sci/lpw/fitpar/2014/11/mvn_lpw_fitpar_20141010_l1a_p1_v00_r00.txt')
               % time in format of 'YYYY-MM-DD/HH:MM:SS'
               % date in format of YYYYMMDD (integer) or 'YYYY-MM-DD'
       prb:    probe no (1/2)
    KEYWORDS:
       lpw_cal_dir:  Specify the data stored directory. If not default is getenv('ROOT_DATA_DIR')
       no_latest  :  Set this keyword to chose old cal file.
    OUTPUT:
       cal info structure.
    EXAMPLE:
     caldata = mvn_lpw_prd_lp_n_t_readcal('lpwdata_dir/data/sci/lpw/fitpar/2014/11/mvn_lpw_fitpar_20141010_l1a_p1_v00_r00.txt')
     caldata = mvn_lpw_prd_lp_n_t_readcal(20141010,1)
     caldata = mvn_lpw_prd_lp_n_t_readcal('2014-10-10/00:00:00.0',1)
     caldata = mvn_lpw_prd_lp_n_t_readcal(1.4129352e+09,1); (time in tplot time format)

    CREATED BY:   Michiko Morooka  11-18-14
    FILE:         mvn_lpw_prd_lp_n_t_readcal.pro
    VERSION:      0.0
    LAST MODIFICATION:

(See projects/maven/lpw/mvn_lpw_prd_lp_n_t_readcal.pro)

## MVN_LPW_PRD_LP_SWPPLOT

[Previous Routine] [Next Routine] [List of Routines]

    ;PROCEDURE: mvn_lpw_prd_lp_swpplot
     Standard plot for Langmuir Probe sweeps. Displays V-I curve of liner, logalithmic, and derivertive.

    ;INPUT:
     sweep_data: list of structured sweep data. To plot mvn lpw data, use 'mvn_lpw_swpextracr'
                 data structure must contain 'U' as voltage and 'I' as current

    KEYWORDS:

    ;CREATED BY:  Michiko W. Morooka Apr. 2014
    FILE      : mvn_lpw_prd_lp_make_l0filelist.pro
    VERSION:   0.0
    LAST MODIFICATION:   04/18/14

(See projects/maven/lpw/mvn_lpw_prd_lp_swpplot.pro)

## MVN_LPW_PRD_LP_SWP_SETUPPARAM

[Previous Routine] [Next Routine] [List of Routines]

    FUNCTION:   mvn_lpw_prd_lp_swp_setupparam_new
     Create initial Sweep parameter set forMAVEN/LPW sweep analysis.

    INPUTS:
    OUTPUTS: gives LP Sweep parameter set specific for the mvn_lpw_prd_lp analys program.
    KEYWORDS:
       win: use this keyword to see the every sweeep plot during the run

    EXAMPLE:
     mvn_lpw_prd_lp_swp_setupparam, PP, 'l0'

    CREATED BY:   Michiko Morooka  10-21-14
    FILE:         mvn_lpw_prd_lp_n_t_fit.pro
    VERSION:      3.0
    LAST MODIFICATION:
     2014-10-20   M. Morooka
     2014-11-17   M. Morooka   Add err information
                               Add current_l0, voltage_l0
     2015-01-13   M. Morooka   Add extra dummy fields
     2015-04-16   M. Morooka   dTe added (2.3)
     2015-05-26   M. Morooka   Change for new_fit (3.0)
     2015-06-19   M. Morooka   Minor addition (3.1) (Ni2, mi2)

(See projects/maven/lpw/mvn_lpw_prd_lp_swp_setupparam.pro)

## MVN_LPW_PRD_MERGE_DLIMIT

[Previous Routine] [Next Routine] [List of Routines]

    PROCEDURE:   mvn_lpw_prd_merge_dlimit

    Routine combines dlimit structures for tplot variables. Input variables must have the same size dlimit structures, with the same field names.

    INPUTS:

     - found_variables: a string array of tplot variables in IDL memory, whose dlimits are to be merged. THE FIRST ENTRY of this array is assumed to be
                        a dummy string with text information.

    KEYWORDS:
     NONE

    EXAMPLE:
     dlimit_merge = mvn_lpw_prd_merge_dlimit(['Variables to merge', 'mvn_lpw_act_e12', 'mvn_lpw_pas_e12'])

    CREATED BY:   Chris Fowler  05-21-2014
    FILE:         mvn_lpw_prd_merge_dlimit.pro
    VERSION:      1.0
    LAST MODIFICATION:

(See projects/maven/lpw/mvn_lpw_prd_merge_dlimit.pro)

## MVN_LPW_PRD_MRG_EXB

[Previous Routine] [Next Routine] [List of Routines]

    PROCEDURE:   mvn_lpw_prd_mrg_ExB

    Routine takes mvn_lpw_w_E12_L2 data and Magnetometer-L2 data and procduse the poynting flux based on the 1-D electrci field information
    the resulting E, B, ExB=S is down sampled so the S product represent a time interal
    The error information provide information of the uncurtanty in the qunatity weighted by the dynamics in the E and B on shorter time scales
    the flag provide the confidnece level of the result
    this product will be the last we finnish working with

    INPUTS:
       ext                                     ;'l1a' 'l1b' or 'l2'  what level of quality to produce ('l2' is full information to be archived)

    KEYWORDS:

    EXAMPLE:
     mvn_lpw_prd_mgr_ExB

    CREATED BY:   Laila Andersson 11-06-2013
    FILE:         mvn_lpw_prd_mgr_ExB.pro
    VERSION:      1.0
    LAST MODIFICATION:
     2014-05-22   L. Andersson   sigificant update and working
     2014-10-06: CF: modified dlimits for ISTP comliance.

(See projects/maven/lpw/mvn_lpw_prd_mrg_exb.pro)

## MVN_LPW_PRD_MRG_SC_POT

[Previous Routine] [Next Routine] [List of Routines]

    PROCEDURE:   mvn_lpw_prd_mrg_sc_pot

    Routine takes V1 and V2 data from all subcycles and combine them to one product.
    Note for some sub-cycles both V1 and V2 is produced simultaniously.
    Presently the sc_pot derived from the IV-sweep -fit is not used
    The cadence change between the different sub-cycles and the different operation modes
    The error information and flag information is taking also into consideration information from other sources such as spacecraft atitude.
       The tplot variables must be loaded into tplot memory before running this routine.
       Presently this routine do not go an grab them if they are missing.

    INPUTS:
       ext                                     ;'l1a' 'l1b' or 'l2'  what level of quality to produce ('l2' is full information to be archived)

    KEYWORDS:

    EXAMPLE:
     mvn_lpw_prd_mrg_sc_pot

    CREATED BY:   Laila Andersson 11-04-2013
    FILE:         mvn_lpw_prd_mrg_sc_pot.pro
    VERSION:      2.1
    LAST MODIFICATION:
     2014-05-22   L. Andersson   sigificant update and working
     2015-05-22   L. Andersson   update to get the L2 working

(See projects/maven/lpw/mvn_lpw_prd_mrg_sc_pot.pro)

## MVN_LPW_PRD_SWP_EXTRACT

[Previous Routine] [Next Routine] [List of Routines]

    ;FUNCTION: mvn_lpw_prd_swp_extract

    ;INPUT: time
       time: in format of 'YYYY-MM-DD/HH:MM:SS'

    ;KEYWORDS: tnameI=tnameI, tnameV=tnameV, prb=prb
       tnameI : default is 'mvn_lpw_swp1_I1'/'mvn_lpw_swp2_I2'
       tnameV : default is 'mvn_lpw_swp1_I1_pot'/'mvn_lpw_swp2_I2_pot'
       prb    : probe no 1/2
    ;OUTPUT
       returns a structure contains, T, U, and I

    ;Examples:
       swp = mm_mvn_lpw_swp_extract('2014-07-07/19:34:00',tnameI='mvn_lpw_swp2_I2',tnameV='mvn_lpw_swp2_I2_pot')
       swp = mm_mvn_lpw_swp_extract('2014-07-07/19:34:00',prb=1)
       swp = mm_mvn_lpw_swp_extract('2014-07-07/19:34:00')
    ;CREATED BY:  Michiko W. Morooka Apr. 2014
    FILE      : mvn_lpw_prd_swp_extract.pro
    VERSION:   0.0
    CREATED:             M. Morooka 04/18/14
    LAST MODIFICATION:   M. Morooka 11/22/14 added to mvn_lpr_prd product

(See projects/maven/lpw/mvn_lpw_prd_swp_extract.pro)

## MVN_LPW_PRD_W_ACT_DENSITY

[Previous Routine] [Next Routine] [List of Routines]

     NAME:
     	mvn_lpw_prd_w_act_density
     PURPOSE:
     	Operates on loaded tplot_variable named "mvn_lpw_spec_hf_act"
     	Creates new variables
    		'mvn_lpw_spec2_hf_act_bgdsub': Background-supressed spectra (dB above background)
    		'mvn_lpw_spec2_hf_act_density': Determined plasma density (cc) from the location of the peak signal.
     			The errors dy and dv give the lower and upper errors, determined from the width of the peak.
                   'mvn_lpw_spec2_hf_act_conf': A rough confidence estimate.  100 being OK, 0 being bad.  Trust anything _above_ 50, for now.
    		'mvn_lpw_spec2_hf_act_flag': Bitfield flag for plasma density
     			Any flag = 0 indicates good data with no noted issues.
     			Otherwise, "(flag AND flag_ID) NE 0" indicates that flag_ID is True, where flag_ID
     			is one of
    				HF_ACT_DENSITY__ERR__SNR_TOO_LOW = 1; Peak SNR too low (<HF_ACT_MIN_SNR)
    				HF_ACT_DENSITY__ERR__HWHM_UNDEF  = 2 ; HWHM could not be determined
    				HF_ACT_DENSITY__ERR__PK_NONUNIQ  = 4 ; Peak is non-unique
    				HF_ACT_DENSITY__ERR__PKWIDTH_UNDEF = 8; Peak width could not be determined
    				HF_ACT_DENSITY__ERR__OFFSCALE_HIGH = 16 ; Frequency located is too high;
    		'mvn_lpw_spec2_hf_act_snr':  SNR for the located peak (dB above background)
    		'mvn_lpw_spec2_hf_act_density_adj': Tweaked density, based on weighted average of the nearest values to the peak.
     CATEGORY:
     	MVN_LPW
     KEYWORD PARAMETERS:
     	print_out and plot_out: For debugging purposes
     MODIFICATION HISTORY:
     2015-01-20: Created by david.andrews@irfu.se
     2015-01-28: DA: added condfidence penalty to data with MIN_SNR > SNR > LOW_SNR.  V2.
     2015-02-03: DA: added a mode flag, default to 'act', but allows running same routine for passive sounding.  Doesn't work well though.
     2015-02-03: DA: rejigged errors, now providing independent upper and lower values, at which the peak falls to background levels.
     2015-02-05: DA: tplot variable storing the located frequency is now created by default. V3
     2015-02-06: DA: applied a small 3% shift to the peak locator, to favour lower frequencies over heigher ones.
                     Also now uses spec2 rather than spec, so that we are now in physical units (?).
                     This has had a knock-on effect of changing the SNR thresholds upwards.
     2015-03-24: DA: V6. Version delivered today, for use in L2 generation. No shifts are now applied.
     		  An attempt is made to flag off-scale-high measurements, and add a confidence penalty here.
     2015-04-28: DA: Fixed for the L2 generation.  Off-scale high test uses SPICE altitudes and SZA to derive an expected density
                     If we're off significnatly from this value, then flagged as bad data.  This can maybe be improved in the future.
    2015-11-10   Remove the line to get the spice kernels, this is done in load

(See projects/maven/lpw/mvn_lpw_prd_w_act_density.pro)

## MVN_LPW_PRD_W_E12

[Previous Routine] [Next Routine] [List of Routines]

    PROCEDURE:   mvn_lpw_prd_w_E12

    Routine takes active and passive E12 data, and combines them into one tplot variable for L2-production.
    Act and Pas occur at different points in  the master cycle and so they never overlap in time.
    They may have different time steps.
    The error information and flag information is taking also into consideration information from other sources such as spacecraft atitude.
       The tplot variables mvn_lpw_act_E12 and mvn_lpw_pas_E12 must be loaded into tplot memory before running this routine.
       Spacecraft attitude and position information will also be used if present.

    INPUTS:

     - ext: file extension for the CDF file: L1a, L1b, L2

    KEYWORDS:

    EXAMPLE:
     mvn_lpw_prd_w_E12

    CREATED BY:   Chris Fowler  10-23-13
    FILE:         mvn_lpw_prd_w_E12.pro
    VERSION:      1.0
    LAST MODIFICATION:
    05-19-2014 CF: cleaned up layout, removed CDF save option (goes in a stand alone routine)

(See projects/maven/lpw/mvn_lpw_prd_w_e12.pro)

## MVN_LPW_PRD_W_E12_BURST

[Previous Routine] [Next Routine] [List of Routines]

    PROCEDURE:   mvn_lpw_prd_w_E12_burst

    Routine takes E12 burst data (lf/mf/hf) produce a L2-product.
    The error information and flag information is taking into consideration information from other sources such as spacecraft atitude.
       There are additional variables that need to be loaded
       Presently this routine do not go an grab them if they are missing.

    INPUTS:
         type                                   ;'lf','mf','hf'
        ext                                     ;'l1a' 'l1b' or 'l2'  what level of quality to produce ('l2' is full information to be archived)

    KEYWORDS:
     - make_cdf                                ;make one L2-cdf for the NASA DPS archive
        dir_cdf                                ; where to put the cdf file

    EXAMPLE:
     mvn_lpw_prd_w_E12_burst,type

    CREATED BY:   Laila Andersson 11-06-2013
    FILE:         mvn_lpw_prd_w_E12_burst.pro
    VERSION:      1.0
    LAST MODIFICATION:
     2014-05-22   L. Andersson   sigificant update and working
     2015-04-09   T. McEnulty - getting ready for L2 production

(See projects/maven/lpw/mvn_lpw_prd_w_e12_burst.pro)

## MVN_LPW_PRD_W_E12_CALIB

[Previous Routine] [Next Routine] [List of Routines]

(See projects/maven/lpw/mvn_lpw_prd_w_e12_calib.pro)

## MVN_LPW_PRD_W_N

[Previous Routine] [Next Routine] [List of Routines]

    PROCEDURE:   mvn_lpw_prd_w_n

    Takes active and passive HF spectra data and searches for the plasma line for L2-production.
    Act and Pas occur at different points in the master cycle so they never overlap in time.
    The error information and flag information is also taken into consideration as well as information from other sources such as spacecraft attitude.
    This routine is expected to be refined so that the preliminary density from the LP-sweep can guide the Langmuir line search in the spectra, not yet implemented
       The tplot variables mvn_lpw_spec_act_hf and mvn_lpw_spec_act_hf must be loaded into tplot memory before running this routine.
       There are additional variables that need to be loaded
       Presently this routine does not go and grab them if they are missing.

    INPUTS:
       ext                                     ;'l1a' 'l1b' or 'l2'  what level of quality to produce ('l2' is full information to be archived)

    KEYWORDS:

    EXAMPLE:
     mvn_lpw_prd_w_n

    CREATED BY:   Laila Andersson  03-30-15
    FILE:         mvn_lpw_prd_w_n.pro
    VERSION:      2.1
    LAST MODIFICATION:
     2014-05-22   L. Andersson   sigificant update and working
     2014-12-01   T. McEnulty  updated sort flag of merged arrays, commented out flags that aren't working
     2014-12-09   T. McEnulty added a flag of 80 with a decimal for the sorting (0.1 for act and 0.2 for pas)
     2014-12-12   T. McEnulty updated dlimit
     2015-01-08 T. McEnulty and L. Andersson - updated limit and dlimit to make tplot output easier to see
    2015 - 03-30   L. Andersson prep it for L2 production
    2015 - 05-25   L. Andersson update for L2 production

    ro mvn_lpw_prd_w_n,ext

    rint,'Running: mvn_lpw_prd_w_n', ' ',ext
    _routine    = SYSTIME(0)
    ers_prd     = 'version prd_w_n 2.1'  ; the version number of this routine

(See projects/maven/lpw/mvn_lpw_prd_w_n.pro)

## MVN_LPW_PRD_W_SPEC

[Previous Routine] [Next Routine] [List of Routines]

    PROCEDURE:   mvn_lpw_prd_w_spec

    Takes LF, MF, and HF spectra (from act or pas) and combines them into one tplot spectra for L2-production.
    The error information and flag information is taking also into consideration information from other sources such as spacecraft atitude.
       The tplot variables mvn_lpw_act_E12 and mvn_lpw_pas_E12 must be loaded into tplot memory before running this routine.
       There are additional variables that need to be loaded
       Presently this routine do not go an grab them if they are missing.

    INPUTS:
     - type                                    ;'act'or 'pas' which subcycle to work with
       ext                                     ;'l1a' 'l1b' or 'l2'  what level of quality to produce ('l2' is full information to be archived)

    EXAMPLE:
     mvn_lpw_prd_w_spec,type,ext

    CREATED BY:   Laila Andersson  03-30-15
    FILE:         mvn_lpw_prd_w_spec.pro
    VERSION:      1.2
    LAST MODIFICATION:
     2014-05-22   L. Andersson   sigificant update and working
     2015-03-30   L. Andersson    update to make the firs L2 production

(See projects/maven/lpw/mvn_lpw_prd_w_spec.pro)

## MVN_LPW_R_BLOCK16

[Previous Routine] [Next Routine] [List of Routines]

    FUNCTION:   mvn_lpw_r_block16
    PURPOSE:
     Decomutater for the compression for block=16 reading the datafile as WORD
     This routine is called by the mvn_lpw_r_header.pro

    USAGE:
      output=mvn_lpw_r_block16(data,ptr,nn_e,mask16,bin_c,index_arr,output)

    INPUTS:
                data            this should be the unsigned array of the read in file
                ptr             this is which element to start with in the data array and updated ptr
                nn_e            this is which bit to start with based on element 'ptr' and array 'data' and update nn_e
                mask16          this is to convert the value in the array 'data' to a 16-bit binary array
                bin_c           this is an index array to convert the bit's to a integer
                index_arr       this is an index array
                output          the result of the read resultinf in an array with values form the array 'data'
                                the size of output depends on the size initiated, mod of 16

    OUTPUTS:
      hsbm - array of [1024,*] or [4096,*], one column for each hunk
      p    - number of hunks, intended to go into variable p20, p21, or p22 in mvn_lpw_r_header

    KEYWORDS:

    CREATED BY:   2011
    FILE: mvn_lpw_r_block16.pro
    VERSION:   2.0
    ;LAST MODIFICATION: 2012 April 24 by: Laila
     ;140718 clean up for check out L. Andersson

(See projects/maven/lpw/mvn_lpw_r_block16.pro)

## MVN_LPW_R_BLOCK16_BYTE

[Previous Routine] [Next Routine] [List of Routines]

    FUNCTION:   mvn_lpw_r_block16_byte
    PURPOSE:
     Decomutater for the compression for block=16 reading the datafile as BYTE
     This routine is called by the mvn_lpw_r_header_l0.pro

    USAGE:
      output=mvn_lpw_r_block16_byte(data,ptr,nn_e,mask16,bin_c,index_arr,output)

    INPUTS:
                data            this should be the unsigned array of the read in file
                ptr             this is which element to start with in the data array and updated ptr
                nn_e            this is which bit to start with based on element 'ptr' and array 'data' and update nn_e
                mask8           this is to convert the value in the array 'data' to a 8-bit binary array
                bin_c           this is an index array to convert the bit's to a integer
                index_arr       this is either 16 or 32. Set fix when calling on the routine
                edac_on:        constains information if errors was found in the de-compression
    OUTPUTS
                output:         the result of the read resulting in an array with values form the array 'data'
      hsbm - array of [1024,*] or [4096,*], one column for each hunk
      p    - number of hunks, intended to go into variable p20, p21, or p22 in mvn_lpw_r_header

    KEYWORDS:

    CREATED BY:   2011
    FILE: mvn_lpw_r_block16_byte.pro
    VERSION:   2.0
     Changes: this routine originally was for a word-array and modified for L0-data 2013 May 10 by Laila Andersson
    ;LAST MODIFICATION: 2014 Jannuary 05 by: Laila
     ;140718 clean up for check out L. Andersson

(See projects/maven/lpw/mvn_lpw_r_block16_byte.pro)

## MVN_LPW_R_BLOCK32

[Previous Routine] [Next Routine] [List of Routines]

    FUNCTION:   mvn_lpw_r_block32
    PURPOSE:
     Decomutater for the compression for block=32 reading the datafile as WORD
     This routine is called by the mvn_lpw_r_header.pro

    USAGE:
      output=mvn_lpw_r_block32(data,ptr,nn_e,mask16,bin_c,index_arr,output)

    INPUTS:
                data            this should be the unsigned array of the read in file
                ptr             this is which element to start with in the data array and updated ptr
                nn_e            this is which bit to start with based on element 'ptr' and array 'data' and update nn_e
                mask16          this is to convert the value in the array 'data' to a 16-bit binary array
                bin_c           this is an index array to convert the bit's to a integer
                index_arr       this is an index array
                output          the result of the read resultinf in an 128 point array with values form the array 'data'

    OUTPUTS:
      hsbm - array of [1024,*] or [4096,*], one column for each hunk
      p    - number of hunks, intended to go into variable p20, p21, or p22 in mvn_lpw_r_header

    KEYWORDS:

    CREATED BY:   2011
    FILE: mvn_lpw_r_block32.pro
    VERSION:   2.0
    ;LAST MODIFICATION: 2012 April 24 by: Laila
     ;140718 clean up for check out L. Andersson

(See projects/maven/lpw/mvn_lpw_r_block32.pro)

## MVN_LPW_R_BLOCK32_BYTE

[Previous Routine] [Next Routine] [List of Routines]

    FUNCTION:   mvn_lpw_r_block32_byte
    PURPOSE:
     Decomutater for the compression for block=32  reading the data file as BYTE

    USAGE:
      output=mvn_lpw_r_block32_byte(data,ptr,nn_e,mask16,bin_c,index_arr,output)

    INPUTS:
                data            this should be the unsigned array of the read in file
                ptr             this is which element to start with in the data array and updated ptr
                nn_e            this is which bit to start with based on element 'ptr' and array 'data' and update nn_e
                mask16          this is to convert the value in the array 'data' to a 16-bit binary array
                bin_c           this is an index array to convert the bit's to a integer
                index_arr       this is an index array
                output          the result of the read resultinf in an 128 point array with values form the array 'data'

    OUTPUTS:
      hsbm - array of [1024,*] or [4096,*], one column for each hunk
      p    - number of hunks, intended to go into variable p20, p21, or p22 in mvn_lpw_r_header

    KEYWORDS:

    CREATED BY:   2011
    FILE: mvn_lpw_r_block32.pro
    VERSION:   1.1
    ; Changes: this routine originally was for a word-array and modified for L0-data 2013 May 10 by Laila Andersson
    ;LAST MODIFICATION: 2014 Jannuary 05 by: Laila
    Got the 'IF nd EQ 16 ' option correct

(See projects/maven/lpw/mvn_lpw_r_block32_byte.pro)

## MVN_LPW_R_CLOCK_CHECK

[Previous Routine] [Next Routine] [List of Routines]

    Program written by Chris Fowler on 2014-06-06 to check for clock jitter in the maven s/c clock.

    USAGE: mvn_lpw_r_clock_check, packet_name, packet_arr, SC, sc_clk1, sc_clk2

    EXAMPLE: mvn_lpw_r_clock_check, 'PAS_HF', pkt_PAS_HF, SC, sc_clk1, sc_clk2

    INPUTS:

     packet_name: a string of the name of the packet you want to check: Acceptable packet names are:
                 'HSK'
                 'EUV'
                 'SWP1'
                 'SWP2'
                 'ACT'
                 'PAS'
                 'ACT_LF'
                 'ACT_MF'
                 'ACT_HF'
                 'PAS_LF'
                 'PAS_MF'
                 'PAS_HF'

     packet_arr: the array containing information for the desired packet. Acceptable packets are:
                 pkt_HSK
                 pkt_EUV
                 pkt_SWP1
                 pkt_SWP2
                 pkt_ACT
                 pkt_PAS
                 pkt_ACT_LF
                 pkt_ACT_MF
                 pkt_ACT_HF
                 pkt_PAS_LF
                 pkt_PAS_MF
                 pkt_PAS_HF

     SC: sequence counter: the array containing packet numbers in the order they are loaded, to check for missing packets.

     sc_clk1: MAVEN s/c second ticks
     sc_clk2: MAVEN s/c sub second ticks (note, these are s/c clock ticks, NOT decimal seconds)

    OUTPUTS:
     sc_clk1 and sc_clk2 will be edited to correct for the ~0.5s clock jitter in the MAVEN s/c clock.

    CREATED: Chris Fowler, 2014, June 6th

     ### routine still under construction - need to add checks

     Version 1.0
     ;140718 clean up for check out L. Andersson
     ;140916: CF: fixed issue where timesteps just over the mod 4 limit (eg 16.000015 for EUV) were triggering false positives. Added in lower limit to correct this.
     ;150119: CF: added common block to store times that the clock jumps, so they can be saved for statistics.

(See projects/maven/lpw/mvn_lpw_r_clock_check.pro)

## MVN_LPW_R_GROUP_HSBM

[Previous Routine] [Next Routine] [List of Routines]

    PROCEDURE:   mvn_lpw_r_group_hsbm
    PURPOSE:
    Group together the data from several HSBM packets into one hunk of data

    Notes:
      A hunk is the data from several packets which all have the same timestamp.

      The program presumes that the packets forming one hunk are in order in comp_p, but not necessarily
      consecutive.

      If the hunk is longer than expected, this is an error.

      If the hunk is shorter, it may not be an error. The program will attempt to pad the hunk, at the beginning
      if it is the first hunk (presuming that the first packet(s) did not make it into the data stream), at the
      end otherwise.

    USAGE:
      mvn_lpw_r_group_hsbm,comp_t_,comp_p,length,hsbm,p,index_i

    INPUTS:
      comp_t - array of doubles representing the timestamp, calculated as sec+subsec/2^16
      comp_p - array of pointers to data from HSBM packets
      length - expected length of each hunk of HSBM data, 1024 for LF and 4096 for MF and HF

    OUTPUTS:
      hsbm - array of [1024,*] or [4096,*], one column for each hunk
      p    - number of hunks, intended to go into variable p20, p21, or p22 in mvn_lpw_r_header

    KEYWORDS:

    CREATED BY:   2011
    FILE: mvn_lpw_r_group_hsbm.pro
    VERSION:   2.0
    ;LAST MODIFICATION:
     ;140718 clean up for check out L. Andersson

(See projects/maven/lpw/mvn_lpw_r_group_hsbm.pro)

## MVN_LPW_R_HEADER

[Previous Routine] [Next Routine] [List of Routines]

    PROCEDURE:   mvn_lpw_r_header
    PURPOSE:
      Decomutater of the LPW telemetry data, this is the old mvn_lpw_r_header THIS IS NOT WORKING ON L0-files
              r_header orignal written by Corinne Vanatta and David Meyer;
              This routine reads the data file as WORD
     This routine strips the packet headers off the data, and stores the data in various ways.
     The data is stored in one long structure
     The routine also prints out how many of each tpye of packet are present in the file.
     filename: the name of the file the data is in
     output:   this is a dummy variable input, but output holds all the data for
               the subsequent plotting routines
     samplerate: Sample rate in Hz. Used to get time, as well as units on subsequent plots

    USAGE:
      mvn_lpw_r_header, filename, output,strip_pad = strip_pad, wrapper = wrapper, compressed=compressed, packet=packet

    INPUTS:
           filename:      The full filename (including path) of a binary file containing
                          zero or more LPW APID's.

    KEYWORDS:
           strip_pad:  Default? -not in use ?? test purpuses
           wrapper:    Default? -not in use ?? test purpuses
           compressed: Default compressed. For test purpuses the data stream can be uncompressed by the FPGA.
           packet:     Which packets to read into memeory, default all packets Options: ['HSK','EUV','AVG','SPEC','HSBM','WPK']

    CREATED BY:   Laila Andersson  06-01-11
    FILE: mvn_lpw_r_header.pro
    VERSION:   2.0
    LAST MODIFICATION:   04/20/13
     ;140718 clean up for check out L. Andersson

(See projects/maven/lpw/mvn_lpw_r_header.pro)

## MVN_LPW_R_HEADER_L0

[Previous Routine] [Next Routine] [List of Routines]

    PROCEDURE:   mvn_lpw_r_header_l0
    PURPOSE:
      Decomutater of the LPW telemetry data, THIS IS FOR L0-files, splitted files and gourn data use mvn_lpw_r_heared.pro
              r_header orignal written by Corinne Vanatta and David Meyer
              This routine reads the data file as BYTE
     This routine strips the packet headers off the data, and stores the data in various ways.
     The data is stored in one long structure
     The routine also prints out how many of each tpye of packet are present in the file.
     filename: the name of the file the data is in
     No WPK-packet will be in a L0-file and the data is always compressed
     SC gitter/clitch is corrected for
     More stricter rules to find the packets

    USAGE:
      mvn_lpw_r_header, filename, output, packet=packet

    INPUTS:
           filename:      The full filename (including path) of a binary file containing
                          zero or more LPW APID's.

    KEYWORDS:
           packet:     Which packets to read into memeory, default all packets Options: ['HSK','EUV','AVG','SPEC','HSBM']

    CREATED BY:   Laila Andersson  06-01-11
    FILE: mvn_lpw_r_header_l0.pro
    VERSION:   2.0
    VERSION:   2.0
    LAST MODIFICATION:  2019-12-30  L. Andersson
    2019-12-30 bad sc clock is removed but was time limeted to ~jan 2020, this is now expanded on
    140718 FIs so that HSBM packets can be read
    Added the correction features for the 0.5 SC clock gitter  (C Fowler)
    Added a additional feature to find the packets using the SC variable (L. Andersson)
    ;140718 clean up for check out L. Andersson
    2014-10-06: CF: 'packet' can now be uppper or lower input; mvn-lpw-load-file converts all to upper case.

(See projects/maven/lpw/mvn_lpw_r_header_l0.pro)

## MVN_LPW_R_MASK

[Previous Routine] [Next Routine] [List of Routines]

    PROCEDURE:   mvn_lpw_r_mask
    PURPOSE:
          This procedure only defines different masks
           so that 8 and 16 bit numbers can be
           quickly decomutated
           some of these masks are no longer used
           used by mvn_lpw_pkt_r_header.pro and mvn_lpw_pkt_r_header_l0.pro

    USAGE:
      mvn_lpw_r_mask,mask16,mask8,bin_c,index_arr,flip_8

    INPUTS:
           r_mask:
           mask16:     array to convert 16-bite (word)
           mask8:      array to convert 8-bite (byte)
           bin_c:
           index_arr:
           flip_8:

    KEYWORDS:

    CREATED BY:   Laila Andersson 17 august 2011
    FILE: mvn_lpw_r_mask.pro
    VERSION:   2.0
    ;LAST MODIFICATION:   05/16/13
     ;140718 clean up for check out L. Andersson

(See projects/maven/lpw/mvn_lpw_r_mask.pro)

## MVN_LPW_SAVE_L0

[Previous Routine] [List of Routines]

    Wrapper to run mvn_lpw_load_l0 for a single day, and create tplot save files for each day. To be used at SSL to create local tplot
    save files of LPW l0 data, to avoid the long load times.

    date: string: 'yyyy-mm-dd': input date to load LPW L0 data for.

    Output: routine saves a tplot file containing most useful LPW L0 data products, with the name 'mvn_lpw_tplot_l0_'+date+'.tplot'.

    Not all LPW L0 data products are saved - the total file size is ~400 mb per day with all products. Only the most useful for comparison
    with STATIC are saved to reduce file sizes.

(See projects/maven/lpw/mvn_lpw_save_l0.pro)