This archive contains answers to questions sent to Unidata support through mid-2025. Note that the archive is no longer being updated. We provide the archive for reference; many of the answers presented here remain technically correct, even if somewhat outdated. For the most up-to-date information on the use of NSF Unidata software and data services, please consult the Software Documentation first.
>To: address@hidden >From: Boyin Huang <address@hidden> >Subject: NF_PUT_ATT >Organization: UCAR/Unidata >Keywords: 200112052104.fB5L4oN16859 Hi Boyin, > I just started to use Fortran to generate NetCDF data > file. I find I can not write the attribute into the > file, although I tried to follow the instructions in > the manual. Otherwise, everything seems fine: > coordinate variables, dimensions, and data are all > fine by using ncdump. The program stops at > NF_PUT_ATT_type. > I tried different types as shown below. > > Looking forward to seeing your comments. > > Boyin Huang at MIT > > ----------------------------------- > > Here are the fortran lines: > > c 1. create file > > > status = nf_create('TOM_bmrc_con.nc', NF_CLOBBER, > mcid) > if(status.ne.NF_NOERR) stop 'create' > > c 2. define dimension > > status = nf_def_dim(mcid, 'longitude', idim, id_lon) > if(status.ne.NF_NOERR) stop 'def_lon' > status = nf_def_dim(mcid, 'latitude', jdim, id_lat) > if(status.ne.NF_NOERR) stop 'def_lat' > status = nf_def_dim(mcid, 'depth', kdim, id_depth) > if(status.ne.NF_NOERR) stop 'def_dep' > status = nf_def_dim(mcid, 'time',4 ,id_time) > if(status.ne.NF_NOERR) stop 'def_time' > > c 3. define variable > > id_var_dim(1) = id_lon > id_var_dim(2) = id_lat > id_var_dim(3) = id_depth > id_var_dim(4) = id_time > > status > =nf_def_var(mcid,'longitude',NF_double,1,id_lon, > id_xx) > if(status.ne.NF_NOERR) stop 'def_xx' > status = > nf_def_var(mcid,'latitude',NF_double,1,id_lat, id_yy) > if(status.ne.NF_NOERR) stop 'def_yy' > status = > nf_def_var(mcid,'depth',NF_double,1,id_depth,id_zz) > if(status.ne.NF_NOERR) stop 'def_zz' > status = nf_def_var(mcid,'time',NF_double,1,id_time > ,id_tt) > if(status.ne.NF_NOERR) stop 'def_tt' > status = nf_def_var(mcid, 'to_c', NF_REAL, 4, > id_var_dim, id_to) > if(status.ne.NF_NOERR) stop 'def_var' > > c 4. put attibute > > print*,mcid,id_zz > > status = > NF_PUT_ATT_TEXT(mcid,NF_GLOBAL,'CMIPdata',4,'BMRC') > if(status.ne.NF_NOERR) stop 'att_go' > > status = nf_put_att_Real(mcid,id_to,'missing value' > 1 ,NF_real,1,1.e20) > if(status.ne.NF_NOERR) stop 'att_to' This will return an error status, because blanks are not permitted in the names of netCDF attributes, dimensions, or variables, as specified in section 2.1.1 Naming Conventions: The names of dimensions, variables and attributes consist of arbitrary sequences of alphanumeric characters (as well as underscore '_' and hyphen '-'), beginning with a letter or underscore. (However names commencing with underscore are reserved for system use.) Case is significant in netCDF names. Since the manual was written, the character "." is also now permitted in a name, but blank is not. So try naming your attribute "missing_value" instead of "missing value". Another tip: use the "nf_strerror()" function to print error messages for non-zero status returns, as in: status = nf_put_att_Real(mcid,id_to,'missing value' 1 ,NF_real,1,1.e20) if(status.ne.NF_NOERR) then print *, nf_strerror(status) stop 'att_to' endif This would print out Attribute or variable name contains illegal characters STOP: att_to You could also encapsulate these error checks in a "handle_err()" function, as in the example in section 5.2 describing nf_strerror(). --Russ _____________________________________________________________________ Russ Rew UCAR Unidata Program address@hidden http://www.unidata.ucar.edu