diff --git a/src/stream_cdf_i.c b/src/stream_cdf_i.c index c7487389acc43625f6e28e3165567ad98a1bf810..56acce51aa37439ad0dcf46f67a213ad988fe976 100644 --- a/src/stream_cdf_i.c +++ b/src/stream_cdf_i.c @@ -1803,7 +1803,7 @@ void cdf_set_dimtype(int nvars, ncvar_t *ncvars, ncdim_t *ncdims) // verify coordinates vars - first scan (dimname == varname) static -void verify_coordinates_vars_1(int ncid, int ndims, ncdim_t *ncdims, ncvar_t *ncvars, int timedimid, bool *lhybrid_cf) +void verify_coordinates_vars_1(int ncid, int ndims, ncdim_t *ncdims, ncvar_t *ncvars, int timedimid, bool *isHybridCF) { for ( int ncdimid = 0; ncdimid < ndims; ncdimid++ ) { @@ -1820,7 +1820,7 @@ void verify_coordinates_vars_1(int ncid, int ndims, ncdim_t *ncdims, ncvar_t *nc if ( isHybridSigmaPressureCoordinate(ncid, ncvarid, ncvars, ncdims) ) { - *lhybrid_cf = true; + *isHybridCF = true; continue; } @@ -3805,46 +3805,61 @@ void find_time_vars(int nvars, ncvar_t *ncvars, ncdim_t *ncdims, int timedimid, static void read_vct_echam(int fileID, int nvars, ncvar_t *ncvars, ncdim_t *ncdims, double **vct, size_t *pvctsize) { - /* find ECHAM VCT */ + // find ECHAM VCT int nvcth_id = CDI_UNDEFID, vcta_id = CDI_UNDEFID, vctb_id = CDI_UNDEFID; + // int p0_id = CDI_UNDEFID; for (int ncvarid = 0; ncvarid < nvars; ncvarid++) { ncvar_t *ncvar = &ncvars[ncvarid]; - if ( ncvar->ndims == 1 ) + const char *name = ncvar->name; + if (ncvar->ndims == 1) { - size_t len = strlen(ncvar->name); - if ( len == 4 && ncvar->name[0] == 'h' && ncvar->name[1] == 'y' ) + const size_t len = strlen(name); + if (len == 4 && name[0] == 'h' && name[1] == 'y') { - if ( ncvar->name[2] == 'a' && ncvar->name[3] == 'i' ) // hyai + if (name[2] == 'a' && name[3] == 'i') // hyai { vcta_id = ncvarid; nvcth_id = ncvar->dimids[0]; ncvar->varStatus = CoordVar; } - else if ( ncvar->name[2] == 'b' && ncvar->name[3] == 'i' ) //hybi + else if (name[2] == 'b' && name[3] == 'i') // hybi { vctb_id = ncvarid; nvcth_id = ncvar->dimids[0]; ncvar->varStatus = CoordVar; } - else if ( (ncvar->name[2] == 'a' || ncvar->name[2] == 'b') && ncvar->name[3] == 'm' ) + else if ((name[2] == 'a' || name[2] == 'b') && name[3] == 'm' ) { ncvar->varStatus = CoordVar; // hyam or hybm } } } + /* + else if (ncvar->ndims == 0) + { + const size_t len = strlen(name); + if (len == 2 && name[0] == 'P' && name[1] == '0') p0_id = ncvarid; + } + */ } - /* read VCT */ - if ( nvcth_id != CDI_UNDEFID && vcta_id != CDI_UNDEFID && vctb_id != CDI_UNDEFID ) + // read VCT + if (nvcth_id != CDI_UNDEFID && vcta_id != CDI_UNDEFID && vctb_id != CDI_UNDEFID) { - size_t vctsize = ncdims[nvcth_id].len; - vctsize *= 2; - *vct = (double *) Malloc(vctsize*sizeof(double)); + const size_t vctsize = 2 * ncdims[nvcth_id].len; + *vct = (double *) Malloc(vctsize * sizeof(double)); cdf_get_var_double(fileID, vcta_id, *vct); - cdf_get_var_double(fileID, vctb_id, *vct+vctsize/2); + cdf_get_var_double(fileID, vctb_id, *vct + vctsize/2); *pvctsize = vctsize; + /* + if (p0_id != CDI_UNDEFID) + { + double p0; + cdf_get_var_double(fileID, p0_id, &p0); + } + */ } } @@ -4160,8 +4175,8 @@ int cdfInqContents(stream_t *streamptr) timedimid = cdf_check_variables(streamptr, nvars, ncvars, ntsteps, timedimid); // verify coordinates vars - first scan (dimname == varname) - bool lhybrid_cf = false; - verify_coordinates_vars_1(fileID, ndims, ncdims, ncvars, timedimid, &lhybrid_cf); + bool isHybridCF = false; + verify_coordinates_vars_1(fileID, ndims, ncdims, ncvars, timedimid, &isHybridCF); // verify coordinates vars - second scan (all other variables) verify_coordinates_vars_2(streamptr, nvars, ncvars); @@ -4229,7 +4244,7 @@ int cdfInqContents(stream_t *streamptr) // read ECHAM VCT if present size_t vctsize = 0; double *vct = NULL; - if (!lhybrid_cf) read_vct_echam(fileID, nvars, ncvars, ncdims, &vct, &vctsize); + if (!isHybridCF) read_vct_echam(fileID, nvars, ncvars, ncdims, &vct, &vctsize); if (CDI_Debug) cdf_print_vars(ncvars, nvars, "cdf_define_all_grids"); diff --git a/src/stream_cdf_o.c b/src/stream_cdf_o.c index cbaae0a55b5986ad5a0ce2d75a6147cb38eaff62..ab8af4767dc784c1cf1f2a9fec3200e9ca7fac93 100644 --- a/src/stream_cdf_o.c +++ b/src/stream_cdf_o.c @@ -911,23 +911,22 @@ static void cdf_def_vct_echam(stream_t *streamptr, int zaxisID) { const int type = zaxisInqType(zaxisID); - if ( type == ZAXIS_HYBRID || type == ZAXIS_HYBRID_HALF ) + if (type == ZAXIS_HYBRID || type == ZAXIS_HYBRID_HALF) { const int ilev = zaxisInqVctSize(zaxisID)/2; - if ( ilev == 0 ) return; + if (ilev == 0) return; const int mlev = ilev - 1; - if ( streamptr->vct.ilev > 0 ) + if (streamptr->vct.ilev > 0) { - if ( streamptr->vct.ilev != ilev ) - Error("More than one VCT for each file unsupported!"); + if (streamptr->vct.ilev != ilev) Error("More than one VCT for each file unsupported!"); return; } const int fileID = streamptr->fileID; - if ( streamptr->ncmode == 2 ) cdf_redef(fileID); + if (streamptr->ncmode == 2) cdf_redef(fileID); int ncdimid = -1, ncdimid2 = -1; int hyaiid, hybiid, hyamid = -1, hybmid = -1; @@ -935,7 +934,7 @@ void cdf_def_vct_echam(stream_t *streamptr, int zaxisID) cdf_def_dim(fileID, "nhyi", (size_t)ilev, &ncdimid2); cdf_def_var(fileID, "hyai", NC_DOUBLE, 1, &ncdimid2, &hyaiid); cdf_def_var(fileID, "hybi", NC_DOUBLE, 1, &ncdimid2, &hybiid); - if ( mlev > 0 ) + if (mlev > 0) { cdf_def_dim(fileID, "nhym", (size_t)mlev, &ncdimid); cdf_def_var(fileID, "hyam", NC_DOUBLE, 1, &ncdimid, &hyamid); @@ -963,7 +962,7 @@ void cdf_def_vct_echam(stream_t *streamptr, int zaxisID) }; enum { tabLen = sizeof (tab) / sizeof (tab[0]) }; const int ids[tabLen] = { hyaiid, hyaiid, hybiid, hybiid }; - for ( size_t i = 0; i < tabLen; ++i ) + for (size_t i = 0; i < tabLen; ++i) cdf_put_att_text(fileID, ids[i], tab[i].attName, tab[i].valLen, tab[i].attVal); } @@ -983,7 +982,7 @@ void cdf_def_vct_echam(stream_t *streamptr, int zaxisID) }; enum { tabLen = sizeof (tab) / sizeof (tab[0]) }; const int ids[tabLen] = { hyamid, hyamid, hybmid, hybmid }; - for ( size_t i = 0; i < tabLen; ++i ) + for (size_t i = 0; i < tabLen; ++i) cdf_put_att_text(fileID, ids[i], tab[i].attName, tab[i].valLen, tab[i].attVal); } @@ -995,13 +994,11 @@ void cdf_def_vct_echam(stream_t *streamptr, int zaxisID) cdf_put_var_double(fileID, hyaiid, vctptr); cdf_put_var_double(fileID, hybiid, vctptr+ilev); - size_t start; size_t count = 1; - double mval; - for ( int i = 0; i < mlev; i++ ) + for (int i = 0; i < mlev; i++) { - start = (size_t)i; - mval = (vctptr[i] + vctptr[i+1]) * 0.5; + size_t start = (size_t)i; + double mval = (vctptr[i] + vctptr[i+1]) * 0.5; cdf_put_vara_double(fileID, hyamid, &start, &count, &mval); mval = (vctptr[ilev+i] + vctptr[ilev+i+1]) * 0.5; cdf_put_vara_double(fileID, hybmid, &start, &count, &mval); @@ -1013,24 +1010,23 @@ static void cdf_def_vct_cf(stream_t *streamptr, int zaxisID, int nclevID, int ncbndsID, int p0status, double p0value) { const int type = zaxisInqType(zaxisID); - if ( type == ZAXIS_HYBRID || type == ZAXIS_HYBRID_HALF ) + if (type == ZAXIS_HYBRID || type == ZAXIS_HYBRID_HALF) { const int ilev = zaxisInqVctSize(zaxisID)/2; - if ( ilev == 0 ) return; + if (ilev == 0) return; const int mlev = ilev - 1; int hyaiid = 0, hybiid = 0, hyamid, hybmid; - if ( streamptr->vct.ilev > 0 ) + if (streamptr->vct.ilev > 0) { - if ( streamptr->vct.ilev != ilev ) - Error("more than one VCT for each file unsupported!"); + if (streamptr->vct.ilev != ilev) Error("more than one VCT for each file unsupported!"); return; } const int fileID = streamptr->fileID; - if ( streamptr->ncmode == 2 ) cdf_redef(fileID); + if (streamptr->ncmode == 2) cdf_redef(fileID); int dimIDs[2]; dimIDs[0] = nclevID; @@ -1041,10 +1037,7 @@ void cdf_def_vct_cf(stream_t *streamptr, int zaxisID, int nclevID, int ncbndsID, streamptr->vct.mlevID = nclevID; streamptr->vct.ilevID = nclevID; - if ( p0status == 0 ) - cdf_def_var(fileID, "a", NC_DOUBLE, 1, dimIDs, &hyamid); - else - cdf_def_var(fileID, "ap", NC_DOUBLE, 1, dimIDs, &hyamid); + cdf_def_var(fileID, (p0status == 0) ? "a" : "ap", NC_DOUBLE, 1, dimIDs, &hyamid); cdf_def_var(fileID, "b", NC_DOUBLE, 1, dimIDs, &hybmid); { @@ -1064,10 +1057,7 @@ void cdf_def_vct_cf(stream_t *streamptr, int zaxisID, int nclevID, int ncbndsID, if ( ncbndsID != -1 ) { - if ( p0status == 0 ) - cdf_def_var(fileID, "a_bnds", NC_DOUBLE, 2, dimIDs, &hyaiid); - else - cdf_def_var(fileID, "ap_bnds", NC_DOUBLE, 2, dimIDs, &hyaiid); + cdf_def_var(fileID, (p0status == 0) ? "a_bnds" : "ap_bnds", NC_DOUBLE, 2, dimIDs, &hyaiid); cdf_def_var(fileID, "b_bnds", NC_DOUBLE, 2, dimIDs, &hybiid); { @@ -1093,21 +1083,21 @@ void cdf_def_vct_cf(stream_t *streamptr, int zaxisID, int nclevID, int ncbndsID, double *vct = (double*) malloc(vctsize*sizeof(double));; zaxisInqVct(zaxisID, vct); - if ( p0status == 0 && IS_NOT_EQUAL(p0value,0) ) - for ( int i = 0; i < vctsize/2; ++i ) vct[i] /= p0value; + if (p0status == 0 && IS_NOT_EQUAL(p0value,0)) + for (int i = 0; i < vctsize/2; ++i) vct[i] /= p0value; - double *tarray = (double*) malloc(ilev*2*sizeof(double)); + double *tarray = (double*) malloc(ilev * 2 * sizeof(double)); - if ( ncbndsID != -1 ) + if (ncbndsID != -1) { - for ( int i = 0; i < mlev; ++i ) + for (int i = 0; i < mlev; ++i) { tarray[2*i ] = vct[i]; tarray[2*i+1] = vct[i+1]; } cdf_put_var_double(fileID, hyaiid, tarray); - for ( int i = 0; i < mlev; ++i ) + for (int i = 0; i < mlev; ++i) { tarray[2*i ] = vct[ilev+i]; tarray[2*i+1] = vct[ilev+i+1];