Ejemplo n.º 1
0
void CUnit::Compile
		()
{
  utUnit	Base;
  m_Slope	= 1.0;
  m_Offset	= 0.0;
  if (CTools::StringToUpper(m_Text) == "DATE")
  {
    m_Text	= "days";
    m_Text.append(GetDateRefAsString());
  }
  
  m_Text = CTools::RemoveCharSurroundingNumber(m_Text);
  
  CheckUdunits(utScan(m_Text.c_str(), &m_Compiled), m_Text);
  Base		= m_Compiled;
  // = SetConversionTo(BaseUnit(self))
  FindBaseUnit(Base);
  CheckUdunits(utConvert(&m_Compiled,
			 &Base,
			 &m_Slope,
			 &m_Offset),
		m_Text,
		"Base unit");
}
Ejemplo n.º 2
0
void CUnit::SetConversionFrom
		(const CUnit	&Source)
{
  CheckUdunits(utConvert(&Source.m_Compiled,
			 &m_Compiled,
			 &m_Slope,
			 &m_Offset),
	       Source.m_Text,
	       m_Text);
}
Ejemplo n.º 3
0
void CUnit::SetConversionTo
		(const CUnit	&Destination)
{
  CheckUdunits(utConvert(&m_Compiled,
			 &Destination.m_Compiled,
			 &m_Slope,
			 &m_Offset),
	       m_Text,
	       Destination.m_Text);
}
Ejemplo n.º 4
0
//----------------------------------------
bool CUnit::IsCompatible(const CUnit& otherUnit) const
{
  double	N1;
  double	N2;

  int32_t	ConvertStatus;
  
  ConvertStatus	= utConvert(&m_Compiled, &otherUnit.m_Compiled, &N1, &N2);

  if (ConvertStatus == 0)
  {
    return true;
  }

  if (ConvertStatus == UT_ECONVERT)
  {
    return false;
  }

  CheckUdunits(ConvertStatus, m_Text, otherUnit.m_Text);

  return false; // never reached but avoids compiler complaint
}
Ejemplo n.º 5
0
NCstate NCdsHandleGetUnitConv (const NCdsHandle_t *dsh, const utUnit *unit, double *scale, double *offset)
{
	switch (dsh->DataType)
	{
		case NCtypeGDisc:
		default:
			CMmsgPrint (CMmsgAppError,  "Data set does not have time series in: %s %d",__FILE__,__LINE__);
			return (NCfailed);
		case NCtypeGCont:
			if (((NCdsHandleGCont_t *) dsh)->DoGUnit)
			{
				if (utConvert (&(((NCdsHandleGCont_t *) dsh)->GUnit),unit, scale, offset) != 0)
				{
					CMmsgPrint (CMmsgAppError, "Unit Conversion error in: %s %d",__FILE__,__LINE__);
					*scale = 1.0; *offset = 0.0;
					return (NCfailed);
				} 
			}
			else { *scale = 1.0; *offset = 0.0; }
			break;
	}
	return (NCsucceeded);
}
Ejemplo n.º 6
0
NCdsHandle_t *NCdsHandleCreate (const char *pattern, const char *name, int dncid, NCtimeStep tsMode, utUnit *tUnitIn, double sTime, double eTime)
{
	size_t i, j, strLen, strOffset, ncNum = 0, index;
	char *str [] = { "{year}", "{month}", "{day}", "{hour}", "{minute}", "{second}" };
	char *searchStr, *subStr, **fileNames = (char **) NULL, tsUnitStr [NCsizeString];
	int *ncids = (int *) NULL, tvarid, status;
	int endYear, endMonth, year, month, day, hour, minute;
	float second;
	double time = 0.0, scale, offset;
	utUnit tUnitOut;
	NCdsHandle_t *dsh;

	if ((searchStr = malloc (strlen (pattern) + 1)) == (char *) NULL)
	{ CMmsgPrint (CMmsgSysError, "Memory allocation error in: NCdsHandleCreate ()!"); return ((NCdsHandle_t *) NULL); }

	if ((utCalendar (eTime,tUnitIn,&endYear,&endMonth,&day,&hour,&minute,&second) != 0) ||
	    (utCalendar (sTime,tUnitIn,&year,   &month,   &day,&hour,&minute,&second) != 0))
	{ CMmsgPrint (CMmsgAppError, "Calender scanning error in:%s %d",__FILE__,__LINE__); goto ABORT; }

	switch (tsMode)
	{
		case NCtimeYear:   sprintf (tsUnitStr,"years since %04d-01-01 00:00 0.0",year);  break;
		case NCtimeMonth:  sprintf (tsUnitStr,"months since %04d-%02d-01 00:00 0.0",year, month); break;
		case NCtimeDay:    sprintf (tsUnitStr,"days since %04d-%02d-%02d 00:00 0.0",year, month, day);   break;
		case NCtimeHour:   sprintf (tsUnitStr,"hours since %04d-%02d-%02d %02d:00 0.0",year, month, day, hour);  break;
		case NCtimeMinute: sprintf (tsUnitStr,"minutes since %04d-%02d-%02d %02d:%02d 0.0", year, month, day, hour, minute); break;
		case NCtimeSecond: sprintf (tsUnitStr,"seconds since %04d-%02d-%02d %02d:%02d %.1f", year, month, day, hour, minute, second); break;
	}
	if (tsMode > NCtimeMonth)
	{
		if ((utScan (tsUnitStr, &tUnitOut) != 0) || (utConvert (tUnitIn, &tUnitOut, &scale, &offset) != 0))
		{ CMmsgPrint (CMmsgAppError, "Time unit scanning error in: %s %d",__FILE__,__LINE__); goto ABORT; }
		sTime = sTime * scale + offset;
		eTime = eTime * scale + offset;
	}
	else
	{
		sTime = 0.0;
		eTime = tsMode > NCtimeYear ? (endYear - year) * 12 + (endMonth - month + 1) : (double) (year - endYear);
	}
	do
	{
		if (tsMode > NCtimeMonth)
		{
			if (utCalendar (sTime + time,&tUnitOut,&year,&month,&day,&hour,&minute,&second) != 0)
			{ CMmsgPrint (CMmsgAppError, "Time unit scaning error in: %s %d",__FILE__,__LINE__); goto ABORT; }
		}
		strcpy (searchStr, pattern);
		for (i = 0;i < tsMode; ++i)
			if ((subStr = strstr (searchStr,str [i])) == (char *) NULL) break;
			else
			{
				strOffset = strlen (str [i]);
				strLen = strlen (subStr) - strOffset;
				switch (i)
				{
					case NCtimeYear:   sprintf (subStr,"%04d", year);    subStr += 4; strOffset -= 4; break;
					case NCtimeMonth:  sprintf (subStr,"%02d", month);   subStr += 2; strOffset -= 2; break;
					case NCtimeDay:    sprintf (subStr,"%02d", day);     subStr += 2; strOffset -= 2; break;
					case NCtimeHour:   sprintf (subStr,"%02d", hour);    subStr += 2; strOffset -= 2; break;
					case NCtimeMinute: sprintf (subStr,"%02d", minute);  subStr += 2; strOffset -= 2; break;
					case NCtimeSecond: sprintf (subStr,"%04.1f",second); subStr += 4; strOffset -= 4; break;
				}
				for (j = 0;j <= strLen; j++) subStr [j] = subStr [j + strOffset];
			} 
		if ((ncNum == 0) || (strcmp (fileNames [ncNum - 1], searchStr) != 0))
		{
			if (((fileNames = (char **) realloc (fileNames, (ncNum + 1) * sizeof (char *))) == (char **) NULL) ||
				 ((ncids     = (int *)   realloc (ncids,     (ncNum + 1) * sizeof (int)))    == (int *)   NULL))
			{ CMmsgPrint (CMmsgSysError, "Memory allocation error in: %s %d",__FILE__,__LINE__); goto ABORT; }
			else ncNum++;
			if  ((fileNames [ncNum - 1] = (char *) malloc (strlen (searchStr) + 1)) == (char *) NULL)
			{ CMmsgPrint (CMmsgSysError, "Memory allocation error in: %s %d",__FILE__,__LINE__); goto ABORT; }
			strcpy (fileNames [ncNum - 1], searchStr);
			if (((ncids [ncNum - 1] = NCfileCreate (fileNames [ncNum - 1], dncid)) == NCfailed) ||
			    (NCfileVarAdd (ncids [ncNum - 1], name, NC_FLOAT, NC_DOUBLE, NC_FLOAT) == NCfailed) ||
				 (NCfileSetTimeUnit   (ncids [ncNum - 1], tsUnitStr)  == NCfailed) ||
				 (NCfileSetMissingVal (ncids [ncNum - 1], -9999.0)    == NCfailed) ||
				 ((tvarid = NCdataGetTVarId (ncids [ncNum - 1])) == NCfailed)) goto ABORT;
			index = 0;
		}
		if ((status = nc_put_var1_double (ncids [ncNum - 1],tvarid,&index, &time)) != NC_NOERR)
		{ NCprintNCError (status,"NCdsHandleCreate"); goto ABORT; }
		index++;
		
		if (tsMode == NCtimeMonth) { month++; if (month > 12) { month = 1; year++;} }
		if (tsMode == NCtimeYear)  { year++; }
		time = time + 1.0;
	} while ((sTime + time) < eTime);
	for (i = 0;i < ncNum; i++) nc_sync (ncids [i]);
	if ((dsh = NCdsHandleOpenByIds (ncids, ncNum)) == (NCdsHandle_t *) NULL) goto ABORT;

	for (i = 0;i < ncNum; i++) free (fileNames [i]);
	utClear (&tUnitOut);
	free (fileNames);
	free (ncids);
	free (searchStr);
	return (dsh);
ABORT:
	for (i = 0;i < ncNum;i++) { nc_abort (ncids [i]); unlink (fileNames [i]); if (fileNames [i] != (char *) NULL) free (fileNames [i]); }
	utClear (&tUnitOut);
	if (fileNames != (char **) NULL) free (fileNames);
	free (searchStr);
	return ((NCdsHandle_t *) NULL);
}
Ejemplo n.º 7
0
int /* [rcd] Successful conversion returns NCO_NOERR */
nco_cln_clc_dff /* [fnc] UDUnits1 Difference between two co-ordinate units */
(const char *fl_unt_sng, /* I [ptr] units attribute string from disk  */     
 const char *fl_bs_sng, /* I [ptr] units attribute string from disk  */     
 double crr_val,
 double *og_val) /* O [ptr] */
{
  const char fnc_nm[]="nco_cln_clc_dff()"; /* [sng] Function name */
    
  double slp;
  double incpt;

  int rcd;

  utUnit udu_sct_in; /* UDUnits structure, input units */
  utUnit udu_sct_out; /* UDUnits structure, output units */

  /* Quick return if units identical */
  if(!strcmp(fl_unt_sng,fl_bs_sng) ){
    *og_val=crr_val;  
    return NCO_NOERR;
  } /* endif */

#ifdef UDUNITS_PATH
  /* UDUNITS_PATH macro expands to where autoconf found database file */
  rcd=utInit(UDUNITS_PATH);
#else /* !UDUNITS_PATH */
  /* When empty, utInit() uses environment variable UDUNITS_PATH, if any
     Otherwise it uses default initial location hardcoded when library was built */
  rcd=utInit("");
#endif /* !UDUNITS_PATH */

  if(rcd != UDUNITS_NOERR){
    (void)fprintf(stdout,"%s: %s failed to initialize UDUnits2 library\n",nco_prg_nm_get(),fnc_nm);
    return NCO_ERR;
  } /* end if err */ 

  /* units string to convert from */
  rcd=utScan(fl_unt_sng,&udu_sct_in); 
  if(rcd != UDUNITS_NOERR){
    if(rcd == UT_EINVALID) (void)fprintf(stderr,"ERROR: units attribute \"%s\" is invalid \n",fl_unt_sng);
    if(rcd == UT_ESYNTAX) (void)fprintf(stderr,"ERROR units attribute \"%s\" contains a syntax error",fl_unt_sng);
    if(rcd == UT_EUNKNOWN) (void)fprintf(stderr,"ERROR units attribute \"%s\" is not in udunits database",fl_unt_sng);
    (void)utTerm(); /* Free memory taken by UDUnits library */
    return NCO_ERR;
  } /* endif unkown type */

  /* units string to convert to */
  rcd=utScan(fl_bs_sng,&udu_sct_out); 
  if(rcd != UDUNITS_NOERR){
    if(rcd == UT_EINVALID) (void)fprintf(stderr,"ERROR: units attribute \"%s\" is invalid \n",fl_bs_sng);
    if(rcd == UT_ESYNTAX) (void)fprintf(stderr,"ERROR units attribute \"%s\" contains a syntax error",fl_bs_sng);
    if(rcd == UT_EUNKNOWN) (void)fprintf(stderr,"ERROR units attribute \"%s\" is not in udunits database",fl_bs_sng);
    (void)utTerm(); /* Free memory taken by UDUnits library */
    return NCO_ERR;
  } /* endif unkown type */

  rcd=utConvert(&udu_sct_in,&udu_sct_out,&slp,&incpt);
  if(rcd == UT_ECONVERT){
    (void)fprintf(stderr,"ERROR: user specified unit \"%s\" cannot be converted to units \"%s\"\n",fl_unt_sng,fl_bs_sng);
    (void)utTerm();
    return NCO_ERR;
  } /* endif */

  *og_val=crr_val*slp+incpt;

  /* debug stuff */
  if(nco_dbg_lvl_get() > nco_dbg_std) (void)fprintf(stderr,"%s: %s reports difference between systems \"%s\" and \"%s\" is %f\n",nco_prg_nm_get(),fnc_nm,fl_unt_sng,fl_bs_sng,*og_val);

  (void)utTerm();

  return NCO_NOERR;   
}  /* end UDUnits1 nco_cln_clc_dff() */