Esempio n. 1
0
    def __init__(
        self,
        filename,
        freqs=None,
        dirs=None,
        x=None,
        y=None,
        time=False,
        id="Swan Spectrum",
        dirorder=False,
        append=False,
        tabfile=None,
    ):
        self.times = False
        self.filename = filename
        self.tabfile = (
            tabfile
            or os.path.splitext(self.filename.replace(".gz", ""))[0] + ".tab")
        self.is_tab = False
        self.buf = None

        extention = os.path.splitext(self.filename)[-1]
        if extention == ".gz":
            fopen = gzip.open
        else:
            fopen = open
        if freqs is not None:  # Writable file
            self.freqs = np.array(freqs)
            self.dirs = np.array(dirs)
            self.x = np.array(x)
            self.y = np.array(y)
            if time:
                self.times = []
            self.fid = fopen(filename, "w")
            self.write_header(time, id)
            self.fmt = len(self.dirs) * "{:5.0f}"
        else:
            self.fid = fopen(filename, "r+" if append else "r")
            header = self._read_header("SWAN")
            while True:
                if not self._read_header("$"):
                    break
            if self._read_header("TIME"):
                self._read_header("1")
                self.times = []
            self.x = []
            self.y = []
            locs = self._read_header("LONLAT", True) or self._read_header(
                "LOCATIONS", True)
            for ip in locs:
                xy = [float(val) for val in ip.split()]
                self.x.append(xy[0])
                self.y.append(xy[1])
            self.x = np.array(self.x)
            self.y = np.array(self.y)
            self.afreq = self._read_header("AFREQ", True)
            self.rfreq = self._read_header("RFREQ", True)
            self.ndir = self._read_header("NDIR", True)
            self.cdir = self._read_header("CDIR", True)
            if self.afreq:
                self.freqs = np.array([float(val) for val in self.afreq])
            else:
                self.freqs = np.array([float(val) for val in self.rfreq])
            if self.ndir:
                self.dirs = np.array([float(val) for val in self.ndir])
            else:
                self.dirs = to_nautical(
                    np.array([float(val) for val in self.cdir]))
            self._read_header("QUANT", True)
            # Figure units out, if Energy density factor needs to be applied
            units = self.fid.readline().strip().split()[0]
            if units.upper().startswith("J"):
                self.units_factor = E2V
            else:
                self.units_factor = 1.0
            self.excval = int(float(self.fid.readline().split()[0]))

        if dirorder:
            self.dirmap = list(np.argsort(self.dirs % 360.0))
            self.dirs = self.dirs[self.dirmap] % 360.0
        else:
            self.dirmap = False
        lons = np.unique(self.x)
        lats = np.unique(self.y)
        self.is_grid = len(lons) * len(lats) == len(self.x)
        self.is_tab = (os.path.isfile(self.tabfile)) & (len(lons) * len(lats)
                                                        == 1)
Esempio n. 2
0
    def __init__(self,
                 filename,
                 freqs=None,
                 dirs=None,
                 x=None,
                 y=None,
                 time=False,
                 id='Swan Spectrum',
                 dirorder=False,
                 append=False,
                 tabfile=None):
        self.times = False
        self.filename = filename
        self.tabfile = tabfile or os.path.splitext(
            self.filename.replace('.gz', ''))[0] + '.tab'
        self.is_tab = False
        self.buf = None

        extention = os.path.splitext(self.filename)[-1]
        if extention == '.gz':
            fopen = gzip.open
        else:
            fopen = open
        if freqs is not None:  # Writable file
            self.freqs = np.array(freqs)
            self.dirs = np.array(dirs)
            self.x = np.array(x)
            self.y = np.array(y)
            if time:
                self.times = []
            self.fid = fopen(filename, 'w')
            self.write_header(time, id)
            self.fmt = len(self.dirs) * '{:5.0f}'
        else:
            self.fid = fopen(filename, 'r+' if append else 'r')
            header = self._read_header('SWAN')
            while True:
                if not self._read_header('$'):
                    break
            if self._read_header('TIME'):
                self._read_header('1')
                self.times = []
            self.x = []
            self.y = []
            locs = self._read_header('LONLAT', True) or self._read_header(
                'LOCATIONS', True)
            for ip in locs:
                xy = [float(val) for val in ip.split()]
                self.x.append(xy[0])
                self.y.append(xy[1])
            self.x = np.array(self.x)
            self.y = np.array(self.y)
            self.afreq = self._read_header('AFREQ', True)
            self.rfreq = self._read_header('RFREQ', True)
            self.ndir = self._read_header('NDIR', True)
            self.cdir = self._read_header('CDIR', True)
            if self.afreq:
                self.freqs = np.array([float(val) for val in self.afreq])
            else:
                self.freqs = np.array([float(val) for val in self.rfreq])
            if self.ndir:
                self.dirs = np.array([float(val) for val in self.ndir])
            else:
                self.dirs = to_nautical(
                    np.array([float(val) for val in self.cdir]))
            self._read_header('QUANT', True)
            # Figure units out, if Energy density factor needs to be applied
            units = self.fid.readline().strip().split()[0]
            if units.upper().startswith('J'):
                self.units_factor = E2V
            else:
                self.units_factor = 1.
            self.excval = int(float(self.fid.readline().split()[0]))

        if dirorder:
            self.dirmap = list(np.argsort(self.dirs % 360.))
            self.dirs = self.dirs[self.dirmap] % 360.
        else:
            self.dirmap = False
        lons = np.unique(self.x)
        lats = np.unique(self.y)
        self.is_grid = (len(lons) * len(lats) == len(self.x))
        self.is_tab = (os.path.isfile(self.tabfile)) & (len(lons) * len(lats)
                                                        == 1)
Esempio n. 3
0
def test_to_nautical():
    assert to_nautical(0) == 270
    assert to_nautical(90) == 180
    assert to_nautical(180) == 90
    assert to_nautical(270) == 0