Commit 263af4eb authored by thomas.forbriger's avatar thomas.forbriger Committed by thomas.forbriger
Browse files

dropped Daniel main code

This is a legacy commit from before 2015-03-01.
It may be incomplete as well as inconsistent.
See COPYING.legacy and README.history for details.


SVN Path:     http://gpitrsvn.gpi.uni-karlsruhe.de/repos/TFSoftware/branches/any2matlab0.1
SVN Revision: 3106
SVN UUID:     67feda4a-a26e-11df-9d6e-31afc202ad0c
parent c261fe75
# this is <Makefile>
# ----------------------------------------------------------------------------
# $Id$
#
# Copyright (c) 2010 by Thomas Forbriger (BFO Schiltach)
#
# compile mex files
#
# REVISIONS and CHANGES
# 15/07/2010 V1.0 Thomas Forbriger & Daniel Armbruster
#
# ============================================================================
#
OS := $(shell uname -m)
MEXFLAGS =-cxx
ifeq ($(OS),x86_64)
TYPE:=mexa64
else
TYPE:=mexglx
endif
install: any2matlab
flist: Makefile $(wildcard *.cc)
echo $^ | tr ' ' '\n' | sort > $@
.PHONY: edit
edit: flist; vim $<
.PHONY: clean
clean: ;
-find . -name \*.bak | xargs --no-run-if-empty /bin/rm -v
-find . -name \*.mexglx | xargs --no-run-if-empty /bin/rm -v
-find . -name \*.mexa64 | xargs --no-run-if-empty /bin/rm -v
#======================================================================
# installation part
# # ------------------
# depends on operating system $(OS)
.PHONY: any2matlab
any2matlab: any2matlab.cc
mex $< $(MEXFLAGS) -output $(LOCBINDIR)/$@.$(TYPE) -L$(LOCLIBDIR) -I$(LOCINCLUDEDIR) -ldatreadxx\
-lsffxx -lgsexx -laff -ltime++
#======================================================================
# documentation part
# # ------------------
DOXYWWWPATH=$(WWWBASEDIR)/mex
.PHONY: doxyclean
doxyclean:
/bin/rm -rfv $(WWWBASEDIR)/mex
/bin/rm -rfv doc
doc/%: doxyfull.cfg
mkdir -vp $(DOXYWWWPATH)
/bin/rm -rfv doc
ln -sfv $(DOXYWWWPATH) doc
doxygen $<
doxyfullview:
$(TF_BROWSER) file:$(DOXYWWWPATH)/html/index.html &
# ----- END OF Makefile -----
%{
* ========================================================================
*
* Filename: any2kasop.m
*
* Description: Conversion of a MATLAB-struct which had been created by any2matlab.mexa64
* any2matlab.mexa64 supports the import of seismic data from Thomas
* Forbriger's software library.
* For further information:
* http://www.rz.uni-karlsruhe.de/~bi77/txt/mycode.html
*
* Version: 1.0
* Created: 10.06.2010 20:02:02
* Revision: none
*
* Author: Daniel Armbruster (HiWi of Joern ;-)
* Usage: kasopstruct = any2kasop(tfstruct)
*
* ========================================================================
%}
function [outstruct]=any2kasop(instruct)
% checking input data
if (nargin==0 || ~isstruct(instruct))
error('ERROR: No struct given!')
outstruct=0;
return
end
if (~isfield(instruct,'date') && ~isfield(instruct,'time') && ~isfield(instruct,'tdate') && ~isfield(instruct,'station') && ~isfield(instruct,'channel') && ~isfield(instruct,'auxid') && ~isfield(instruct,'dt') && ~isfield(instruct,'calper') && ~isfield(instruct,'instype'))
outstruct=0;
error('ERROR: input data is not a valid struct!')
return
end
% copy data from instruct to kasop-struct
for i=1:length(instruct)
% create outstruct
outstruct(i) = gettimeseriesdatastruct;
% set tsn-field
outstruct(i).tsn = datenum(instruct(i).tdate);
% set staname-field
outstruct(i).staname = strtrim(instruct(i).station);
% set channel-field
channel = strtrim(instruct(i).channel);
outstruct(i).channel = channel;
% set comp-field
outstruct(i).comp = channel(3);
% set npts-field
outstruct(i).npts = instruct(i).samps;
% set delta-field
outstruct(i).delta = instruct(i).dt;
% set trace-field
outstruct(i).trace = instruct(i).trace;
end
clear(channel);
outstruct = checktimeseriesdatastruct(outstruct);
return
% EOF any2kasop.m
/*!
* \file any2matlab.cc
* \brief This es a MEX-file using Thomas Forbriger's datread library importing seismic dataformats
* to MATLAB.
*
* Currently supported formats are:
* \li mseed: MiniSEED (SeisComP, EDL, etc.)
* \li pdas: PDAS100 (i.e. DaDisp)
* \li sac: SAC binary format
* \li sff: Stuttgart File Format
* \li gse: raw GSE format
* \li hpmo: HP-MO data format defined by W. Grossmann (BFO)
* \li bonjer: K2 ASCII data format (defined by K. Bonjer?)
*
*
* \author Daniel Armbruster
* \version V1.0
* \date 01/07/2010
*
* ----
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
* ----
*
* \section general General
*
* - For information how to use go to \ref usage .
* - For \ref info how the program works.
*
* \brief The gateway routine to every MEX-file is called <b>mexFunction</b>.
*
* \param nrhs: Number of inputs (Right Hand Side)
* \param prhs: Array of pointers to input data.
*
* The input data is read-only and should not be altered by your mexFunction .
* \return nlhs Number of expected mxArrays (Left Hand Side)
* \return plhs: Array of pointers to expected outputs
*
* The variables nrhs and \c nlhs are the number of variables that MATLAB requested at this instance.
* They are analogous to \c NARGIN and \c NARGOUT in MATLAB.
* \n
*
* The variables \c prhs and \c plhs are not mxArrays. They are arrays of pointers to mxArrays. So if a
* function is given three inputs, \c prhs will be an array of three pointers to the mxArrays that
* contain the data passed in. The variable \c prhs is declared as \c const. This means that the values
* that are passed into the MEX-file should not be altered. Doing so can cause segmentation
* violations in MATLAB. The values in \c plhs are invalid when the MEX-file begins. The mxArrays they
* point to must be explicitly created before they are used. Compilers will not catch this issue,
* but it will cause incorrect results or segmentation violations.
*
* \section usage Usage
* <tt>tfstruct = any2matlab('fname.ftype');\n</tt>
* or: <tt>tfstruct = any2matlab('fname', 'ftype');\n</tt>
* or: <tt>tfstruct = any2matlab('ftype', 'fname');\n</tt>
*
* - \p fname is the filename of the datafile. A "." seperates \p fname from \p ftype
* e.g. \p data.sff
* - \p fname is the filename of the datafile in an arbitrary form. \p ftype is one of the options
* below.
* - Same as the previous one, with different order.
* - \p ftype is one of the following filetypes:
* -# pdas
* -# sff
* -# hpmo
* -# bonjer
* -# mseed
* -# sac
* -# gse
* - \p tfstruct is a MATLAB struct with the following fields:
* -# \c date: date of first sample. format: \c 'YYYY/MM/DD'
* -# \c time: time of the first sample. format: \c 'hh:mm:ss.milsec'
* -# \c tdate: timevector: <tt>[YYYY MM DD hh mm ss.milsec]</tt>
* -# \c station: Station code. type: \c string
* -# \c channel: FDSN channel code. type: \c string
* -# \c auxid: Auxiliary identification code. type: \c string
* -# \c samps: Number of samples. type: \c int
* -# \c dt: Sampling interval (sec). type: \c double
* -# \c calib: Calibration factor. type: \c double
* -# \c calper: Calibration reference period. type: \c double
* -# \c instype: Instrument type. type: \c string
* -# \c trace: [<tt>samps</tt>x1 \c double]
*
* For further information watch sff::WID2
*
* \section info Detailed Information
*
* To allocate sufficient memory any2matlab has to open a first time the desired datafile to get the
* number of traces and the number of samps per trace.\n
* While opening the datafile a second time any2matlab will read one trace and write it immediately
* to \c tfstruct until there are no traces left.\n
* With this method you are able to read as much traces as fit to your machines memory.
*
*/
#include <iostream>
#include <fstream>
#include <sstream>
#include <cstdio>
#include <ctype.h>
#include <cstring>
#include <string>
#include <algorithm>
#include <vector>
#include <datreadxx/readany.h>
#include <libtime++.h>
#include <aff/series.h>
#include <gsexx.h>
#include <sffxx.h>
#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
const char *field_names[] = {"date", "time", "tdate", "station", "channel", "auxid", "samps", "dt",
"calib", "calper", "instype", "trace"};
int number_of_fields;
int date_field, time_field, tdate_field, station_field, channel_field;
int auxid_field, samps_field, dt_field, calib_field, calper_field;
int instype_field, trace_field;
std::vector<int> ntraces;
ntraces.reserve(100);
// calculate number of fields
number_of_fields = sizeof(field_names) / sizeof(*field_names);
// check: if input is correct
if (nrhs == 0)
mexErrMsgTxt("ERROR: missing input filename!");
// check: more than 2 arguments
else if (nrhs > 2)
mexErrMsgTxt("ERROR: too many input arguments!");
// check: one argument and string
else if ((nrhs == 1) && (mxIsChar(prhs[0]) != 1))
mexErrMsgTxt("ERROR: input must be a string!");
// check: 2 arguments and string
else if ((nrhs == 2) && (((mxIsChar(prhs[0]) != 1) || (mxIsChar(prhs[1])) != 1)))
mexErrMsgTxt("Error: input must be a string!");
// input must be a row vectors
else if ((nrhs == 2) && ((mxGetM(prhs[0]) != 1) || mxGetM(prhs[1]) != 1))
mexErrMsgTxt("Error: input isn't valid!");
int trace = 0;
bool hot;
std::string infile;
std::string filetype;
// If only one argument is given to any2matlab.mexa64
if (nrhs == 1) {
infile = std::string(mxArrayToString(prhs[0]));
/*
* Important note:
* If only one argument is given to any2matlab.mexa64 the given filename must contain at least one
* dot. After the the dot there must be one of the following endings.
*/
if (infile.rfind(".") != string::npos) {
filetype = infile.substr(infile.rfind(".") + 1);
}
else
mexErrMsgTxt("Error: not a valid filename!");
}
// check: 2 arguments
if (nrhs == 2) {
std::string arg0 = std::string(mxArrayToString(prhs[0]));
std::string arg1 = std::string(mxArrayToString(prhs[1]));
// check: if one contains a dot
if (!(arg0.rfind(".") == string::npos)) {
infile = arg0;
filetype = arg1;
}
else {
infile = arg1;
filetype = arg0;
}
}
// set filetype to lower case
transform(filetype.begin(), filetype.end(), filetype.begin(), tolower);
datread::Eformat format = datread::anyID(filetype);
{
std::ifstream ifs(infile.c_str());
datread::ianystream is(ifs, format);
/* get number of traces in infile */
/* while skipping series */
hot=is.good();
while (hot) {
datread::Tdseries series;
is.skipseries();
sff::WID2 wid2;
// read the WID2 header
is >> wid2;
ntraces.push_back(wid2.nsamples);
trace++;
hot=(!is.last());
}
}
int dims[2] = {1, trace};
// create a 1-by-n array of strucntraces.
plhs[0] = mxCreateStructArray(2, dims, number_of_fields, field_names);
date_field = mxGetFieldNumber(plhs[0], "date");
time_field = mxGetFieldNumber(plhs[0], "time");
tdate_field = mxGetFieldNumber(plhs[0], "tdate");
station_field = mxGetFieldNumber(plhs[0], "station");
auxid_field = mxGetFieldNumber(plhs[0], "auxid");
channel_field = mxGetFieldNumber(plhs[0], "channel");
samps_field = mxGetFieldNumber(plhs[0], "samps");
dt_field = mxGetFieldNumber(plhs[0], "dt");
calib_field = mxGetFieldNumber(plhs[0], "calib");
calper_field = mxGetFieldNumber(plhs[0], "calper");
instype_field = mxGetFieldNumber(plhs[0], "instype");
trace_field = mxGetFieldNumber(plhs[0], "trace");
{
std::ifstream ifs(infile.c_str());
datread::ianystream is(ifs, format);
hot = is.good();
int i = 0;
while(hot) {
mxArray *field_value;
datread::Tdseries series;
// read the series
is >> series;
// set trace-field
field_value = mxCreateDoubleMatrix(ntraces[i], 1, mxREAL);
memcpy(mxGetPr(field_value), series.pointer(), ntraces[i] * sizeof(double));
mxSetFieldByNumber(plhs[0], i, trace_field, field_value);
sff::WID2 wid2;
// read the WID2 header
is >> wid2;
// set date-field
{
std::ostringstream str;
str << wid2.date.year() << "/" << wid2.date.month() << "/" << wid2.date.day() << std::ends;
std::string datestring;
datestring.append(str.str());
mxSetFieldByNumber(plhs[0],i,date_field,mxCreateString(datestring.c_str()));
}
// set date-field
{
std::ostringstream str;
std::string timestring;
str << wid2.date.hour() << ":" << wid2.date.minute() << ":" << wid2.date.second() <<
"." << wid2.date.milsec() << std::ends;
timestring.append(str.str());
mxSetFieldByNumber(plhs[0],i,time_field,mxCreateString(timestring.c_str()));
}
// set tdate-field
double time [6];
time [0] = wid2.date.year();
time [1] = wid2.date.month();
time [2] = wid2.date.day();
time [3] = wid2.date.hour();
time [4] = wid2.date.minute();
time [5] = wid2.date.second() + wid2.date.milsec() * 0.001;
field_value = mxCreateDoubleMatrix(1, sizeof(time) / sizeof(time[0]), mxREAL);
memmove(mxGetPr(field_value), &time, sizeof(time) / sizeof(time[0]) * sizeof(double));
mxSetFieldByNumber(plhs[0], i, tdate_field, field_value);
// set station-field
mxSetFieldByNumber(plhs[0],i,station_field,mxCreateString(wid2.station.c_str()));
// set channel-field
mxSetFieldByNumber(plhs[0],i,channel_field,mxCreateString(wid2.channel.c_str()));
// set auxid-field
mxSetFieldByNumber(plhs[0],i,auxid_field,mxCreateString(wid2.auxid.c_str()));
// set samps-field
field_value = mxCreateDoubleMatrix(1, 1, mxREAL);
*mxGetPr(field_value) = wid2.nsamples;
mxSetFieldByNumber(plhs[0], i, samps_field, field_value);
// set dt-field
field_value = mxCreateDoubleMatrix(1, 1, mxREAL);
*mxGetPr(field_value) = wid2.dt;
mxSetFieldByNumber(plhs[0], i, dt_field, field_value);
// set calib-field
field_value = mxCreateDoubleMatrix(1, 1, mxREAL);
*mxGetPr(field_value) = wid2.calib;
mxSetFieldByNumber(plhs[0], i, calib_field, field_value);
// set calper-field
field_value = mxCreateDoubleMatrix(1, 1, mxREAL);
*mxGetPr(field_value) = wid2.calper;
mxSetFieldByNumber(plhs[0], i, calper_field, field_value);
// set instype-field
mxSetFieldByNumber(plhs[0],i, instype_field,mxCreateString(wid2.instype.c_str()));
i++;
hot=(!is.last());
}
}
}
This diff is collapsed.
/*! \mainpage
*
* \authors Thomas Forbriger, Daniel Armbruster
*
* \since March 2004
* \date 2010
* \version V1.0
*
* Contents of the page:
* - \link concept Concept of the library \endlink
* - \link mex \c any2matlab.mexa64 \endlink
*
* \section concept Concept of the library
* The library provides seismic data file reading modules for several data formats. Functionality
* for all formats is provided through a common interface defined in class datread::idatstream. All
* format specific classes are derived from this base. The class datread::ianystream has the same
* common interface but provides an inheritance transparent constructor mechanism. Programs making
* use of datread::ianystream for reading seismic data will directly benefit from each additional
* waveform format added to libdatreadxx without the need of changing the source code of the
* library. Just link against libdatreadxx and go...
*
* Currently supported formats are:
*
* - mseed: MiniSEED (SeisComP, EDL, etc.)
* - pdas: PDAS100 (i.e. DaDisp)
* - sac: SAC binary format
* - sff: Stuttgart File Format
* - gse: raw GSE format
* - hpmo: HP-MO data format defined by W. Grossmann (BFO)
* - bonjer: K2 ASCII data format (defined by K. Bonjer?)
*
* Each format specific module will be placed into its own namespace, like datread::hpmo. There you
* will find specific functions like datread::hpmo::readdata. The interface provided through these
* functions is specific to the data format. However, for each format we provide a class derived
* from datread::idatstream, like datread::ihpmostream. Being derived from a common base class all
* these format specific classes provide a common interface once defined in datread::idastream. This
* interface handles data in terms of SFF (Stuttgart File Format) data elements (like INFO lines and
* FREE blocks). Thus the format specific input classes perform the conversion from the specific
* format to SFF. Any data that does not fit into SFF, must be dropped or may be only passed via the
* FREE blocks. All SFF data elements not present in the specific format must be replaced by default
* values.
*
* datread::idatstream behaves like an input stream for SFF data elements. Appropriate input
* operators are presented in namespace datread. All derived classes and datread::ianystream provide
* the same interface and operator set. datread::ianystream is not derived from datread::idatstream.
* It rather serves as a handle to any class derived from datread::idatstream. anyway
* datread::ianystream provides the same interface. Consider to use datread::sequentialtracereader
* instead of datread::ianystream since it provides higher efficiency when reading several traces
* from the same file.
*
* \section mex any2matlab.mexa64
* This program allows the import of the supported dataformats in MATLAB via MEX.
*
* \c any2matlab.mexa64 is the built program of any2matlab.cc, which had been compiled with the MATLAB
* mex compiler.
*
* <hr>
* \section revision Revision
* <hr>
* \section requirements Requirements
* <hr>
* @todo Adding option of integer data import to any2matlab.cc
* @todo
*/
This diff is collapsed.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment