ncdf4a13/libsrc4/tst_files.c File Reference

#include <config.h>
#include <nc4internal.h>
#include <netcdf.h>
#include <nc_tests.h>

Include dependency graph for tst_files.c:

Go to the source code of this file.

Defines

#define FILE_NAME   "tst_files.nc"
#define ATT1_NAME   "MoneyOwned"
#define ATT2_NAME   "Number_of_Shoes"
#define ATT3_NAME   "att3"
#define DIM1_NAME   "Character_Growth"
#define DIM1_LEN   10
#define DIM2_NAME   "TimeInMonths"
#define DIM2_LEN   15
#define VAR1_NAME   "HuckFinn"
#define VAR2_NAME   "TomSawyer"
#define VAR3_NAME   "Jim"
#define REDEF_ATT1_NAME   "CANTERBURY"
#define REDEF_ATT2_NAME   "ELY"
#define REDEF_ATT3_NAME   "KING_HENRY_V"
#define REDEF_DIM1_NAME   "performance_length"
#define REDEF_DIM1_LEN   101
#define REDEF_NAME_ILLEGAL   "?"
#define REDEF_DIM2_NAME   "ZZ_number_of_performers_or_perhaps_actors_or_maybe_I_should_say_players_or_one_could_call_them_artists_but_one_doesnt_like_to_get"
#define REDEF_DIM2_LEN   999
#define REDEF_VAR1_NAME   "Royal_Shakespeare_Company_season_of_2004"
#define REDEF_VAR2_NAME   "Ms_Beths_Kindergardern_class_of_2003"
#define REDEF_VAR3_NAME   "Costumed_Mice_in_my_Garage_in_the_Winter_of_my_Discontent"
#define REDEF_NDIMS   2

Functions

void test_redef (int format)
int main (int argc, char **argv)

Variables

NC_FILE_INFO_Tnc_file


Define Documentation

#define ATT1_NAME   "MoneyOwned"
 

Definition at line 18 of file tst_files.c.

#define ATT2_NAME   "Number_of_Shoes"
 

Definition at line 19 of file tst_files.c.

#define ATT3_NAME   "att3"
 

Definition at line 20 of file tst_files.c.

#define DIM1_LEN   10
 

Definition at line 22 of file tst_files.c.

#define DIM1_NAME   "Character_Growth"
 

Definition at line 21 of file tst_files.c.

#define DIM2_LEN   15
 

Definition at line 24 of file tst_files.c.

#define DIM2_NAME   "TimeInMonths"
 

Definition at line 23 of file tst_files.c.

#define FILE_NAME   "tst_files.nc"
 

Definition at line 17 of file tst_files.c.

#define REDEF_ATT1_NAME   "CANTERBURY"
 

Definition at line 301 of file tst_files.c.

Referenced by test_redef().

#define REDEF_ATT2_NAME   "ELY"
 

Definition at line 302 of file tst_files.c.

Referenced by test_redef().

#define REDEF_ATT3_NAME   "KING_HENRY_V"
 

Definition at line 303 of file tst_files.c.

Referenced by test_redef().

#define REDEF_DIM1_LEN   101
 

Definition at line 305 of file tst_files.c.

Referenced by test_redef().

#define REDEF_DIM1_NAME   "performance_length"
 

Definition at line 304 of file tst_files.c.

Referenced by test_redef().

#define REDEF_DIM2_LEN   999
 

Definition at line 309 of file tst_files.c.

Referenced by test_redef().

#define REDEF_DIM2_NAME   "ZZ_number_of_performers_or_perhaps_actors_or_maybe_I_should_say_players_or_one_could_call_them_artists_but_one_doesnt_like_to_get"
 

Definition at line 307 of file tst_files.c.

Referenced by test_redef().

#define REDEF_NAME_ILLEGAL   "?"
 

Definition at line 306 of file tst_files.c.

Referenced by test_redef().

#define REDEF_NDIMS   2
 

Definition at line 313 of file tst_files.c.

Referenced by test_redef().

#define REDEF_VAR1_NAME   "Royal_Shakespeare_Company_season_of_2004"
 

Definition at line 310 of file tst_files.c.

Referenced by test_redef().

#define REDEF_VAR2_NAME   "Ms_Beths_Kindergardern_class_of_2003"
 

Definition at line 311 of file tst_files.c.

Referenced by test_redef().

#define REDEF_VAR3_NAME   "Costumed_Mice_in_my_Garage_in_the_Winter_of_my_Discontent"
 

Definition at line 312 of file tst_files.c.

Referenced by test_redef().

#define VAR1_NAME   "HuckFinn"
 

Definition at line 25 of file tst_files.c.

#define VAR2_NAME   "TomSawyer"
 

Definition at line 26 of file tst_files.c.

#define VAR3_NAME   "Jim"
 

Definition at line 27 of file tst_files.c.


Function Documentation

int main int  argc,
char **  argv
 

Definition at line 30 of file tst_files.c.

References ATT1_NAME, ATT2_NAME, DIM1_LEN, DIM1_NAME, DIM2_LEN, DIM2_NAME, dim_len, dim_name, ERR, FILE_NAME, FINAL_RESULTS, natts, NC_64BIT_OFFSET, NC_BYTE, NC_CLASSIC_MODEL, NC_CLOBBER, nc_close, nc_create, nc_def_dim, nc_def_var, NC_EEXIST, NC_EINVAL, nc_enddef, NC_ENOTINDEFINE, NC_ERANGE, nc_exit, NC_FLOAT, NC_FORMAT_64BIT, NC_FORMAT_CLASSIC, NC_FORMAT_NETCDF4, NC_FORMAT_NETCDF4_CLASSIC, nc_get_att_float, nc_get_att_int, NC_GLOBAL, nc_inq, nc_inq_dim, nc_inq_format, nc_inq_libvers, nc_inq_var, NC_INT, NC_MAX_NAME, NC_MPIIO, NC_MPIPOSIX, NC_NETCDF4, NC_NOCLOBBER, nc_open, nc_put_att_float, nc_put_att_int, nc_put_var_uchar, nc_redef, NC_UINT, NC_WRITE, ncid, ndims, nvars, SUMMARIZE_ERR, test_redef(), VAR1_NAME, VAR2_NAME, VAR3_NAME, var_name, and var_type.

00031 {
00032    printf("\n*** Testing netcdf-4 file functions.\n");
00033    {
00034       char str[NC_MAX_NAME+1];
00035       
00036       /* Actually we never make any promises about the length of the
00037        * version string, but it is always smaller than NC_MAX_NAME. */
00038       if (strlen(nc_inq_libvers()) > NC_MAX_NAME) ERR;
00039       strcpy(str, nc_inq_libvers());
00040       if (strcmp(str, PACKAGE_VERSION)) ERR;
00041       printf("*** testing version %s...", str);
00042    }
00043    SUMMARIZE_ERR;
00044 
00045    printf("*** testing with bad inputs...");
00046    {
00047       int ncid;
00048 
00049       /* Make sure bad create mode causes failure. */
00050       /*if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;*/
00051       
00052       /* Create an empty file. */
00053       if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
00054       if (nc_close(ncid)) ERR;
00055 
00056       /* Make sure bad mode causes failure in nc_open. */
00057       if (nc_open(FILE_NAME, NC_CLASSIC_MODEL, &ncid) != NC_EINVAL) ERR;
00058       if (nc_open(FILE_NAME, NC_MPIIO|NC_MPIPOSIX, &ncid) != NC_EINVAL) ERR;
00059       if (nc_open(FILE_NAME, NC_NOCLOBBER, &ncid) != NC_EINVAL) ERR;
00060 
00061       /* Make sure bad mode causes failure for nc_create. */
00062       if (nc_create(FILE_NAME, NC_WRITE, &ncid) != NC_EINVAL) ERR;
00063       if (nc_create(FILE_NAME, NC_64BIT_OFFSET|NC_NETCDF4, &ncid) != NC_EINVAL) ERR;
00064       if (nc_create(FILE_NAME, NC_64BIT_OFFSET|NC_CLASSIC_MODEL, &ncid) != NC_EINVAL) ERR;
00065       if (nc_create(FILE_NAME, NC_CLASSIC_MODEL, &ncid) != NC_EINVAL) ERR;
00066       if (nc_create(FILE_NAME, NC_CLASSIC_MODEL|NC_MPIIO|NC_MPIPOSIX, &ncid) != NC_EINVAL) ERR;
00067       if (nc_create(FILE_NAME, NC_MPIIO|NC_MPIPOSIX, &ncid) != NC_EINVAL) ERR;
00068    }
00069    SUMMARIZE_ERR;
00070 
00071    printf("*** testing simple opens and creates...");
00072    {
00073       int ncid, varid, dimids[2];
00074       int ndims, nvars, natts, unlimdimid;
00075       int dimids_var[1], var_type;
00076       size_t dim_len;
00077       char dim_name[NC_MAX_NAME+1], var_name[NC_MAX_NAME+1];
00078       unsigned char uchar_out[DIM1_LEN] = {0, 128, 255};
00079 
00080       /*nc_set_log_level(3);*/
00081       
00082       /* Open and close empty file. */
00083       if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
00084       if (nc_close(ncid)) ERR;
00085 
00086       /* Recreate it again. */
00087       if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
00088       if (nc_def_dim(ncid, DIM1_NAME, DIM1_LEN, &dimids[0])) ERR;
00089       if (nc_def_dim(ncid, DIM2_NAME, DIM2_LEN, &dimids[1])) ERR;
00090       if (nc_def_var(ncid, VAR1_NAME, NC_INT, 1, dimids, &varid)) ERR;
00091       if (nc_enddef(ncid)) ERR;
00092       if (nc_def_var(ncid, VAR2_NAME, NC_UINT, 2, dimids, &varid)) ERR;
00093       if (nc_close(ncid)) ERR;
00094 
00095       /* Check the contents. Then define a new variable. Since it's
00096        * netcdf-4, nc_enddef isn't required - it's called
00097        * automatically. */
00098       if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR;
00099       if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR;
00100       if (ndims != 2 || nvars != 2 || natts != 0 || unlimdimid != -1) ERR;
00101       if (nc_redef(ncid)) ERR;
00102       if (nc_enddef(ncid)) ERR;
00103       if (nc_def_var(ncid, VAR3_NAME, NC_INT, 2, dimids, &varid)) ERR;
00104       if (nc_close(ncid)) ERR;
00105 
00106       /* Open and close empty file. */
00107       if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
00108       if (nc_close(ncid)) ERR;
00109 
00110       /* Check the contents. */
00111       if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR;
00112       if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR;
00113       if (ndims != 0 || nvars != 0 || natts != 0 || unlimdimid != -1) ERR;
00114       if (nc_close(ncid)) ERR;
00115 
00116       /* Create a file with one dimension and nothing else. */
00117       if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
00118       if (nc_def_dim(ncid, DIM1_NAME, DIM1_LEN, &dimids[0])) ERR;
00119       if (nc_enddef(ncid)) ERR;
00120       if (nc_close(ncid)) ERR;
00121 
00122       /* Check the contents. */
00123       if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR;
00124       if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR;
00125       if (ndims != 1 || nvars != 0 || natts != 0 || unlimdimid != -1) ERR;
00126       if (nc_inq_dim(ncid, 0, dim_name, &dim_len)) ERR;
00127       if (dim_len != DIM1_LEN || strcmp(dim_name, DIM1_NAME)) ERR;
00128       if (nc_close(ncid)) ERR;
00129 
00130       /* Create a simple file, and write some data to it. */
00131       if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
00132       if (nc_def_dim(ncid, DIM1_NAME, DIM1_LEN, &dimids[0])) ERR;
00133       if (nc_def_var(ncid, VAR1_NAME, NC_BYTE, 1, dimids, &varid)) ERR;
00134       if (nc_enddef(ncid)) ERR;
00135       if (nc_put_var_uchar(ncid, varid, uchar_out) != NC_ERANGE) ERR;
00136       if (nc_close(ncid)) ERR;
00137 
00138       /* Check the contents. */
00139       if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR;
00140       if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR;
00141       if (ndims != 1 || nvars != 1 || natts != 0 || unlimdimid != -1) ERR;
00142       if (nc_inq_dim(ncid, 0, dim_name, &dim_len)) ERR;
00143       if (dim_len != DIM1_LEN || strcmp(dim_name, DIM1_NAME)) ERR;
00144       if (nc_inq_var(ncid, 0, var_name, &var_type, &ndims, dimids_var, &natts)) ERR;
00145       if (ndims != 1 || strcmp(var_name, VAR1_NAME) || var_type != NC_BYTE ||
00146           dimids_var[0] != dimids[0] || natts != 0) ERR;
00147       if (nc_close(ncid)) ERR;
00148 
00149       /* Recreate the file. */
00150       if (nc_create(FILE_NAME, NC_NETCDF4|NC_CLASSIC_MODEL, &ncid)) ERR;
00151       if (nc_def_dim(ncid, DIM1_NAME, DIM1_LEN, &dimids[0])) ERR;
00152       if (nc_def_var(ncid, VAR1_NAME, NC_BYTE, 1, dimids, &varid)) ERR;
00153       if (nc_enddef(ncid)) ERR;
00154       if (nc_put_var_uchar(ncid, varid, uchar_out)) ERR;
00155       if (nc_close(ncid)) ERR;
00156 
00157       /* Recreate it, then make sure NOCLOBBER works. */
00158       if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
00159       if (nc_close(ncid)) ERR;
00160       if (nc_create(FILE_NAME, NC_NETCDF4|NC_NOCLOBBER, &ncid) != NC_EEXIST) ERR;
00161 
00162       /* Recreate it again. */
00163       if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
00164       if (nc_def_dim(ncid, DIM1_NAME, DIM1_LEN, &dimids[0])) ERR;
00165       if (nc_def_dim(ncid, DIM2_NAME, DIM2_LEN, &dimids[1])) ERR;
00166       if (nc_def_var(ncid, VAR1_NAME, NC_INT, 1, dimids, &varid)) ERR;
00167       if (nc_enddef(ncid)) ERR;
00168       if (nc_def_var(ncid, VAR2_NAME, NC_UINT, 2, dimids, &varid)) ERR;
00169       if (nc_close(ncid)) ERR;
00170 
00171       /* Check the contents. Then define a new variable. Since it's
00172        * netcdf-4, nc_enddef isn't required - it's called
00173        * automatically. */
00174       if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR;
00175       if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR;
00176       if (ndims != 2 || nvars != 2 || natts != 0 || unlimdimid != -1) ERR;
00177       if (nc_redef(ncid)) ERR;
00178       if (nc_enddef(ncid)) ERR;
00179       if (nc_def_var(ncid, VAR3_NAME, NC_INT, 2, dimids, &varid)) ERR;
00180       if (nc_close(ncid)) ERR;
00181 
00182       /* Recreate it again with netcdf-3 rules turned on. */
00183       if (nc_create(FILE_NAME, NC_NETCDF4|NC_CLASSIC_MODEL, &ncid)) ERR;
00184       if (nc_def_dim(ncid, DIM1_NAME, DIM1_LEN, &dimids[0])) ERR;
00185       if (nc_def_var(ncid, VAR1_NAME, NC_INT, 1, dimids, &varid)) ERR;
00186       if (nc_enddef(ncid)) ERR;
00187       if (nc_close(ncid)) ERR;
00188 
00189       /* Check the contents. Check that netcdf-3 rules are in effect. */
00190       if (nc_open(FILE_NAME, 0, &ncid)) ERR;
00191       if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR;
00192       if (ndims != 1 || nvars != 1 || natts != 0 || unlimdimid != -1) ERR;
00193       if (nc_def_var(ncid, VAR2_NAME, NC_UINT, 2, dimids, &varid) != NC_ENOTINDEFINE) ERR;
00194       if (nc_close(ncid)) ERR;
00195 
00196       /* Check some other stuff about it. Closing and reopening the
00197        * file forces HDF5 to tell us if we forgot to free some HDF5
00198        * resource associated with the file. */
00199       if (nc_open(FILE_NAME, 0, &ncid)) ERR;
00200       if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR;
00201       if (nc_inq_dim(ncid, 0, dim_name, &dim_len)) ERR;
00202       if (dim_len != DIM1_LEN || strcmp(dim_name, DIM1_NAME)) ERR;
00203       if (nc_inq_var(ncid, 0, var_name, &var_type, &ndims, dimids_var, &natts)) ERR;
00204       if (ndims != 1 || strcmp(var_name, VAR1_NAME) || var_type != NC_INT ||
00205           dimids_var[0] != dimids[0] || natts != 0) ERR;
00206       if (nc_close(ncid)) ERR;      
00207    }
00208 
00209    SUMMARIZE_ERR;
00210    printf("*** testing more complex opens and creates...");
00211    {
00212       int ncid, varid, dimids[2];
00213       int ndims, nvars, natts, unlimdimid;
00214       int dimids_var[2], var_type;
00215       size_t dim_len;
00216       char dim_name[NC_MAX_NAME+1], var_name[NC_MAX_NAME+1];
00217       float float_in, float_out = 99.99;
00218       int int_in, int_out = -9999;
00219 
00220       /* Create a file, this time with attributes. */
00221       if (nc_create(FILE_NAME, NC_NETCDF4|NC_CLOBBER, &ncid)) ERR;
00222       if (nc_def_dim(ncid, DIM1_NAME, DIM1_LEN, &dimids[0])) ERR;
00223       if (nc_def_dim(ncid, DIM2_NAME, DIM2_LEN, &dimids[1])) ERR;
00224       if (nc_def_var(ncid, VAR1_NAME, NC_INT, 2, dimids, &varid)) ERR;
00225       if (nc_def_var(ncid, VAR2_NAME, NC_UINT, 2, dimids, &varid)) ERR;
00226       if (nc_put_att_float(ncid, NC_GLOBAL, ATT1_NAME, NC_FLOAT, 1, &float_out)) ERR;
00227       if (nc_put_att_int(ncid, NC_GLOBAL, ATT2_NAME, NC_INT, 1, &int_out)) ERR;
00228       if (nc_close(ncid)) ERR;
00229 
00230       /* Reopen the file and check it. */
00231       if (nc_open(FILE_NAME, 0, &ncid)) ERR;
00232       if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR;
00233       if (ndims != 2 || nvars != 2 || natts != 2 || unlimdimid != -1) ERR;
00234       if (nc_close(ncid)) ERR;
00235 
00236       /* Reopen it and check each dim, var, and att. */
00237       if (nc_open(FILE_NAME, 0, &ncid)) ERR;
00238       if (nc_inq_dim(ncid, 0, dim_name, &dim_len)) ERR;
00239       if (dim_len != DIM1_LEN || strcmp(dim_name, DIM1_NAME)) ERR;
00240       if (nc_inq_dim(ncid, 1, dim_name, &dim_len)) ERR;
00241       if (dim_len != DIM2_LEN || strcmp(dim_name, DIM2_NAME)) ERR;
00242       if (nc_inq_var(ncid, 0, var_name, &var_type, &ndims, dimids_var, &natts)) ERR;
00243       if (ndims != 2 || strcmp(var_name, VAR1_NAME) || var_type != NC_INT ||
00244           dimids_var[0] != dimids[0] || natts != 0) ERR;
00245       if (nc_inq_var(ncid, 1, var_name, &var_type, &ndims, dimids_var, &natts)) ERR;
00246       if (ndims != 2 || strcmp(var_name, VAR2_NAME) || var_type != NC_UINT ||
00247           dimids_var[1] != dimids[1] || natts != 0) ERR;
00248       if (nc_get_att_float(ncid, NC_GLOBAL, ATT1_NAME, &float_in)) ERR;
00249       if (float_in != float_out) ERR;
00250       if (nc_get_att_int(ncid, NC_GLOBAL, ATT2_NAME, &int_in)) ERR;
00251       if (int_in != int_out) ERR;
00252       if (nc_close(ncid)) ERR;      
00253    }
00254 
00255    SUMMARIZE_ERR;
00256 
00257    printf("*** testing redef for netCDF classic...");
00258    test_redef(NC_FORMAT_CLASSIC);
00259    SUMMARIZE_ERR;
00260    printf("*** testing redef for netCDF 64-bit offset...");
00261    test_redef(NC_FORMAT_64BIT);
00262    SUMMARIZE_ERR;
00263    printf("*** testing redef for netCDF-4 ...");
00264    test_redef(NC_FORMAT_NETCDF4);
00265    SUMMARIZE_ERR;
00266    printf("*** testing redef for netCDF-4, with strict netCDF-3 rules...");
00267    test_redef(NC_FORMAT_NETCDF4_CLASSIC);
00268    SUMMARIZE_ERR;
00269 
00270    printf("*** testing different formats...");
00271    {
00272       int ncid;
00273       int format;
00274 
00275       /* Create a netcdf-3 file. */
00276       if (nc_create(FILE_NAME, NC_CLOBBER, &ncid)) ERR;
00277       if (nc_inq_format(ncid, &format)) ERR;
00278       if (format != NC_FORMAT_CLASSIC) ERR;
00279       if (nc_close(ncid)) ERR;
00280 
00281       /* Create a netcdf-3 64-bit offset file. */
00282       if (nc_create(FILE_NAME, NC_64BIT_OFFSET|NC_CLOBBER, &ncid)) ERR;
00283       if (nc_inq_format(ncid, &format)) ERR;
00284       if (format != NC_FORMAT_64BIT) ERR;
00285       if (nc_close(ncid)) ERR;
00286 
00287       /* Create a netcdf-3 file. */
00288       if (nc_create(FILE_NAME, NC_NETCDF4|NC_CLOBBER, &ncid)) ERR;
00289       if (nc_inq_format(ncid, &format)) ERR;
00290       if (format != NC_FORMAT_NETCDF4) ERR;
00291       if (nc_close(ncid)) ERR;
00292    }
00293 
00294    nc_exit();
00295 
00296    SUMMARIZE_ERR;
00297    
00298    FINAL_RESULTS;
00299 }

void test_redef int  format  ) 
 

Definition at line 316 of file tst_files.c.

References dim_len, dim_name, ERR, FILE_NAME, natts, NC_64BIT_OFFSET, NC_BYTE, NC_CHAR, NC_CLASSIC_MODEL, NC_CLOBBER, nc_close, nc_create, nc_def_dim, nc_def_var, NC_DOUBLE, NC_EBADNAME, nc_enddef, NC_ENOTINDEFINE, NC_EPERM, NC_ERANGE, NC_FORMAT_64BIT, NC_FORMAT_NETCDF4, NC_FORMAT_NETCDF4_CLASSIC, nc_get_att_float, nc_get_att_int, nc_get_att_uchar, NC_GLOBAL, nc_inq, nc_inq_dim, nc_inq_var, NC_INT, NC_MAX_NAME, NC_NETCDF4, NC_NOWRITE, nc_open, nc_put_att_double, nc_put_att_short, nc_put_att_uchar, nc_redef, NC_SHORT, NC_UBYTE, NC_WRITE, ncid, ndims, nvars, REDEF_ATT1_NAME, REDEF_ATT2_NAME, REDEF_ATT3_NAME, REDEF_DIM1_LEN, REDEF_DIM1_NAME, REDEF_DIM2_LEN, REDEF_DIM2_NAME, REDEF_NAME_ILLEGAL, REDEF_NDIMS, REDEF_VAR1_NAME, REDEF_VAR2_NAME, REDEF_VAR3_NAME, var_name, and var_type.

Referenced by main().

00317 {
00318    int ncid, varid, dimids[REDEF_NDIMS], dimids_in[REDEF_NDIMS];
00319    int ndims, nvars, natts, unlimdimid;
00320    int dimids_var[REDEF_NDIMS], var_type;
00321    int cflags = 0;
00322    size_t dim_len;
00323    char dim_name[NC_MAX_NAME+1], var_name[NC_MAX_NAME+1];
00324    float float_in;
00325    double double_out = 99E99;
00326    int int_in;
00327    unsigned char uchar_in, uchar_out = 255;
00328    short short_out = -999;
00329    nc_type xtype_in;
00330    int ret;
00331 
00332    if (format == NC_FORMAT_64BIT)
00333       cflags |= NC_64BIT_OFFSET;
00334    else if (format == NC_FORMAT_NETCDF4_CLASSIC)
00335       cflags |= (NC_NETCDF4|NC_CLASSIC_MODEL);
00336    else if (format == NC_FORMAT_NETCDF4)
00337       cflags |= NC_NETCDF4;
00338 
00339    /* Create a file with two dims, two vars, and two atts. */
00340    if (nc_create(FILE_NAME, cflags|NC_CLOBBER, &ncid)) ERR;
00341 
00342    /* This will fail. */
00343    if ((ret = nc_def_dim(ncid, REDEF_NAME_ILLEGAL, REDEF_DIM2_LEN, &dimids[1])) != NC_EBADNAME) ERR;
00344 
00345    if (nc_def_dim(ncid, REDEF_DIM1_NAME, REDEF_DIM1_LEN, &dimids[0])) ERR;
00346    if (nc_def_dim(ncid, REDEF_DIM2_NAME, REDEF_DIM2_LEN, &dimids[1])) ERR;
00347    if (nc_def_var(ncid, REDEF_VAR1_NAME, NC_INT, REDEF_NDIMS, dimids, &varid)) ERR;
00348    if (nc_def_var(ncid, REDEF_VAR2_NAME, NC_BYTE, REDEF_NDIMS, dimids, &varid)) ERR;
00349    if (nc_put_att_double(ncid, NC_GLOBAL, REDEF_ATT1_NAME, NC_DOUBLE, 1, &double_out)) ERR;
00350    if (nc_put_att_short(ncid, NC_GLOBAL, REDEF_ATT2_NAME, NC_SHORT, 1, &short_out)) ERR;
00351 
00352    /* Check it out. */
00353    if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR;
00354    if (ndims != REDEF_NDIMS || nvars != 2 || natts != 2 || unlimdimid != -1) ERR;
00355    if (nc_inq_var(ncid, 0, var_name, &xtype_in, &ndims, dimids_in, &natts)) ERR;
00356    if (strcmp(var_name, REDEF_VAR1_NAME) || xtype_in != NC_INT || ndims != REDEF_NDIMS || 
00357        dimids_in[0] != dimids[0] || dimids_in[1] != dimids[1]) ERR;
00358    if (nc_inq_var(ncid, 1, var_name, &xtype_in, &ndims, dimids_in, &natts)) ERR;
00359    if (strcmp(var_name, REDEF_VAR2_NAME) || xtype_in != NC_BYTE || ndims != REDEF_NDIMS || 
00360        dimids_in[0] != dimids[0] || dimids_in[1] != dimids[1]) ERR;
00361 
00362    /* Close it up. */
00363    if (format != NC_FORMAT_NETCDF4)
00364       if (nc_enddef(ncid)) ERR;
00365    if (nc_close(ncid)) ERR;
00366 
00367    /* Reopen as read only - make sure it doesn't let us change file. */
00368    if (nc_open(FILE_NAME, 0, &ncid)) ERR;
00369    if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR;
00370    if (ndims != REDEF_NDIMS || nvars != 2 || natts != 2 || unlimdimid != -1) ERR;
00371    if (nc_inq_var(ncid, 0, var_name, &xtype_in, &ndims, dimids_in, &natts)) ERR;
00372    if (strcmp(var_name, REDEF_VAR1_NAME) || xtype_in != NC_INT || ndims != REDEF_NDIMS || 
00373        dimids_in[0] != dimids[0] || dimids_in[1] != dimids[1]) ERR;
00374    if (nc_inq_var(ncid, 1, var_name, &xtype_in, &ndims, dimids_in, &natts)) ERR;
00375    if (strcmp(var_name, REDEF_VAR2_NAME) || xtype_in != NC_BYTE || ndims != REDEF_NDIMS || 
00376        dimids_in[0] != dimids[0] || dimids_in[1] != dimids[1]) ERR;
00377 
00378    /* This will fail. */
00379    ret = nc_def_var(ncid, REDEF_VAR3_NAME, NC_UBYTE, REDEF_NDIMS, 
00380                           dimids, &varid);
00381    if ((format != NC_FORMAT_NETCDF4 && ret != NC_ENOTINDEFINE) ||
00382        (format == NC_FORMAT_NETCDF4 && ret != NC_EPERM)) ERR;
00383 
00384    /* This will fail. */
00385    if (!nc_put_att_uchar(ncid, NC_GLOBAL, REDEF_ATT3_NAME, NC_CHAR, 1, &uchar_out)) ERR;
00386    if (nc_close(ncid)) ERR;      
00387 
00388    /* Make sure we can't redef a file opened for NOWRITE. */
00389    if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR;
00390    if (nc_redef(ncid) != NC_EPERM) ERR;
00391 
00392    /* Check it out again. */
00393    if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR;
00394    if (ndims != REDEF_NDIMS || nvars != 2 || natts != 2 || unlimdimid != -1) ERR;
00395    if (nc_inq_var(ncid, 0, var_name, &xtype_in, &ndims, dimids_in, &natts)) ERR;
00396    if (strcmp(var_name, REDEF_VAR1_NAME) || xtype_in != NC_INT || ndims != REDEF_NDIMS || 
00397        dimids_in[0] != dimids[0] || dimids_in[1] != dimids[1]) ERR;
00398    if (nc_inq_var(ncid, 1, var_name, &xtype_in, &ndims, dimids_in, &natts)) ERR;
00399    if (strcmp(var_name, REDEF_VAR2_NAME) || xtype_in != NC_BYTE || ndims != REDEF_NDIMS || 
00400        dimids_in[0] != dimids[0] || dimids_in[1] != dimids[1]) ERR;
00401 
00402    if (nc_close(ncid)) ERR;      
00403 
00404    /* Reopen the file and check it, add a variable and attribute. */
00405    if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR;
00406 
00407    /* Check it out. */
00408    if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR;
00409    if (ndims != REDEF_NDIMS || nvars != 2 || natts != 2 || unlimdimid != -1) ERR;
00410 
00411    /* Add var. */
00412    if ((format != NC_FORMAT_NETCDF4) && nc_redef(ncid)) ERR;
00413    if (nc_def_var(ncid, REDEF_VAR3_NAME, NC_BYTE, REDEF_NDIMS, dimids, &varid)) ERR;
00414 
00415    /* Add att. */
00416    ret = nc_put_att_uchar(ncid, NC_GLOBAL, REDEF_ATT3_NAME, NC_BYTE, 1, &uchar_out);
00417    if (format != NC_FORMAT_NETCDF4 && ret) ERR;
00418    else if (format == NC_FORMAT_NETCDF4 && ret != NC_ERANGE) ERR;
00419 
00420    /* Check it out. */
00421    if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR;
00422    if (ndims != REDEF_NDIMS || nvars != 3 || natts != 3 || unlimdimid != -1) ERR;
00423    if (nc_inq_var(ncid, 0, var_name, &xtype_in, &ndims, dimids_in, &natts)) ERR;
00424    if (strcmp(var_name, REDEF_VAR1_NAME) || xtype_in != NC_INT || ndims != REDEF_NDIMS || 
00425        dimids_in[0] != dimids[0] || dimids_in[1] != dimids[1]) ERR;
00426    if (nc_inq_var(ncid, 1, var_name, &xtype_in, &ndims, dimids_in, &natts)) ERR;
00427    if (strcmp(var_name, REDEF_VAR2_NAME) || xtype_in != NC_BYTE || ndims != REDEF_NDIMS || 
00428        dimids_in[0] != dimids[0] || dimids_in[1] != dimids[1]) ERR;
00429    if (nc_inq_var(ncid, 2, var_name, &var_type, &ndims, dimids_var, &natts)) ERR;
00430    if (ndims != REDEF_NDIMS || strcmp(var_name, REDEF_VAR3_NAME) || var_type != NC_BYTE ||
00431        natts != 0) ERR;
00432 
00433    if (nc_close(ncid)) ERR;      
00434 
00435    /* Reopen it and check each dim, var, and att. */
00436    if (nc_open(FILE_NAME, 0, &ncid)) ERR;
00437    if (nc_inq_dim(ncid, 0, dim_name, &dim_len)) ERR;
00438    if (dim_len != REDEF_DIM1_LEN || strcmp(dim_name, REDEF_DIM1_NAME)) ERR;
00439    if (nc_inq_dim(ncid, 1, dim_name, &dim_len)) ERR;
00440    if (dim_len != REDEF_DIM2_LEN || strcmp(dim_name, REDEF_DIM2_NAME)) ERR;
00441    if (nc_inq_var(ncid, 0, var_name, &var_type, &ndims, dimids_var, &natts)) ERR;
00442    if (ndims != REDEF_NDIMS || strcmp(var_name, REDEF_VAR1_NAME) || var_type != NC_INT ||
00443        natts != 0) ERR;
00444    if (nc_inq_var(ncid, 1, var_name, &var_type, &ndims, dimids_var, &natts)) ERR;
00445    if (ndims != REDEF_NDIMS || strcmp(var_name, REDEF_VAR2_NAME) || var_type != NC_BYTE ||
00446        natts != 0) ERR;
00447    if (nc_inq_var(ncid, 2, var_name, &var_type, &ndims, dimids_var, &natts)) ERR;
00448    if (ndims != REDEF_NDIMS || strcmp(var_name, REDEF_VAR3_NAME) || var_type != NC_BYTE ||
00449        natts != 0) ERR;
00450    if (nc_get_att_float(ncid, NC_GLOBAL, REDEF_ATT1_NAME, &float_in) != NC_ERANGE) ERR;
00451    if (nc_get_att_int(ncid, NC_GLOBAL, REDEF_ATT2_NAME, &int_in)) ERR;
00452    if (int_in != short_out) ERR;
00453    ret = nc_get_att_uchar(ncid, NC_GLOBAL, REDEF_ATT3_NAME, &uchar_in);
00454    if (format == NC_FORMAT_NETCDF4)
00455    {
00456       if (ret != NC_ERANGE) ERR;
00457    }
00458    else if (ret) ERR;
00459          
00460    if (uchar_in != uchar_out) ERR;
00461    if (nc_close(ncid)) ERR;      
00462 
00463 }


Variable Documentation

NC_FILE_INFO_T* nc_file
 

Definition at line 23 of file nc4internal.c.


Generated on Thu Mar 16 18:11:42 2006 for nco by  doxygen 1.4.4