<commit_before>import functools
import math
import operator
import os
import logging
import numpy as np

logger = logging.getLogger(__name__)


from hyo.soundspeed.formats.writers.abstract import AbstractTextWriter
from hyo.soundspeed.profile.dicts import Dicts
from hyo.soundspeed.profile.oceanography import Oceanography as Oc


class Asvp(AbstractTextWriter):
    """Kongsberg asvp writer"""

    abs_freqs = [12, 32, 60, 70, 80, 90, 95, 100, 200, 250, 300, 350, 400]

    def __init__(self):
        super(Asvp, self).__init__()
        self.desc = "Konsgberg asvp"
        self._ext.add('asvp')
        self.header = None  # required for checksum

    def write(self, ssp, data_path, data_file=None, project=''):
        logger.debug('*** %s ***: start' % self.driver)

        self.ssp = ssp
        self._write(data_path=data_path, data_file=data_file)

        self._write_header()
        self._write_body()

        self.finalize()

        # this part write the absorption files only if the temp and the sal are present
        ti = self.ssp.cur.sis_thinned
        if (np.sum(self.ssp.cur.sis.temp[ti]) != 0) and (np.sum(self.ssp.cur.sis.sal[ti]) != 0) \
                and (np.sum(self.ssp.cur.sis.speed[ti]) != 0):

            asvp_base_name = self.fod.basename
            for abs_freq in self.abs_freqs:
                abs_file = "%s_%dkHz.abs" % (asvp_base_name, abs_freq)
                self._write(data_path=data_path, data_file=abs_file)
                self._write_header_abs(abs_freq)
                self._write_body_abs(abs_freq)

        else:
            logger.warning("not temperature and/or salinity to create absorption files")

        logger.debug('*** %s ***: done' % self.driver)
        return True

    def _write_header(self):
        logger.debug('generating header')
        header = self._convert_header(fmt=Dicts.kng_formats['ASVP'])
        self.fod.io.write(header)
        return header

    def _write_body(self):
        logger.debug('generating body')
        body = self._convert_body(fmt=Dicts.kng_formats['ASVP'])
        self.fod.io.write(body)
        self.fod.io.close()

    def _write_header_abs(self, freq):
        logger.debug('generating header for %d kHz' % freq)

        ti = self.ssp.cur.sis_thinned

        # e.g., ( Absorption  1.0 0 201203212242 22.50000000 -156.50000000 -1 0 0 OMS01_00000 P 0035 )
        abs_header = "( Absorption  1.0 0 "
        abs_header += self.ssp.cur.meta.utc_time.strftime("%Y%m%d%H%M ")
        abs_header += "%.8f %.8f -1 0 0 OMS01_00000 P %04d )\n" \
                       % (self.ssp.cur.meta.latitude,
                          self.ssp.cur.meta.longitude,
                          self.ssp.cur.sis.depth[ti].size)
        self.fod.io.write(abs_header)

    def _write_body_abs(self, freq):
        logger.debug('generating body for %d kHz' % freq)

        ti = self.ssp.cur.sis_thinned

        top_mean = 0
        bottom_mean = 0

        body = str()
        sample_sz = np.sum(ti)
        for i in range(sample_sz):

            if self.ssp.cur.sis.sal[ti][i] <= 0:
                logger.info("skipping invalid salinity")
                continue

            abs = Oc.attenuation(f=freq, t=self.ssp.cur.sis.temp[ti][i], d=self.ssp.cur.sis.depth[ti][i],
                                 s=self.ssp.cur.sis.sal[ti][i], ph=8.1)

            if i == 0:  # first
                delta = (self.ssp.cur.sis.depth[ti][0] + self.ssp.cur.sis.depth[ti][1]) / 2.0

            elif i == sample_sz - 1:  # last
                delta = (self.ssp.cur.sis.depth[ti][sample_sz - 1] -
                         (self.ssp.cur.sis.depth[ti][sample_sz - 1] + self.ssp.cur.sis.depth[ti][sample_sz - 2]) / 2.0)

            else:
                delta = ((self.ssp.cur.sis.depth[ti][i + 1] + self.ssp.cur.sis.depth[ti][i]) / 2.0 -
                         (self.ssp.cur.sis.depth[ti][i] + self.ssp.cur.sis.depth[ti][i - 1]) / 2.0)

            top_mean += abs * delta
            bottom_mean += delta

            mean_abs = top_mean / bottom_mean

            body += "%.3f %.3f %.3f %s\n" \
                    % (self.ssp.cur.sis.depth[ti][i], abs, mean_abs, "999.000")

        self.fod.io.write(body)
        self.fod.io.close()

    def convert(self, ssp, fmt, send_proc=False):
        """Convert a profile in a given Kongsberg format"""
        self.ssp = ssp

        header = self._convert_header(fmt, send_proc)
        body = self._convert_body(fmt, send_proc)

        return header + body

    def _convert_header(self, fmt, send_proc):
        self.header = self.get_km_prefix(fmt)  # start with the format prefix
        if send_proc:
            ti = self.ssp.cur.proc_valid
            ti_size = self.ssp.cur.proc.depth[ti].size
        else:
            ti = self.ssp.cur.sis_thinned
            ti_size = self.ssp.cur.sis.depth[ti].size

        if fmt != Dicts.kng_formats['ASVP']:
            self.header += "%04d," % ti_size
            self.header += self.ssp.cur.meta.utc_time.strftime("%H%M%S,%d,%m,%Y,")

        else:
            # e.g., ( SoundVelocity  1.0 0 201203212242 22.50000000 -156.50000000 -1 0 0 MVS01_00000 P 0035 )
            self.header += "( SoundVelocity  1.0 0 "
            self.header += self.ssp.cur.meta.utc_time.strftime("%Y%m%d%H%M ")
            self.header += "%.8f %.8f -1 0 0 OMS01_00000 P %04d )\n" \
                           % (self.ssp.cur.meta.latitude,
                              self.ssp.cur.meta.longitude,
                              ti_size)
        return self.header

    def _convert_body(self, fmt, send_proc):
        body = str()
        if send_proc:
            ti = self.ssp.cur.proc_valid
            depths = self.ssp.cur.proc.depth[ti]
            speeds = self.ssp.cur.proc.speed[ti]
            temps = self.ssp.cur.proc.temp[ti]
            sals = self.ssp.cur.proc.sal[ti]
        else:
            ti = self.ssp.cur.sis_thinned
            depths = self.ssp.cur.sis.depth[ti]
            speeds = self.ssp.cur.sis.speed[ti]
            temps = self.ssp.cur.sis.temp[ti]
            sals = self.ssp.cur.sis.sal[ti]

        for i in range(int(np.sum(ti))):
            if (fmt == Dicts.kng_formats['S00']) or (fmt == Dicts.kng_formats['S10']):
                body += "%.2f,%.1f,,,\r\n" % (depths[i], speeds[i])<commit_msg>bug fix after having changes the asvp writer<commit_after>import functools
import math
import operator
import os
import logging
import numpy as np

logger = logging.getLogger(__name__)


from hyo.soundspeed.formats.writers.abstract import AbstractTextWriter
from hyo.soundspeed.profile.dicts import Dicts
from hyo.soundspeed.profile.oceanography import Oceanography as Oc


class Asvp(AbstractTextWriter):
    """Kongsberg asvp writer"""

    abs_freqs = [12, 32, 60, 70, 80, 90, 95, 100, 200, 250, 300, 350, 400]

    def __init__(self):
        super(Asvp, self).__init__()
        self.desc = "Konsgberg asvp"
        self._ext.add('asvp')
        self.header = None  # required for checksum

    def write(self, ssp, data_path, data_file=None, project=''):
        logger.debug('*** %s ***: start' % self.driver)

        self.ssp = ssp
        self._write(data_path=data_path, data_file=data_file)

        self._write_header()
        self._write_body()

        self.finalize()

        # this part write the absorption files only if the temp and the sal are present
        ti = self.ssp.cur.sis_thinned
        if (np.sum(self.ssp.cur.sis.temp[ti]) != 0) and (np.sum(self.ssp.cur.sis.sal[ti]) != 0) \
                and (np.sum(self.ssp.cur.sis.speed[ti]) != 0):

            asvp_base_name = self.fod.basename
            for abs_freq in self.abs_freqs:
                abs_file = "%s_%dkHz.abs" % (asvp_base_name, abs_freq)
                self._write(data_path=data_path, data_file=abs_file)
                self._write_header_abs(abs_freq)
                self._write_body_abs(abs_freq)

        else:
            logger.warning("not temperature and/or salinity to create absorption files")

        logger.debug('*** %s ***: done' % self.driver)
        return True

    def _write_header(self):
        logger.debug('generating header')
        header = self._convert_header(fmt=Dicts.kng_formats['ASVP'])
        self.fod.io.write(header)
        return header

    def _write_body(self):
        logger.debug('generating body')
        body = self._convert_body(fmt=Dicts.kng_formats['ASVP'])
        self.fod.io.write(body)
        self.fod.io.close()

    def _write_header_abs(self, freq):
        logger.debug('generating header for %d kHz' % freq)

        ti = self.ssp.cur.sis_thinned

        # e.g., ( Absorption  1.0 0 201203212242 22.50000000 -156.50000000 -1 0 0 OMS01_00000 P 0035 )
        abs_header = "( Absorption  1.0 0 "
        abs_header += self.ssp.cur.meta.utc_time.strftime("%Y%m%d%H%M ")
        abs_header += "%.8f %.8f -1 0 0 OMS01_00000 P %04d )\n" \
                       % (self.ssp.cur.meta.latitude,
                          self.ssp.cur.meta.longitude,
                          self.ssp.cur.sis.depth[ti].size)
        self.fod.io.write(abs_header)

    def _write_body_abs(self, freq):
        logger.debug('generating body for %d kHz' % freq)

        ti = self.ssp.cur.sis_thinned

        top_mean = 0
        bottom_mean = 0

        body = str()
        sample_sz = np.sum(ti)
        for i in range(sample_sz):

            if self.ssp.cur.sis.sal[ti][i] <= 0:
                logger.info("skipping invalid salinity")
                continue

            abs = Oc.attenuation(f=freq, t=self.ssp.cur.sis.temp[ti][i], d=self.ssp.cur.sis.depth[ti][i],
                                 s=self.ssp.cur.sis.sal[ti][i], ph=8.1)

            if i == 0:  # first
                delta = (self.ssp.cur.sis.depth[ti][0] + self.ssp.cur.sis.depth[ti][1]) / 2.0

            elif i == sample_sz - 1:  # last
                delta = (self.ssp.cur.sis.depth[ti][sample_sz - 1] -
                         (self.ssp.cur.sis.depth[ti][sample_sz - 1] + self.ssp.cur.sis.depth[ti][sample_sz - 2]) / 2.0)

            else:
                delta = ((self.ssp.cur.sis.depth[ti][i + 1] + self.ssp.cur.sis.depth[ti][i]) / 2.0 -
                         (self.ssp.cur.sis.depth[ti][i] + self.ssp.cur.sis.depth[ti][i - 1]) / 2.0)

            top_mean += abs * delta
            bottom_mean += delta

            mean_abs = top_mean / bottom_mean

            body += "%.3f %.3f %.3f %s\n" \
                    % (self.ssp.cur.sis.depth[ti][i], abs, mean_abs, "999.000")

        self.fod.io.write(body)
        self.fod.io.close()

    def convert(self, ssp, fmt, send_proc=False):
        """Convert a profile in a given Kongsberg format"""
        self.ssp = ssp

        header = self._convert_header(fmt, send_proc)
        body = self._convert_body(fmt, send_proc)

        return header + body

    def _convert_header(self, fmt, send_proc=False):
        self.header = self.get_km_prefix(fmt)  # start with the format prefix
        if send_proc:
            ti = self.ssp.cur.proc_valid
            ti_size = self.ssp.cur.proc.depth[ti].size
        else:
            ti = self.ssp.cur.sis_thinned
            ti_size = self.ssp.cur.sis.depth[ti].size

        if fmt != Dicts.kng_formats['ASVP']:
            self.header += "%04d," % ti_size
            self.header += self.ssp.cur.meta.utc_time.strftime("%H%M%S,%d,%m,%Y,")

        else:
            # e.g., ( SoundVelocity  1.0 0 201203212242 22.50000000 -156.50000000 -1 0 0 MVS01_00000 P 0035 )
            self.header += "( SoundVelocity  1.0 0 "
            self.header += self.ssp.cur.meta.utc_time.strftime("%Y%m%d%H%M ")
            self.header += "%.8f %.8f -1 0 0 OMS01_00000 P %04d )\n" \
                           % (self.ssp.cur.meta.latitude,
                              self.ssp.cur.meta.longitude,
                              ti_size)
        return self.header

    def _convert_body(self, fmt, send_proc=False):
        body = str()
        if send_proc:
            ti = self.ssp.cur.proc_valid
            depths = self.ssp.cur.proc.depth[ti]
            speeds = self.ssp.cur.proc.speed[ti]
            temps = self.ssp.cur.proc.temp[ti]
            sals = self.ssp.cur.proc.sal[ti]
        else:
            ti = self.ssp.cur.sis_thinned
            depths = self.ssp.cur.sis.depth[ti]
            speeds = self.ssp.cur.sis.speed[ti]
            temps = self.ssp.cur.sis.temp[ti]
            sals = self.ssp.cur.sis.sal[ti]

        for i in range(int(np.sum(ti))):
            if (fmt == Dicts.kng_formats['S00']) or (fmt == Dicts.kng_formats['S10']):
                body += "%.2f,%.1f,,,\r\n" % (depths[i], speeds[i])
{'fraction_non_alphanumeric': 0.13758203526528032, 'fraction_numerical': 0.03289317624733138, 'mean_word_length': 1.8732394366197183, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 8, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3384260', 'n_tokens_mistral': 4769, 'n_tokens_neox': 4370, 'n_words': 832}