DD_TimeVar.c 1.74 KB
/*=====================================================================
 *                DD SYSTEM base package
 *                  DD_Server library
 *                    DD_TimeVar.c
 *                      V.1.0
 * Last revision:
 *   June 03 2019: Version 1.0. Common methods used for time variable
 */

#include <netcdf.h>
#include "DD.h"
#include "DD_comm.h"

int Time_GetVarID(int ncID) {
	static char TimeName[] = "Time";
	int status = NC_NOERR;
	int TimeID = -1;
	status = nc_inq_varid(ncID, TimeName, &TimeID);
	if (status != NC_NOERR) {
		return -1;
	}
	return TimeID;
}

int Time_IsDoubleVar(int ncID, int timeVarID) {
	nc_type TimeType;
	int status = NC_NOERR;
	status = nc_inq_var (ncID, timeVarID, NULL, &TimeType, NULL, NULL, NULL);
	if (status != NC_NOERR) {
		return 0;
	}
	return (TimeType == NC_DOUBLE) ? 1 : 0;
}

double Time_GetValueFromTextVar(int ncID, int timeVarID, int recNum) {
	static char TimeStr[TIMELENGTH];
	static size_t TimeCountText[2] = {1,TIMELENGTH};
	size_t TimeStartText[2] = {recNum,0};
	int status = NC_NOERR;
	status = nc_get_vara_text(ncID,timeVarID,TimeStartText,TimeCountText,TimeStr);
	if (status != NC_NOERR) {
		return 0.;
	}
	return DD_Time2Double(TimeStr);
}

double Time_GetValueFromDoubleVar(int ncID, int timeVarID, int recNum) {
	static size_t TimeCountDouble[1] = {1};
	size_t TimeStartDouble[1] = {recNum};
	double val;
	int status = NC_NOERR;
	status = nc_get_vara_double (ncID,timeVarID,TimeStartDouble,TimeCountDouble,&val);
	if (status != NC_NOERR) {
		return 0.;
	}
	return val;
}

double Time_GetValueFromVar(int ncID, int timeVarID, int isDoubleTime, int recNum) {
	if (isDoubleTime) {
		return Time_GetValueFromDoubleVar(ncID, timeVarID, recNum);
	}
	return Time_GetValueFromTextVar(ncID, timeVarID, recNum);
}