Exemple #1
0
int ggrd_grdtrack_init(double *west, double *east,
		       double *south, double *north, 
		       float **f,int *mm,char *grdfile,
		       struct GRD_HEADER **grd,
		       struct GMT_EDGEINFO **edgeinfo,
		       char *edgeinfo_string, 
		       ggrd_boolean *geographic_in,
		       int *pad,ggrd_boolean three_d, 
		       char *dfile, float **z,int *nz,		
		       ggrd_boolean interpolant,
		       ggrd_boolean verbose,
		       ggrd_boolean change_depth_sign,
		       struct BCR *loc_bcr)
#endif
{
  FILE *din;
  float dz1,dz2;
  struct GRD_HEADER ogrd;
  int i,one_or_zero,nx,ny,mx,my;
  char filename[BUFSIZ*2],*cdummy;
  static int gmt_init = FALSE;
  /* 
     deal with edgeinfo 
  */
  *edgeinfo = (struct GMT_EDGEINFO *)
    GMT_memory (VNULL, (size_t)1, sizeof(struct GMT_EDGEINFO), "ggrd_grdtrack_init");
  /* init with nonsense to avoid compiler warning */
  ogrd.x_min = ogrd.y_min =ogrd.x_max = ogrd.y_max = -100;
  ogrd.x_inc = ogrd.y_inc = -1;
  ogrd.node_offset = 0;ogrd.nx = ogrd.ny = -1;
#ifndef USE_GMT3

  if(!gmt_init){
    /* this should be OK as is. init only once globally */
    GMT_program = "ggrd";
    GMT_make_fnan (GMT_f_NaN);
    GMT_make_dnan (GMT_d_NaN);
    GMT_io_init ();/* Init the table i/o structure */
    GMT_grdio_init();
    if(strcmp(edgeinfo_string,"-fg")==0){
      GMT_io.in_col_type[GMT_X] = GMT_io.out_col_type[GMT_X] = GMT_IS_LON;
      GMT_io.in_col_type[GMT_Y] = GMT_io.out_col_type[GMT_Y] = GMT_IS_LAT;
    }
    if(strcmp(edgeinfo_string,"-fx")==0){
      GMT_io.in_col_type[GMT_X] = GMT_io.out_col_type[GMT_X] = GMT_IS_LON;
    }
    if(strcmp(edgeinfo_string,"-fy")==0){
      GMT_io.in_col_type[GMT_Y] = GMT_io.out_col_type[GMT_Y] = GMT_IS_LAT;
    }
    gmt_init = TRUE;
  }

#endif
  /* 
     init first edgeinfo (period/global?)
  */
  GMT_boundcond_init (*edgeinfo);
  /* check if geographic */
  if (strlen(edgeinfo_string)>2){ /* the boundary flag was set */
    /* parse */
    GMT_boundcond_parse (*edgeinfo, (edgeinfo_string+2));
    if ((*edgeinfo)->gn)
      *geographic_in = 1;
    else if((*edgeinfo)->nxp == -1)
      *geographic_in = 2;
    else
      *geographic_in = 0;
  }else{
    *geographic_in = 0;
  }
  if(verbose >= 2)
    if(*geographic_in)
      fprintf(stderr,"ggrd_grdtrack_init: detected geographic region from geostring: %s\n",
	      edgeinfo_string);
  
  *z = (float *) GMT_memory 
    (VNULL, (size_t)1, sizeof(float), "ggrd_grdtrack_init");
 
  if(three_d){
    /*
      
    three D part first
    
    */
    /* 
       init the layers
    */
    din = fopen(dfile,"r");
    if(!din){
      fprintf(stderr,"ggrd_grdtrack_init: could not open depth file %s\n",
	      dfile);
      return 1;
    }
    /* read in the layers */
    *nz = 0;
    dz1 = -1;
    while(fscanf(din,"%f",(*z+ (*nz))) == 1){ 
      if(change_depth_sign)
	*(*z+ (*nz)) = -(*(*z+ (*nz)));
      /* read in each depth layer */
      *z = (float *) GMT_memory ((void *)(*z), (size_t)((*nz)+2), sizeof(float), "ggrd_grdtrack_init");
      if(*nz > 0){		/* check for increasing layers */
	if(dz1 < 0){	
	  /* init first interval */
	  dz1 = *(*z+(*nz)) - *(*z+(*nz)-1);
	  dz2 = dz1;
	}else{
	  /* later intervals */
	  dz2 = *(*z+(*nz)) - *(*z+(*nz)-1);
	}
	if(dz2 <= 0.0){		/* check for monotonic increase */
	  fprintf(stderr,"%s: error: levels in %s have to increase monotonically: n: %i dz; %g\n",
		  "ggrd_grdtrack_init",dfile,*nz,dz2);
	  return 2;
	}
      }
      *nz += 1;
    }
    fclose(din);
    /* end layer init"ggrd_grdtrack_initialization */
    if(*nz < 2){
      fprintf(stderr,"%s: error: need at least two layers in %s\n",
	      "ggrd_grdtrack_init", dfile);
      return 3;
    }
    if(verbose)
      fprintf(stderr,"%s: read %i levels from %s between zmin: %g and zmax: %g\n",
	      "ggrd_grdtrack_init",*nz,dfile,*(*z+0),*(*z+(*nz)-1));
  }else{
    *nz = 1;
    *(*z) = 0.0;
    if(verbose >= 2)
      fprintf(stderr,"ggrd_grdtrack_init: single level at z: %g\n",*(*z));
  }
  /* 
     get nz grd and edgeinfo structures 
  */
  *grd = (struct GRD_HEADER *)
    GMT_memory (NULL, (size_t)(*nz), sizeof(struct GRD_HEADER), "ggrd_grdtrack_init");
  *edgeinfo = (struct GMT_EDGEINFO *)
    GMT_memory (*edgeinfo, (size_t)(*nz), sizeof(struct GMT_EDGEINFO), "ggrd_grdtrack_init");
  if(verbose >= 2)
    fprintf(stderr,"ggrd_grdtrack_init: mem alloc ok\n");
#ifndef USE_GMT3  
  /* init the header */
  GMT_grd_init (*grd,0,&cdummy,FALSE);
#endif
  if(*nz == 1){
    if(verbose >= 2)
      
#ifdef USE_GMT3		/* old */
      fprintf(stderr,"ggrd_grdtrack_init: opening single file %s, GMT3 mode\n",grdfile);
    if (GMT_cdf_read_grd_info (grdfile,(*grd))) {
      fprintf (stderr, "%s: error opening file %s\n", 
	       "ggrd_grdtrack_init", grdfile);
      return 4;
    }
   
#else  /* >=4.1.2 */
    if(verbose >= 2)
      fprintf(stderr,"ggrd_grdtrack_init: opening single file %s, GMT4 mode\n",grdfile);
    if(GMT_read_grd_info (grdfile,*grd)){
      fprintf (stderr, "%s: error opening file %s for header\n", 
	       "ggrd_grdtrack_init", grdfile);
      return 4;
    }
#endif 
  }else{
    /* loop through headers for testing purposess */
    for(i=0;i<(*nz);i++){
      sprintf(filename,"%s.%i.grd",grdfile,i+1);
#ifdef USE_GMT3
      if (GMT_cdf_read_grd_info (filename, (*grd+i))) {
	fprintf (stderr, "%s: error opening file %s (-D option was used)\n", 
		 "ggrd_grdtrack_init", filename);
	return 6;
      }
#else  /* gmt 4 */
      if (GMT_read_grd_info (filename,(*grd+i))) {
	fprintf (stderr, "%s: error opening file %s (-D option was used)\n", 
		 "ggrd_grdtrack_init", filename);
	return 6;
      }
#endif
      if(i == 0){
	/* save the first grid parameters */
	ogrd.x_min = (*grd)[0].x_min;
	ogrd.y_min = (*grd)[0].y_min;
	ogrd.x_max = (*grd)[0].x_max;
	ogrd.y_max = (*grd)[0].y_max;
	ogrd.x_inc = (*grd)[0].x_inc;
	ogrd.y_inc = (*grd)[0].y_inc;
	ogrd.node_offset = (*grd)[0].node_offset;
	ogrd.nx = (*grd)[0].nx;
	ogrd.ny = (*grd)[0].ny;
	/* 
	   
	make sure we are in 0 ... 360 system

	*/
	if((ogrd.x_min < 0)||(ogrd.x_max<0)){
	  fprintf(stderr,"%s: WARNING: geographic grids should be in 0..360 lon system (found %g - %g)\n",
		  "ggrd_grdtrack_init",ogrd.x_min,ogrd.x_max);
	}
      }else{
	/* test */
	if((fabs(ogrd.x_min -  (*grd)[i].x_min)>5e-7)||
	   (fabs(ogrd.y_min -  (*grd)[i].y_min)>5e-7)||
	   (fabs(ogrd.x_max -  (*grd)[i].x_max)>5e-7)||
	   (fabs(ogrd.y_max -  (*grd)[i].y_max)>5e-7)||
	   (fabs(ogrd.x_inc -  (*grd)[i].x_inc)>5e-7)||
	   (fabs(ogrd.y_inc -  (*grd)[i].y_inc)>5e-7)||
	   (fabs(ogrd.nx    -  (*grd)[i].nx)>5e-7)||
	   (fabs(ogrd.ny    -  (*grd)[i].ny)>5e-7)||
	   (fabs(ogrd.node_offset - (*grd)[i].node_offset)>5e-7)){
	  fprintf(stderr,"%s: error: grid %i out of %i has different dimensions or setting from first\n",
		 "ggrd_grdtrack_init",i+1,(*nz));
	  return 8;
	}
      }
    }
  }
  if(verbose > 2)
    fprintf(stderr,"ggrd_grdtrack_init: read %i headers OK, grids appear to be same size\n",*nz);
  if (fabs(*west - (*east)) < 5e-7) {	/* No subset asked for , west same as east*/
    *west =  (*grd)[0].x_min;
    *east =  (*grd)[0].x_max;
    *south = (*grd)[0].y_min;
    *north = (*grd)[0].y_max;
  }
  one_or_zero = ((*grd)[0].node_offset) ? 0 : 1;
  nx = irint ( (*east - *west) / (*grd)[0].x_inc) + one_or_zero;
  ny = irint ( (*north - *south) / (*grd)[0].y_inc) + one_or_zero;
  /* real size of data */
  //nn = nx * ny;

  /* padded */
  mx = nx + 4;
  my = ny + 4;
  /* 
     get space for all layers
  */
  *mm = mx * my;

  *f = (float *) calloc((*mm) * (*nz) ,sizeof (float));
  if(!(*f)){
    fprintf(stderr,"ggrd_grdtrack_init: f memory error, mm: %i (%i by %i) by nz: %i \n",*mm,mx,my, *nz);
    return 9;
  }
  if(verbose >= 2){
    fprintf(stderr,"ggrd_grdtrack_init: mem alloc 2 ok, %g %g %g %g %i %i\n",
	    *west,*east,*south,*north,nx,ny);
  }
  /* 
     pad on sides 
  */
  pad[0] = pad[1] = pad[2] = pad[3] = 2;
  for(i=0;i < (*nz);i++){
    /* 
       loop through layers
    */
    if(i != 0)			/* copy first edgeinfo over */
      memcpy((*edgeinfo+i),(*edgeinfo),sizeof(struct GMT_EDGEINFO));
    if((*nz) == 1){
      sprintf(filename,"%s",grdfile);
    }else{			/* construct full filename */
      sprintf(filename,"%s.%i.grd",grdfile,i+1);
    }
    if (verbose) 
      fprintf(stderr,"ggrd_grdtrack_init: reading grd file %s (%g - %g (%i) %g - %g (%i); geo: %i flag: %s\n",
	      filename,*west,*east,nx,*south,*north,ny,
	      *geographic_in,edgeinfo_string);

    /* 
       read the grd files
    */
#ifndef USE_GMT3
    /* GMT 4 */
    if (GMT_read_grd (filename,(*grd+i), (*f+i* (*mm)), 
		      *west, *east, *south, *north, 
		      pad, FALSE)) {
      fprintf (stderr, "%s: error reading file %s\n", "ggrd_grdtrack_init", grdfile);
      return 10;
    }
    //fprintf(stderr,"%g %g %i %i %i %i\n",(*grd)->z_scale_factor,(*grd)->z_add_offset,nx,ny,mx,my);
#else
    /* old GMT */
    if (GMT_cdf_read_grd (filename, (*grd+i), (*f+i* (*mm)), 
			  *west, *east, *south, *north, 
			  pad, FALSE)) {
      fprintf (stderr, "%s: error reading file %s\n", "ggrd_grdtrack_init", grdfile);
      return 10;
    }
#endif
    /* 
       prepare the boundaries 
    */
    GMT_boundcond_param_prep ((*grd+i), (*edgeinfo+i));
    if(i == 0){
      
      /* 
	 Initialize bcr structure, this can be the same for 
	 all grids as long as they have the same dimensions

      */
#ifndef USE_GMT3
      GMT_bcr_init ((*grd+i), pad, interpolant,1.0,loc_bcr);
#else
      my_GMT_bcr_init ((*grd+i), pad, interpolant,loc_bcr);
#endif
     }
    /* Set boundary conditions  */
    GMT_boundcond_set ((*grd+i), (*edgeinfo+i), pad, 
		       (*f+i*(*mm)));
  } /* end layer loop */
  if(verbose){
    ggrd_print_layer_avg(*f,*z,mx,my,*nz,stderr,pad);
  }
  return 0;

}
Exemple #2
0
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {

	int i, j, nx, ny, n_read = 0, n_points = 0, one_or_zero;
	int n_output = 0, n_fields, n_pts, ii, jj, GMT_pad[4];
	
	int error = FALSE, suppress = FALSE, node = FALSE, z_only = FALSE;
	int is_double = FALSE, is_single = FALSE, is_int32 = FALSE, is_int16 = FALSE;
	int is_uint16 = FALSE, is_uint8 = FALSE, is_int8 = FALSE;
	int free_copy = TRUE, need_padding = FALSE, row_maj = FALSE;
	
	double value, west, east, south, north, threshold = 1.0, i_dx, i_dy, half, *in, *out;
	float *f;

	int	i2, argc = 0, nc_h, nr_h, mx, n_arg_no_char = 0, *i_4, interpolant = BCR_BICUBIC;
	short int *i_2;
	unsigned short int *ui_2;
	char	**argv, *i_1;
	unsigned char *ui_1;
	float	*z_4;
	double	*pdata_d, *z_8, *head;
	
	struct GRD_HEADER grd;
	struct GMT_EDGEINFO edgeinfo;
	struct GMT_BCR bcr;

	argc = nrhs;
	for (i = 0; i < nrhs; i++) {		/* Check input to find how many arguments are of type char */
		if(!mxIsChar(prhs[i])) {
			argc--;
			n_arg_no_char++;	/* Number of arguments that have a type other than char */
		}
	}
	argc++;			/* to account for the program's name to be inserted in argv[0] */

	/* get the length of the input string */
	argv = (char **)mxCalloc(argc, sizeof(char *));
	argv[0] = "grdtrack_m";
	for (i = 1; i < argc; i++)
		argv[i] = (char *)mxArrayToString(prhs[i+n_arg_no_char-1]);

	west = east = south = north = 0.0;
	
	GMT_boundcond_init (&edgeinfo);

	for (i = 1; i < argc; i++) {
		if (argv[i][0] == '-') {
			switch (argv[i][1]) {
			
				case 'R':
					error += decode_R (argv[i], &west, &east, &south, &north);
					break;
				case 'L':
					if (argv[i][2]) {
						error += GMT_boundcond_parse (&edgeinfo, &argv[i][2]);
						/*if (edgeinfo.gn) {
							GMT_io.in_col_type[0] = GMT_io.out_col_type[0] = GMT_IS_LON;
							GMT_io.in_col_type[1] = GMT_io.out_col_type[1] = GMT_IS_LAT;
						}*/
					}
					/*else {
						GMT_io.in_col_type[0] = GMT_io.out_col_type[0] = GMT_IS_LON;
						GMT_io.in_col_type[1] = GMT_io.out_col_type[1] = GMT_IS_LAT;
					}*/
					break;
				case 'N':
					node = TRUE;
					break;
				case 'Q':
					interpolant = BCR_BILINEAR;
					threshold = (argv[i][2]) ? atof (&argv[i][2]) : 1.0;
					break;
				case 'S':
					suppress = TRUE;
					break;
				case 'Z':
					z_only = TRUE;
					break;
				default:
					error = TRUE;
					break;
			}
		}
	}
	
	if (argc == 1 || error) {
		mexPrintf ("grdtrack - Sampling of a 2-D gridded netCDF grdfile along 1-D trackline\n\n");
		mexPrintf ("usage: out = grdtrack_m(grd,head,xydata, ['-L<flag>'], ['-N']\n"); 
		mexPrintf ("\t['-Q[<value>]'], ['-R<west/east/south/north>[r]'] ['-S'] ['-Z'] ['-f[i|o]<colinfo>']\n");
				
		mexPrintf ("\t<xydata> is an multicolumn array with (lon,lat) in the first two columns\n");
		mexPrintf ("\n\tOPTIONS:\n");
		mexPrintf ("\t-L sets boundary conditions.  <flag> can be either\n");
		mexPrintf ("\t   g for geographic boundary conditions\n");
		mexPrintf ("\t   or one or both of\n");
		mexPrintf ("\t   x for periodic boundary conditions on x\n");
		mexPrintf ("\t   y for periodic boundary conditions on y\n");
		mexPrintf ("\t-N Report value at nearest node instead of interpolating\n");
		mexPrintf ("\t-Q Quick mode, use bilinear rather than bicubic interpolation.\n");
		mexPrintf ("\t   Optionally, append <value> in the 0 < value <= 1 range.\n");
		mexPrintf ("\t   [Default = 1 requires all 4 nodes to be non-NaN.], <value> = 0.5\n");
		mexPrintf ("\t   will interpolate about 1/2 way from a non-NaN to a NaN node, while\n");
		mexPrintf ("\t   0.1 will go about 90%% of the way, etc.\n");
		mexPrintf ("\t-R specifies a subregion [Default is old region]\n");
		mexPrintf ("\t-S Suppress output when result equals NaN\n");
		mexPrintf ("\t-Z only output z-values [Default gives all columns]\n");
		mexPrintf ("\n\tSECRET INFO:\n");
		mexPrintf ("\t   When input points are inside the outer skirt of 2 rows and columns of\n");
		mexPrintf ("\t   the 2-D grid we don't need to set boundary conditions and as\n");
		mexPrintf ("\t   such we can use the input array without further to C order\n");
		mexPrintf ("\t   conversion (to row major). This save a lot of memory and execution\n");
		mexPrintf ("\t   time. However, this possibility works only when input 2-D array is\n");
		mexPrintf ("\t   of tipe SINGLE (though the computations are all done in doubles).\n");
		mexPrintf ("\t   The other cases request using a temporary array of size (M+2)x(N+2)\n");
		return;
	}

	if (threshold <= 0.0 || threshold > 1.0) {
		mexPrintf ("GRDTRACK_M SYNTAX ERROR -Q:  threshold must be in <0,1] range\n");
		error++;
	}
	if (error) return;

	if (nlhs == 0) {
		mexPrintf("ERROR: Must provide an output.\n");
		return;
	}

	/* Find out in which data type was given the input array */
	if (mxIsDouble(prhs[0])) {
		z_8 = mxGetPr(prhs[0]);
		is_double = TRUE;
	}
	else if (mxIsSingle(prhs[0])) {
		z_4 = mxGetData(prhs[0]);
		is_single = TRUE;
	}
	else if (mxIsInt32(prhs[0])) {
		i_4 = mxGetData(prhs[0]);
		is_int32 = TRUE;
	}
	else if (mxIsInt16(prhs[0])) {
		i_2 = mxGetData(prhs[0]);
		is_int16 = TRUE;
	}
	else if (mxIsUint16(prhs[0])) {
		ui_2 = mxGetData(prhs[0]);
		is_uint16 = TRUE;
	}
	else if (mxIsUint8(prhs[0])) {
		ui_1 = mxGetData(prhs[0]);
		is_uint8 = TRUE;
	}
	else if (mxIsInt8(prhs[0])) {
		i_1 = mxGetData(prhs[0]);
		is_int8 = TRUE;
	}
	else {
		mexPrintf("GRDTRACK ERROR: Unknown input data type.\n");
		mexErrMsgTxt("Valid types are:double, single, Int32, Int16, UInt16, UInt8 and Int8.\n");
	}

	nx = mxGetN (prhs[0]);
	ny = mxGetM (prhs[0]);
	if (!mxIsNumeric(prhs[0]) || ny < 2 || nx < 2)
		mexErrMsgTxt("First argument must contain a decent array\n");

	nc_h = mxGetN (prhs[1]);
	nr_h = mxGetM (prhs[1]);
	if (!mxIsNumeric(prhs[1]) || nr_h > 1 || nc_h < 9)
		mexErrMsgTxt("Second argument must contain a valid header of the input array.\n");
	
	head  = mxGetPr(prhs[1]);		/* Get header info */

	/* Check that thirth argument contains at least a mx2 table */
	n_pts = mxGetM (prhs[2]);
	n_fields = mxGetN(prhs[2]);
	if (!mxIsNumeric(prhs[2]) || (n_fields < 2))
		mexErrMsgTxt("GRDTRACK ERROR: thirth argument must contain the x,y positions where to interpolate.\n");
	if (z_only)
		n_fields = 0;

	/* Read the interpolation points and convert them to double */
	if (mxIsDouble(prhs[2]))
		in = mxGetPr(prhs[2]);
	else if (mxIsSingle(prhs[2]))
		in = mxGetData(prhs[2]);

	grd.x_min = head[0];	grd.x_max = head[1];
	grd.y_min = head[2];	grd.y_max = head[3];
	grd.z_min = head[4];	grd.z_max = head[5];
	grd.x_inc = head[7];	grd.y_inc = head[8];
	grd.nx = nx;			grd.ny = ny;
	grd.node_offset = irint(head[6]);
	mx = nx + 4;

	if (west == east) {	/* No subset asked for */
		west = grd.x_min;
		east = grd.x_max;
		south = grd.y_min;
		north = grd.y_max;
	}
	one_or_zero = (grd.node_offset) ? 0 : 1;
	half = (grd.node_offset) ? 0.5 : 0.0;
	nx = irint ( (east - west) / grd.x_inc) + one_or_zero;
	ny = irint ( (north - south) / grd.y_inc) + one_or_zero;
	i_dx = 1.0 / grd.x_inc;
	i_dy = 1.0 / grd.y_inc;

	if (!node) {
		/* If we don't have any point inside the two outer row/columns
		   there is no need to set boundary conditions plus all the extra
		   ovehead that it implies. So check it out here. */
		int n;
		double this_xmin, this_xmax, this_ymin, this_ymax;
		n = (interpolant == BCR_BILINEAR) ? 1 : 2;
		this_xmin = grd.x_min + n * grd.x_inc;
		this_xmax = grd.x_max - n * grd.x_inc;
		this_ymin = grd.y_min + n * grd.y_inc;
		this_ymax = grd.y_max - n * grd.y_inc;
		for (i = 0; i < n_pts; i++) {
			if (in[i] < this_xmin || in[i] > this_xmax) {
				need_padding = TRUE;
				break;
			}
			if (in[i+n_pts] < this_ymin || in[i+n_pts] > this_ymax) {
				need_padding = TRUE;
				break;
			}
		}
	}

#if original_GMT_code
	need_padding = TRUE;
#endif

	if (need_padding) row_maj = TRUE;	/* Here we have to use the old row major code */

	if (!need_padding) {		/* We can use the column major order of the Matlab array */

		if (!is_single)
			f = mxCalloc (nx * ny, sizeof (float));

		if (is_double) 
			for (j = 0; j < nx*ny; j++) f[j] = (float)z_8[j];

		else if (is_single) {
			f = z_4;
			free_copy = FALSE;	/* Signal that we shouldn't free f */
		}

		else if (is_int32)
			for (j = 0; j < nx*ny; j++) f[j] = (float)i_4[j];

		else if (is_int16)
			for (j = 0; j < nx*ny; j++) f[j] = (float)i_2[j];

		else if (is_uint16)
			for (j = 0; j < nx*ny; j++) f[j] = (float)ui_2[j];

		else if (is_uint8)
			for (j = 0; j < nx*ny; j++) f[j] = (float)ui_1[j];

		else if (is_int8)
			for (j = 0; j < nx*ny; j++) f[j] = (float)i_1[j];

		GMT_pad[0] = GMT_pad[1] = GMT_pad[2] = GMT_pad[3] = 0;
	}

	else {

		f = mxCalloc ((nx+4)*(ny+4), sizeof (float));

		/* Transpose from Matlab orientation to gmt grd orientation */
		if (is_double) {
			for (i = 0, i2 = ny - 1; i < ny; i++, i2--) {
				ii = (i2 + 2)*mx + 2;
				for (j = 0; j < nx; j++) f[ii + j] = (float)z_8[j*ny+i];
			}
		}
		else if (is_single) {
			for (i = 0, i2 = ny - 1; i < ny; i++, i2--) {
				ii = (i2 + 2)*mx + 2;
				for (j = 0; j < nx; j++) f[ii + j] = z_4[j*ny+i];
			}
		}
		else if (is_int32) {
			for (i = 0, i2 = ny - 1; i < ny; i++, i2--) {
				ii = (i2 + 2)*mx + 2;
				for (j = 0; j < nx; j++) f[ii + j] = (float)i_4[j*ny+i];
			}
		}
		else if (is_int16) {
			for (i = 0, i2 = ny - 1; i < ny; i++, i2--) {
			ii = (i2 + 2)*mx + 2;
				for (j = 0; j < nx; j++) f[ii + j] = (float)i_2[j*ny+i];
			}
		}
		else if (is_uint16) {
			for (i = 0, i2 = ny - 1; i < ny; i++, i2--) {
				ii = (i2 + 2)*mx + 2;
				for (j = 0; j < nx; j++) f[ii + j] = (float)ui_2[j*ny+i];
			}
		}
		else if (is_uint8) {
			for (i = 0, i2 = ny - 1; i < ny; i++, i2--) {
				ii = (i2 + 2)*mx + 2;
				for (j = 0; j < nx; j++) f[ii + j] = (float)ui_1[j*ny+i];
			}
		}
		else if (is_int8) {
			for (i = 0, i2 = ny - 1; i < ny; i++, i2--) {
				ii = (i2 + 2)*mx + 2;
				for (j = 0; j < nx; j++) f[ii + j] = (float)i_1[j*ny+i];
			}
		}

		GMT_pad[0] = GMT_pad[1] = GMT_pad[2] = GMT_pad[3] = 2;

		GMT_boundcond_param_prep (&grd, &edgeinfo);
	}
	
	/*project_info.w = west;	project_info.e = east;
	project_info.s = south;	project_info.n = north;*/
	
	/* Initialize bcr structure:  */

	GMT_bcr_init (&grd, GMT_pad, interpolant, threshold, &bcr);

	if (need_padding)
		/* Set boundary conditions  */
		GMT_boundcond_set (&grd, &edgeinfo, GMT_pad, f);
	
	if ((out = mxCalloc(n_pts * (n_fields+1), sizeof (double))) == 0)
		mexErrMsgTxt("GRDTRACK ERROR: Could not allocate memory\n");

	for (i = 0; i < n_pts; i++) {
		while ( (mxIsNaN(in[i]) || mxIsNaN(in[i+n_pts])) && !z_only) {
			for (j = 0; j < n_fields; j++) out[j*n_pts+i] = in[j*n_pts+i];
			out[j*n_pts+i] = mxGetNaN();
			i++;
		}

		/* If point is outside grd area, shift it using periodicity or skip if not periodic. */
		while ( (in[i+n_pts] < grd.y_min) && (edgeinfo.nyp > 0) ) in[i+n_pts] += (grd.y_inc * edgeinfo.nyp);
		if (in[i+n_pts] < grd.y_min) continue;

		while ( (in[i+n_pts] > grd.y_max) && (edgeinfo.nyp > 0) ) in[i+n_pts] -= (grd.y_inc * edgeinfo.nyp);
		if (in[i+n_pts] > grd.y_max) continue;

		while ( (in[i] < grd.x_min) && (edgeinfo.nxp > 0) ) in[i] += (grd.x_inc * edgeinfo.nxp);
		if (in[i] < grd.x_min) continue;

		while ( (in[i] > grd.x_max) && (edgeinfo.nxp > 0) ) in[i] -= (grd.x_inc * edgeinfo.nxp);
		if (in[i] > grd.x_max) continue;
		
		if (node) {
			ii = irint ((in[i] - grd.x_min) * i_dx - half) + one_or_zero;
			jj = irint ((grd.y_max - in[i+n_pts]) * i_dy - half) + one_or_zero;
			value = f[(jj+GMT_pad[3])*mx+ii+GMT_pad[0]];
		}
		else
			value = GMT_get_bcr_z(&grd, in[i], in[i+n_pts], f, &edgeinfo, &bcr, row_maj);

		if (suppress && mxIsNaN (value)) continue;

		if (z_only) {	/* Simply print out value */
			out[i] = value;
		}
		else {	/* Simply copy other columns, append value, and output */
			for (j = 0; j < n_fields; j++) out[j*n_pts+i] = in[j*n_pts+i];
			out[j*n_pts+i] = value;
		}
	}

	/*if (!(!need_padding && !is_single)) {
		mexPrintf("Merda vou Friar %d\t%d\n", need_padding, is_single);
		mxFree((void *)f);
	}*/
	if (free_copy)
		mxFree((void *)f);

	plhs[0] = mxCreateDoubleMatrix (n_pts,n_fields+1, mxREAL);
	pdata_d = mxGetPr(plhs[0]);
	memcpy(pdata_d, out, n_pts*(n_fields+1)*8);
	mxFree(out);
}