#!/bin/sh
# unpack-lrauv-logs.sh

FLOCK=/usr/bin/flock
LOCKDIR=/tmp

# ... only want one unpack or unserialize instance running at a time (for now)

#TODO make the locations below more portable
DATA_ROOT=/mbari/LRAUV
LRAUV=/home/tethysadmin/workspace/LRAUV
UNSERIALIZE=/mbari/LRAUV/Tools/linuxi64/unserialize

# TODO define vars in a separate file and pull them in? or just move to a python script...
MINVARS="latitude longitude depth \
    platform_orientation platform_pitch_angle platform_roll_angle \
    platform_speed_wrt_sea_water platform_speed_wrt_ground \
    platform_mass_position platform_buoyancy_position \
    platform_rudder_angle platform_elevator_angle \
    sea_floor_depth_below_geoid height_above_sea_floor \
    "
# explicitly leave top-level 'time' variable out of .mat files

SCIVARS="sea_water_temperature sea_water_salinity sea_water_density\
	sea_water_pressure sea_water_electrical_conductivity \
    upward_derivative_of_sea_water_temperature \
    mass_concentration_of_chlorophyll_in_sea_water \
    WetLabsBB2FL.OutputChl \
    volume_scattering_470_nm volume_scattering_650_nm \
    WetLabsBB2FL.Output470 WetLabsBB2FL.VolumeScatCoeff117deg470nm \
    WetLabsBB2FL.Output650 WetLabsBB2FL.VolumeScatCoeff117deg650nm \
    mass_concentration_of_oxygen_in_sea_water \
    mole_concentration_of_nitrate_in_sea_water \
    downwelling_photosynthetic_photon_flux_in_sea_water \
    PAR_Licor.downwelling_photosynthetic_photon_flux_in_sea_water \
    PAR_Licor.adcCount \
    sea_water_rhodamine rhodamine.voltage \
    "

LQVARS="DVL_micro.height_above_sea_floor \
    DVL_micro.AdcpNumBins DVL_micro.AdcpStartDepth \
    DVL_micro.AdcpXWaterVelocity \
    DVL_micro.AdcpYWaterVelocity \
    DVL_micro.AdcpZWaterVelocity \
    DVL_micro.RSSIFilterLength DVL_micro.RSSIPoints \
    DVL_micro.Beam1RSSI \
    DVL_micro.Beam2RSSI \
    DVL_micro.Beam3RSSI \
    DVL_micro.Beam4RSSI \
    "

ADCPVARS="Rowe_600.height_above_sea_floor \
    Rowe_600.soundspeed Rowe_600.vertical_range Rowe_600.signal_to_noise \
    Rowe_600.bottom_track_amplitude Rowe_600.bottom_track_correlation \
    Rowe_600.bottom_track_beam_velocity Rowe_600.bottom_track_instrument_velocity \
    Rowe_600.ensemble_number Rowe_600.payload_size Rowe_600.ensemble_timestamp \
    Rowe_600.beam_velocity Rowe_600.instrument_velocity Rowe_600.earth_velocity \
    Rowe_600.amplitude Rowe_600.correlation \
    Rowe_600.good_beam_pings Rowe_600.good_earth_pings \
    "

TURBVARS="Turbulence_NPS.MicroTemp1 Turbulence_NPS.MicroTemp2 \
    Turbulence_NPS.MicroCond \
    Turbulence_NPS.NumTempCondSamples \
    Turbulence_NPS.ACMPath \
    Turbulence_NPS.platform_roll_angle \
    Turbulence_NPS.platform_pitch_angle \
    Turbulence_NPS.platform_orientation \
    Turbulence_NPS.DeltaXVelocity \
    Turbulence_NPS.DeltaYVelocity \
    Turbulence_NPS.DeltaZVelocity \
    Turbulence_NPS.FwdSigStrength \
    Turbulence_NPS.packetNum \
    "


#TODO there is almost definitely a better way to do the task below
# TODO add dlists and run for 2010 & 2011
for year in devel 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023
do
  for vehicle in tethys daphne makai ahi aku opah whoidhs galene brizo pontus triton
  do
    lck=$LOCKDIR/handle-lrauv-logs.$vehicle.lck  # don't unpack during push or pull
    for d in `ls -d "$DATA_ROOT"/"$vehicle"/missionlogs/"$year"/*/*`
    do
      echo "checking for slate in $d"
      if [ -f "$d"/slate0000 ]; # only continue if slate0000 is already there
      then
        echo "unpacking logs from $d"

	echo "find output: $(find $d -maxdepth 1 -name 20*.mat -print -quit)"
        if test -n "$(find $d -maxdepth 1 -name 20*.mat -print -quit)"
        then
          echo "20*.mat already exists in $d, skipping"
	else
          MSG="$d/20*.mat does not exist, unpacking"
          CMD="nice $UNSERIALIZE -m $d/slate"
          $FLOCK -n -e $lck -c "echo $MSG && echo $CMD && $CMD"
        fi

	echo "find output: $(find $d -maxdepth 1 -name science_20*.mat -print -quit)"
        if test -n "$(find $d -maxdepth 1 -name science_20*.mat -print -quit)"
        then
          echo "science_20*.mat already exists in $d, skipping"
	else
          MSG="$d/science_20*.mat does not exist, unpacking"
          CMD="nice $UNSERIALIZE -m -o $d/science.mat $d/slate $MINVARS $SCIVARS $ADCPVARS"
          $FLOCK -n -e $lck -c "echo $MSG && echo $CMD && $CMD"
        fi

	echo "find output: $(find $d -maxdepth 1 -name 20*.nc4 -print -quit)"
        if test -n "$(find $d -maxdepth 1 -name 20*.nc4 -print -quit)"
        then
          echo "20*.nc4 already exists in $d, skipping"
	else
          MSG="$d/20*.nc4 does not exist, unpacking"
          CMD="nice $UNSERIALIZE -N $d/slate"
          $FLOCK -n -e $lck -c "echo $MSG && echo $CMD && $CMD"
        fi

        echo "unpacked logs from $d"

      else
        echo "did not find slate"
      fi
    done
    #TODO zip unpacked deployment logs together
  done
done
