コード例 #1
0
ファイル: NCdata.c プロジェクト: bmfekete/RGIS
int NCdataGetCoreVarId(int ncid) {
    int status;
    int varid, nvars, ndims;
    int dimid[4], xdim = NCundefined, ydim = NCundefined, i, j;

    if ((status = nc_inq_nvars(ncid, &nvars)) != NC_NOERR) {
        NCprintNCError (status, "NCdataGetGridVarId");
        return (NCfailed);
    }

    if (((xdim = NCdataGetCDimId(ncid)) == NCfailed) &&
        (((xdim = NCdataGetXDimId(ncid)) == NCfailed) || ((ydim = NCdataGetYDimId(ncid)) == NCfailed)))
        return (NCfailed);

    for (varid = 0; varid < nvars; ++varid) {
        if ((status = nc_inq_varndims(ncid, varid, &ndims)) != NC_NOERR) {
            NCprintNCError (status, "NCdataGetGridVarId");
            return (NCfailed);
        }
        if ((ndims < 1) || (ndims > 4)) continue;
        if ((status = nc_inq_vardimid(ncid, varid, dimid)) != NC_NOERR) {
            NCprintNCError (status, "NCdataGetGridVarId");
            return (NCfailed);
        }
        for (i = 0; i < ndims; ++i)
            if (xdim == dimid[i]) {
                if (ydim == NCundefined) return (varid);
                for (j = 0; j < ndims; ++j) if (ydim == dimid[j]) return (varid);
            }
    }
    return (NCfailed);
}
コード例 #2
0
ファイル: NCdsHandleGCont.c プロジェクト: bmfekete/RGIS
NCstate NCdsHandleGContSaveCache(NCdsHandleGCont_t *gCont, size_t tStep, size_t level) {
    int status;
    size_t i = 0, start[4], count[4];
    size_t ncidx;

    if (NCdsHandleGContCLStats(gCont, tStep, level) != NCsucceeded) return (NCfailed);
    ncidx = gCont->NCindex[tStep];
    start[i] = tStep - gCont->NCoffset[ncidx];
    count[i++] = 1;
    if (gCont->LVarIds[ncidx] != NCundefined) {
        start[i] = level;
        count[i++] = 1;
    }
    start[i] = (size_t) 0;
    count[i++] = gCont->RowNum;
    start[i] = (size_t) 0;
    count[i++] = gCont->ColNum;

    if ((status = nc_put_vara_double(gCont->NCIds[ncidx], gCont->GVarIds[ncidx], start, count, gCont->Data)) !=
        NC_NOERR) { NCprintNCError (status, "NCdsHandleGContSaveCache"); }
    if ((gCont->TVarIds[ncidx] != NCundefined) &&
        ((status = nc_put_var1_double(gCont->NCIds[ncidx], gCont->TVarIds[ncidx], start, gCont->Times + tStep)) !=
         NC_NOERR)) { NCprintNCError (status, "NCdsHandleGContSaveCache"); }
    if ((gCont->LVarIds[ncidx] != NCundefined) &&
        ((status = nc_put_var1_double(gCont->NCIds[ncidx], gCont->LVarIds[ncidx], start + 1, gCont->Levels + level)) !=
         NC_NOERR)) { NCprintNCError (status, "NCdsHandleGContSaveCache"); }
    if ((status = nc_sync(gCont->NCIds[ncidx])) != NC_NOERR) {
        NCprintNCError (status, "NCdsHandleGContLoadCache");
        return (NCfailed);
    }
    return (NCsucceeded);
}
コード例 #3
0
ファイル: NCdsHandleGCont.c プロジェクト: bmfekete/RGIS
NCstate NCdsHandleGContCLStats(NCdsHandleGCont_t *gCont, size_t tStep, size_t level) {
    int status;
    size_t row, col, obsNum = 0, index[2], ncidx;
    double val, weight = 1.0, sumWeight = 0.0;
    double min = HUGE_VAL, max = -HUGE_VAL, avg = 0.0, stdDev = 0.0;

    for (row = 0; row < gCont->RowNum; row++)
        for (col = 0; col < gCont->ColNum; col++) {
            val = gCont->Data[gCont->ColNum * row + col];
            if (_NCdsHandleGContTestNodata(gCont, val)) continue;
            sumWeight += weight;
            avg = avg + val * weight;
            min = min < val ? min : val;
            max = max > val ? max : val;
            stdDev = stdDev + val * val * weight;
            obsNum++;
        }
    if (obsNum > 0) {
        avg = avg / sumWeight;
        stdDev = stdDev / sumWeight;
        stdDev = stdDev - avg * avg;
        stdDev = sqrt(stdDev);
    }
    else
        switch (gCont->GType) {
            default:
                CMmsgPrint(CMmsgAppError, "Invalid NetCDF type in: %s %d", __FILE__, __LINE__);
                return (NCfailed);
            case NC_BYTE:
            case NC_SHORT:
            case NC_INT:
                avg = stdDev = min = max = (double) gCont->FillValue.Int;
                break;
            case NC_FLOAT:
            case NC_DOUBLE:
                avg = stdDev = min = max = gCont->FillValue.Float;
                break;
        }
    ncidx = gCont->NCindex[tStep];
    index[0] = tStep - gCont->NCoffset[ncidx];
    index[1] = level;
    if ((gCont->MeanIds[ncidx] != NCundefined) &&
        ((status = nc_put_var1_double(gCont->NCIds[ncidx], gCont->MeanIds[ncidx], index, &avg)) != NC_NOERR))
        NCprintNCError (status, "NCdsHandleGContCLStats");
    if ((gCont->MinIds[ncidx] != NCundefined) &&
        ((status = nc_put_var1_double(gCont->NCIds[ncidx], gCont->MinIds[ncidx], index, &min)) != NC_NOERR))
        NCprintNCError (status, "NCdsHandleGContCLStats");
    if ((gCont->MaxIds[ncidx] != NCundefined) &&
        ((status = nc_put_var1_double(gCont->NCIds[ncidx], gCont->MaxIds[ncidx], index, &max)) != NC_NOERR))
        NCprintNCError (status, "NCdsHandleGContCLStats");
    if ((gCont->StdIds[ncidx] != NCundefined) &&
        ((status = nc_put_var1_double(gCont->NCIds[ncidx], gCont->StdIds[ncidx], index, &stdDev)) != NC_NOERR))
        NCprintNCError (status, "NCdsHandleGContCLStats");
    return (NCsucceeded);
}
コード例 #4
0
ファイル: NCdsHandleGCont.c プロジェクト: bmfekete/RGIS
NCstate NCdsHandleGContLoadCache(NCdsHandleGCont_t *gCont, size_t sTStep, size_t nTSteps, size_t sLevel,
                                 size_t nLevels) {
    int status;
    size_t start[4], count[4];
    size_t ncidx, tStep, level, row, col;
    double val;

    count[0] = 1;
    if (gCont->LVarIds[0] == NCundefined) {
        start[1] = (size_t) 0;
        count[1] = gCont->RowNum;
        start[2] = (size_t) 0;
        count[2] = gCont->ColNum;
    }
    else {
        count[1] = 1;
        start[2] = (size_t) 0;
        count[2] = gCont->RowNum;
        start[3] = (size_t) 0;
        count[3] = gCont->ColNum;
    }
    for (row = 0; row < gCont->RowNum; row++)
        for (col = 0; col < gCont->ColNum; col++) {
            gCont->ObsNum[gCont->ColNum * row + col] = (size_t) 0;
            gCont->Data[gCont->ColNum * row + col] = (double) 0.0;
        }
    for (tStep = sTStep; tStep < sTStep + nTSteps; tStep++)
        for (level = sLevel; level < sLevel + nLevels; level++) {
            ncidx = gCont->NCindex[tStep];
            start[0] = tStep - gCont->NCoffset[ncidx];
            if (gCont->LVarIds[ncidx] != NCundefined) start[1] = level;
            if ((status = nc_sync(gCont->NCIds[ncidx])) != NC_NOERR) {
                NCprintNCError (status, "NCdsHandleGContLoadCache");
                return (NCfailed);
            }
            if ((status = nc_get_vara_double(gCont->NCIds[ncidx], gCont->GVarIds[ncidx], start, count,
                                             gCont->AuxData)) != NC_NOERR) {
                NCprintNCError (status, "NCdsHandleGContLoadCache");
                return (NCfailed);
            }
            for (row = 0; row < gCont->RowNum; row++)
                for (col = 0; col < gCont->ColNum; col++)
                    if (_NCdsHandleGContTestNodata(gCont, (val = gCont->AuxData[gCont->ColNum * row + col])) == false) {
                        gCont->Data[gCont->ColNum * row + col] += val;
                        gCont->ObsNum[gCont->ColNum * row + col] += 1;
                    }
        }
    for (row = 0; row < gCont->RowNum; row++)
        for (col = 0; col < gCont->ColNum; col++)
            if (gCont->ObsNum[gCont->ColNum * row + col] == 0) NCdsHandleGContSetFill(gCont, row, col);
            else if (gCont->ObsNum[gCont->ColNum * row + col] > 1)
                gCont->Data[gCont->ColNum * row + col] /= (double) gCont->ObsNum[gCont->ColNum * row + col];
    return (NCsucceeded);
}
コード例 #5
0
ファイル: NCdsHandle.c プロジェクト: bandi13/RGIS
NCdsHandle_t *NCdsHandleOpen (const char *pattern)
{
	int status, *ncids = (int *) NULL, i;
	size_t ncNum = 0;
	char **fileList = (char **) NULL;
	NCdsHandle_t *dsHandle = (NCdsHandle_t *) NULL;

	if ((fileList = NCfileList (pattern,&ncNum)) == (char **) NULL) goto ABORT;

	if ((ncids = (int *) calloc (ncNum, sizeof (int))) == (int *) NULL)
	{ CMmsgPrint (CMmsgSysError, "Memory allocation error in: NCdsHandleOpenByPattern ()"); goto ABORT; }

	for (i = 0;i < ncNum;++i)
		if ((status = nc_open (fileList [i],NC_NOWRITE,ncids + i)) != NC_NOERR)
		{ CMmsgPrint (CMmsgUsrError, "Filename: %s",fileList [0]); NCprintNCError (status,"NCdsHandleOpenByPattern"); ncids [i] = NCundefined; goto ABORT; }

	dsHandle = NCdsHandleOpenByIds (ncids,ncNum);
	free (ncids);
	NCfileListFree (fileList,ncNum);
	return (dsHandle);

ABORT:
	if (fileList != (char **) NULL) NCfileListFree (fileList,ncNum);
	if (ncids != (int *) NULL)
	{
		for (i = i - 1;i >= 0;--i) if (ncids [i] != NCundefined) nc_close (ncids [i]);
		free (ncids);
	}
	return (dsHandle);
}
コード例 #6
0
ファイル: NCtable.c プロジェクト: amiara/RGIS
static int _NCtableVarLen(int ncid, int varid, int dimid, int *dimids)
{
	int status, ndims, i, len = 1;
	size_t dimlen;

	if((status = nc_inq_varndims (ncid,varid,&ndims))  != NC_NOERR)
	{ NCprintNCError (status,"_NCtableVarCheck"); return (NCfailed); }
	if((status = nc_inq_vardimid (ncid,varid,dimids)) != NC_NOERR)
	{ NCprintNCError (status,"_NCtableVarCheck"); return (NCfailed); }
	if(dimids[0] != dimid) return (NCfailed);
	for(i = 1; i < ndims; i++)
	{
		if((status = nc_inq_dimlen(ncid,dimids [i],&dimlen)) != NC_NOERR)
		{ NCprintNCError (status,"_NCtableVarCheck"); return (NCfailed); }
		len = len * dimlen;
	}
	return (len);
}
コード例 #7
0
ファイル: NCdata.c プロジェクト: bmfekete/RGIS
NCstate NCdataCopyAllAttibutes(int inNCid, int outNCid, bool overwrite) {
    int status, inVarid, outVarid, varnum;
    char varName[NC_MAX_NAME];
    if (NCdataCopyAttributes(inNCid, NC_GLOBAL, outNCid, NC_GLOBAL, overwrite) == NCfailed) return (NCfailed);

    if ((status = nc_inq_nvars(inNCid, &varnum)) != NC_NOERR) {
        NCprintNCError (status, "NCdataCopyAttibutes");
        return (NCfailed);
    }
    for (inVarid = 0; inVarid < varnum; ++inVarid) {
        if ((status = nc_inq_varname(inNCid, inVarid, varName)) != NC_NOERR) {
            NCprintNCError (status, "NCdataCopyAttibutes");
            return (NCfailed);
        }
        if ((status = nc_inq_varid(outNCid, varName, &outVarid)) != NC_NOERR) continue;
        if (NCdataCopyAttributes(inNCid, inVarid, outNCid, outVarid, overwrite) == NCfailed) return (NCfailed);
    }
    return (NCsucceeded);
}
コード例 #8
0
ファイル: NCdsHandleNet.c プロジェクト: bmfekete/RGIS
NCstate NCdsHandleNetworkDefine(NCdsHandleNetwork_t *net, int ncid) {
    int status;
    if (NCdataGetType(ncid) != NCtypeNetwork) {
        CMmsgPrint(CMmsgAppError, "Invalid network in: %s %d", __FILE__, __LINE__);
        return (NCfailed);
    }

    if (NCdsHandleGLayoutDefine((NCdsHandleGLayout_t *) net, &ncid, 1) == NCfailed) return (NCfailed);

    net->Data = (int *) NULL;
    net->Basins.Table = net->Cells.Table = (NCtable_t *) NULL;
    if ((net->Data = (int *) calloc(net->ColNum * net->RowNum, sizeof(int))) == (int *) NULL) {
        CMmsgPrint(CMmsgSysError, "Memory allocation error in: %s %d", __FILE__, __LINE__);
        NCdsHandleNetworkClear(net);
        return (NCfailed);
    }
    if ((status = nc_get_var_int(ncid, net->GVarIds[0], net->Data)) != NC_NOERR) {
        NCprintNCError (status, "NCdsHandleNetworkDefine");
        NCdsHandleNetworkClear(net);
        return (NCfailed);
    }

    if ((net->Basins.Table = NCtableOpen(ncid, NCnameTBItems)) == (NCtable_t *) NULL) {
        NCdsHandleNetworkClear(net);
        return (NCfailed);
    }
    if ((net->Cells.Table = NCtableOpen(ncid, NCnameTBCells)) == (NCtable_t *) NULL) {
        NCdsHandleNetworkClear(net);
        return (NCfailed);
    }

    if (((net->Basins.NameFld = NCtableGetFieldByName(net->Basins.Table, NCnameTBItems)) == (NCfield_t *) NULL) ||
        ((net->Basins.RowFld = NCtableGetFieldByName(net->Basins.Table, NCnameBSNRow)) == (NCfield_t *) NULL) ||
        ((net->Basins.ColFld = NCtableGetFieldByName(net->Basins.Table, NCnameBSNCol)) == (NCfield_t *) NULL) ||
        ((net->Basins.OrderFld = NCtableGetFieldByName(net->Basins.Table, NCnameBSNOrder)) == (NCfield_t *) NULL) ||
        ((net->Basins.SymbolFld = NCtableGetFieldByName(net->Basins.Table, NCnameBSNSymbol)) == (NCfield_t *) NULL) ||
        ((net->Basins.LengthFld = NCtableGetFieldByName(net->Basins.Table, NCnameBSNLength)) == (NCfield_t *) NULL) ||
        ((net->Basins.AreaFld = NCtableGetFieldByName(net->Basins.Table, NCnameBSNArea)) == (NCfield_t *) NULL) ||
        ((net->Cells.RowFld = NCtableGetFieldByName(net->Cells.Table, NCnameCLSRow)) == (NCfield_t *) NULL) ||
        ((net->Cells.ColFld = NCtableGetFieldByName(net->Cells.Table, NCnameCLSCol)) == (NCfield_t *) NULL) ||
        ((net->Cells.ToCellFld = NCtableGetFieldByName(net->Cells.Table, NCnameCLSToCell)) == (NCfield_t *) NULL) ||
        ((net->Cells.FromCellFld = NCtableGetFieldByName(net->Cells.Table, NCnameCLSFromCell)) == (NCfield_t *) NULL) ||
        ((net->Cells.BasinFld = NCtableGetFieldByName(net->Cells.Table, NCnameCLSBasinId)) == (NCfield_t *) NULL) ||
        ((net->Cells.NCellsFld = NCtableGetFieldByName(net->Cells.Table, NCnameCLSNCells)) == (NCfield_t *) NULL) ||
        ((net->Cells.TravelFld = NCtableGetFieldByName(net->Cells.Table, NCnameCLSTravel)) == (NCfield_t *) NULL) ||
        ((net->Cells.OrderFld = NCtableGetFieldByName(net->Cells.Table, NCnameCLSOrder)) == (NCfield_t *) NULL) ||
        ((net->Cells.LengthFld = NCtableGetFieldByName(net->Cells.Table, NCnameCLSLength)) == (NCfield_t *) NULL) ||
        ((net->Cells.AreaFld = NCtableGetFieldByName(net->Cells.Table, NCnameCLSArea)) == (NCfield_t *) NULL)) {
        CMmsgPrint(CMmsgAppError, "Corrupt networkd data in: %s %d", __FILE__, __LINE__);
        NCdsHandleNetworkClear(net);
        return (NCfailed);
    }
    return (NCsucceeded);
}
コード例 #9
0
ファイル: NCdata.c プロジェクト: bmfekete/RGIS
double *NCdataGetVector(int ncid, int dimid, int varid, size_t *len) {
    int status;
    size_t start;
    double *coords;

    if ((dimid == NCfailed) || (varid == NCfailed)) return ((double *) NULL);
    if ((status = nc_inq_dimlen(ncid, dimid, len)) != NC_NOERR) {
        NCprintNCError (status, "NCdataGetVector");
        return ((double *) NULL);
    }
    if ((coords = (double *) calloc(*len, sizeof(double))) == (double *) NULL) {
        CMmsgPrint(CMmsgSysError, "Memory allocation error in: %s %d", __FILE__, __LINE__);
        return ((double *) NULL);
    }
    start = 0;
    if ((status = nc_get_vara_double(ncid, varid, &start, len, coords)) != NC_NOERR) {
        NCprintNCError (status, "NCdataGetVector");
        free(coords);
        return ((double *) NULL);
    }
    return (coords);
}
コード例 #10
0
ファイル: NCdata.c プロジェクト: bmfekete/RGIS
NCstate NCdataSetTextAttribute(int ncid, int varid, const char *attName, const char *text) {
    int status;
    bool redef;


    redef = nc_redef(ncid) == NC_NOERR ? true : false;

    status = nc_put_att_text(ncid, varid, attName, strlen(text) + 1, text);
    if (redef) nc_enddef(ncid);
    if (status != NC_NOERR) {
        NCprintNCError (status, "NCdataSetTextAttribute");
        return (NCfailed);
    }
    return (NCsucceeded);
}
コード例 #11
0
ファイル: NCdata.c プロジェクト: bmfekete/RGIS
NCstate NCdataCopyAttributes(int inNCid, int inVarid, int outNCid, int outVarid, bool overwrite) {
    bool redef;
    int status, att = 0, attId;
    char attName[NC_MAX_NAME];

    redef = nc_redef(outNCid) == NC_NOERR;
    while ((status = nc_inq_attname(inNCid, inVarid, att++, attName)) == NC_NOERR) {
        if (overwrite || (nc_inq_attid(outNCid, outVarid, attName, &attId) != NC_NOERR)) {
            if ((status = nc_copy_att(inNCid, inVarid, attName, outNCid, outVarid)) != NC_NOERR) {
                NCprintNCError (status, "_NCdataCopyAttributes");
                return (NCfailed);
            }
        }
    }
    if (redef) nc_enddef(outNCid);
    return (NCsucceeded);
}
コード例 #12
0
ファイル: NCdata.c プロジェクト: bmfekete/RGIS
char *NCdataGetTextAttribute(int ncid, int varid, const char *attName) {
    int status;
    char *att;
    size_t attlen;

    if ((status = nc_inq_attlen(ncid, varid, attName, &attlen)) != NC_NOERR) return ("undefined");

    if ((att = (char *) malloc(attlen + 1)) == (char *) NULL) {
        CMmsgPrint(CMmsgSysError, "Memory allocation error in: %s %d", __FILE__, __LINE__);
        return ((char *) NULL);
    }
    if ((status = nc_get_att_text(ncid, varid, attName, att)) != NC_NOERR) {
        NCprintNCError (status, "NCdataGetTextAttribute");
        free(att);
        return ((char *) NULL);
    }
    att[attlen] = '\0';
    return (att);
}
コード例 #13
0
ファイル: NCtable.c プロジェクト: amiara/RGIS
NCstate NCtableCommitField (int ncid, char *tablename, NCfield_t *field)
{
	int status, dimid, varid;

	if (nc_inq_varid(ncid,field->Name,&varid) != NC_NOERR)
	{
		if(field->Len > 1) return (NCfailed);
		if((status = nc_inq_dimid(ncid,tablename,&dimid)) != NC_NOERR)
		{ NCprintNCError (status,"NCtableCommitField"); return (NCfailed);}
		if((status = nc_redef(ncid) != NC_NOERR))
		{ NCprintNCError (status,"NCtableCommitField"); return (NCfailed);}
		if ((status = nc_def_var(ncid,field->Name,field->Type,1,&dimid,&varid)) != NC_NOERR)
		{ NCprintNCError (status,"NCtableCommitField"); return (NCfailed);}
		if((status = nc_enddef(ncid) != NC_NOERR))
		{ NCprintNCError (status,"NCtableCommitField"); return (NCfailed);}
	}
	switch (field->Type)
	{
		default:
		case NC_CHAR:
			if((status = nc_put_var_text(ncid,varid,(char *) field->Data)) != NC_NOERR)
			{ NCprintNCError (status,"NCtableCommitField"); return (NCfailed);}
			break;
		case NC_BYTE:
		case NC_SHORT:
		case NC_INT:
			if((status = nc_put_var_int(ncid,varid, (int *)  field->Data)) != NC_NOERR)
			{ NCprintNCError (status,"NCtableCommitField"); return (NCfailed);}
			break;
		case NC_FLOAT:
		case NC_DOUBLE:
			if((status = nc_put_var_double(ncid,varid,(double *) field->Data)) != NC_NOERR)
			{ NCprintNCError (status,"NCtableCommitField"); return (NCfailed);}
			break;
	}
	return (NCsucceeded);
}
コード例 #14
0
ファイル: NCtable.c プロジェクト: amiara/RGIS
NCtable_t *NCtableOpen(int ncid, char *tablename)
{
	int status, *dimids, dimid, i = 0, j = 0;
	int ndims, nvars;
	size_t nrecords;
	size_t len;
	char varname [NC_MAX_NAME];
	nc_type type;
	NCfield_t *field = (NCfield_t *) NULL;
	NCtable_t *tbl   = (NCtable_t *) NULL;

	if((status = nc_inq_dimid  (ncid,tablename,&dimid)) != NC_NOERR)
	{ NCprintNCError (status,"NCtableOpen"); return ((NCtable_t *) NULL); }
	if((status = nc_inq_dimlen (ncid,dimid,&nrecords))  != NC_NOERR)
	{ NCprintNCError (status,"NCtableOpen"); return ((NCtable_t *) NULL); }
	if((status = nc_inq_nvars  (ncid,&nvars))           != NC_NOERR)
	{ NCprintNCError (status,"NCtableOpen"); return ((NCtable_t *) NULL); }
	if(nc_inq_ndims(ncid,&ndims) != NC_NOERR)
	{ NCprintNCError (status,"NCtableOpen"); return ((NCtable_t *) NULL); }
	if ((dimids = (int *) malloc(sizeof(int) * ndims)) == (int *) NULL)
	{ CMmsgPrint (CMmsgSysError, "Memory allocation error in: %s %d",__FILE__,__LINE__); return (NCtable_t *) NULL; }

	if ((tbl = (NCtable_t *) malloc(sizeof(NCtable_t))) == (NCtable_t *) NULL)
	{ CMmsgPrint (CMmsgSysError, "Memory allocation error in: %s %d",__FILE__,__LINE__); free (dimids); return ((NCtable_t *) NULL); }
	if ((tbl->Name = (char *) malloc (strlen(tablename) + 1)) == (char *) NULL)
	{ CMmsgPrint (CMmsgSysError, "Memory allocation error in: %s %d",__FILE__,__LINE__); free (dimids); free (tbl); return ((NCtable_t *) NULL); }
	strcpy(tbl->Name,tablename);
	tbl->NFields  = 0;
	tbl->Fields   = (NCfield_t *) NULL;

	for(i = 0; i < nvars; i++)
	{
		if ((status = nc_inq_vartype(ncid,i,&type)) != NC_NOERR)
		{ NCprintNCError (status,"NCtableOpen"); free (dimids); NCtableClose (tbl); return ((NCtable_t *) NULL); }
		if ((len = _NCtableVarLen(ncid,i,dimid,dimids)) == NCfailed) continue;
		if ((type != NC_CHAR) && (len > 1)) continue;

		if((status = nc_inq_varname(ncid,i,varname)) != NC_NOERR)
		{ NCprintNCError (status,"NCtableOpen"); free (dimids); NCtableClose (tbl); return ((NCtable_t *) NULL); }

		if ((tbl->Fields = (NCfield_t *) realloc (tbl->Fields, sizeof (NCfield_t) * (tbl->NFields + 1))) == (NCfield_t *) NULL)
		{ CMmsgPrint (CMmsgSysError, "Error allocating memory in: %s %d",__FILE__,__LINE__); free (dimids); NCtableClose (tbl); return ((NCtable_t *) NULL); }
		field = tbl->Fields + tbl->NFields;
		tbl->NFields += 1;
		field->NRecords = nrecords;
		field->Data = (void *) NULL;
		field->Name = (char *) NULL;
		field->Type = type;
		field->Len  = len;

		if ((field->Name = malloc (strlen (varname) + 1)) == (char *) NULL)
		{ CMmsgPrint (CMmsgSysError, "Error allocating memory in: %s %d",__FILE__,__LINE__); free (dimids); NCtableClose (tbl); return ((NCtable_t *) NULL); }
		strcpy (field->Name,varname);

		if (nc_get_att_double (ncid,i,NCnameVAScaleFactor,&field->Scale)      != NC_NOERR) field->Scale      = 1.0;
		if (nc_get_att_double (ncid,i,NCnameVAAddOffset,  &field->Offset)     != NC_NOERR) field->Offset     = 0.0;

		switch(field->Type)
		{
			case NC_CHAR:
				if ((field->Data = (void *) malloc(field->NRecords * field->Len * sizeof (char)))   == (void *) NULL)
				{ CMmsgPrint (CMmsgSysError, "Error allocating memory in: %s %d",__FILE__,__LINE__); free (dimids); NCtableClose (tbl); return ((NCtable_t *) NULL); }
				if((status = nc_get_var_text(ncid,i,(char *) (field->Data))) != NC_NOERR)
				{ NCprintNCError (status,"NCtableOpen"); free (dimids); NCtableClose (tbl); return ((NCtable_t *) NULL); }
				break;
			case NC_BYTE:
			case NC_SHORT:
			case NC_INT:
				if ((field->Data = (void *) malloc(field->NRecords * field->Len * sizeof (int)))   == (void *) NULL)
				{ CMmsgPrint (CMmsgSysError, "Error allocating memory in: %s %d",__FILE__,__LINE__); free (dimids); NCtableClose (tbl); return ((NCtable_t *) NULL); }
				if((status = nc_get_var_int(ncid,i,(int *) (field->Data))) != NC_NOERR)
				{ NCprintNCError (status,"NCtableOpen"); free (dimids); NCtableClose (tbl); return ((NCtable_t *) NULL); }
				if (nc_get_att_int    (ncid,i,NCnameVAFillValue,  &field->FillValue.Int)  != NC_NOERR) field->FillValue.Int  = INT_NOVALUE;
				if (nc_get_att_double (ncid,i,NCnameVAMissingVal, &field->MissingVal)     != NC_NOERR) field->MissingVal     = FLOAT_NOVALUE;
				break;
			case NC_FLOAT:
			case NC_DOUBLE:
				if ((field->Data = (void *) malloc(field->NRecords * field->Len * sizeof (double))) == (void *) NULL)
				{ CMmsgPrint (CMmsgSysError, "Error allocating memory in: %s %d",__FILE__,__LINE__); free (dimids); NCtableClose (tbl); return ((NCtable_t *) NULL); }
				if((status = nc_get_var_double(ncid,i,(double *) (field->Data))) != NC_NOERR)
				{ NCprintNCError (status,"NCtableOpen"); free (dimids); NCtableClose (tbl); return ((NCtable_t *) NULL); }
				if (nc_get_att_double (ncid,i,NCnameVAFillValue,  &field->FillValue.Float)  != NC_NOERR) field->FillValue.Float = FLOAT_NOVALUE;
				if (nc_get_att_double (ncid,i,NCnameVAMissingVal, &field->MissingVal)       != NC_NOERR) field->MissingVal      = FLOAT_NOVALUE;
				break;
			default:        field->Data = (void *) NULL; break;
		}
		if(GetDebug()) CMmsgPrint (CMmsgUsrError, "Loaded: %s(dimid: %d)\n",field->Name,dimid);
	}

	if(GetDebug())
	{
		CMmsgPrint (CMmsgUsrError, "Dim: %d Name: %s Cols: %d Rows: %d\n",dimid,tbl->Name,tbl->NFields,field->NRecords);
		for(i = 0; i < tbl->NFields; i++)
		{
			field = tbl->Fields + i;
			CMmsgPrint (CMmsgUsrError, "\tField: %d Name: %s ",i,field->Name);
			switch(field->Type)
			{
				case NC_CHAR:
					if(field->Len == 1)
					{
						CMmsgPrint (CMmsgUsrError, "Type: char\n");
						for(j = 0; j < 5; j++) CMmsgPrint (CMmsgUsrError, "\t\t%d %c\n",j,((char *) (field->Data)) [j]);
					}
					else
					{
						CMmsgPrint (CMmsgUsrError, "Type: string\n");
						for(j = 0; j < 5; j++) CMmsgPrint (CMmsgUsrError, "\t\t%d %s\n",j,((char *) (field->Data)) + j * field->Len);
					}
					break;
				case NC_BYTE:
				case NC_SHORT:
				case NC_INT:
					CMmsgPrint (CMmsgUsrError, "Type: int\n");
					for(j = 0; j < 5; j++) CMmsgPrint (CMmsgUsrError, "\t\t%d %i\n",j,((int *)    (field->Data)) [j]);
					break;
				case NC_FLOAT:
				case NC_DOUBLE:
					CMmsgPrint (CMmsgUsrError, "Type: double\n");
					for(j = 0; j < 5; j++) CMmsgPrint (CMmsgUsrError, "\t\t%d %f\n",j,((double *) (field->Data)) [j]);
					break;
				default: break;
			}
		}
	}
	return (tbl);
}
コード例 #15
0
ファイル: NCdsHandle.c プロジェクト: bandi13/RGIS
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);
}
コード例 #16
0
ファイル: NCdsHandleGCont.c プロジェクト: bmfekete/RGIS
NCstate NCdsHandleGContUpdateRanges(const NCdsHandleGCont_t *gCont) {
    int status;
    double *minArray = (double *) NULL, *maxArray = (double *) NULL, *meanArray = (double *) NULL, meanRange[2], minRange[2], maxRange[2], varRange[2], glbRange[2];
    size_t i, offset, ncidx, level;
    size_t start[2], count[2];

    if ((gCont->MinIds == (int *) NULL) || (gCont->MaxIds == (int *) NULL)) {
        CMmsgPrint(CMmsgAppError, "Missing minimum or maximum variables in:  %s %d", __FILE__, __LINE__);
        goto ABORT;
    }

    if (((meanArray = (double *) calloc(gCont->TNum, sizeof(double))) == (double *) NULL) ||
        ((minArray = (double *) calloc(gCont->TNum, sizeof(double))) == (double *) NULL) ||
        ((maxArray = (double *) calloc(gCont->TNum, sizeof(double))) == (double *) NULL)) {
        CMmsgPrint(CMmsgSysError, "Memory allocation error in: %s %d", __FILE__, __LINE__);
        goto ABORT;
    }

    glbRange[0] = HUGE_VAL;
    glbRange[1] = -HUGE_VAL;
    start[0] = 0;
    count[1] = 1;
    for (ncidx = 0; ncidx < gCont->NCnum; ncidx++) {
        meanRange[0] = minRange[0] = maxRange[0] = HUGE_VAL;
        meanRange[1] = minRange[1] = maxRange[1] = -HUGE_VAL;
        if ((gCont->MinIds[ncidx] == NCundefined) || (gCont->MaxIds[ncidx] == NCundefined)) {
            CMmsgPrint(CMmsgAppError, "Missing minimum or maximum variables in: %s %d", __FILE__, __LINE__);
            goto ABORT;
        }
        count[0] = (ncidx + 1 < gCont->NCnum ? gCont->NCoffset[ncidx + 1] : gCont->TNum) - gCont->NCoffset[ncidx];
        offset = gCont->NCoffset[ncidx];
        for (level = 0; level < gCont->LNum; level++) {
            start[1] = level;
            if (((status = nc_get_vara_double(gCont->NCIds[ncidx], gCont->MeanIds[ncidx], start, count,
                                              meanArray + offset)) != NC_NOERR) ||
                ((status = nc_get_vara_double(gCont->NCIds[ncidx], gCont->MinIds[ncidx], start, count,
                                              minArray + offset)) != NC_NOERR) ||
                ((status = nc_get_vara_double(gCont->NCIds[ncidx], gCont->MaxIds[ncidx], start, count,
                                              maxArray + offset)) != NC_NOERR)) {
                NCprintNCError (status, "_NCdsHandleGContUpdateRange");
                goto ABORT;
            }
            for (i = 0; i < count[0]; i++) {
                if (meanArray[i + offset] < meanRange[0]) meanRange[0] = meanArray[i + offset];
                if (meanArray[i + offset] > meanRange[1]) meanRange[1] = meanArray[i + offset];
                if (minArray[i + offset] < minRange[0]) minRange[0] = minArray[i + offset];
                if (minArray[i + offset] > minRange[1]) minRange[1] = minArray[i + offset];
                if (maxArray[i + offset] < maxRange[0]) maxRange[0] = maxArray[i + offset];
                if (maxArray[i + offset] > maxRange[1]) maxRange[1] = maxArray[i + offset];
            }
            varRange[0] = minRange[0];
            varRange[1] = maxRange[1];
            if (varRange[0] < glbRange[0]) glbRange[0] = varRange[0];
            if (varRange[1] > glbRange[1]) glbRange[1] = varRange[1];
            if (((status = nc_redef(gCont->NCIds[ncidx])) != NC_NOERR) ||
                ((status = nc_put_att_double(gCont->NCIds[ncidx], gCont->GVarIds[ncidx], NCnameVAActualRange, NC_FLOAT,
                                             2, varRange)) != NC_NOERR) ||
                ((status = nc_put_att_double(gCont->NCIds[ncidx], gCont->MeanIds[ncidx], NCnameVAActualRange, NC_FLOAT,
                                             2, meanRange)) != NC_NOERR) ||
                ((status = nc_put_att_double(gCont->NCIds[ncidx], gCont->MinIds[ncidx], NCnameVAActualRange, NC_FLOAT,
                                             2, minRange)) != NC_NOERR) ||
                ((status = nc_put_att_double(gCont->NCIds[ncidx], gCont->MaxIds[ncidx], NCnameVAActualRange, NC_FLOAT,
                                             2, maxRange)) != NC_NOERR) ||
                ((status = nc_enddef(gCont->NCIds[ncidx])) != NC_NOERR)) {
                NCprintNCError (status, "_NCdsHandleGContUpdateRange");
                goto ABORT;
            }
        }
    }
    for (ncidx = 0; ncidx < gCont->NCnum; ncidx++)
        if (((status = nc_redef(gCont->NCIds[ncidx])) != NC_NOERR) ||
            ((status = nc_put_att_double(gCont->NCIds[ncidx], gCont->GVarIds[ncidx], NCnameVAValidRange, NC_FLOAT, 2,
                                         glbRange)) != NC_NOERR) ||
            ((status = nc_put_att_double(gCont->NCIds[ncidx], gCont->MeanIds[ncidx], NCnameVAValidRange, NC_FLOAT, 2,
                                         glbRange)) != NC_NOERR) ||
            ((status = nc_put_att_double(gCont->NCIds[ncidx], gCont->MinIds[ncidx], NCnameVAValidRange, NC_FLOAT, 2,
                                         glbRange)) != NC_NOERR) ||
            ((status = nc_put_att_double(gCont->NCIds[ncidx], gCont->MaxIds[ncidx], NCnameVAValidRange, NC_FLOAT, 2,
                                         glbRange)) != NC_NOERR) ||
            ((status = nc_enddef(gCont->NCIds[ncidx])) != NC_NOERR)) {
            NCprintNCError (status, "_NCdsHandleGContUpdateRange");
            goto ABORT;
        }
    free(meanArray);
    free(minArray);
    free(maxArray);
    return (NCsucceeded);

    ABORT:
    if (meanArray != (double *) NULL) free(meanArray);
    if (minArray != (double *) NULL) free(minArray);
    if (maxArray != (double *) NULL) free(maxArray);
    return (NCfailed);
}