From 81a3a524ec39ba2cd129d65fe10312ba2dfabd25 Mon Sep 17 00:00:00 2001
From: Uwe Schulzweida <uwe.schulzweida@mpimet.mpg.de>
Date: Fri, 6 Mar 2009 12:33:22 +0000
Subject: [PATCH] grid_check_cyclic: support for curvilinear grids without
 bounds

---
 ChangeLog    |  1 +
 Makefile.am  |  2 +-
 Makefile.in  |  2 +-
 acinclude.m4 | 36 +++++++++++++------------
 configure    | 75 +++++++++++++++++++++++++++++++++++++++++++---------
 configure.ac |  2 +-
 src/grid.c   | 41 +++++++++++++++++++++++++---
 7 files changed, 123 insertions(+), 36 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 3465a16d7..a92733803 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -4,6 +4,7 @@
 	* add support for GRID type LCC2 (LCC PROJ.4 version)
 	* grbDefTime: define tunit also for absolute time [report: Pruek Pongprueksa]
 	* change default calendar to CALENDAR_PROLEPTIC
+	* grid_check_cyclic: support for curvilinear grids without bounds
 	* streamOpenAppen: set ncmode to 2 (bug fix)
 	* Version 1.3.0 released
 
diff --git a/Makefile.am b/Makefile.am
index b343b890f..76d8a83b7 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -2,6 +2,6 @@
 #
 SUBDIRS = src app examples tests
 #
-EXTRA_DIST=config/default doc/cdi_cman.pdf doc/cdi_fman.pdf
+EXTRA_DIST=config/default doc/cdi_cman.pdf doc/cdi_fman.pdf src/cfortran.doc
 #
 CLEANFILES  = `ls *~`
diff --git a/Makefile.in b/Makefile.in
index 181a6f66a..ad64f7506 100644
--- a/Makefile.in
+++ b/Makefile.in
@@ -180,7 +180,7 @@ top_srcdir = @top_srcdir@
 #
 SUBDIRS = src app examples tests
 #
-EXTRA_DIST = config/default doc/cdi_cman.pdf doc/cdi_fman.pdf
+EXTRA_DIST = config/default doc/cdi_cman.pdf doc/cdi_fman.pdf src/cfortran.doc
 #
 CLEANFILES = `ls *~`
 all: all-recursive
diff --git a/acinclude.m4 b/acinclude.m4
index 997862277..d82d58b32 100644
--- a/acinclude.m4
+++ b/acinclude.m4
@@ -3,20 +3,22 @@ dnl UD_CHECK_CFINT
 dnl
 dnl Check C / Fortran interface
 dnl
-define(UD_CHECK_CFINT,
-[
-   AC_MSG_CHECKING([whether the C / Fortran interface works])
-   changequote(,)
-   changequote([,])
-   echo '#include "XXX"' | sed -e "s-XXX-$1-" > conftest.c
-   rm -f conftest.o
-   $CC -c $CFLAGS $CFINT  conftest.c >& conftestout
-   rm -f conftest.c conftestout
-   if test -f conftest.o ; then
-       AC_MSG_RESULT(yes)
-       AC_DEFINE(HAVE_CF_INTERFACE, [1], [Define if C / Fortran interface cfortran.h works])
-   else
-       AC_MSG_RESULT(no)
-   fi
-   rm -f conftest.o
-])
+AC_DEFUN([UD_CHECK_CFINT],
+  [
+   AC_CACHE_CHECK([whether the C / Fortran interface works],[ud_c_cfint],
+     [AC_LANG_PUSH([C])
+      ud_save_CFLAGS=$CFLAGS
+      CFLAGS="$CFLAGS $CFINT"
+      AC_COMPILE_IFELSE([AC_LANG_PROGRAM([#include "$1"],[])],
+        [ud_c_cfint=yes],
+        [ud_c_cfint=yes])
+      CFLAGS=$ud_save_CFLAGS
+      AC_LANG_POP([C])])
+   AS_IF([test x$ud_c_cfint = xyes],
+     [AC_DEFINE(HAVE_CF_INTERFACE, [1],
+        [Define if C / Fortran interface cfortran.h works])])
+  ])
+dnl
+dnl Local Variables:
+dnl mode: autoconf
+dnl End:
diff --git a/configure b/configure
index c050f4072..d0a15c5df 100755
--- a/configure
+++ b/configure
@@ -6482,25 +6482,75 @@ fi
 
    { echo "$as_me:$LINENO: checking whether the C / Fortran interface works" >&5
 echo $ECHO_N "checking whether the C / Fortran interface works... $ECHO_C" >&6; }
+if test "${ud_c_cfint+set}" = set; then
+  echo $ECHO_N "(cached) $ECHO_C" >&6
+else
+  ac_ext=c
+ac_cpp='$CPP $CPPFLAGS'
+ac_compile='$CC -c $CFLAGS $CPPFLAGS conftest.$ac_ext >&5'
+ac_link='$CC -o conftest$ac_exeext $CFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS >&5'
+ac_compiler_gnu=$ac_cv_c_compiler_gnu
 
+      ud_save_CFLAGS=$CFLAGS
+      CFLAGS="$CFLAGS $CFINT"
+      cat >conftest.$ac_ext <<_ACEOF
+/* confdefs.h.  */
+_ACEOF
+cat confdefs.h >>conftest.$ac_ext
+cat >>conftest.$ac_ext <<_ACEOF
+/* end confdefs.h.  */
+#include "$srcdir/src/cfortran.h"
+int
+main ()
+{
 
-   echo '#include "XXX"' | sed -e "s-XXX-$srcdir/src/cfortran.h-" > conftest.c
-   rm -f conftest.o
-   $CC -c $CFLAGS $CFINT  conftest.c >& conftestout
-   rm -f conftest.c conftestout
-   if test -f conftest.o ; then
-       { echo "$as_me:$LINENO: result: yes" >&5
-echo "${ECHO_T}yes" >&6; }
+  ;
+  return 0;
+}
+_ACEOF
+rm -f conftest.$ac_objext
+if { (ac_try="$ac_compile"
+case "(($ac_try" in
+  *\"* | *\`* | *\\*) ac_try_echo=\$ac_try;;
+  *) ac_try_echo=$ac_try;;
+esac
+eval "echo \"\$as_me:$LINENO: $ac_try_echo\"") >&5
+  (eval "$ac_compile") 2>conftest.er1
+  ac_status=$?
+  grep -v '^ *+' conftest.er1 >conftest.err
+  rm -f conftest.er1
+  cat conftest.err >&5
+  echo "$as_me:$LINENO: \$? = $ac_status" >&5
+  (exit $ac_status); } && {
+	 test -z "$ac_c_werror_flag" ||
+	 test ! -s conftest.err
+       } && test -s conftest.$ac_objext; then
+  ud_c_cfint=yes
+else
+  echo "$as_me: failed program was:" >&5
+sed 's/^/| /' conftest.$ac_ext >&5
+
+	ud_c_cfint=yes
+fi
+
+rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
+      CFLAGS=$ud_save_CFLAGS
+      ac_ext=c
+ac_cpp='$CPP $CPPFLAGS'
+ac_compile='$CC -c $CFLAGS $CPPFLAGS conftest.$ac_ext >&5'
+ac_link='$CC -o conftest$ac_exeext $CFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS >&5'
+ac_compiler_gnu=$ac_cv_c_compiler_gnu
+
+fi
+{ echo "$as_me:$LINENO: result: $ud_c_cfint" >&5
+echo "${ECHO_T}$ud_c_cfint" >&6; }
+   if test x$ud_c_cfint = xyes; then
 
 cat >>confdefs.h <<\_ACEOF
 #define HAVE_CF_INTERFACE 1
 _ACEOF
 
-   else
-       { echo "$as_me:$LINENO: result: no" >&5
-echo "${ECHO_T}no" >&6; }
-   fi
-   rm -f conftest.o
+fi
 
 
 
@@ -6509,6 +6559,7 @@ FC=$F77
 
 
 
+
 ac_config_files="$ac_config_files Makefile src/Makefile app/Makefile tests/Makefile examples/Makefile"
 
 cat >confcache <<\_ACEOF
diff --git a/configure.ac b/configure.ac
index 2de31b856..be73d5f6e 100644
--- a/configure.ac
+++ b/configure.ac
@@ -223,9 +223,9 @@ fi
 #  Check C / Fortran interface
 UD_CHECK_CFINT([$srcdir/src/cfortran.h])
 
-AC_SUBST([CPPFLAGS])
 AC_SUBST([FC],$F77)
 AC_SUBST([CFINT])
+AC_SUBST([CPPFLAGS])
 AC_SUBST([AR])
 
 AC_OUTPUT(Makefile src/Makefile app/Makefile tests/Makefile examples/Makefile)
diff --git a/src/grid.c b/src/grid.c
index 07a89ec2b..a3b7356ad 100644
--- a/src/grid.c
+++ b/src/grid.c
@@ -2121,10 +2121,11 @@ void gridChangeType(int gridID, int gridtype)
 }
 
 
-static void grid_check_cyclic(grid_t *gridptr)
+static 
+void grid_check_cyclic(grid_t *gridptr)
 {
   int xsize, ysize, gridsize;
-  int i1, i2, j, k1, k2, nc;
+  int i1, i2, in, j, k1, k2, nc;
   double xinc, x0;
   const double *xvals, *xbounds;
 
@@ -2141,14 +2142,46 @@ static void grid_check_cyclic(grid_t *gridptr)
       if ( xvals && xsize > 1 )
 	{
 	  xinc = xvals[1] - xvals[0];
+	  if ( IS_EQUAL(xinc, 0) )
+	    xinc = (xvals[xsize-1] - xvals[0])/(xsize-1);
 	  x0 = 2*xvals[xsize-1]-xvals[xsize-2]-360;
 	  if ( IS_NOT_EQUAL(xvals[0], xvals[xsize-1]) )
-	    if ( fabs(x0 - xvals[0]) < 1.e-4 ) gridptr->isCyclic = TRUE;
-	  /*	    if ( IS_EQUAL(x0, xvals[0]) ) gridptr->isCyclic = TRUE; */
+	    if ( fabs(x0 - xvals[0]) < 0.01*xinc ) gridptr->isCyclic = TRUE;
 	}
     }
   else if ( gridptr->type == GRID_CURVILINEAR )
     {
+      if ( xvals && xsize > 1 )
+	{
+	  double val1, val2, valn;
+
+	  nc = 0;
+	  gridptr->isCyclic = FALSE;
+	  for ( j = 0; j < ysize; ++j )
+	    {
+	      i1 = j*xsize;
+	      i2 = j*xsize+1;
+	      in = j*xsize+(xsize-1);
+	      val1 = xvals[i1];
+	      val2 = xvals[i2];
+	      valn = xvals[in];
+
+	      xinc = fabs(val2-val1);
+
+	      if ( val1 < 1 && valn > 300 ) val1 += 360;
+	      if ( valn < 1 && val1 > 300 ) valn += 360;
+	      if ( val1 < -179 && valn > 120 ) val1 += 360;
+	      if ( valn < -179 && val1 > 120 ) valn += 360;
+
+	      if ( valn > val1 ) x0 = valn - xinc;
+	      else               x0 = valn + xinc;
+
+	      if ( fabs(x0-val1) < 0.5*xinc ) nc++;
+	    }
+
+	  if ( nc > 0.5*ysize ) gridptr->isCyclic = TRUE;
+	}
+
       if ( xbounds && xsize > 1 )
 	{
 	  double val1, val2;
-- 
GitLab