⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mexcdf53.c

📁 MATLAB中读写、处理科学数据文件格式NETCDF的程序
💻 C
📖 第 1 页 / 共 4 页
字号:
	)

{
	int		i;
	int		j;
		
# if defined __DATE__
# if defined __TIME__

# if !defined __FILE__
# define	__FILE__	"mexcdf53.c"
# endif

	mexPrintf("Program %s Version %s %s\n", __FILE__, __DATE__, __TIME__);
	
# endif
# endif
	
	if (VERBOSE)	{
		mexPrintf("\n");
		mexPrintf("mexcdf53(\'USAGE\')\n");
		mexPrintf("\n");
		mexPrintf("mexcdf53(\'CREATE\', \'path\', cmode) ==> [cdfid, status]\n");
		mexPrintf("mexcdf53(\'OPEN\', \'path\', mode) ==> [cdfid, status]\n");
		mexPrintf("mexcdf53(\'REDEF\', cdfid) ==> status\n");
		mexPrintf("mexcdf53(\'ENDEF\', cdfid) ==> status\n");
		mexPrintf("mexcdf53(\'INQUIRE\', cdfid) ==> [ndims, nvars, natts, recdim, status]\n");
		mexPrintf("mexcdf53(\'SYNC\', cdfid) ==> status\n");
		mexPrintf("mexcdf53(\'ABORT\', cdfid) ==> status\n");
		mexPrintf("mexcdf53(\'CLOSE\', cdfid) ==> status\n");
		mexPrintf("\n");
		mexPrintf("mexcdf53(\'DIMDEF\', cdfid, \'name\', length) ==> [dimid, status]\n");
		mexPrintf("mexcdf53(\'DIMID\', cdfid, \'name\') ==> [dimid, status]\n");
		mexPrintf("mexcdf53(\'DIMINQ\', cdfid, dimid) ==> [\'name\', length, status]\n");
		mexPrintf("mexcdf53(\'DIMRENAME\', cdfid, \'name\') ==> status\n");
		mexPrintf("\n");
		mexPrintf("mexcdf53(\'VARDEF\', cdfid, \'name\', datatype, ndims, [dim]) ==> [varid, status]\n");
		mexPrintf("mexcdf53(\'VARID\', cdfid, \'name\') ==> [varid, status]\n");
		mexPrintf("mexcdf53(\'VARINQ\', cdfid, varid) ==> [\'name\', datatype, ndims, [dim], natts, status]\n");
		mexPrintf("mexcdf53(\'VARPUT1\', cdfid, varid, [coords], value, autoscale) ==> status\n");
		mexPrintf("mexcdf53(\'VARGET1\', cdfid, varid, [coords], flag) ==> [value, status]\n");
		mexPrintf("mexcdf53(\'VARPUT\', cdfid, varid, [start], [count], [value], autoscale) ==> status\n");
		mexPrintf("mexcdf53(\'VARGET\', cdfid, varid, [start], [count], autoscale) ==> [[value], status]\n");
		mexPrintf("mexcdf53(\'VARPUTG\', cdfid, varid, [start], [count], [stride], [imap], [value], autoscale) ==> status\n");
		mexPrintf("mexcdf53(\'VARGETG\', cdfid, varid, [start], [count], [stride], [imap], autoscale) ==> [[value], status]\n");
		mexPrintf("mexcdf53(\'VARRENAME\', cdfid, varid, \'name\') ==> status\n");
		mexPrintf("\n");
		mexPrintf("mexcdf53(\'ATTPUT\', cdfid, varid, \'name\', datatype, len, [value]) ==> status\n");
		mexPrintf("mexcdf53(\'ATTINQ\', cdfid, varid, \'name\') ==> [datatype, len, status]\n");
		mexPrintf("mexcdf53(\'ATTGET\', cdfid, varid, \'name\') ==> [[value], len, status]\n");
		mexPrintf("mexcdf53(\'ATTCOPY\', incdf, invar, \'name\', outcdf, outvar) ==> status\n");
		mexPrintf("mexcdf53(\'ATTNAME\', cdfid, varid, attnum) ==> [\'name\', status]\n");
		mexPrintf("mexcdf53(\'ATTRENAME\', cdfid, varid, \'name\', \'newname\') ==> status\n");
		mexPrintf("mexcdf53(\'ATTDEL\', cdfid, varid, \'name\') ==> status\n");
		mexPrintf("\n");
		mexPrintf("mexcdf53(\'TYPELEN\', datatype) ==> [len, status]\n");
		mexPrintf("mexcdf53(\'SETFILL\', cdfid, fillmode) ==> [old_fillmode, status]\n");
		mexPrintf("mexcdf53(\'SETOPTS\', ncopts) ==> [old_ncopts, status]\n");
		mexPrintf("mexcdf53(\'ERR\') ==> [ncerr, status]\n");
		mexPrintf("mexcdf53(\'PARAMETER\', \'NC_...\') ==> [code, status]\n");
	}
	
	else	{
	
		i = 0;
		while (ops[i].opcode != NONE)	{
		
			mexPrintf("mexcdf53(\'%s\'", ops[i].opname);
			for (j = 1; j < ops[i].nrhs; j++)	{
				mexPrintf(", in%d", j);
			}
			mexPrintf(")");
			if (ops[i].nlhs > 0)	{
				mexPrintf(" ==> [");
				for (j = 1; j <= ops[i].nlhs; j++)	{
					mexPrintf("out%d", j);
					if (j < ops[i].nlhs)	{
						mexPrintf(", ");
					}
				}
				mexPrintf("]");
			}
			mexPrintf("\n");
			
			i++;
		}
	}
	
	return;
}


/*	Parameter(): Get NetCDF parameter by name.	*/

static int
Parameter	(
	Matrix	*	mat
	)

{
	int			parameter;
	char	*	p;
	char	*	q;
	int			i;
	
	parameter = -1;
	
	if (mxIsNumeric(mat))	{
		parameter = Scalar2Int(mat);
	}
	else	{
		p = Mat2Str(mat);
		q = p;
		for (i = 0; i < strlen(p); i++)	{
			*q = (char) toupper((int) *q);
			q++;
		}
		if (strncmp(p, "NC_", 3) == 0)	{	/*	Trim away "NC_".	*/
			q = p + 3;
		}
		else	{
			q = p;
		}
		
		i = 0;
		while (strcmp(parms[i].name, "NONE") != 0)	{
			if (strncmp(q, parms[i].name, parms[i].len) == 0)	{
				parameter = parms[i].code;
				break;
			}
			else	{
				i++;
			}
		}
		
		Free ((VOIDPP) & p);
	}
	
	return (parameter);
}


/*	Scale_Factor: Return "scale_factor" attribute as DOUBLE.	*/

static DOUBLE
Scale_Factor	(
	int	cdfid,
	int	varid
	)

{
	int			status;
	nc_type		datatype;
	int			len;
	char		value[32];
	DOUBLE		d;
	
	d = 1.0;
	
	if ((status = ncattinq(cdfid, varid, "scale_factor", &datatype, &len)) == -1)	{
	}
	else if ((status = ncattget(cdfid, varid, "scale_factor", value)) == -1)	{
	}
	else	{
		switch (RepairBadDataType(datatype))	{
			case NC_BYTE:
				d = (DOUBLE) *((signed char *) value);
				break;
			case NC_CHAR:
				d = (DOUBLE) *((char *) value);
				break;
			case NC_SHORT:
				d = (DOUBLE) *((short *) value);
				break;
			case NC_LONG:
				d = (DOUBLE) *((nclong *) value);
				break;
			case NC_FLOAT:
				d = (DOUBLE) *((float *) value);
				break;
			case NC_DOUBLE:
				d = (DOUBLE) *((double *) value);
				break;
			default:
				break;
		}
	}
	
	return (d);
}


/*	Add_Offset: Return "add_offset" attribute as DOUBLE.	*/

static DOUBLE
Add_Offset	(
	int	cdfid,
	int	varid
	)

{
	int			status;
	nc_type		datatype;
	int			len;
	char		value[32];
	DOUBLE		d;
	
	d = 0.0;
	
	if ((status = ncattinq(cdfid, varid, "add_offset", &datatype, &len)) == -1)	{
	}
	else if ((status = ncattget(cdfid, varid, "add_offset", value)) == -1)	{
	}
	else	{
		switch (RepairBadDataType(datatype))	{
			case NC_BYTE:
				d = (DOUBLE) *((signed char *) value);
				break;
			case NC_CHAR:
				d = (DOUBLE) *((char *) value);
				break;
			case NC_SHORT:
				d = (DOUBLE) *((short *) value);
				break;
			case NC_LONG:
				d = (DOUBLE) *((nclong *) value);
				break;
			case NC_FLOAT:
				d = (DOUBLE) *((float *) value);
				break;
			case NC_DOUBLE:
				d = (DOUBLE) *((double *) value);
				break;
			default:
				break;
		}
	}
	
	return (d);
}


/*	SetNum(): Convert matrix to numeric matrix.	*/

static Matrix *
SetNum	(
	Matrix	*	mat
	)

{
	Matrix	*	result = NULL;
	int			status;
	
	if (mxIsString(mat))	{
		mexSetTrapFlag(1);
		status = mexCallMATLAB(1, & result, 1, & mat, "abs");
		if (status == 1)	{
			result = NULL;
		}
		mexSetTrapFlag(0);
	}
	
	return (result);
}


/*	SetStr(): Convert matrix to string matrix.	*/

static Matrix *
SetStr	(
	Matrix	*	mat
	)

{
	Matrix	*	result = NULL;
	int			status;
	
	if (mxIsNumeric(mat))	{
		mexSetTrapFlag(1);
		status = mexCallMATLAB(1, & result, 1, & mat, "setstr");
		if (status == 1)	{
			result = NULL;
		}
		mexSetTrapFlag(0);
	}
	
	return (result);
}


/*	Mat2Str(): Return string from a string-matrix.	*/

static char	*
Mat2Str	(
	Matrix	*	mat
	)

{
	DOUBLE	*	pr;
	char	*	p;
	char	*	str;
	int			len;
	int			i;

	len = mxGetM(mat) * mxGetN(mat);
	
	str = (char *) mxCalloc(len + 1, sizeof(char));
	
	mxGetString(mat, str, len + 1);
	
	return (str);
}


/*	Str2Mat():	Convert string into a string-matrix.	*/

static Matrix *
Str2Mat	(
	char	*	str
	)

{
	Matrix	*	mat;

	mat = mxCreateString(str);
	
	return (mat);
}


/*	Mat2Long(): Return matrix values as a long integer array.	*/

static long *
Mat2Long	(
	Matrix	*	mat
	)

{
	DOUBLE	*	pr;
	long	*	plong;
	long	*	p;
	int			len;
	int			i;

	len = mxGetM(mat) * mxGetN(mat);
	
	plong = (long *) mxCalloc(len, sizeof(long));
	p = plong;
	pr = mxGetPr(mat);
	
	for (i = 0; i < len; i++)	{
		*p++ = (long) *pr++;
	}
	
	return (plong);
}


/*	Long2Mat(): Convert long integer array to a matrix.	*/

static Matrix *
Long2Mat	(
	long	*	plong,
	int			m,
	int			n
	)

{
	Matrix	*	mat;
	DOUBLE	*	pr;
	long	*	p;
	int			len;
	int			i;

	mat = mxCreateFull(m, n, REAL);
	
	pr = mxGetPr(mat);
	p = plong;
	
	len = m * n;
	for (i = 0; i < len; i++)	{
		*pr++ = (long) *p++;
	}
	
	return (mat);
}


/*	Mat2Int(): Return matrix values as an integer array.	*/

static int *
Mat2Int	(
	Matrix	*	mat
	)

{
	DOUBLE	*	pr;
	int		*	pint;
	int		*	p;
	int			len;
	int			i;

	len = mxGetM(mat) * mxGetN(mat);
	
	pint = (int *) mxCalloc(len, sizeof(int));
	p = pint;
	pr = mxGetPr(mat);
	
	for (i = 0; i < len; i++)	{
		*p++ = (int) *pr++;
	}
	
	return (pint);
}


/*	Int2Mat(): Convert integer array to a matrix.	*/

static Matrix *
Int2Mat	(
	int		*	pint,
	int			m,
	int			n
	)

{
	Matrix	*	mat;
	DOUBLE	*	pr;
	int	*	p;
	int			len;
	int			i;

	mat = mxCreateFull(m, n, REAL);
	
	pr = mxGetPr(mat);
	p = pint;
	
	len = m * n;
	for (i = 0; i < len; i++)	{
		*pr++ = (int) *p++;
	}
	
	return (mat);
}


/*	Int2Scalar(): Convert integer value to a scalar matrix.	*/

static Matrix *
Int2Scalar	(
	int		i
	)

{
	Matrix	*	scalar;
	
	scalar = mxCreateFull(1, 1, REAL);
	
	*(mxGetPr(scalar)) = (DOUBLE) i;
	
	return (scalar);
}


/*	Scalar2Int(): Return integer value of a scalar matrix.*/

static int
Scalar2Int	(
	Matrix	*	scalar
	)

{
	return ((int) *(mxGetPr(scalar)));
}


/*	Long2Scalar(): Convert long integer value to a scalar matrix.	*/

static Matrix *
Long2Scalar	(
	long		along
	)

{
	Matrix	*	scalar;
	
	scalar = mxCreateFull(1, 1, REAL);
	
	*(mxGetPr(scalar)) = (DOUBLE) along;
	
	return (scalar);
}


/*	Scalar2Long(): Return long integer value of a scalar matrix.	*/

static long
Scalar2Long	(
	Matrix	*	scalar
	)

{
	return ((long) *(mxGetPr(scalar)));
}


/*	Count(): Element count of a matrix.	*/

static int
Count	(
	Matrix	*	mat
	)

{
	return ((int) (mxGetM(mat) * mxGetN(mat)));
}


/*	Free(): De-allocate memory by address of pointer.	*/

static VOID
Free	(
	VOIDPP		p
	)

{
	if (*p)	{
		if (1)	{
			mxFree(*p);
			*p = (VOIDP) 0;
		}
	}
	else if (VERBOSE)	{
		mexPrintf(" ## MexCDF53/Free(): Attempt to free null-pointer.\n");
	}
}


/*	RepairBadDataType(): Repair bad datatype.	*/

static nc_type
RepairBadDataType	(
	nc_type		theDataType
	)
	
{
	if (theDataType < NC_BYTE || theDataType > NC_DOUBLE)	{
		if (VERBOSE)	{
			mexPrintf(" ## MexCDF53/RepairBadDataType: %d", theDataType);
		}
		if (theDataType < NC_BYTE)	{
			theDataType = -1;
		}
		else if	(theDataType > NC_DOUBLE)	{
			while (theDataType > 255)	{
				theDataType /= 256;
			}
			while (theDataType > NC_DOUBLE)	{
				theDataType /= 2;
			}
		}
		if (VERBOSE)	{
			mexPrintf(" Converted To %d.\n", theDataType);
		}
	}
	
	return (theDataType);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -