[cig-commits] [commit] devel: Adds a tool to create ascii file for bathymetry from sep files (7c7afa6)

cig_noreply at geodynamics.org cig_noreply at geodynamics.org
Fri Oct 10 12:52:10 PDT 2014


Repository : https://github.com/geodynamics/specfem3d

On branch  : devel
Link       : https://github.com/geodynamics/specfem3d/compare/bc64b621279b3c6217beb0dc4260269179e694c1...71975003792df8248827739dbc2d2a7afc347878

>---------------------------------------------------------------

commit 7c7afa6e52479cad6e2e58797e18c35fd52b5c2b
Author: Matthieu Lefebvre <ml15 at princeton.edu>
Date:   Fri Oct 10 15:33:06 2014 -0400

    Adds a tool to create ascii file for bathymetry from sep files


>---------------------------------------------------------------

7c7afa6e52479cad6e2e58797e18c35fd52b5c2b
 utils/bathy_sep_to_ascii/CMakeLists.txt            |  28 +++
 utils/bathy_sep_to_ascii/Makefile                  | 247 +++++++++++++++++++++
 utils/bathy_sep_to_ascii/inc/check_errors.h        |  30 +++
 utils/bathy_sep_to_ascii/inc/convert_topo.h        |  29 +++
 utils/bathy_sep_to_ascii/inc/parse_sep.h           |  43 ++++
 utils/bathy_sep_to_ascii/inc/sep_header.h          |  46 ++++
 utils/bathy_sep_to_ascii/src/convert_topo.c        |  82 +++++++
 .../src/create_ascii_bathy_from_sep_vs.c           | 122 ++++++++++
 .../bathy_sep_to_ascii/src}/parse_sep.c            | 116 +++++-----
 utils/bathy_sep_to_ascii/src/sep_header.c          |  30 +++
 10 files changed, 719 insertions(+), 54 deletions(-)

diff --git a/utils/bathy_sep_to_ascii/CMakeLists.txt b/utils/bathy_sep_to_ascii/CMakeLists.txt
new file mode 100644
index 0000000..f84c5f2
--- /dev/null
+++ b/utils/bathy_sep_to_ascii/CMakeLists.txt
@@ -0,0 +1,28 @@
+cmake_minimum_required (VERSION 2.6)
+
+### Project Configuration
+project(sep_topo)
+
+include(CheckCCompilerFlag)
+check_c_compiler_flag(-std=gnu99 HAS_STD_GNU99)
+
+if(HAS_STD_GNU99)
+  add_definitions(-std=gnu99)
+endif()
+file(MAKE_DIRECTORY ./bin)
+set(EXECUTABLE_OUTPUT_PATH bin/${CMAKE_BUILD_TYPE})
+
+include_directories(
+  ./inc
+)
+
+set(sources
+    src/sep_header.c
+    src/convert_topo.c
+    src/parse_sep.c)
+
+add_executable(
+  create_ascii_bathy_from_sep_vs
+  src/create_ascii_bathy_from_sep_vs.c
+  ${sources} 
+)
diff --git a/utils/bathy_sep_to_ascii/Makefile b/utils/bathy_sep_to_ascii/Makefile
new file mode 100644
index 0000000..cc6f347
--- /dev/null
+++ b/utils/bathy_sep_to_ascii/Makefile
@@ -0,0 +1,247 @@
+# CMAKE generated file: DO NOT EDIT!
+# Generated by "Unix Makefiles" Generator, CMake Version 2.8
+
+# Default target executed when no arguments are given to make.
+default_target: all
+.PHONY : default_target
+
+#=============================================================================
+# Special targets provided by cmake.
+
+# Disable implicit rules so canonical targets will work.
+.SUFFIXES:
+
+# Remove some rules from gmake that .SUFFIXES does not remove.
+SUFFIXES =
+
+.SUFFIXES: .hpux_make_needs_suffix_list
+
+# Suppress display of executed commands.
+$(VERBOSE).SILENT:
+
+# A target that is always out of date.
+cmake_force:
+.PHONY : cmake_force
+
+#=============================================================================
+# Set environment variables for the build.
+
+# The shell in which to execute make rules.
+SHELL = /bin/sh
+
+# The CMake executable.
+CMAKE_COMMAND = /usr/bin/cmake28
+
+# The command to remove a file.
+RM = /usr/bin/cmake28 -E remove -f
+
+# Escaping for special characters.
+EQUALS = =
+
+# The program to use to edit the cache.
+CMAKE_EDIT_COMMAND = /usr/bin/ccmake28
+
+# The top-level source directory on which CMake was run.
+CMAKE_SOURCE_DIR = /data1/ml15/Workspace/specfem3d/utils/bathy_sep_to_ascii
+
+# The top-level build directory on which CMake was run.
+CMAKE_BINARY_DIR = /data1/ml15/Workspace/specfem3d/utils/bathy_sep_to_ascii
+
+#=============================================================================
+# Targets provided globally by CMake.
+
+# Special rule for the target edit_cache
+edit_cache:
+	@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Running CMake cache editor..."
+	/usr/bin/ccmake28 -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)
+.PHONY : edit_cache
+
+# Special rule for the target edit_cache
+edit_cache/fast: edit_cache
+.PHONY : edit_cache/fast
+
+# Special rule for the target rebuild_cache
+rebuild_cache:
+	@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Running CMake to regenerate build system..."
+	/usr/bin/cmake28 -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)
+.PHONY : rebuild_cache
+
+# Special rule for the target rebuild_cache
+rebuild_cache/fast: rebuild_cache
+.PHONY : rebuild_cache/fast
+
+# The main all target
+all: cmake_check_build_system
+	$(CMAKE_COMMAND) -E cmake_progress_start /data1/ml15/Workspace/specfem3d/utils/bathy_sep_to_ascii/CMakeFiles /data1/ml15/Workspace/specfem3d/utils/bathy_sep_to_ascii/CMakeFiles/progress.marks
+	$(MAKE) -f CMakeFiles/Makefile2 all
+	$(CMAKE_COMMAND) -E cmake_progress_start /data1/ml15/Workspace/specfem3d/utils/bathy_sep_to_ascii/CMakeFiles 0
+.PHONY : all
+
+# The main clean target
+clean:
+	$(MAKE) -f CMakeFiles/Makefile2 clean
+.PHONY : clean
+
+# The main clean target
+clean/fast: clean
+.PHONY : clean/fast
+
+# Prepare targets for installation.
+preinstall: all
+	$(MAKE) -f CMakeFiles/Makefile2 preinstall
+.PHONY : preinstall
+
+# Prepare targets for installation.
+preinstall/fast:
+	$(MAKE) -f CMakeFiles/Makefile2 preinstall
+.PHONY : preinstall/fast
+
+# clear depends
+depend:
+	$(CMAKE_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake 1
+.PHONY : depend
+
+#=============================================================================
+# Target rules for targets named create_ascii_bathy_from_sep_vs
+
+# Build rule for target.
+create_ascii_bathy_from_sep_vs: cmake_check_build_system
+	$(MAKE) -f CMakeFiles/Makefile2 create_ascii_bathy_from_sep_vs
+.PHONY : create_ascii_bathy_from_sep_vs
+
+# fast build rule for target.
+create_ascii_bathy_from_sep_vs/fast:
+	$(MAKE) -f CMakeFiles/create_ascii_bathy_from_sep_vs.dir/build.make CMakeFiles/create_ascii_bathy_from_sep_vs.dir/build
+.PHONY : create_ascii_bathy_from_sep_vs/fast
+
+src/convert_topo.o: src/convert_topo.c.o
+.PHONY : src/convert_topo.o
+
+# target to build an object file
+src/convert_topo.c.o:
+	$(MAKE) -f CMakeFiles/create_ascii_bathy_from_sep_vs.dir/build.make CMakeFiles/create_ascii_bathy_from_sep_vs.dir/src/convert_topo.c.o
+.PHONY : src/convert_topo.c.o
+
+src/convert_topo.i: src/convert_topo.c.i
+.PHONY : src/convert_topo.i
+
+# target to preprocess a source file
+src/convert_topo.c.i:
+	$(MAKE) -f CMakeFiles/create_ascii_bathy_from_sep_vs.dir/build.make CMakeFiles/create_ascii_bathy_from_sep_vs.dir/src/convert_topo.c.i
+.PHONY : src/convert_topo.c.i
+
+src/convert_topo.s: src/convert_topo.c.s
+.PHONY : src/convert_topo.s
+
+# target to generate assembly for a file
+src/convert_topo.c.s:
+	$(MAKE) -f CMakeFiles/create_ascii_bathy_from_sep_vs.dir/build.make CMakeFiles/create_ascii_bathy_from_sep_vs.dir/src/convert_topo.c.s
+.PHONY : src/convert_topo.c.s
+
+src/create_ascii_bathy_from_sep_vs.o: src/create_ascii_bathy_from_sep_vs.c.o
+.PHONY : src/create_ascii_bathy_from_sep_vs.o
+
+# target to build an object file
+src/create_ascii_bathy_from_sep_vs.c.o:
+	$(MAKE) -f CMakeFiles/create_ascii_bathy_from_sep_vs.dir/build.make CMakeFiles/create_ascii_bathy_from_sep_vs.dir/src/create_ascii_bathy_from_sep_vs.c.o
+.PHONY : src/create_ascii_bathy_from_sep_vs.c.o
+
+src/create_ascii_bathy_from_sep_vs.i: src/create_ascii_bathy_from_sep_vs.c.i
+.PHONY : src/create_ascii_bathy_from_sep_vs.i
+
+# target to preprocess a source file
+src/create_ascii_bathy_from_sep_vs.c.i:
+	$(MAKE) -f CMakeFiles/create_ascii_bathy_from_sep_vs.dir/build.make CMakeFiles/create_ascii_bathy_from_sep_vs.dir/src/create_ascii_bathy_from_sep_vs.c.i
+.PHONY : src/create_ascii_bathy_from_sep_vs.c.i
+
+src/create_ascii_bathy_from_sep_vs.s: src/create_ascii_bathy_from_sep_vs.c.s
+.PHONY : src/create_ascii_bathy_from_sep_vs.s
+
+# target to generate assembly for a file
+src/create_ascii_bathy_from_sep_vs.c.s:
+	$(MAKE) -f CMakeFiles/create_ascii_bathy_from_sep_vs.dir/build.make CMakeFiles/create_ascii_bathy_from_sep_vs.dir/src/create_ascii_bathy_from_sep_vs.c.s
+.PHONY : src/create_ascii_bathy_from_sep_vs.c.s
+
+src/parse_sep.o: src/parse_sep.c.o
+.PHONY : src/parse_sep.o
+
+# target to build an object file
+src/parse_sep.c.o:
+	$(MAKE) -f CMakeFiles/create_ascii_bathy_from_sep_vs.dir/build.make CMakeFiles/create_ascii_bathy_from_sep_vs.dir/src/parse_sep.c.o
+.PHONY : src/parse_sep.c.o
+
+src/parse_sep.i: src/parse_sep.c.i
+.PHONY : src/parse_sep.i
+
+# target to preprocess a source file
+src/parse_sep.c.i:
+	$(MAKE) -f CMakeFiles/create_ascii_bathy_from_sep_vs.dir/build.make CMakeFiles/create_ascii_bathy_from_sep_vs.dir/src/parse_sep.c.i
+.PHONY : src/parse_sep.c.i
+
+src/parse_sep.s: src/parse_sep.c.s
+.PHONY : src/parse_sep.s
+
+# target to generate assembly for a file
+src/parse_sep.c.s:
+	$(MAKE) -f CMakeFiles/create_ascii_bathy_from_sep_vs.dir/build.make CMakeFiles/create_ascii_bathy_from_sep_vs.dir/src/parse_sep.c.s
+.PHONY : src/parse_sep.c.s
+
+src/sep_header.o: src/sep_header.c.o
+.PHONY : src/sep_header.o
+
+# target to build an object file
+src/sep_header.c.o:
+	$(MAKE) -f CMakeFiles/create_ascii_bathy_from_sep_vs.dir/build.make CMakeFiles/create_ascii_bathy_from_sep_vs.dir/src/sep_header.c.o
+.PHONY : src/sep_header.c.o
+
+src/sep_header.i: src/sep_header.c.i
+.PHONY : src/sep_header.i
+
+# target to preprocess a source file
+src/sep_header.c.i:
+	$(MAKE) -f CMakeFiles/create_ascii_bathy_from_sep_vs.dir/build.make CMakeFiles/create_ascii_bathy_from_sep_vs.dir/src/sep_header.c.i
+.PHONY : src/sep_header.c.i
+
+src/sep_header.s: src/sep_header.c.s
+.PHONY : src/sep_header.s
+
+# target to generate assembly for a file
+src/sep_header.c.s:
+	$(MAKE) -f CMakeFiles/create_ascii_bathy_from_sep_vs.dir/build.make CMakeFiles/create_ascii_bathy_from_sep_vs.dir/src/sep_header.c.s
+.PHONY : src/sep_header.c.s
+
+# Help Target
+help:
+	@echo "The following are some of the valid targets for this Makefile:"
+	@echo "... all (the default if no target is provided)"
+	@echo "... clean"
+	@echo "... depend"
+	@echo "... create_ascii_bathy_from_sep_vs"
+	@echo "... edit_cache"
+	@echo "... rebuild_cache"
+	@echo "... src/convert_topo.o"
+	@echo "... src/convert_topo.i"
+	@echo "... src/convert_topo.s"
+	@echo "... src/create_ascii_bathy_from_sep_vs.o"
+	@echo "... src/create_ascii_bathy_from_sep_vs.i"
+	@echo "... src/create_ascii_bathy_from_sep_vs.s"
+	@echo "... src/parse_sep.o"
+	@echo "... src/parse_sep.i"
+	@echo "... src/parse_sep.s"
+	@echo "... src/sep_header.o"
+	@echo "... src/sep_header.i"
+	@echo "... src/sep_header.s"
+.PHONY : help
+
+
+
+#=============================================================================
+# Special targets to cleanup operation of make.
+
+# Special rule to run CMake to check the build system integrity.
+# No rule that depends on this can have commands that come from listfiles
+# because they might be regenerated.
+cmake_check_build_system:
+	$(CMAKE_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake 0
+.PHONY : cmake_check_build_system
+
diff --git a/utils/bathy_sep_to_ascii/inc/check_errors.h b/utils/bathy_sep_to_ascii/inc/check_errors.h
new file mode 100644
index 0000000..a716e8b
--- /dev/null
+++ b/utils/bathy_sep_to_ascii/inc/check_errors.h
@@ -0,0 +1,30 @@
+/*
+ * Copyright [2014] [Matthieu Lefebvre]
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef _CHECK_ERRORS_H_
+#define _CHECK_ERRORS_H_
+
+#include "err.h"
+
+
+/**
+ * \brief Macro to be called in case a function return an error code,
+ *        print a message on stderr and exit.
+ */
+#define EXIT_ON_ERR(msg) \
+    err(1, "%s(%d) -- %s: %s", __FILE__, __LINE__, __func__, msg);
+
+#endif /* end of include guard: _CHECK_ERRORS_H_ */
diff --git a/utils/bathy_sep_to_ascii/inc/convert_topo.h b/utils/bathy_sep_to_ascii/inc/convert_topo.h
new file mode 100644
index 0000000..d1d6438
--- /dev/null
+++ b/utils/bathy_sep_to_ascii/inc/convert_topo.h
@@ -0,0 +1,29 @@
+/*
+ * Copyright [2014] [Matthieu Lefebvre]
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef _CONVERT_TOPO_H_
+#define _CONVERT_TOPO_H_
+
+void convert_topo_sep_to_ascii(char *sep_name, char *ascii_name);
+
+void read_topo_sep(sep_header_t *header, int *sep_topo);
+
+void assign_ascii_topo_values(sep_header_t *header,
+                              int *sep_topo, float *ascii_topo);
+
+void write_ascii_topo(char *ascii_name, float *ascii_topo, int n);
+
+#endif /* end of include guard: _CONVERT_TOPO_H_ */
diff --git a/utils/bathy_sep_to_ascii/inc/parse_sep.h b/utils/bathy_sep_to_ascii/inc/parse_sep.h
new file mode 100644
index 0000000..00adb34
--- /dev/null
+++ b/utils/bathy_sep_to_ascii/inc/parse_sep.h
@@ -0,0 +1,43 @@
+/*
+ * Copyright [2014] [Matthieu Lefebvre]
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/*****************************************************************************/
+#include "sep_header.h"
+
+#ifndef _SEP_PARSER_H_
+#define _SEP_PARSER_H_
+
+/*****************************************************************************/
+
+/** 
+  * \brief Fill a sep_header structure from a sep_header
+  * 
+  * \param vs_name Name of the sep header
+  * \param sep_header Structure to fill
+  */
+void parse_sep_vs_header(char *vs_name, sep_header_t *sep_header);
+
+/** 
+  * \brief Get bathimetry location, where vs == 0.
+  * 
+  * \param vs_header SEP header indicating dimensions and file location.
+  * \param topo Array to store index where liquid / solid interface is found.
+  */
+void parse_sep_vs_binary(sep_header_t *vs_header, int *topo);
+
+/*****************************************************************************/
+
+#endif /* end of include guard: _SEP_PARSER_H_ */
diff --git a/utils/bathy_sep_to_ascii/inc/sep_header.h b/utils/bathy_sep_to_ascii/inc/sep_header.h
new file mode 100644
index 0000000..b9f3124
--- /dev/null
+++ b/utils/bathy_sep_to_ascii/inc/sep_header.h
@@ -0,0 +1,46 @@
+/*
+ * Copyright [2014] [Matthieu Lefebvre]
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef _SEP_HEADER_H_
+#define _SEP_HEADER_H_
+
+/*****************************************************************************/
+
+/**
+  * brief SEP header informations 
+  *
+  * 3D files at most.
+  */
+typedef struct {
+  int n1, n2, n3;
+  int o1, o2, o3;
+  float d1, d2, d3;
+  char label1[512], label2[512], label3[512];
+  char unit1[512], unit2[512], unit3[512];
+  char data_format[512];
+  char in[512];
+} sep_header_t;
+
+/** 
+  * \brief Display the values in a sep header structure.
+  * 
+  * \param h Header information to display
+  */
+void show_header(sep_header_t h);
+
+/*****************************************************************************/
+
+#endif /* end of include guard: _SEP_HEADER_H_ */
diff --git a/utils/bathy_sep_to_ascii/src/convert_topo.c b/utils/bathy_sep_to_ascii/src/convert_topo.c
new file mode 100644
index 0000000..bb280e0
--- /dev/null
+++ b/utils/bathy_sep_to_ascii/src/convert_topo.c
@@ -0,0 +1,82 @@
+/*
+ * Copyright [2014] [Matthieu Lefebvre]
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/*****************************************************************************/
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "sep_header.h"
+#include "parse_sep.h"
+#include "check_errors.h"
+
+#include "convert_topo.h"
+
+/*****************************************************************************/
+
+void convert_topo_sep_to_ascii(char *sep_name, char *ascii_name) {
+  /* Get sep header from sep file 'sep_name' */
+  sep_header_t sep_header;
+  parse_sep_vs_header(sep_name, &sep_header);
+  show_header(sep_header);
+  /* Read topo value from sep binary file */
+  int *sep_topo = (int *) malloc(sep_header.n1 * sep_header.n2
+                                    * sizeof(int));
+  read_topo_sep(&sep_header, sep_topo);
+  /* Create ascii topo according to geometry */
+  float *ascii_topo = (float *) malloc(sep_header.n1 * sep_header.n2 
+                                      * sizeof (float));
+  /* sep topo index to ascii topo depth (m) */
+  /* Interpolation is done in meshfem 3d*/
+  assign_ascii_topo_values(&sep_header, sep_topo, ascii_topo);
+  write_ascii_topo(ascii_name, ascii_topo, sep_header.n1 * sep_header.n2);
+  /* Clean */
+  free(sep_topo);
+  free(ascii_topo);
+}
+
+void read_topo_sep(sep_header_t *header, int *sep_topo) {
+  FILE *fd = fopen(header->in, "r");
+  if (!fd) EXIT_ON_ERR("fopen");
+
+  fread(sep_topo, sizeof(int), header->n1 * header->n2, fd);
+
+  fclose(fd);
+}
+
+void assign_ascii_topo_values(sep_header_t *header,
+                              int *sep_topo, float *ascii_topo) {
+  int nx = header->n1;
+  int ny = header->n2;
+  
+  float oz = header->o3;
+  float dz = header->d3;
+  
+  for (int i = 0; i < nx*ny; ++i) {
+    ascii_topo[i] = - dz*(oz + sep_topo[i]);
+  }
+}
+
+void write_ascii_topo(char *ascii_name, float *ascii_topo, int n) {
+  FILE *fd = fopen(ascii_name, "w");
+  if (fd == NULL) 
+    EXIT_ON_ERR("fopen");
+ 
+  for (int i = 0; i < n; ++i) {
+    fprintf(fd, "%f\n", ascii_topo[i]);
+  }
+
+  fclose(fd);
+}
diff --git a/utils/bathy_sep_to_ascii/src/create_ascii_bathy_from_sep_vs.c b/utils/bathy_sep_to_ascii/src/create_ascii_bathy_from_sep_vs.c
new file mode 100644
index 0000000..28c3d97
--- /dev/null
+++ b/utils/bathy_sep_to_ascii/src/create_ascii_bathy_from_sep_vs.c
@@ -0,0 +1,122 @@
+/*
+ * Copyright [2014] [Matthieu Lefebvre]
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/*****************************************************************************/
+#include <stdio.h>
+#include <stdlib.h>
+#include <getopt.h>
+#include <string.h>
+
+#include "sep_header.h"
+#include "parse_sep.h"
+#include "convert_topo.h"
+
+/*****************************************************************************/
+/** 
+  * \brief Parse arguments form the command line.
+  * 
+  * \param argc 
+  * \param argv 
+  * \param vs_name From '-i' option.
+  * \param topo_name From '-o' option.
+  * 
+  * \return Success if all args are correct.
+  */
+int parse_args(int argc, char *argv[], char **vs_name, char **topo_name);
+
+/*****************************************************************************/
+int main(int argc, char *argv[]) {
+  char *vs_name, *topo_name;
+  parse_args(argc, argv, &vs_name, &topo_name);
+
+  sep_header_t vs_header;
+  parse_sep_vs_header(vs_name, &vs_header);
+  show_header(vs_header);
+
+  int *topo = (int*) malloc(vs_header.n1 * vs_header.n2 * sizeof(int));
+  parse_sep_vs_binary(&vs_header, topo);
+
+  /* Create ascii topo according to geometry */
+  float *ascii_topo = (float *) malloc(vs_header.n1 * vs_header.n2 
+                                      * sizeof (float));
+  /* sep topo index to ascii topo depth (m) */
+  /* Interpolation is done in meshfem 3d*/
+  assign_ascii_topo_values(&vs_header, topo, ascii_topo);
+  write_ascii_topo(topo_name, ascii_topo, vs_header.n1 * vs_header.n2);
+  /* Clean */
+  free(topo);
+  free(ascii_topo);
+
+  if (vs_name) free(vs_name);
+  if (topo_name) free(topo_name);
+  
+  return EXIT_SUCCESS;
+}
+
+/*****************************************************************************/
+int parse_args(int argc, char *argv[], char **vs_name, char **topo_name) {
+  int choice;
+  while (1) {
+    static struct option long_options[] =
+    {
+      /* Argument styles: no_argument, required_argument, optional_argument */
+      {"help", optional_argument, 0, 'h'},
+      {"input", required_argument, 0, 'i'},
+      {"output",required_argument, 0, 'o'},
+      {0,0,0,0}
+    };
+  
+    int option_index = 0;
+  
+    /* Argument parameters:
+      no_argument: " "
+      required_argument: ":"
+      optional_argument: "::" */
+  
+    choice = getopt_long( argc, argv, "h::i:o:",
+          long_options, &option_index);
+  
+    if (choice == -1)
+      break;
+
+    switch( choice ) {
+      case 'v':
+        break;
+      case 'i':
+        *vs_name = malloc (strlen(optarg) * sizeof(char));
+        strncpy(*vs_name, optarg, strlen(optarg));
+        break;
+      case 'o':
+        *topo_name = malloc (strlen(optarg) * sizeof(char));
+        strncpy(*topo_name, optarg, strlen(optarg));
+        break;
+      /* For all following cases, print a hel string. */
+      case 'h':
+      case '?':
+      default:
+        printf("Usage: ./bin/create_ascii_bathy_from_sep_vs "
+               "-i [input_sep_header.H] -o [output_bathy.txt]\n");
+        exit(EXIT_FAILURE);
+    }
+  }
+  /* Deal with non-option arguments here */
+  if ( optind < argc ) {
+    while ( optind < argc ) {
+      continue;
+    }
+  }
+  return EXIT_SUCCESS;
+}
diff --git a/src/generate_databases/parse_sep.c b/utils/bathy_sep_to_ascii/src/parse_sep.c
similarity index 50%
copy from src/generate_databases/parse_sep.c
copy to utils/bathy_sep_to_ascii/src/parse_sep.c
index c0ec9e9..4380014 100644
--- a/src/generate_databases/parse_sep.c
+++ b/utils/bathy_sep_to_ascii/src/parse_sep.c
@@ -15,61 +15,36 @@
  */
 
 /*****************************************************************************/
-#include "config.h"
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <regex.h>
 #include "err.h"
 
-#define EXIT_ON_ERR(msg) \
-    err(1, "%s(%d) -- %s: %s", __FILE__, __LINE__, __func__, msg);
-
+#include "parse_sep.h"
+#include "check_errors.h"
 
 /*****************************************************************************/
-/**
- * Parse a sep header file and get information of interest
- *
- * \param header_name Name of the sep header to parse
- * \param n1 Number of sample in X direction
- * \param n2 Number of sample in Y direction
- * \param n3 Number of sample in Z direction
- * \param o1 Offset (m) in X direction
- * \param o2 Offset (m) in Y direction
- * \param o3 Offset (m) in Z direction
- * \param d1 Spatial increment (m) in X direction
- * \param d2 Spatial increment (m) in Y direction
- * \param d3 Spatial increment (m) in Z direction
- * \param in Name and location of the SEP binary file
- */
-void
-FC_FUNC_(parse_sep_header, PARSE_SEP_HEADER)
-    (char *header_name,
-     int *n1, int *n2, int *n3,
-     float *o1, float *o2, float *o3,
-     float *d1, float *d2, float *d3,
-     char *in) {
+void parse_sep_vs_header(char *vs_name, sep_header_t *sep_header) {
   char *line = NULL;
   size_t len = 0;
 
-  FILE *fd = fopen(header_name, "r");
+  FILE *fd = fopen(vs_name, "r");
   if (fd == NULL) 
-    EXIT_ON_ERR(header_name);
+    EXIT_ON_ERR("fopen");
 
-  /* Build regex mathing "name=value" with optional spaces*/
   regex_t re;
   if(regcomp(&re, "^[ \t]*\\(.*\\)[ \t]*=[ \t]*\\(.*\\)[ \t]*", 
              REG_ICASE | REG_NEWLINE))
     EXIT_ON_ERR("regcomp");
 
-  /* Apply the regex to every line of the sep header */
   while(getline(&line, &len, fd) != -1) {
 
     char name[512], value[512];
     size_t nmatch = 3;
     regmatch_t pmatch[3];
     if(REG_NOMATCH == regexec(&re, line, nmatch, pmatch, 0)) {
-      continue; /* These files are usually quite messy... */
+      continue;
     } else {
       size_t name_len = pmatch[1].rm_eo - pmatch[1].rm_so;
       strncpy(name, &line[pmatch[1].rm_so], name_len);
@@ -80,48 +55,81 @@ FC_FUNC_(parse_sep_header, PARSE_SEP_HEADER)
       value[value_len] = '\0';
 
       if (!strcmp(name, "n1")) {
-        *n1 = atoi(value);
+        sep_header->n1 = atoi(value);
       } else if (!strcmp(name, "n2")) {
-        *n2 = atoi(value);
+        sep_header->n2 = atoi(value);
       } else if (!strcmp(name, "n3")) {
-        *n3 = atoi(value);
+        sep_header->n3 = atoi(value);
       } else if (!strcmp(name, "o1")) {
-        *o1 = atof(value);
+        sep_header->o1 = atoi(value);
       } else if (!strcmp(name, "o2")) {
-        *o2 = atof(value);
+        sep_header->o2 = atoi(value);
       } else if (!strcmp(name, "o3")) {
-        *o3 = atof(value);
+        sep_header->o3 = atoi(value);
       } else if (!strcmp(name, "d1")) {
-        *d1 = atof(value);
+        sep_header->d1 = atof(value);
       } else if (!strcmp(name, "d2")) {
-        *d2 = atof(value);
+        sep_header->d2 = atof(value);
       } else if (!strcmp(name, "d3")) {
-        *d3 = atof(value);
-        /* We do not have use for other fields. */
-      /*} else if (!strcmp(name, "label1")) {
-        strncpy(*label1, value, value_len+1);
+        sep_header->d3 = atof(value);
+      } else if (!strcmp(name, "label1")) {
+        strncpy(sep_header->label1, value, value_len+1);
       } else if (!strcmp(name, "label2")) {
-        strncpy(*label2, value, value_len+1);
+        strncpy(sep_header->label2, value, value_len+1);
       } else if (!strcmp(name, "label3")) {
-        strncpy(*label3, value, value_len+1);
+        strncpy(sep_header->label3, value, value_len+1);
       } else if (!strcmp(name, "unit1")) {
-        strncpy(*unit1, value, value_len+1);
+        strncpy(sep_header->unit1, value, value_len+1);
       } else if (!strcmp(name, "unit2")) {
-        strncpy(*unit2, value, value_len+1);
+        strncpy(sep_header->unit2, value, value_len+1);
       } else if (!strcmp(name, "unit3")) {
-        strncpy(*unit3, value, value_len+1);
+        strncpy(sep_header->unit3, value, value_len+1);
       } else if (!strcmp(name, "data_format")) {
-        strncpy(*data_format, value, value_len+1);*/
+        strncpy(sep_header->data_format, value, value_len+1);
       } else if (!strcmp(name, "in")) {
-        strncpy(in, value, value_len+1);
+        strncpy(sep_header->in, value, value_len+1);
       }
     }
+
+    len = 0;
   }
+  if (line)
+    free(line);
+  fclose(fd);
+  regfree(&re);
+}
 
-  // buffer will be reallocated by getline
-  // should be freed even if getline is not successful
-  free(line);
+/*****************************************************************************/
+void parse_sep_vs_binary(sep_header_t *vs_header, int *topo) {
+  FILE *fd = fopen(vs_header->in, "r");
+  if (fd == NULL)
+    EXIT_ON_ERR("fopen");
 
+  size_t buf_size = vs_header->n1 * vs_header->n2;
+  float *buf = (float *) malloc(buf_size * sizeof(float));
+
+
+  int count = 0;
+  memset(topo, -1, vs_header->n1 * vs_header->n2 *sizeof(int));
+  
+
+  for (int k = 0; k < vs_header->n3; ++k) {
+    size_t num_read = fread(buf, sizeof(float), buf_size, fd);
+    if (num_read != buf_size)
+      EXIT_ON_ERR("fread");
+    for (int j = 0; j < vs_header->n2; ++j) {
+      for (int i = 0; i < vs_header->n1; ++i) {
+        if ((topo[i + j*vs_header->n1] == -1) &&
+            (buf[i + j*vs_header->n1] != 0.0)) {
+          topo[i + j*vs_header->n1] = k;
+          count++;
+        }
+      } 
+    }
+    if (count == vs_header->n1 * vs_header->n2) 
+      break;
+  }
+  free(buf);
   fclose(fd);
-  regfree(&re);
+  fprintf(stderr, "Found : %d bathymetry points.\n", count);
 }
diff --git a/utils/bathy_sep_to_ascii/src/sep_header.c b/utils/bathy_sep_to_ascii/src/sep_header.c
new file mode 100644
index 0000000..896bc36
--- /dev/null
+++ b/utils/bathy_sep_to_ascii/src/sep_header.c
@@ -0,0 +1,30 @@
+/*
+ * Copyright [2014] [Matthieu Lefebvre]
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/*****************************************************************************/
+#include <stdio.h>
+#include "sep_header.h"
+
+/*****************************************************************************/
+void show_header(sep_header_t h) {
+  fprintf(stderr, "n1: %d / n2: %d / n3: %d\n", h.n1, h.n2, h.n3);
+  fprintf(stderr, "o1: %d / o2: %d / o3: %d\n", h.o1, h.o2, h.o3);
+  fprintf(stderr, "d1: %f / d2: %f / d3: %f\n", h.d1, h.d2, h.d3);
+  fprintf(stderr, "label1: %s / label2: %s / label3: %s\n", h.label1, h.label2, h.label3);
+  fprintf(stderr, "unit1: %s / unit2: %s / unit3: %s\n", h.unit1, h.unit2, h.unit3);
+  fprintf(stderr, "data_format: %s\n", h.data_format);
+  fprintf(stderr, "in: %s\n", h.in);
+}



More information about the CIG-COMMITS mailing list