NOTICE: This version of the NSF Unidata web site (archive.unidata.ucar.edu) is no longer being updated.
Current content can be found at unidata.ucar.edu.
To learn about what's going on, see About the Archive Site.
>To: <address@hidden> >From: "Laurent Bocahut" <address@hidden> >Subject: Changing date of nc file >Organization: UIUC >Keywords: 200307171637.h6HGbSLd013507 Hi Laurent, > I'm working with air pollutants emission data under the netCDF format. > The file I have was processed with SMOKE and is for June 1-2 1996 and I > would like to change this date to June 1-2 1995. > It's the first time I have to use the NCO utilities and I don't really > know where to start. I cannot interpolate since I only have one file. I > would just like to rename the field corresponding to the date in the > netCDF format. > > Can you give me a few hints on how to do that ? If the file is fairly small, you could use the ncdump and ncgen utilities to edit it. For example, assume the file is named "smoke.nc", then you would proceed as follows: 1. Convert the file to CDL (an ASCII form of netCDF) using ncdump. On a Unix system, this would look like: ncdump smoke.nc > smoke.cdl 2. Edit the resulting file in an ordinary text editor of your choice, changing the date as desired. Save the result in smoke.cdl. Make sure your editor doesn't add any extra line ending characters or change the smoke.cdl file in way other than changing the data you want to change. 3. Convert the file back to netCDF form using the ncgen utility. On a Unix system, this would look like: ncgen -b smoke.cdl -o newsmoke.nc storing the new netCDF file in "newsmoke.nc". If this looks OK, you could just rename it to match the old file. Although the above procedure is relatively easy, converting floating-point data to text and back again may sometimes change a few least-significant bits in the data values, so if that's a problem, you shouldn't try this. Instead, you'll need to write a small program that changes the date value, using the netCDF library interfaces. For this, you'll need to choose what language to use, since there are netCDF interfaces available for C, Java, C++, Fortran77, Fortran90, Python, Ruby, Perl, and other languages. If you are already familiar with one of these, I'd recommend trying that. For the C interface, assuming the file is named "smoke.nc" and the variable is named "date" for example, all you need to do is: #include <netcdf.h> static char date_string[] = "June 1-2 1995"; ... nc_open("smoke.nc", NC_WRITE, &ncid); /* open the file writable */ ... nc_inq_varid (ncid, "date", &date_id); /* get id of date variable */ ... /* write a new value for "date" */ start = 0; count = strlen(date_string) + 1; nc_put_vara_text(ncid, date_id, &start, &count, date_string); ... nc_close(ncid); /* close the file */ You'll have to do something a little more complicated if your date variable is multidimensional, or if the date is stored in a netCDF attribute rather than a variable. And I've left error checking out of the above for simplicity, but you should check the error return of each call. --Russ _____________________________________________________________________ Russ Rew UCAR Unidata Program address@hidden http://my.unidata.ucar.edu