void NCdsHandleNetworkClear(NCdsHandleNetwork_t *net) { NCdsHandleGLayoutClear((NCdsHandleGLayout_t *) net); if (net->Data != (int *) NULL) free(net->Data); if (net->Basins.Table != (NCtable_t *) NULL) NCtableClose(net->Basins.Table); if (net->Cells.Table != (NCtable_t *) NULL) NCtableClose(net->Cells.Table); }
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); }