Add variable in a NetCDF file. Variable POSITION

Good morning,

I am using the NetCDF library and the C interface to write and modify 
variables in an exsisting NetCDF file; specifically, I am addidng a single 
date value as relative (or absolute according to the use) to some files that 
were generated without a date (0000-00-00).

I need them to have a date specified in order to make operations on them using 
CDO or NCL. 

I can perfectly write the missign date and have a new file with the date as I 
want it, but the position of the value is put is at the very end of the file 
by default.

Is there a way to control the position inside the file where the variable 
value is added?
I need to put it in a certain position because when I use CDO to merge several 
files, the output merged file does not have a date anymore.

I hope I explained the problem well, if not, please contact me

Kind regards

Simone.

Here the code in C that opens and modify an exsisting *.nc file

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
C Code that uses the NetCDF library for entering and modifying the files:
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<netcdf.h>

main(int argc, char *argv[])
{

        const char *fileANDpath[128];
        
        int status;                                                             
//Error status
        int ncid;                                                               
        //NetCDF ID
        int time_dim;                                                   
//Dimension ID
        int time_id;                                                            
//Variable ID
        int time_dimids[0];                                             
//Variable shape
        int ndims, nvars, ngatts, unlimdimid;           //Existing variables 
characteristics
        
        static char time_att1[] = "day as %Y%m%d.%f"; 
//  static char time_att1[] = "days since ";

        char *s2;                                       
        strcat(time_att1,s2);
        
        static size_t time_index[] = {0,0};             //Value starting from 
the reference. 
        static double time_val;                                 //Variable "ex. 
19900101"

//If the number fo raguments is at least 1, then the program will execute from 
here:

/****************/
/***User inputs**/
/****************/

        static char time_att2[] = "gregorian";
        static char time_att3[] = "time";
        
/****************/
/*END Inputs*****/
/****************/
        
/***********************************************************/
/*Preliminary computations to prepare some strings**********/
/***********************************************************/
        
//Take the terminal input of the path and file name
                *fileANDpath = argv[2];
                printf("\n - PATH = %s\n", *fileANDpath);
                
        //Take the second input from the executable: argv[2]
                s2 = argv[2];
                strcat(time_att1, s2);
                
        //Open dataset to access it in read-only (putting 0 instead of 
NC_WRITE) or 
read-write mode:
                status = nc_open(*fileANDpath, NC_WRITE, &ncid);
                if(status != NC_NOERR) printf("Error in Opening file\n");
                else printf("\nOK - File open with ID: %d\n", ncid);
        
                status = nc_inq(ncid, &ndims,  &nvars, &ngatts, &unlimdimid);
                printf("\n File: %s\n\n - ID = %d\n - ndims = %d\n - nvars = 
%d\n - ngatts = 
%d\n - unlimdimid = %d\n\n", *fileANDpath, ncid, ndims, nvars, ngatts, 
unlimdimid);
        
        //Put the dataset in define mode to add dimensions, variables or 
attributes:
                status = nc_redef(ncid);
                if(status != NC_NOERR) printf("Error in Opening file\n");
                else printf("OK - File in define mode now\n");
        
        
        //Define dimensions, variables, attributes
        
        //A) Define dimensions for the "time" variable:
                status = nc_def_dim(ncid, "time", NC_UNLIMITED, &time_dim);
        
        //B) Define variable:
                time_dimids[0] = time_dim;
                status = nc_def_var(ncid, "time", NC_DOUBLE, 1, time_dimids, 
&time_id);
                                
        //C) Define Attributes:
status = nc_put_att_text(ncid, time_id, "units", strlen(time_att1),time_att1);
status = nc_put_att_text(ncid, time_id, "calendar", strlen(time_att2), 
time_att2);
status = nc_put_att_text(ncid, time_id, "long_name", strlen(time_att3) , 
time_att3);

        //D)Leave define mode, checking consistency of additions:
                status = nc_enddef(ncid);
                if(status != NC_NOERR) printf("Error in creating the 
variable\n");
                else printf("OK - Variable created\n");
                        
        //E) Write a single data value: nc_put_var1_type: argv[1]
                time_val = atof(argv[1]);
                status = nc_put_var1_double(ncid, time_id, time_index, 
&time_val); 
                if(status != NC_NOERR) printf("Error in nc_put_var1_type the 
variable\n");
                else{
                        printf("OK - Value Added to the file\n");
                        printf("time_value = %.0f\n",time_val);
                }
                        
        //Close the file and write it:
        status = nc_close(ncid);
                
//Call the bash and view the file created:
//      
system("ncdump 
/home/smarras/Documents/climateData/GISSdata/PROVA_ncmodifyFiles/decade_1980.ij.nc
 
> ncdump_output");
//      system("pico ncdump_output");

return 0;
}




-- 
Simone Marras, Ph.D. Student
BSC & Universitat Politecnica de Catalunya
Jordi Jirona 29
08034 Barcelona
Spain


-- 
Simone Marras, Ph.D. Student
BSC & Universitat Politecnica de Catalunya
Jordi Jirona 29
08034 Barcelona
Spain

==============================================================================
To unsubscribe netcdfgroup, visit:
http://www.unidata.ucar.edu/mailing-list-delete-form.html
==============================================================================


  • 2007 messages navigation, sorted by:
    1. Thread
    2. Subject
    3. Author
    4. Date
    5. ↑ Table Of Contents
  • Search the netcdfgroup archives: