witteveenbos / KPZSS

KPZSS code
3 stars 0 forks source link

IPM S2D read tab file #5

Open engt2 opened 2 years ago

engt2 commented 2 years ago

@casvbem : please share available code

casvbem commented 2 years ago

sure! here you go:

"""
--- Synopsis --- 
This script is used to read SWAN .tab output files

--- Version --- 
Created on Fri April 19 14:33:33 2019
@author: BEMC

Witteveen+Bos Consulting Engineers 
Van Twickelostraat 2 
P.O. box 233 
7400 AE Deventer 
The Netherlands 
"""

# 1. Import modules 
import numpy as np 
import matplotlib as plt 
import pandas as pd 

import numpy as np
import os
import pandas as pd

from hmtoolbox import WB_basic

import xlsxwriter

# 2. Define new functions 
def Freadtab(tabfile): 
    '''this function reads a tab-file'''

    #get header 
    headerSWAN = pd.read_csv(tabfile, index_col=None, skiprows=4, nrows=0, sep='\s+')  #configData['swanfilename']
    # headerSWAN = pd.read_csv(configData['swanfilename'], index_col=None, skiprows=4, nrows=0, sep='\s+')
    headerSWAN.drop('%', axis=1, inplace=True) # (skip the %-colomn)

    #get units 
    unitsSWAN = pd.read_csv(tabfile, index_col=None, header=None, skiprows=5, nrows=1, sep='\s\s+')  #configData['swanfilename']
    unitsSWAN.drop(columns=0,axis=1, inplace=True)
    unitsSWAN.columns = headerSWAN.columns

    dataSWAN = pd.read_csv(tabfile, index_col=None, names=headerSWAN.columns,
                            skiprows=7, sep='\s+')  #configData['swanfilename']
    dataSWAN['Botlev'] = -dataSWAN['Botlev']
    print("please be aware that the level of the seabed 'Botlev' is made negative")

    # All neccesary columns should exists ...
    obligedCols = ['Hsig', 'Tm_10', 'Watlev']
    if not all(col in dataSWAN.columns for col in obligedCols):
        # Break the other functions
        print('Not all obliged columns are present: the following is missing:')
        [print('-> {}'.format(col)) for col in obligedCols if not col in dataSWAN.columns]
        raise KeyError
    # ... however: in underlying tool, the names are a little different
    # dataSWAN.rename(columns={'Hsig': 'Hs', 'Tm_10': 'Tm10', 'Watlev': 'SWL'}, inplace=True)
    return dataSWAN, unitsSWAN

## some dummy code to create a Pandas Excel writer using XlsxWriter as the engine.
#writer = pd.ExcelWriter('SWAN_output.xlsx', engine = 'xlsxwriter')
#tab_data,units = Freadtab(file)
#df = pd.concat([units,tab_data])
#    
#df.index = df.index + 1  # shifting index
#df = df.sort_index()
#    
#as_list = df.index.tolist()
#as_list[0] = 'Loc.'
#df.index = as_list

#df.to_excel(writer,sheet_name='case 1')