io.h File Reference

Include file for input/output library. More...

#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <signal.h>
#include <time.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <sys/mman.h>
#include <libgen.h>
#include <zlib.h>
#include <hdf5.h>
#include <netcdf.h>
#include <udunits.h>
#include <misc.h>
#include <utils.h>
Include dependency graph for io.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  info_struct
 Data structure for NetCDF metadata info_struct. More...
struct  info_field_struct
 Data structure for field information info_field_struct. More...
struct  proj_struct
 Data structure for mapping projection proj_struct. More...
struct  time_vect_struct
 Easy time structure of vectors. More...

Defines

#define _GNU_SOURCE
 GNU extensions.
#define TRUE   1
 TRUE value macro is 1.
#define FALSE   0
 FALSE value macro is 0.
#define MAXPATH   5000
 Maximum length of paths/filenames strings.

Functions

int read_netcdf_dims_3d (double **lon, double **lat, double **timeval, char **cal_type, char **time_units, int *nlon, int *nlat, int *ntime, info_struct *info, char *coords, char *gridname, char *lonname, char *latname, char *dimxname, char *dimyname, char *timename, char *filename)
 Read dimensions in a NetCDF file for 3D variables.
int read_netcdf_latlon (double **lon, double **lat, int *nlon, int *nlat, char *dimcoords, char *coords, char *gridname, char *lonname, char *latname, char *dimxname, char *dimyname, char *filename)
 Read latitude and longitude dimensions in a NetCDF file.
int read_netcdf_xy (double **y, double **x, int *nx, int *ny, char *xname, char *yname, char *dimxname, char *dimyname, char *filename)
 Read X and Y dimensions in a NetCDF file.
int read_netcdf_dims_eof (double **lon, double **lat, int *nlon, int *nlat, int *neof, char *coords, char *lonname, char *latname, char *dimxname, char *dimyname, char *eofname, char *filename)
 Read dimensions in a NetCDF file for EOF data.
int read_netcdf_var_3d (double **buf, info_field_struct *info_field, proj_struct *proj, char *filename, char *varname, char *dimxname, char *dimyname, char *timename, int *nlon, int *nlat, int *ntime, int outinfo)
 Read a 3D variable in a NetCDF file, and return information in info_field_struct structure and proj_struct.
int read_netcdf_var_3d_2d (double **buf, info_field_struct *info_field, proj_struct *proj, char *filename, char *varname, char *dimxname, char *dimyname, char *timename, int t, int *nlon, int *nlat, int *ntime, int outinfo)
 Read a 2D field from a 3D variable in a NetCDF file, and return information in info_field_struct structure and proj_struct.
int read_netcdf_var_2d (double **buf, info_field_struct *info_field, proj_struct *proj, char *filename, char *varname, char *dimxname, char *dimyname, int *nlon, int *nlat, int outinfo)
 Read a 2D variable in a NetCDF file, and return information in info_field_struct structure and proj_struct.
int read_netcdf_var_1d (double **buf, info_field_struct *info_field, char *filename, char *varname, char *dimname, int *ndim, int outinfo)
 Read a 1D variable in a NetCDF file, and return information in info_field_struct structure.
int read_netcdf_var_generic_val (double *buf, info_field_struct *info_field, char *filename, char *varname, int index)
 Read a NetCDF variable scalar double at the index position, and return information in info_field_struct structure.
int write_netcdf_var_3d (double *buf, double fillvalue, char *filename, char *varname, char *gridname, char *lonname, char *latname, char *timename, int format, int compression_level, int nlon, int nlat, int ntime, int outinfo)
 Write a 3D field in a NetCDF output file.
int write_netcdf_var_3d_2d (double *buf, double *timein, double fillvalue, char *filename, char *varname, char *longname, char *units, char *height, char *gridname, char *lonname, char *latname, char *timename, int t, int newfile, int format, int compression_level, int nlon, int nlat, int ntime, int outinfo)
 Write a 2D field in a 3D NetCDF variable.
int write_netcdf_dims_3d (double *lon, double *lat, double *x, double *y, double *alt, double *timein, char *cal_type, char *time_units, int nlon, int nlat, int ntime, char *timestep, char *gridname, char *coords, char *grid_mapping_name, double latin1, double latin2, double lonc, double lat0, double false_easting, double false_northing, double lonpole, double latpole, char *lonname, char *latname, char *timename, char *filename, int outinfo)
 Write NetCDF dimensions and create output file.
int create_netcdf (char *title, char *title_french, char *summary, char *summary_french, char *keywords, char *processor, char *software, char *description, char *institution, char *creator_email, char *creator_url, char *creator_name, char *version, char *scenario, char *scenario_co2, char *model, char *institution_model, char *country, char *member, char *downscaling_forcing, char *contact_email, char *contact_name, char *other_contact_email, char *other_contact_name, char *filename, int outinfo, int format, int compression)
 Create a new NetCDF file with global CF-1.0 attributes.
int get_time_attributes (char **time_units, char **cal_type, char *filename, char *varname)
 Get main time attributes in a NetCDF file.
int get_attribute_str (char **var, int ncinid, int varid, char *attrname)
 Get NetCDF string attribute.
int get_time_info (time_vect_struct *time_s, double **timeval, char **time_units, char **cal_type, int *ntime, char *filename, char *varname, int outinfo)
 Get time information in a NetCDF file.
int compute_time_info (time_vect_struct *time_s, double *timeval, char *time_units, char *cal_type, int ntime)
 Compute time info from NetCDF time.
void handle_netcdf_error (int status, char *srcfilename, int lineno)
 Handle NetCDF error.

Detailed Description

Include file for input/output library.

Definition in file io.h.


Define Documentation

#define _GNU_SOURCE

GNU extensions.

Definition at line 55 of file io.h.

#define FALSE   0
#define MAXPATH   5000

Maximum length of paths/filenames strings.

Definition at line 97 of file io.h.

#define TRUE   1

Function Documentation

int compute_time_info ( time_vect_struct time_s,
double *  timeval,
char *  time_units,
char *  cal_type,
int  ntime 
)

Compute time info from NetCDF time.

Parameters:
[out] time_s Time field in time structure
[in] timeval Time field
[in] time_units Time units (udunits)
[in] cal_type Calendar type (udunits)
[in] ntime Time dimension
Returns:
Status.

Definition at line 67 of file compute_time_info.c.

References alloc_error(), time_vect_struct::day, time_vect_struct::hour, time_vect_struct::minutes, time_vect_struct::month, time_vect_struct::seconds, and time_vect_struct::year.

Referenced by read_field_subdomain_period(), and read_large_scale_fields().

00067                                                                                                           {
00068 
00079   int istat; /* Diagnostic status */
00080   ut_system *unitSystem = NULL; /* Unit System (udunits) */
00081   ut_unit *dataunits = NULL; /* Data units (udunits) */
00082   int t; /* Time loop counter */
00083 
00084   /* Check values of time variable because many times they are all zero. In that case assume a 1 increment and a start at zero. */
00085   for (t=0; t<ntime; t++)
00086     if (timeval[t] != 0.0)
00087       break;
00088   if (t == ntime) {
00089     (void) fprintf(stderr, "WARNING: Time variable values all zero!!! Fixing time variable to index value...\n");
00090     for (t=0; t<ntime; t++)
00091       timeval[t] = (double) t;
00092   }
00093 
00094   /* Compute time info */
00095   time_s->year = (int *) malloc(ntime * sizeof(int));
00096   if (time_s->year == NULL) alloc_error(__FILE__, __LINE__);
00097   time_s->month = (int *) malloc(ntime * sizeof(int));
00098   if (time_s->month == NULL) alloc_error(__FILE__, __LINE__);
00099   time_s->day = (int *) malloc(ntime * sizeof(int));
00100   if (time_s->day == NULL) alloc_error(__FILE__, __LINE__);
00101   time_s->hour = (int *) malloc(ntime * sizeof(int));
00102   if (time_s->hour == NULL) alloc_error(__FILE__, __LINE__);
00103   time_s->minutes = (int *) malloc(ntime * sizeof(int));
00104   if (time_s->minutes == NULL) alloc_error(__FILE__, __LINE__);
00105   time_s->seconds = (double *) malloc(ntime * sizeof(double));
00106   if (time_s->seconds == NULL) alloc_error(__FILE__, __LINE__);
00107 
00108   /* Initialize udunits */
00109   ut_set_error_message_handler(ut_ignore);
00110   unitSystem = ut_read_xml(NULL);
00111   ut_set_error_message_handler(ut_write_to_stderr);
00112 
00113   dataunits = ut_parse(unitSystem, time_units, UT_ASCII);
00114   for (t=0; t<ntime; t++) {
00115     istat = utCalendar2_cal(timeval[t], dataunits, &(time_s->year[t]), &(time_s->month[t]), &(time_s->day[t]),
00116                             &(time_s->hour[t]), &(time_s->minutes[t]), &(time_s->seconds[t]), cal_type);
00117     if (istat < 0) {
00118       (void) ut_free(dataunits);
00119       (void) ut_free_system(unitSystem);  
00120       return -1;
00121     }
00122   }
00123 
00124   (void) ut_free(dataunits);
00125   (void) ut_free_system(unitSystem);  
00126 
00127   /* Success status */
00128   return 0;
00129 }

int create_netcdf ( char *  title,
char *  title_french,
char *  summary,
char *  summary_french,
char *  keywords,
char *  processor,
char *  software,
char *  description,
char *  institution,
char *  creator_email,
char *  creator_url,
char *  creator_name,
char *  version,
char *  scenario,
char *  scenario_co2,
char *  model,
char *  institution_model,
char *  country,
char *  member,
char *  downscaling_forcing,
char *  contact_email,
char *  contact_name,
char *  other_contact_email,
char *  other_contact_name,
char *  filename,
int  outinfo,
int  format,
int  compression 
)

Create a new NetCDF file with global CF-1.0 attributes.

Parameters:
[in] title Title (english)
[in] title_french Title (french)
[in] summary Summary (english)
[in] summary_french Summary *french)
[in] keywords Keyword
[in] processor Program, processor which have generated the data
[in] software Software and version which have generated the data
[in] description Main description of the data
[in] institution Institution which generated the data
[in] creator_email Contact email of the creator of the data
[in] creator_url Website creator of the data
[in] creator_name Name of the creator of the data
[in] version Version of the data
[in] scenario Climate scenario
[in] scenario_co2 CO2 scenario
[in] model Numerical model used
[in] institution_model Institution who developed the numerical model used
[in] country Country of the institution who developed the numerical model used
[in] member Member in the case of multi-member model configurations
[in] downscaling_forcing Observations database used when downscaling
[in] contact_email Contact email
[in] contact_name Contact name
[in] other_contact_email Other contact email
[in] other_contact_name Other contact name
[in] filename NetCDF output filename
[in] outinfo TRUE if we want information output, FALSE if not
[in] format File format version for NetCDF
[in] compression Compression flag for NetCDF-4 file format
Returns:
Status.

Definition at line 67 of file create_netcdf.c.

References alloc_error(), handle_netcdf_error(), MAXPATH, and TRUE.

Referenced by output_downscaled_analog(), and remove_clim().

00073                                                                         {
00107   int istat = 0; /* Diagnostic status */
00108   int ncoutid; /* NetCDF output file handle ID */
00109   char *tmpstr = NULL; /* Temporary string */
00110 
00111   if (outinfo == TRUE)
00112     (void) fprintf(stdout, "%s: Creating NetCDF file %s\n", __FILE__, filename);
00113 
00114   /* Allocate memory */
00115   tmpstr = (char *) malloc(MAXPATH * sizeof(char));
00116   if (tmpstr == NULL) alloc_error(__FILE__, __LINE__);
00117 
00118   /* Open NetCDF file for writing, overwrite and truncate existing file if any */
00119   if (format == 4 && compression == TRUE)
00120 #ifdef NC_NETCDF4
00121     istat = nc_create(filename, NC_CLOBBER | NC_NETCDF4 | NC_CLASSIC_MODEL, &ncoutid);
00122 #else
00123     istat = nc_create(filename, NC_CLOBBER, &ncoutid);
00124 #endif
00125   else
00126 #ifdef NC_NETCDF4
00127     if (format == 4)
00128       istat = nc_create(filename, NC_CLOBBER | NC_NETCDF4 | NC_CLASSIC_MODEL, &ncoutid);
00129     else
00130       istat = nc_create(filename, NC_CLOBBER, &ncoutid);
00131 #else
00132     istat = nc_create(filename, NC_CLOBBER, &ncoutid);
00133 #endif
00134     //    istat = nc_create(filename, NC_CLOBBER, &ncoutid);
00135   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00136 
00137   /* Set global attributes */
00138   (void) strcpy(tmpstr, "CF-1.0");
00139   istat = nc_put_att_text(ncoutid, NC_GLOBAL, "Conventions", strlen(tmpstr), tmpstr);
00140   (void) strcpy(tmpstr, "Unidata Dataset Discovery v1.0");
00141   istat = nc_put_att_text(ncoutid, NC_GLOBAL, "Metadata_Conventions", strlen(tmpstr), tmpstr);
00142   istat = nc_put_att_text(ncoutid, NC_GLOBAL, "title", strlen(title), title);
00143   istat = nc_put_att_text(ncoutid, NC_GLOBAL, "title_french", strlen(title_french), title_french);
00144   istat = nc_put_att_text(ncoutid, NC_GLOBAL, "summary", strlen(summary), summary);
00145   istat = nc_put_att_text(ncoutid, NC_GLOBAL, "summary_french", strlen(summary_french), summary_french);
00146   istat = nc_put_att_text(ncoutid, NC_GLOBAL, "keywords", strlen(keywords), keywords);
00147   istat = nc_put_att_text(ncoutid, NC_GLOBAL, "processor", strlen(processor), processor);
00148   istat = nc_put_att_text(ncoutid, NC_GLOBAL, "software", strlen(software), software);
00149   istat = nc_put_att_text(ncoutid, NC_GLOBAL, "description", strlen(description), description);
00150   istat = nc_put_att_text(ncoutid, NC_GLOBAL, "institution", strlen(institution), institution);
00151   istat = nc_put_att_text(ncoutid, NC_GLOBAL, "creator_email", strlen(creator_email), creator_email);
00152   istat = nc_put_att_text(ncoutid, NC_GLOBAL, "creator_url", strlen(creator_url), creator_url);
00153   istat = nc_put_att_text(ncoutid, NC_GLOBAL, "creator_name", strlen(creator_name), creator_name);
00154   istat = nc_put_att_text(ncoutid, NC_GLOBAL, "version", strlen(version), version);
00155   istat = nc_put_att_text(ncoutid, NC_GLOBAL, "scenario", strlen(scenario), scenario);
00156   istat = nc_put_att_text(ncoutid, NC_GLOBAL, "scenario_co2", strlen(scenario_co2), scenario_co2);
00157   istat = nc_put_att_text(ncoutid, NC_GLOBAL, "model", strlen(model), model);
00158   istat = nc_put_att_text(ncoutid, NC_GLOBAL, "institution_model", strlen(institution_model), institution_model);
00159   istat = nc_put_att_text(ncoutid, NC_GLOBAL, "country", strlen(country), country);
00160   istat = nc_put_att_text(ncoutid, NC_GLOBAL, "member", strlen(member), member);
00161   istat = nc_put_att_text(ncoutid, NC_GLOBAL, "downscaling_forcing", strlen(downscaling_forcing), downscaling_forcing);
00162   istat = nc_put_att_text(ncoutid, NC_GLOBAL, "contact_email", strlen(contact_email), contact_email);
00163   istat = nc_put_att_text(ncoutid, NC_GLOBAL, "contact_name", strlen(contact_name), contact_name);
00164   istat = nc_put_att_text(ncoutid, NC_GLOBAL, "other_contact_email", strlen(other_contact_email), other_contact_email);
00165   istat = nc_put_att_text(ncoutid, NC_GLOBAL, "other_contact_name", strlen(other_contact_name), other_contact_name);
00166 
00167   /* End definition mode */
00168   istat = nc_enddef(ncoutid);
00169   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00170 
00171   /* Close the output netCDF file */
00172   istat = ncclose(ncoutid);
00173   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00174 
00175   /* Free memory */
00176   (void) free(tmpstr);
00177 
00178   /* Success status */
00179   return 0;
00180 }

int get_attribute_str ( char **  var,
int  ncinid,
int  varid,
char *  attrname 
)

Get NetCDF string attribute.

Parameters:
[out] var String attribute value
[in] ncinid NetCDF input filename ID
[in] varid NetCDF variable ID
[in] attrname NetCDF attribute name

Definition at line 66 of file get_attribute_str.c.

References alloc_error().

Referenced by read_netcdf_dims_3d(), read_netcdf_var_2d(), read_netcdf_var_3d(), and read_netcdf_var_3d_2d().

00067 {
00075   int istat; /* Diagnostic status */
00076   size_t t_len; /* Length of attribute value string */
00077 
00078   /* Get attribute length */
00079   istat = nc_inq_attlen(ncinid, varid, attrname, &t_len);
00080   if (istat == NC_NOERR) {
00081     /* Allocate required space before retrieving values */
00082     (*var) = (char *) malloc(t_len + 1);
00083     if ((*var) == NULL) alloc_error(__FILE__, __LINE__);
00084     /* Get attribute value */
00085     istat = nc_get_att_text(ncinid, varid, attrname, (*var));
00086     if (istat != NC_NOERR)
00087       (*var)[0] = '\0';
00088     else
00089       if ((*var)[t_len-1] != '\0')
00090         (*var)[t_len] = '\0'; /* null terminate if needed */
00091   }
00092   else {
00093     /* Allocate required space */
00094     (*var) = (char *) malloc(sizeof(char));
00095     if ((*var) == NULL) alloc_error(__FILE__, __LINE__);    
00096     (*var)[0] = '\0';
00097   }
00098 
00099   /* Diagnostic status */
00100   return ((int) istat);
00101 }

int get_time_attributes ( char **  time_units,
char **  cal_type,
char *  filename,
char *  varname 
)

Get main time attributes in a NetCDF file.

Parameters:
[out] time_units Time units (udunits)
[out] cal_type Calendar type (udunits)
[in] filename NetCDF input filename
[in] varname NetCDF time variable name
Returns:
Status.

Definition at line 66 of file get_time_attributes.c.

References alloc_error(), and handle_netcdf_error().

00066                                                                                        {
00067 
00077   int istat; /* Diagnostic status */
00078 
00079   int ncinid; /* NetCDF input file handle ID */
00080   int timeinid; /* NetCDF time variable ID */
00081 
00082   size_t t_len; /* Length of time units string */
00083 
00084   /* Read data in NetCDF file */
00085 
00086   /* Open NetCDF file for reading */
00087   printf("%s: Opening for reading time attributes in NetCDF input file %s\n", __FILE__, filename);
00088   istat = nc_open(filename, NC_NOWRITE, &ncinid);  /* open for reading */
00089   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00090 
00091   /* Get ID for time variable */
00092   istat = nc_inq_varid(ncinid, varname, &timeinid);
00093   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00094 
00095   /* Get time units attribute length */
00096   istat = nc_inq_attlen(ncinid, timeinid, "units", &t_len);
00097   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00098 
00099   /* Allocate required space before retrieving values */
00100   (*time_units) = (char *) malloc((t_len+1) * sizeof(char));
00101   if ((*time_units) == NULL) alloc_error(__FILE__, __LINE__);
00102 
00103   /* Get time units attribute value */
00104   istat = nc_get_att_text(ncinid, timeinid, "units", *time_units);
00105   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00106   if ((*time_units)[t_len-2] == 'Z')
00107     (*time_units)[t_len-2] = '\0'; /* null terminate */
00108   else if ((*time_units)[t_len-1] == 'Z')
00109     (*time_units)[t_len-1] = '\0'; /* null terminate */
00110   else
00111     (*time_units)[t_len] = '\0';
00112 
00113   /* Get calendar type attribute length */
00114   istat = nc_inq_attlen(ncinid, timeinid, "calendar", &t_len);
00115   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00116 
00117   /* Allocate required space before retrieving values */
00118   (*cal_type) = (char *) malloc(t_len + 1);
00119   if ((*cal_type) == NULL) alloc_error(__FILE__, __LINE__);
00120 
00121   /* Get calendar type attribute value */
00122   istat = nc_get_att_text(ncinid, timeinid, "calendar", *cal_type);
00123   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00124   (*cal_type)[t_len] = '\0'; /* null terminate */
00125 
00126   /* Close the intput netCDF file. */
00127   istat = ncclose(ncinid);
00128   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00129 
00130   /* Success status */
00131   return 0;
00132 }

int get_time_info ( time_vect_struct time_s,
double **  timeval,
char **  time_units,
char **  cal_type,
int *  ntime,
char *  filename,
char *  varname,
int  outinfo 
)

Get time information in a NetCDF file.

Parameters:
[out] time_s Time information in a time structure
[out] timeval Time field
[out] time_units Time units (udunits)
[out] cal_type Calendar-type (udunits)
[out] ntime Time dimension
[in] filename NetCDF input filename
[in] varname Variable name
[in] outinfo TRUE if we want information output, FALSE if not
Returns:
Status.

Close NetCDF file

Definition at line 67 of file get_time_info.c.

References alloc_error(), time_vect_struct::day, handle_netcdf_error(), time_vect_struct::hour, time_vect_struct::minutes, time_vect_struct::month, time_vect_struct::seconds, TRUE, and time_vect_struct::year.

Referenced by output_downscaled_analog(), read_learning_fields(), read_learning_obs_eof(), read_learning_rea_eof(), and read_obs_period().

00068                                           {
00082   int istat; /* Diagnostic status */
00083 
00084   size_t dimval; /* Variable used to retrieve dimension length */
00085 
00086   int ncinid; /* NetCDF input file handle ID */
00087   int timediminid; /* Time dimension ID */
00088   int timeinid; /* Time variable ID */
00089   nc_type vartype_time; /* Type of the time variable (NC_FLOAT, NC_DOUBLE, etc.) */
00090 
00091   int varndims; /* Number of dimensions of variable */
00092   int vardimids[NC_MAX_VAR_DIMS]; /* Variable dimension ids */
00093 
00094   size_t start[3]; /* Start position to read */
00095   size_t count[3]; /* Number of elements to read */
00096 
00097   size_t t_len; /* Length of time units attribute string */
00098   ut_system *unitSystem = NULL; /* Unit System (udunits) */
00099   ut_unit *dataunits = NULL; /* Data units (udunits) */
00100 
00101   int t; /* Time loop counter */
00102 
00103   /* Read data in NetCDF file */
00104 
00105   /* Open NetCDF file for reading */
00106   if (outinfo == TRUE)
00107     printf("%s: Opening for reading time information in NetCDF input file %s\n", __FILE__, filename);
00108   istat = nc_open(filename, NC_NOWRITE, &ncinid);  /* open for reading */
00109   if (istat != NC_NOERR) {
00110     (void) fprintf(stderr, "%s: filename = %s\n", __FILE__, filename);
00111     handle_netcdf_error(istat, __FILE__, __LINE__);
00112   }
00113 
00114   /* Get dimensions length and ID */
00115   istat = nc_inq_dimid(ncinid, varname, &timediminid);  /* get ID for time dimension */
00116   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00117   istat = nc_inq_dimlen(ncinid, timediminid, &dimval); /* get time length */
00118   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00119   *ntime = (int) dimval;
00120 
00121   istat = nc_inq_varid(ncinid, varname, &timeinid);  /* get ID for time variable */
00122   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00123 
00124   /* Get time dimensions and type */
00125   istat = nc_inq_var(ncinid, timeinid, (char *) NULL, &vartype_time, &varndims, vardimids, (int *) NULL); /* get variable information */
00126   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00127   
00128   if (varndims != 1) {
00129     (void) fprintf(stderr, "Error NetCDF type and/or dimensions Line %d.\n", __LINE__);
00130     return -1;
00131   }
00132 
00133   /* Allocate memory and set start and count */
00134   start[0] = 0;
00135   count[0] = (size_t) (*ntime);
00136   (*timeval) = malloc((*ntime) * sizeof(double));
00137   if ((*timeval) == NULL) alloc_error(__FILE__, __LINE__);
00138 
00139   /* Read values from netCDF variable */
00140   istat = nc_get_vara_double(ncinid, timeinid, start, count, *timeval);
00141   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00142 
00143   /* Check values of time variable because many times they are all zero. In that case assume a 1 increment and a start at zero. */
00144   for (t=0; t<(*ntime); t++)
00145     if ((*timeval)[t] != 0.0)
00146       break;
00147   if (t == (*ntime)) {
00148     fprintf(stderr, "WARNING: Time variable values all zero!!! Fixing time variable to index value...\n");
00149     for (t=0; t<(*ntime); t++)
00150       (*timeval)[t] = (double) t;
00151   }
00152 
00153   /* Get time units attribute length */
00154   istat = nc_inq_attlen(ncinid, timeinid, "units", &t_len);
00155   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00156   /* Allocate required space before retrieving values */
00157   (*time_units) = (char *) malloc((t_len+1) * sizeof(char));
00158   if ((*time_units) == NULL) alloc_error(__FILE__, __LINE__);
00159   /* Get time units attribute value */
00160   istat = nc_get_att_text(ncinid, timeinid, "units", *time_units);
00161   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00162   /* Correct if time units ends incorrectly with Z */
00163   if ((*time_units)[t_len-2] == 'Z')
00164     (*time_units)[t_len-2] = '\0'; /* null terminate */
00165   else if ((*time_units)[t_len-1] == 'Z')
00166     (*time_units)[t_len-1] = '\0'; /* null terminate */
00167   else
00168     (*time_units)[t_len] = '\0';
00169 
00170   /* Get calendar type attribute length */
00171   istat = nc_inq_attlen(ncinid, timeinid, "calendar", &t_len);
00172   if (istat != NC_NOERR) {
00173     /* If not present, assume standard calendar */
00174     *cal_type = strdup("gregorian");
00175   }
00176   else {
00177     /* Allocate required space before retrieving values */
00178     (*cal_type) = (char *) malloc(t_len + 1);
00179     if ((*cal_type) == NULL) alloc_error(__FILE__, __LINE__);
00180     /* Get calendar type attribute value */
00181     istat = nc_get_att_text(ncinid, timeinid, "calendar", *cal_type);
00182     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00183     (*cal_type)[t_len] = '\0'; /* null terminate */
00184   }
00185 
00186   /* Compute time info and store into easy time structure */
00187   time_s->year = (int *) malloc((*ntime) * sizeof(int));
00188   if (time_s->year == NULL) alloc_error(__FILE__, __LINE__);
00189   time_s->month = (int *) malloc((*ntime) * sizeof(int));
00190   if (time_s->month == NULL) alloc_error(__FILE__, __LINE__);
00191   time_s->day = (int *) malloc((*ntime) * sizeof(int));
00192   if (time_s->day == NULL) alloc_error(__FILE__, __LINE__);
00193   time_s->hour = (int *) malloc((*ntime) * sizeof(int));
00194   if (time_s->hour == NULL) alloc_error(__FILE__, __LINE__);
00195   time_s->minutes = (int *) malloc((*ntime) * sizeof(int));
00196   if (time_s->minutes == NULL) alloc_error(__FILE__, __LINE__);
00197   time_s->seconds = (double *) malloc((*ntime) * sizeof(double));
00198   if (time_s->seconds == NULL) alloc_error(__FILE__, __LINE__);
00199 
00200   /* Initialize udunits */
00201   ut_set_error_message_handler(ut_ignore);
00202   unitSystem = ut_read_xml(NULL);
00203   ut_set_error_message_handler(ut_write_to_stderr);
00204 
00205   dataunits = ut_parse(unitSystem, (*time_units), UT_ASCII);
00206   for (t=0; t<(*ntime); t++) {
00207     istat = utCalendar2_cal((*timeval)[t], dataunits, &(time_s->year[t]), &(time_s->month[t]), &(time_s->day[t]),
00208                             &(time_s->hour[t]), &(time_s->minutes[t]), &(time_s->seconds[t]), *cal_type);
00209     if (istat < 0) {
00210       (void) ut_free(dataunits);
00211       (void) ut_free_system(unitSystem);  
00212       return -1;
00213     }
00214   }
00215 
00216   (void) ut_free(dataunits);
00217   (void) ut_free_system(unitSystem);  
00218 
00220   istat = ncclose(ncinid);
00221   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00222 
00223   /* Success status */
00224   return 0;
00225 }

void handle_netcdf_error ( int  status,
char *  srcfilename,
int  lineno 
)

Handle NetCDF error.

Parameters:
[in] status Return status error code.
[in] srcfilename Source filename.
[in] lineno Line number of the error.

Definition at line 66 of file handle_netcdf_error.c.

Referenced by create_netcdf(), get_time_attributes(), get_time_info(), main(), output_downscaled_analog(), read_analog_data(), read_netcdf_dims_3d(), read_netcdf_dims_eof(), read_netcdf_latlon(), read_netcdf_var_1d(), read_netcdf_var_2d(), read_netcdf_var_3d(), read_netcdf_var_3d_2d(), read_netcdf_var_generic_val(), read_netcdf_xy(), save_analog_data(), write_learning_fields(), write_netcdf_dims_3d(), write_netcdf_var_3d(), write_netcdf_var_3d_2d(), and write_regression_fields().

00067 {
00074   if (status != NC_NOERR) {
00075     fprintf(stderr, "Source file: %s  Line: %d  Error %d: %s\n", srcfilename, lineno, status, nc_strerror(status));
00076   }
00077 }

int read_netcdf_dims_3d ( double **  lon,
double **  lat,
double **  timeval,
char **  cal_type,
char **  time_units,
int *  nlon,
int *  nlat,
int *  ntime,
info_struct info,
char *  coords,
char *  gridname,
char *  lonname,
char *  latname,
char *  dimxname,
char *  dimyname,
char *  timename,
char *  filename 
)

Read dimensions in a NetCDF file for 3D variables.

Parameters:
[out] lon Longitude field
[out] lat Latitude field
[out] timeval Time field
[out] cal_type Calendar type (udunits)
[out] time_units Time units (udunits)
[out] nlon Longitude dimension
[out] nlat Latitude dimension
[out] ntime Time dimension
[out] info Information structure for global NetCDF attributes
[in] coords Coordinates arrangement of latitude and longitude data: either 1D or 2D
[in] gridname Projection grid name
[in] lonname Longitude dimension name
[in] latname Latitude dimension name
[in] dimxname X Dimension name
[in] dimyname Y Dimension name
[in] timename Time dimension name
[in] filename Input NetCDF filename
Returns:
Status.

1D dimensions lat & lon

1D dimensions x and y with 2D lat & lon related variables

1D dimensions x and y with 2D lat & lon related variables

Read dimensions variables

Definition at line 66 of file read_netcdf_dims_3d.c.

References alloc_error(), info_struct::contact_email, info_struct::contact_name, info_struct::country, info_struct::creator_email, info_struct::creator_name, info_struct::creator_url, info_struct::description, info_struct::downscaling_forcing, FALSE, get_attribute_str(), handle_netcdf_error(), info_struct::institution, info_struct::institution_model, info_struct::keywords, info_struct::member, info_struct::model, info_struct::other_contact_email, info_struct::other_contact_name, info_struct::processor, info_struct::scenario, info_struct::scenario_co2, info_struct::summary, info_struct::summary_french, info_struct::timestep, info_struct::title, info_struct::title_french, TRUE, and info_struct::version.

Referenced by read_field_subdomain_period(), and read_large_scale_fields().

00068                                                                                                                   {
00091   int istat; /* Diagnostic status */
00092   int fixtime = FALSE; /* If we fixed time or not */
00093 
00094   size_t dimval; /* Variable used to retrieve dimension length */
00095 
00096   int ncinid; /* NetCDF input file handle ID */
00097   int latinid; /* Latitude variable ID */
00098   int loninid; /* Longitude variable ID */
00099   int timeinid; /* Time variable ID */
00100   nc_type vartype; /* Type of the variable (NC_FLOAT, NC_DOUBLE, etc.) */
00101   int varndims; /* Number of dimensions of variable */
00102   int vardimids[NC_MAX_VAR_DIMS]; /* Variable dimension ids */
00103   int timediminid; /* Time dimension ID */
00104   int londiminid; /* Longitude dimension ID */
00105   int latdiminid; /* Latitude dimension ID */
00106 
00107   size_t start[3]; /* Start position to read */
00108   size_t count[3]; /* Number of elements to read */
00109 
00110   double *tmpd = NULL; /* Temporary buffer to read variable from NetCDF file */
00111 
00112   int i; /* Loop counter */
00113   int j; /* Loop counter */
00114   int t; /* Time loop counter */
00115   int ndims; /* Number of dimensions of latitude and longitude variables, 1 or 2 for 1D and 2D respectively */
00116 
00117   /* Read data in NetCDF file */
00118 
00119   /* Open NetCDF file for reading */
00120   printf("%s: Reading info from NetCDF input file %s\n", __FILE__, filename);
00121   istat = nc_open(filename, NC_NOWRITE, &ncinid);  /* open for reading */
00122   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00123 
00124   /* Get dimensions ID */
00125   istat = nc_inq_dimid(ncinid, timename, &timediminid);  /* get ID for time dimension */
00126   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00127   istat = nc_inq_dimlen(ncinid, timediminid, &dimval); /* get time length */
00128   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00129   *ntime = (int) dimval;
00130 
00131   if ( !strcmp(coords, "1D") ) {
00133     ndims = 1;
00134 
00135     /* Get dimensions length */
00136     istat = nc_inq_dimid(ncinid, dimyname, &latdiminid);  /* get ID for lat dimension */
00137     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00138     istat = nc_inq_dimlen(ncinid, latdiminid, &dimval); /* get lat length */
00139     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00140     *nlat = (int) dimval;
00141     
00142     istat = nc_inq_dimid(ncinid, dimxname, &londiminid);  /* get ID for lon dimension */
00143     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00144     istat = nc_inq_dimlen(ncinid, londiminid, &dimval); /* get lon length */
00145     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00146     *nlon = (int) dimval;
00147   }
00148   else if ( !strcmp(coords, "2D") && !strcmp(gridname, "Latitude_Longitude") ) {
00150     ndims = 2;
00151 
00152     /* Get dimensions length */
00153     istat = nc_inq_dimid(ncinid, dimyname, &latdiminid);  /* get ID for lat dimension */
00154     if (istat != NC_NOERR) {
00155       (void) fprintf(stderr, "%s: Dimension name %s.\n", __FILE__, dimyname);
00156       handle_netcdf_error(istat, __FILE__, __LINE__);
00157     }
00158     istat = nc_inq_dimlen(ncinid, latdiminid, &dimval); /* get lat length */
00159     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00160     *nlat = (int) dimval;
00161     
00162     istat = nc_inq_dimid(ncinid, dimxname, &londiminid);  /* get ID for lon dimension */
00163     if (istat != NC_NOERR) {
00164       (void) fprintf(stderr, "%s: Dimension name %s.\n", __FILE__, dimxname);
00165       handle_netcdf_error(istat, __FILE__, __LINE__);
00166     }
00167     istat = nc_inq_dimlen(ncinid, londiminid, &dimval); /* get lon length */
00168     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00169     *nlon = (int) dimval;
00170   }
00171   else {
00173     ndims = 2;
00174 
00175     /* Get dimensions length */
00176     istat = nc_inq_dimid(ncinid, "y", &latdiminid);  /* get ID for lat dimension */
00177     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00178     istat = nc_inq_dimlen(ncinid, latdiminid, &dimval); /* get lat length */
00179     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00180     *nlat = (int) dimval;
00181     
00182     istat = nc_inq_dimid(ncinid, "x", &londiminid);  /* get ID for lon dimension */
00183     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00184     istat = nc_inq_dimlen(ncinid, londiminid, &dimval); /* get lon length */
00185     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00186     *nlon = (int) dimval;
00187   }
00188   
00189   /* Get time dimension ID */
00190   istat = nc_inq_varid(ncinid, timename, &timeinid);  /* get ID for time variable */
00191   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00192   /* Get time dimensions and type */
00193   istat = nc_inq_var(ncinid, timeinid, (char *) NULL, &vartype, &varndims, vardimids, (int *) NULL); /* get variable information */
00194   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00195   if (varndims != 1) {
00196     (void) fprintf(stderr, "Error NetCDF type and/or dimensions %d != 1.\n", varndims);
00197     istat = ncclose(ncinid);
00198     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00199     return -1;
00200   }
00201 
00202   /* Get dimension ID */
00203   istat = nc_inq_varid(ncinid, latname, &latinid);  /* get ID for lat variable */
00204   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00205   /* Get lat dimensions and type */
00206   istat = nc_inq_var(ncinid, latinid, (char *) NULL, &vartype, &varndims, vardimids, (int *) NULL); /* get variable information */
00207   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);  
00208   if (varndims != ndims) {
00209     (void) fprintf(stderr, "Error NetCDF type and/or dimensions %d != %d.\n", varndims, ndims);
00210     istat = ncclose(ncinid);
00211     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00212     return -1;
00213   }
00214 
00215   /* Get dimension ID */
00216   istat = nc_inq_varid(ncinid, lonname, &loninid);  /* get ID for lon variable */
00217   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00218   /* Get lat dimensions and type */
00219   istat = nc_inq_var(ncinid, loninid, (char *) NULL, &vartype, &varndims, vardimids, (int *) NULL); /* get variable information */
00220   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);  
00221   if (varndims != ndims) {
00222     (void) fprintf(stderr, "Error NetCDF type and/or dimensions %d != %d.\n", varndims, ndims);
00223     istat = ncclose(ncinid);
00224     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00225     return -1;
00226   }
00227 
00228   /* Get time units attributes */
00229   (void) get_attribute_str(time_units, ncinid, timeinid, "units");
00230   (void) get_attribute_str(cal_type, ncinid, timeinid, "calendar");
00231   if ( (*cal_type)[0] == '\0' ) {
00232     (void) free(*cal_type);
00233     (*cal_type) = strdup("standard");
00234   }
00235 
00236   /* Get global attributes */
00237   if (info->title == NULL) {
00238     (void) get_attribute_str(&(info->title), ncinid, NC_GLOBAL, "title");
00239     (void) get_attribute_str(&(info->title_french), ncinid, NC_GLOBAL, "title_french");
00240     (void) get_attribute_str(&(info->summary), ncinid, NC_GLOBAL, "summary");
00241     (void) get_attribute_str(&(info->summary_french), ncinid, NC_GLOBAL, "summary_french");
00242     (void) get_attribute_str(&(info->keywords), ncinid, NC_GLOBAL, "keywords");
00243     (void) get_attribute_str(&(info->processor), ncinid, NC_GLOBAL, "processor");
00244     (void) get_attribute_str(&(info->description), ncinid, NC_GLOBAL, "description");
00245     (void) get_attribute_str(&(info->institution), ncinid, NC_GLOBAL, "institution");
00246     (void) get_attribute_str(&(info->creator_email), ncinid, NC_GLOBAL, "creator_email");
00247     (void) get_attribute_str(&(info->creator_url), ncinid, NC_GLOBAL, "creator_url");
00248     (void) get_attribute_str(&(info->creator_name), ncinid, NC_GLOBAL, "creator_name");
00249     (void) get_attribute_str(&(info->version), ncinid, NC_GLOBAL, "version");
00250     (void) get_attribute_str(&(info->scenario), ncinid, NC_GLOBAL, "scenario");
00251     (void) get_attribute_str(&(info->scenario_co2), ncinid, NC_GLOBAL, "scenario_co2");
00252     (void) get_attribute_str(&(info->model), ncinid, NC_GLOBAL, "model");
00253     (void) get_attribute_str(&(info->institution_model), ncinid, NC_GLOBAL, "institution_model");
00254     (void) get_attribute_str(&(info->country), ncinid, NC_GLOBAL, "country");
00255     (void) get_attribute_str(&(info->member), ncinid, NC_GLOBAL, "member");
00256     (void) get_attribute_str(&(info->downscaling_forcing), ncinid, NC_GLOBAL, "downscaling_forcing");
00257     (void) get_attribute_str(&(info->timestep), ncinid, NC_GLOBAL, "timestep");
00258     (void) get_attribute_str(&(info->contact_email), ncinid, NC_GLOBAL, "contact_email");
00259     (void) get_attribute_str(&(info->contact_name), ncinid, NC_GLOBAL, "contact_name");
00260     (void) get_attribute_str(&(info->other_contact_email), ncinid, NC_GLOBAL, "other_contact_email");
00261     (void) get_attribute_str(&(info->other_contact_name), ncinid, NC_GLOBAL, "other_contact_name");
00262   }
00263 
00265   if ( !strcmp(coords, "1D") ) {
00266     /* Allocate memory and set start and count */
00267     start[0] = 0;
00268     start[1] = 0;
00269     start[2] = 0;
00270     count[0] = (size_t) (*nlat);
00271     count[1] = 0;
00272     count[2] = 0;
00273     (*lat) = (double *) malloc((*nlat) * (*nlon) * sizeof(double));
00274     if ((*lat) == NULL) alloc_error(__FILE__, __LINE__);
00275     tmpd = (double *) malloc((*nlat) * sizeof(double));
00276     if (tmpd == NULL) alloc_error(__FILE__, __LINE__);
00277     
00278     /* Read values from netCDF variable */
00279     istat = nc_get_vara_double(ncinid, latinid, start, count, tmpd);
00280     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00281     for (j=0; j<(*nlat); j++)
00282       for (i=0; i<(*nlon); i++)
00283         (*lat)[i+j*(*nlon)] = tmpd[j];
00284     
00285     /* Allocate memory and set start and count */
00286     start[0] = 0;
00287     start[1] = 0;
00288     start[2] = 0;
00289     count[0] = (size_t) (*nlon);
00290     count[1] = 0;
00291     count[2] = 0;
00292     (*lon) = (double *) malloc((*nlat) * (*nlon) * sizeof(double));
00293     if ((*lon) == NULL) alloc_error(__FILE__, __LINE__);
00294     tmpd = (double *) realloc(tmpd, (*nlon) * sizeof(double));
00295     if (tmpd == NULL) alloc_error(__FILE__, __LINE__);
00296     
00297     /* Read values from netCDF variable */
00298     istat = nc_get_vara_double(ncinid, loninid, start, count, tmpd);
00299     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00300     for (j=0; j<(*nlat); j++)
00301       for (i=0; i<(*nlon); i++)
00302         (*lon)[i+j*(*nlon)] = tmpd[i];
00303 
00304     (void) free(tmpd);
00305     
00306   }
00307   else {  
00308     /* Allocate memory and set start and count */
00309     start[0] = 0;
00310     start[1] = 0;
00311     start[2] = 0;
00312     count[0] = (size_t) (*nlat);
00313     count[1] = (size_t) (*nlon);
00314     count[2] = 0;
00315     (*lat) = (double *) malloc((*nlat) * (*nlon) * sizeof(double));
00316     if ((*lat) == NULL) alloc_error(__FILE__, __LINE__);
00317     
00318     /* Read values from netCDF variable */
00319     istat = nc_get_vara_double(ncinid, latinid, start, count, (*lat));
00320     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00321     
00322     /* Allocate memory and set start and count */
00323     start[0] = 0;
00324     start[1] = 0;
00325     start[2] = 0;
00326     count[0] = (size_t) (*nlat);
00327     count[1] = (size_t) (*nlon);
00328     count[2] = 0;
00329     (*lon) = (double *) malloc((*nlat) * (*nlon) * sizeof(double));
00330     if ((*lon) == NULL) alloc_error(__FILE__, __LINE__);
00331     
00332     /* Read values from netCDF variable */
00333     istat = nc_get_vara_double(ncinid, loninid, start, count, (*lon));
00334     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00335   }
00336 
00337   /* Allocate memory and set start and count */
00338   start[0] = 0;
00339   count[0] = (size_t) (*ntime);
00340   (*timeval) = malloc((*ntime) * sizeof(double));
00341   if ((*timeval) == NULL) alloc_error(__FILE__, __LINE__);
00342 
00343   /* Read values from netCDF variable */
00344   istat = nc_get_vara_double(ncinid, timeinid, start, count, (*timeval));
00345   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00346 
00347   /* Check values of time variable because many times they are all zero. In that case assume a 1 increment and a start at zero. */
00348   for (t=0; t<(*ntime); t++)
00349     if ((*timeval)[t] != 0.0)
00350       break;
00351   if (t == (*ntime)) {
00352     fprintf(stderr, "\n%s: IMPORTANT WARNING: Time variable values all zero!!! Fixing time variable to index value, STARTING at 0...\n\n", __FILE__);
00353     fixtime = TRUE;
00354     for (t=0; t<(*ntime); t++)
00355       (*timeval)[t] = (double) t;
00356   }
00357 
00358   /* Close NetCDF file */
00359   istat = ncclose(ncinid);
00360   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00361 
00362   if (fixtime == FALSE)
00363     /* Success status */
00364     return 0;
00365   else
00366     return 1;
00367 }

int read_netcdf_dims_eof ( double **  lon,
double **  lat,
int *  nlon,
int *  nlat,
int *  neof,
char *  coords,
char *  lonname,
char *  latname,
char *  dimxname,
char *  dimyname,
char *  eofname,
char *  filename 
)

Read dimensions in a NetCDF file for EOF data.

Parameters:
[out] lon Longitude field
[out] lat Latitude field
[out] nlon Longitude dimension
[out] nlat Latitude dimension
[out] neof EOF dimension
[in] coords Coordinates arrangement of latitude and longitude data: either 1D or 2D
[in] lonname Longitude variable name
[in] latname Latitude variable name
[in] dimxname Longitude dimension name
[in] dimyname Latitude dimension name
[in] eofname EOF dimension name
[in] filename Input NetCDF filename
Returns:
Status.

1D dimensions lat & lon

2D lat & lon variables

Read dimensions variables

Definition at line 66 of file read_netcdf_dims_eof.c.

References alloc_error(), and handle_netcdf_error().

Referenced by read_large_scale_eof().

00067                                                                                                                   {
00085   int istat; /* Diagnostic status */
00086 
00087   size_t dimval; /* Variable used to retrieve dimension length */
00088 
00089   int ncinid; /* NetCDF input file handle ID */
00090   int latinid; /* Latitude variable ID */
00091   int loninid; /* Longitude variable ID */
00092   nc_type vartype; /* Type of the variable (NC_FLOAT, NC_DOUBLE, etc.) */
00093   int varndims; /* Number of dimensions of variable */
00094   int vardimids[NC_MAX_VAR_DIMS]; /* Variable dimension ids */
00095   int eofdiminid; /* EOF dimension ID */
00096   int londiminid; /* Longitude dimension ID */
00097   int latdiminid; /* Latitude dimension ID */
00098 
00099   size_t start[3]; /* Start position to read */
00100   size_t count[3]; /* Number of elements to read */
00101 
00102   double *tmpd = NULL; /* Temporary buffer to read variable from NetCDF file */
00103 
00104   int i; /* Loop counter */
00105   int j; /* Loop counter */
00106   int ndims; /* Number of dimensions of latitude and longitude variables, 1 or 2 for 1D and 2D respectively */
00107 
00108   /* Read data in NetCDF file */
00109 
00110   /* Open NetCDF file for reading */
00111   printf("%s: Reading info from NetCDF input file %s\n", __FILE__, filename);
00112   istat = nc_open(filename, NC_NOWRITE, &ncinid);  /* open for reading */
00113   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00114 
00115   if ( !strcmp(coords, "1D") ) {
00117     ndims = 1;
00118 
00119     /* Get dimensions length */
00120     istat = nc_inq_dimid(ncinid, dimyname, &latdiminid);  /* get ID for lat dimension */
00121     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00122     istat = nc_inq_dimlen(ncinid, latdiminid, &dimval); /* get lat length */
00123     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00124     *nlat = (int) dimval;
00125     
00126     istat = nc_inq_dimid(ncinid, dimxname, &londiminid);  /* get ID for lon dimension */
00127     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00128     istat = nc_inq_dimlen(ncinid, londiminid, &dimval); /* get lon length */
00129     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00130     *nlon = (int) dimval;
00131   }
00132   else {
00134     ndims = 2;
00135 
00136     /* Get dimensions length */
00137     istat = nc_inq_dimid(ncinid, dimyname, &latdiminid);  /* get ID for lat dimension */
00138     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00139     istat = nc_inq_dimlen(ncinid, latdiminid, &dimval); /* get lat length */
00140     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00141     *nlat = (int) dimval;
00142     
00143     istat = nc_inq_dimid(ncinid, dimxname, &londiminid);  /* get ID for lon dimension */
00144     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00145     istat = nc_inq_dimlen(ncinid, londiminid, &dimval); /* get lon length */
00146     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00147     *nlon = (int) dimval;
00148   }
00149   
00150   /* Get dimensions length */
00151   istat = nc_inq_dimid(ncinid, eofname, &eofdiminid);  /* get ID for eof dimension */
00152   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00153   istat = nc_inq_dimlen(ncinid, eofdiminid, &dimval); /* get eof length */
00154   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00155   *neof = (int) dimval;
00156 
00157   /* Get variables IDs */
00158   istat = nc_inq_varid(ncinid, latname, &latinid);  /* get ID for lat variable */
00159   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00160   /* Get lat dimensions and type */
00161   istat = nc_inq_var(ncinid, latinid, (char *) NULL, &vartype, &varndims, vardimids, (int *) NULL); /* get variable information */
00162   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);  
00163   if (varndims != ndims) {
00164     (void) fprintf(stderr, "Error NetCDF type and/or dimensions %d != %d.\n", varndims, ndims);
00165     istat = ncclose(ncinid);
00166     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00167     return -1;
00168   }
00169   istat = nc_inq_varid(ncinid, lonname, &loninid);  /* get ID for lon variable */
00170   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00171   /* Get lat dimensions and type */
00172   istat = nc_inq_var(ncinid, loninid, (char *) NULL, &vartype, &varndims, vardimids, (int *) NULL); /* get variable information */
00173   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);  
00174   if (varndims != ndims) {
00175     (void) fprintf(stderr, "Error NetCDF type and/or dimensions %d != %d.\n", varndims, ndims);
00176     istat = ncclose(ncinid);
00177     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00178     return -1;
00179   }
00180 
00182   if ( !strcmp(coords, "1D") ) {
00183     /* Allocate memory and set start and count */
00184     start[0] = 0;
00185     start[1] = 0;
00186     start[2] = 0;
00187     count[0] = (size_t) (*nlat);
00188     count[1] = 0;
00189     count[2] = 0;
00190     (*lat) = (double *) malloc((*nlat) * (*nlon) * sizeof(double));
00191     if ((*lat) == NULL) alloc_error(__FILE__, __LINE__);
00192     tmpd = (double *) malloc((*nlat) * sizeof(double));
00193     if (tmpd == NULL) alloc_error(__FILE__, __LINE__);
00194     
00195     /* Read values from netCDF variable */
00196     istat = nc_get_vara_double(ncinid, latinid, start, count, tmpd);
00197     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00198     for (j=0; j<(*nlat); j++)
00199       for (i=0; i<(*nlon); i++)
00200         (*lat)[i+j*(*nlon)] = tmpd[j];
00201     
00202     /* Allocate memory and set start and count */
00203     start[0] = 0;
00204     start[1] = 0;
00205     start[2] = 0;
00206     count[0] = (size_t) (*nlon);
00207     count[1] = 0;
00208     count[2] = 0;
00209     (*lon) = (double *) malloc((*nlat) * (*nlon) * sizeof(double));
00210     if ((*lon) == NULL) alloc_error(__FILE__, __LINE__);
00211     tmpd = (double *) realloc(tmpd, (*nlon) * sizeof(double));
00212     if (tmpd == NULL) alloc_error(__FILE__, __LINE__);
00213     
00214     /* Read values from netCDF variable */
00215     istat = nc_get_vara_double(ncinid, loninid, start, count, tmpd);
00216     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00217     for (j=0; j<(*nlat); j++)
00218       for (i=0; i<(*nlon); i++)
00219         (*lon)[i+j*(*nlon)] = tmpd[i];
00220 
00221     (void) free(tmpd);
00222     
00223   }
00224   else {  
00225     /* Allocate memory and set start and count */
00226     start[0] = 0;
00227     start[1] = 0;
00228     start[2] = 0;
00229     count[0] = (size_t) (*nlat);
00230     count[1] = (size_t) (*nlon);
00231     count[2] = 0;
00232     (*lat) = (double *) malloc((*nlat) * (*nlon) * sizeof(double));
00233     if ((*lat) == NULL) alloc_error(__FILE__, __LINE__);
00234     
00235     /* Read values from netCDF variable */
00236     istat = nc_get_vara_double(ncinid, latinid, start, count, (*lat));
00237     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00238     
00239     /* Allocate memory and set start and count */
00240     start[0] = 0;
00241     start[1] = 0;
00242     start[2] = 0;
00243     count[0] = (size_t) (*nlat);
00244     count[1] = (size_t) (*nlon);
00245     count[2] = 0;
00246     (*lon) = (double *) malloc((*nlat) * (*nlon) * sizeof(double));
00247     if ((*lon) == NULL) alloc_error(__FILE__, __LINE__);
00248     
00249     /* Read values from netCDF variable */
00250     istat = nc_get_vara_double(ncinid, loninid, start, count, (*lon));
00251     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00252   }
00253 
00254   istat = ncclose(ncinid);
00255   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00256 
00257   /* Success status */
00258   return 0;
00259 }

int read_netcdf_latlon ( double **  lon,
double **  lat,
int *  nlon,
int *  nlat,
char *  dimcoords,
char *  coords,
char *  gridname,
char *  lonname,
char *  latname,
char *  dimxname,
char *  dimyname,
char *  filename 
)

Read latitude and longitude dimensions in a NetCDF file.

Parameters:
[out] lon Longitude field
[out] lat Latitude field
[out] nlon Longitude dimension
[out] nlat Latitude dimension
[in] dimcoords Coordinates arrangement for X and Y dimensions: either 1D or 2D
[in] coords Coordinates arrangement of latitude and longitude data: either 1D or 2D
[in] gridname Projection grid name
[in] lonname Longitude variable name
[in] latname Latitude variable name
[in] dimxname X dimension name
[in] dimyname Y dimension name
[in] filename Input NetCDF filename
Returns:
Status.

1D dimensions lat and lon dimensions

2D dimensions lat and lon dimensions

2D dimensions lat and lon dimensions

1D dimensions X & Y

1D dimensions x and y with 2D lat & lon related variables

1D dimensions x and y with 2D lat & lon related variables

Read dimensions variables

Definition at line 66 of file read_netcdf_latlon.c.

References alloc_error(), and handle_netcdf_error().

Referenced by output_downscaled_analog(), read_mask(), and read_obs_period().

00067                                                                                                  {
00085   int istat; /* Diagnostic status */
00086 
00087   size_t dimval; /* Variable used to retrieve dimension length */
00088 
00089   int ncinid; /* NetCDF input file handle ID */
00090   int latinid; /* Latitude variable ID */
00091   int loninid; /* Longitude variable ID */
00092   nc_type vartype; /* Type of the variable (NC_FLOAT, NC_DOUBLE, etc.) */
00093   int varndims; /* Number of dimensions of variable */
00094   int vardimids[NC_MAX_VAR_DIMS]; /* Variable dimension ids */
00095   int londiminid; /* Longitude dimension ID */
00096   int latdiminid; /* Latitude dimension ID */
00097 
00098   size_t start[3]; /* Start position to read */
00099   size_t count[3]; /* Number of elements to read */
00100 
00101   double *tmpd = NULL; /* Temporary buffer to read variable from NetCDF file */
00102 
00103   int i; /* Loop counter */
00104   int j; /* Loop counter */
00105   int ndims; /* Number of dimensions of latitude and longitude variables, 1 or 2 for 1D and 2D respectively */
00106   int ndims_xy; /* Number of dimensions of X and Y dimensions, 1 or 2 for 1D and 2D respectively */
00107   int npts; /* Number of points for list of latitude + longitude points */
00108 
00109   /* Read data in NetCDF file */
00110 
00111   /* Open NetCDF file for reading */
00112   printf("%s: Reading info from NetCDF input file %s\n", __FILE__, filename);
00113   istat = nc_open(filename, NC_NOWRITE, &ncinid);  /* open for reading */
00114   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00115 
00116   if ( !strcmp(coords, "1D") )
00118     ndims = 1;
00119   else if ( !strcmp(coords, "2D") )
00121     ndims = 2;
00122   else
00124     ndims = 2;
00125   
00126   if ( !strcmp(dimcoords, "1D") ) {
00128     ndims_xy = 1;
00129 
00130     /* Get dimensions length */
00131     istat = nc_inq_dimid(ncinid, dimyname, &latdiminid);  /* get ID for Y dimension */
00132     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00133     istat = nc_inq_dimlen(ncinid, latdiminid, &dimval); /* get lat length */
00134     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00135     *nlat = (int) dimval;
00136     
00137     istat = nc_inq_dimid(ncinid, dimxname, &londiminid);  /* get ID for X dimension */
00138     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00139     istat = nc_inq_dimlen(ncinid, londiminid, &dimval); /* get lon length */
00140     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00141     *nlon = (int) dimval;
00142   }
00143   else if ( !strcmp(dimcoords, "2D") && !strcmp(gridname, "Latitude_Longitude") ) {
00145     ndims_xy = 2;
00146 
00147     /* Get dimensions length */
00148     istat = nc_inq_dimid(ncinid, dimyname, &latdiminid);  /* get ID for Y dimension */
00149     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00150     istat = nc_inq_dimlen(ncinid, latdiminid, &dimval); /* get lat length */
00151     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00152     *nlat = (int) dimval;
00153     
00154     istat = nc_inq_dimid(ncinid, dimxname, &londiminid);  /* get ID for X dimension */
00155     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00156     istat = nc_inq_dimlen(ncinid, londiminid, &dimval); /* get lon length */
00157     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00158     *nlon = (int) dimval;
00159   }
00160   else {
00162     ndims_xy = 2;
00163 
00164     /* Get dimensions length */
00165     istat = nc_inq_dimid(ncinid, "y", &latdiminid);  /* get ID for lat dimension */
00166     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00167     istat = nc_inq_dimlen(ncinid, latdiminid, &dimval); /* get lat length */
00168     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00169     *nlat = (int) dimval;
00170     
00171     istat = nc_inq_dimid(ncinid, "x", &londiminid);  /* get ID for lon dimension */
00172     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00173     istat = nc_inq_dimlen(ncinid, londiminid, &dimval); /* get lon length */
00174     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00175     *nlon = (int) dimval;
00176   }
00177   
00178   /* Get dimension ID */
00179   istat = nc_inq_varid(ncinid, latname, &latinid);  /* get ID for lat variable */
00180   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00181   /* Get lat dimensions and type */
00182   istat = nc_inq_var(ncinid, latinid, (char *) NULL, &vartype, &varndims, vardimids, (int *) NULL); /* get variable information */
00183   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);  
00184   if (varndims != ndims) {
00185     (void) fprintf(stderr, "Error NetCDF type and/or dimensions %d != %d.\n", varndims, ndims);
00186     istat = ncclose(ncinid);
00187     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00188     return -1;
00189   }
00190 
00191   /* Get dimension ID */
00192   istat = nc_inq_varid(ncinid, lonname, &loninid);  /* get ID for lon variable */
00193   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00194   /* Get lat dimensions and type */
00195   istat = nc_inq_var(ncinid, loninid, (char *) NULL, &vartype, &varndims, vardimids, (int *) NULL); /* get variable information */
00196   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);  
00197   if (varndims != ndims) {
00198     (void) fprintf(stderr, "Error NetCDF type and/or dimensions %d != %d.\n", varndims, ndims);
00199     istat = ncclose(ncinid);
00200     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00201     return -1;
00202   }
00203 
00205   if ( !strcmp(gridname, "list") ) {
00206     /* 1D list of lat + lon points */
00207     npts = *nlon;
00208     *nlat = 0;
00209     /* Allocate memory and set start and count */
00210     start[0] = 0;
00211     start[1] = 0;
00212     start[2] = 0;
00213     count[0] = (size_t) npts;
00214     count[1] = 0;
00215     count[2] = 0;
00216     (*lat) = (double *) malloc(npts * sizeof(double));
00217     if ((*lat) == NULL) alloc_error(__FILE__, __LINE__);
00218     
00219     /* Read values from netCDF variable */
00220     istat = nc_get_vara_double(ncinid, latinid, start, count, (*lat));
00221     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00222     
00223     /* Allocate memory and set start and count */
00224     start[0] = 0;
00225     start[1] = 0;
00226     start[2] = 0;
00227     count[0] = (size_t) npts;
00228     count[1] = 0;
00229     count[2] = 0;
00230     (*lon) = (double *) malloc(npts * sizeof(double));
00231     if ((*lon) == NULL) alloc_error(__FILE__, __LINE__);
00232     
00233     /* Read values from netCDF variable */
00234     istat = nc_get_vara_double(ncinid, loninid, start, count, (*lon));
00235     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00236   }
00237   else if ( !strcmp(coords, "1D") ) {
00238     /* Allocate memory and set start and count */
00239     start[0] = 0;
00240     start[1] = 0;
00241     start[2] = 0;
00242     count[0] = (size_t) (*nlat);
00243     count[1] = 0;
00244     count[2] = 0;
00245     (*lat) = (double *) malloc((*nlat) * (*nlon) * sizeof(double));
00246     if ((*lat) == NULL) alloc_error(__FILE__, __LINE__);
00247     tmpd = (double *) malloc((*nlat) * sizeof(double));
00248     if (tmpd == NULL) alloc_error(__FILE__, __LINE__);
00249     
00250     /* Read values from netCDF variable */
00251     istat = nc_get_vara_double(ncinid, latinid, start, count, tmpd);
00252     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00253     for (j=0; j<(*nlat); j++)
00254       for (i=0; i<(*nlon); i++)
00255         (*lat)[i+j*(*nlon)] = tmpd[j];
00256     
00257     /* Allocate memory and set start and count */
00258     start[0] = 0;
00259     start[1] = 0;
00260     start[2] = 0;
00261     count[0] = (size_t) (*nlon);
00262     count[1] = 0;
00263     count[2] = 0;
00264     (*lon) = (double *) malloc((*nlat) * (*nlon) * sizeof(double));
00265     if ((*lon) == NULL) alloc_error(__FILE__, __LINE__);
00266     tmpd = (double *) realloc(tmpd, (*nlon) * sizeof(double));
00267     if (tmpd == NULL) alloc_error(__FILE__, __LINE__);
00268     
00269     /* Read values from netCDF variable */
00270     istat = nc_get_vara_double(ncinid, loninid, start, count, tmpd);
00271     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00272     for (j=0; j<(*nlat); j++)
00273       for (i=0; i<(*nlon); i++)
00274         (*lon)[i+j*(*nlon)] = tmpd[i];
00275 
00276     (void) free(tmpd);
00277     
00278   }
00279   else {  
00280     /* Allocate memory and set start and count */
00281     start[0] = 0;
00282     start[1] = 0;
00283     start[2] = 0;
00284     count[0] = (size_t) (*nlat);
00285     count[1] = (size_t) (*nlon);
00286     count[2] = 0;
00287     (*lat) = (double *) malloc((*nlat) * (*nlon) * sizeof(double));
00288     if ((*lat) == NULL) alloc_error(__FILE__, __LINE__);
00289     
00290     /* Read values from netCDF variable */
00291     istat = nc_get_vara_double(ncinid, latinid, start, count, (*lat));
00292     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00293     
00294     /* Allocate memory and set start and count */
00295     start[0] = 0;
00296     start[1] = 0;
00297     start[2] = 0;
00298     count[0] = (size_t) (*nlat);
00299     count[1] = (size_t) (*nlon);
00300     count[2] = 0;
00301     (*lon) = (double *) malloc((*nlat) * (*nlon) * sizeof(double));
00302     if ((*lon) == NULL) alloc_error(__FILE__, __LINE__);
00303     
00304     /* Read values from netCDF variable */
00305     istat = nc_get_vara_double(ncinid, loninid, start, count, (*lon));
00306     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00307   }
00308 
00309   istat = ncclose(ncinid);
00310   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00311 
00312   /* Success status */
00313   return 0;
00314 }

int read_netcdf_var_1d ( double **  buf,
info_field_struct info_field,
char *  filename,
char *  varname,
char *  dimname,
int *  ndim,
int  outinfo 
)

Read a 1D variable in a NetCDF file, and return information in info_field_struct structure.

Parameters:
[out] buf 1D variable
[out] info_field Information about the output variable
[in] filename NetCDF input filename
[in] varname NetCDF variable name
[in] dimname Dimension name
[out] ndim Dimension length
[in] outinfo TRUE if we want information output, FALSE if not
Returns:
Status.

Read data variable

Definition at line 66 of file read_netcdf_var_1d.c.

References alloc_error(), info_field_struct::fillvalue, handle_netcdf_error(), info_field_struct::height, info_field_struct::long_name, MAXPATH, TRUE, and info_field_struct::units.

Referenced by read_large_scale_eof(), read_learning_fields(), read_learning_obs_eof(), read_learning_rea_eof(), and read_regression_points().

00067                                                           {
00080   int istat; /* Diagnostic status */
00081 
00082   size_t dimval; /* Variable used to retrieve dimension length */
00083 
00084   int ncinid; /* NetCDF input file handle ID */
00085   int varinid; /* NetCDF variable ID */
00086   int diminid; /* NetCDF dimension ID */
00087   nc_type vartype_main; /* Type of the variable (NC_FLOAT, NC_DOUBLE, etc.) */
00088   int varndims; /* Number of dimensions of variable */
00089   int vardimids[NC_MAX_VAR_DIMS]; /* Variable dimension ids */
00090 
00091   size_t start[3]; /* Start position to read */
00092   size_t count[3]; /* Number of elements to read */
00093 
00094   float valf; /* Variable used to retrieve fillvalue */
00095   char *tmpstr = NULL; /* Temporary string */
00096   size_t t_len; /* Length of string attribute */
00097 
00098   /* Allocate memory */
00099   tmpstr = (char *) malloc(MAXPATH * sizeof(char));
00100   if (tmpstr == NULL) alloc_error(__FILE__, __LINE__);
00101 
00102   /* Read data in NetCDF file */
00103 
00104   /* Open NetCDF file for reading */
00105   if (outinfo == TRUE)
00106     printf("%s: Opening for reading NetCDF input file %s\n", __FILE__, filename);
00107   istat = nc_open(filename, NC_NOWRITE, &ncinid);  /* open for reading */
00108   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00109 
00110   if (outinfo == TRUE)
00111     printf("%s: READ %s %s\n", __FILE__, varname, filename);
00112 
00113   /* Get dimension length */
00114   istat = nc_inq_dimid(ncinid, dimname, &diminid);  /* get ID for dimension */
00115   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00116   istat = nc_inq_dimlen(ncinid, diminid, &dimval); /* get dimension length */
00117   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00118   *ndim = (int) dimval;
00119 
00120   /* Get main variable ID */
00121   istat = nc_inq_varid(ncinid, varname, &varinid); /* get main variable ID */
00122   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00123 
00126   /* Get variable information */
00127   istat = nc_inq_var(ncinid, varinid, (char *) NULL, &vartype_main, &varndims, vardimids, (int *) NULL);
00128   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00129 
00130   /* Verify that variable is really 1D */
00131   if (varndims != 1) {
00132     (void) fprintf(stderr, "%s: Error NetCDF type and/or dimensions.\n", __FILE__);
00133     (void) free(tmpstr);
00134     istat = ncclose(ncinid);
00135     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00136     return -1;
00137   }
00138 
00139   /* If info_field si not NULL, get some information about the read variable */
00140   if (info_field != NULL) {
00141     /* Get missing value */
00142     if (vartype_main == NC_FLOAT) {
00143       istat = nc_get_att_float(ncinid, varinid, "missing_value", &valf);
00144       if (istat != NC_NOERR)
00145         info_field->fillvalue = -9999.0;
00146       else
00147         info_field->fillvalue = (double) valf;
00148     }
00149     else if (vartype_main == NC_DOUBLE) {
00150       istat = nc_get_att_double(ncinid, varinid, "missing_value", &(info_field->fillvalue));
00151       if (istat != NC_NOERR)
00152         info_field->fillvalue = -9999.0;
00153     }
00154 
00155     /* Get units */
00156     istat = nc_inq_attlen(ncinid, varinid, "units", &t_len);
00157     if (istat == NC_NOERR) {
00158       handle_netcdf_error(istat, __FILE__, __LINE__);
00159       istat = nc_get_att_text(ncinid, varinid, "units", tmpstr);
00160       if (istat == NC_NOERR) {
00161         if (tmpstr[t_len-1] != '\0')
00162           tmpstr[t_len] = '\0';
00163         info_field->units = strdup(tmpstr);
00164       }
00165       else
00166         info_field->units = strdup("unknown");
00167     }
00168     else
00169       info_field->units = strdup("unknown");
00170 
00171     /* Get height */
00172     istat = nc_inq_attlen(ncinid, varinid, "height", &t_len);
00173     if (istat == NC_NOERR) {
00174       handle_netcdf_error(istat, __FILE__, __LINE__);
00175       istat = nc_get_att_text(ncinid, varinid, "height", tmpstr);
00176       if (istat == NC_NOERR) {
00177         if (tmpstr[t_len-1] != '\0')
00178           tmpstr[t_len] = '\0';
00179         info_field->height = strdup(tmpstr);
00180       }
00181       else
00182         info_field->height = strdup("unknown");
00183     }
00184     else
00185       info_field->height = strdup("unknown");
00186 
00187     /* Get long name */
00188     istat = nc_inq_attlen(ncinid, varinid, "long_name", &t_len);
00189     if (istat == NC_NOERR) {
00190       handle_netcdf_error(istat, __FILE__, __LINE__);
00191       istat = nc_get_att_text(ncinid, varinid, "long_name", tmpstr);
00192       if (istat == NC_NOERR) {
00193         if (tmpstr[t_len-1] != '\0')
00194           tmpstr[t_len] = '\0';
00195         info_field->long_name = strdup(tmpstr);
00196       }
00197       else
00198         info_field->long_name = strdup(varname);
00199     }
00200     else
00201       info_field->long_name = strdup(varname);
00202   }
00203 
00204   /* Allocate memory and set start and count */
00205   start[0] = 0;
00206   start[1] = 0;
00207   start[2] = 0;
00208   count[0] = (size_t) *ndim;
00209   count[1] = 0;
00210   count[2] = 0;
00211   /* Allocate memory */
00212   (*buf) = (double *) malloc((*ndim) * sizeof(double));
00213   if ((*buf) == NULL) alloc_error(__FILE__, __LINE__);
00214 
00215   /* Read values from netCDF variable */
00216   istat = nc_get_vara_double(ncinid, varinid, start, count, *buf);
00217   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00218 
00219   /* Close the input netCDF file. */
00220   istat = ncclose(ncinid);
00221   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00222 
00223   /* Free memory */
00224   (void) free(tmpstr);
00225 
00226   /* Success status */
00227   return 0;
00228 }

int read_netcdf_var_2d ( double **  buf,
info_field_struct info_field,
proj_struct proj,
char *  filename,
char *  varname,
char *  dimxname,
char *  dimyname,
int *  nlon,
int *  nlat,
int  outinfo 
)

Read a 2D variable in a NetCDF file, and return information in info_field_struct structure and proj_struct.

Parameters:
[out] buf 2D variable
[out] info_field Information about the output variable
[out] proj Information about the horizontal projection of the output variable
[in] filename NetCDF input filename
[in] varname NetCDF variable name
[in] dimxname Longitude dimension name
[in] dimyname Latitude dimension name
[out] nlon Longitude dimension length
[out] nlat Latitude dimension length
[in] outinfo TRUE if we want information output, FALSE if not
Returns:
Status.

Read data variable

Definition at line 67 of file read_netcdf_var_2d.c.

References alloc_error(), info_field_struct::coordinates, proj_struct::false_easting, proj_struct::false_northing, info_field_struct::fillvalue, get_attribute_str(), info_field_struct::grid_mapping, proj_struct::grid_mapping_name, handle_netcdf_error(), info_field_struct::height, proj_struct::lat0, proj_struct::latin1, proj_struct::latin2, proj_struct::latpole, proj_struct::lonc, info_field_struct::long_name, proj_struct::lonpole, MAXPATH, proj_struct::name, TRUE, and info_field_struct::units.

Referenced by output_downscaled_analog(), read_learning_fields(), read_learning_obs_eof(), read_learning_rea_eof(), and read_mask().

00068                                                                                       {
00084   int istat; /* Diagnostic status */
00085 
00086   size_t dimval; /* Variable used to retrieve dimension length */
00087 
00088   int ncinid; /* NetCDF input file handle ID */
00089   int varinid; /* NetCDF variable ID */
00090   int projinid; /* Projection variable ID */
00091   nc_type vartype_main; /* Type of the variable (NC_FLOAT, NC_DOUBLE, etc.) */
00092   int varndims; /* Number of dimensions of variable */
00093   int vardimids[NC_MAX_VAR_DIMS]; /* Variable dimension ids */
00094   int londiminid; /* Longitude dimension ID */
00095   int latdiminid; /* Latitude dimension ID */
00096 
00097   size_t start[3]; /* Start position to read */
00098   size_t count[3]; /* Number of elements to read */
00099 
00100   float valf; /* Variable used to retrieve fillvalue */
00101   int vali; /* Variable used to retrieve integer values */
00102   char *tmpstr = NULL; /* Temporary string */
00103   size_t t_len; /* Length of string attribute */
00104 
00105   float *proj_latin = NULL; /* Parallel latitudes of projection */
00106   int npts = 0; /* Number of points for 1D variables */
00107   char *grid_mapping = NULL;
00108 
00109   /* Allocate memory */
00110   tmpstr = (char *) malloc(MAXPATH * sizeof(char));
00111   if (tmpstr == NULL) alloc_error(__FILE__, __LINE__);
00112 
00113   /* Read data in NetCDF file */
00114 
00115   /* Open NetCDF file for reading */
00116   if (outinfo == TRUE)
00117     printf("%s: Opening for reading NetCDF input file %s\n", __FILE__, filename);
00118   istat = nc_open(filename, NC_NOWRITE, &ncinid);  /* open for reading */
00119   if (istat != NC_NOERR) {
00120     printf("%s: Failed Opening for reading NetCDF input file %s\n", __FILE__, filename);
00121     handle_netcdf_error(istat, __FILE__, __LINE__);
00122     return -1;
00123   }
00124 
00125   if (outinfo == TRUE)
00126     printf("%s: READ %s %s\n", __FILE__, varname, filename);
00127 
00128   /* Get dimensions length */
00129   istat = nc_inq_dimid(ncinid, dimyname, &latdiminid);  /* get ID for lat dimension */
00130   if (istat != NC_NOERR) { handle_netcdf_error(istat, __FILE__, __LINE__); return -1; }
00131   istat = nc_inq_dimlen(ncinid, latdiminid, &dimval); /* get lat length */
00132   if (istat != NC_NOERR) { handle_netcdf_error(istat, __FILE__, __LINE__); return -1; }
00133   *nlat = (int) dimval;
00134 
00135   istat = nc_inq_dimid(ncinid, dimxname, &londiminid);  /* get ID for lon dimension */
00136   if (istat != NC_NOERR) { handle_netcdf_error(istat, __FILE__, __LINE__); return -1; }
00137   istat = nc_inq_dimlen(ncinid, londiminid, &dimval); /* get lon length */
00138   if (istat != NC_NOERR) { handle_netcdf_error(istat, __FILE__, __LINE__); return -1; }
00139   *nlon = (int) dimval;
00140   
00141   /* Get main variable ID */
00142   istat = nc_inq_varid(ncinid, varname, &varinid); /* get main variable ID */
00143   if (istat != NC_NOERR) { handle_netcdf_error(istat, __FILE__, __LINE__); return -1; }
00144 
00147   /* Get variable information */
00148   istat = nc_inq_var(ncinid, varinid, (char *) NULL, &vartype_main, &varndims, vardimids, (int *) NULL);
00149   if (istat != NC_NOERR) { handle_netcdf_error(istat, __FILE__, __LINE__); return -1; }
00150 
00151   /* Verify that variable is really 2D or 1D */
00152   if (varndims != 2 && varndims != 1) {
00153     (void) fprintf(stderr, "%s: Error NetCDF type and/or dimensions nlon %d nlat %d. The dimensions of the data variable is neither 1D or 2D...\n", __FILE__, *nlon, *nlat);
00154     (void) free(tmpstr);
00155     istat = ncclose(ncinid);
00156     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00157     return -1;
00158   }
00159 
00160   if (varndims == 1) {
00161     if ((*nlat) != (*nlon)) {
00162       (void) fprintf(stderr, "%s: Error NetCDF type and/or dimensions nlon %d nlat %d. When dimensions are specified as 1D, nlon must be equal to nlat because the grid cannot be assumed to be regular in latitude and longitude.\n", __FILE__, *nlon, *nlat);
00163       (void) free(tmpstr);
00164       istat = ncclose(ncinid);
00165       if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00166       return -1;
00167     }
00168     npts = *nlon;
00169     *nlat = 0;
00170   }
00171 
00172   /* If info_field si not NULL, get some information about the read variable */
00173   if (info_field != NULL) {
00174     /* Get missing value */
00175     if (vartype_main == NC_FLOAT) {
00176       istat = nc_get_att_float(ncinid, varinid, "missing_value", &valf);
00177       if (istat != NC_NOERR)
00178         info_field->fillvalue = -9999.0;
00179       else
00180         info_field->fillvalue = (double) valf;
00181     }
00182     else if (vartype_main == NC_DOUBLE) {
00183       istat = nc_get_att_double(ncinid, varinid, "missing_value", &(info_field->fillvalue));
00184       if (istat != NC_NOERR)
00185         info_field->fillvalue = -9999.0;
00186     }
00187     
00188     /* Get coordinates */
00189     istat = nc_inq_attlen(ncinid, varinid, "coordinates", &t_len);
00190     if (istat == NC_NOERR) {
00191       istat = nc_get_att_text(ncinid, varinid, "coordinates", tmpstr);
00192       if (istat == NC_NOERR) {
00193         if (tmpstr[t_len-1] != '\0')
00194           tmpstr[t_len] = '\0';
00195         info_field->coordinates = strdup(tmpstr);
00196       }
00197       else
00198         info_field->coordinates = strdup("lon lat");
00199     }
00200     else
00201       info_field->coordinates = strdup("lon lat");
00202     
00203     /* Get grid projection */
00204     istat = nc_inq_attlen(ncinid, varinid, "grid_mapping", &t_len);
00205     if (istat == NC_NOERR) {
00206       istat = nc_get_att_text(ncinid, varinid, "grid_mapping", tmpstr);
00207       if (istat == NC_NOERR) {
00208         if (tmpstr[t_len-1] != '\0')
00209           tmpstr[t_len] = '\0';
00210         info_field->grid_mapping = strdup(tmpstr);
00211       }
00212       else
00213         info_field->grid_mapping = strdup("unknown");
00214     }
00215     else
00216       info_field->grid_mapping = strdup("unknown");
00217     
00218     /* Get units */
00219     istat = nc_inq_attlen(ncinid, varinid, "units", &t_len);
00220     if (istat == NC_NOERR) {
00221       istat = nc_get_att_text(ncinid, varinid, "units", tmpstr);
00222       if (istat == NC_NOERR) {
00223         if (tmpstr[t_len-1] != '\0')
00224           tmpstr[t_len] = '\0';
00225         info_field->units = strdup(tmpstr);
00226       }
00227       else
00228         info_field->units = strdup("unknown");
00229     }
00230     else
00231       info_field->units = strdup("unknown");
00232     
00233     /* Get height */
00234     istat = nc_inq_attlen(ncinid, varinid, "height", &t_len);
00235     if (istat == NC_NOERR) {
00236       istat = nc_get_att_text(ncinid, varinid, "height", tmpstr);
00237       if (istat == NC_NOERR) {
00238         if (tmpstr[t_len-1] != '\0')
00239           tmpstr[t_len] = '\0';
00240         info_field->height = strdup(tmpstr);
00241       }
00242       else
00243         info_field->height = strdup("unknown");
00244     }
00245     else
00246       info_field->height = strdup("unknown");
00247 
00248     /* Get long name */
00249     istat = nc_inq_attlen(ncinid, varinid, "long_name", &t_len);
00250     if (istat == NC_NOERR) {
00251       istat = nc_get_att_text(ncinid, varinid, "long_name", tmpstr);
00252       if (istat == NC_NOERR) {
00253         if (tmpstr[t_len-1] != '\0')
00254           tmpstr[t_len] = '\0';
00255         info_field->long_name = strdup(tmpstr);
00256       }
00257       else
00258         info_field->long_name = strdup(varname);
00259     }
00260     else
00261       info_field->long_name = strdup(varname);
00262   }
00263 
00264   /* if proj is not NULL, retrieve informations about the horizontal projection parameters */
00265   if (proj != NULL) {
00266     if (info_field == NULL)
00267       grid_mapping = strdup("unknown");
00268     else
00269       grid_mapping = strdup(info_field->grid_mapping);
00270     /* Get projection variable ID */
00271     if ( !strcmp(grid_mapping, "Lambert_Conformal") ) {
00272       istat = nc_inq_varid(ncinid, grid_mapping, &projinid); /* get projection variable ID */
00273       if (istat != NC_NOERR) { handle_netcdf_error(istat, __FILE__, __LINE__); return -1; }
00274     }
00275     else if ( !strcmp(grid_mapping, "rotated_latitude_longitude") ) {
00276       istat = nc_inq_varid(ncinid, grid_mapping, &projinid); /* get projection variable ID */
00277       if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00278     }
00279     if (proj->name != NULL) {
00280       if ( strcmp(grid_mapping, "Lambert_Conformal") && strcmp(grid_mapping, "Latitude_Longitude") &&
00281            ( !strcmp(proj->name, "Latitude_Longitude") || !strcmp(proj->name, "Lambert_Conformal") ) ) {
00282         (void) free(grid_mapping);
00283         grid_mapping = strdup(proj->name);
00284       }
00285       else if ( strcmp(grid_mapping, "rotated_latitude_longitude") && !strcmp(proj->name, "rotated_pole") ) {
00286         (void) free(grid_mapping);
00287         grid_mapping = strdup("rotated_latitude_longitude");
00288       }
00289     }
00290     if ( !strcmp(grid_mapping, "Lambert_Conformal") ) {
00291       /*              int Lambert_Conformal ;
00292                       Lambert_Conformal:grid_mapping_name = "lambert_conformal_conic" ;
00293                       Lambert_Conformal:standard_parallel = 45.89892f, 47.69601f ;
00294                       Lambert_Conformal:longitude_of_central_meridian = 2.337229f ;
00295                       Lambert_Conformal:latitude_of_projection_origin = 46.8f ;
00296                       Lambert_Conformal:false_easting = 600000.f ;
00297                       Lambert_Conformal:false_northing = 2200000.f ;
00298       */
00299       istat = nc_get_var1_int(ncinid, projinid, 0, &vali);
00300       if (istat != NC_NOERR) { handle_netcdf_error(istat, __FILE__, __LINE__); return -1; }
00301       proj->name = strdup(grid_mapping);
00302       istat = get_attribute_str(&(proj->grid_mapping_name), ncinid, projinid, "grid_mapping_name");
00303       
00304       proj_latin = (float *) malloc(2 * sizeof(float));
00305       if (proj_latin == NULL) alloc_error(__FILE__, __LINE__);
00306       istat = nc_get_att_float(ncinid, projinid, "standard_parallel", proj_latin);
00307       proj->latin1 = (double) proj_latin[0];
00308       proj->latin2 = (double) proj_latin[1];
00309       (void) free(proj_latin);
00310       
00311       istat = nc_get_att_double(ncinid, projinid, "longitude_of_central_meridian", &(proj->lonc));
00312       istat = nc_get_att_double(ncinid, projinid, "latitude_of_projection_origin", &(proj->lat0));
00313       istat = nc_get_att_double(ncinid, projinid, "false_easting", &(proj->false_easting));
00314       istat = nc_get_att_double(ncinid, projinid, "false_northing", &(proj->false_northing));
00315       
00316     }
00317     else if ( !strcmp(grid_mapping, "rotated_latitude_longitude") ) {
00318       /*        int rotated_pole ;
00319                 rotated_pole:grid_mapping_name = "rotated_latitude_longitude" ;
00320                 rotated_pole:grid_north_pole_latitude = 39.25 ;
00321                 rotated_pole:grid_north_pole_longitude = -162. ;
00322       */
00323       istat = nc_get_var1_int(ncinid, projinid, 0, &vali);
00324       if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);  
00325       proj->name = strdup("rotated_pole");
00326       istat = get_attribute_str(&(proj->grid_mapping_name), ncinid, projinid, "grid_mapping_name");
00327     
00328       istat = nc_get_att_double(ncinid, projinid, "grid_north_pole_latitude", &(proj->latpole));
00329       istat = nc_get_att_double(ncinid, projinid, "grid_north_pole_longitude", &(proj->lonpole));
00330     
00331     }
00332     else if ( !strcmp(grid_mapping, "Latitude_Longitude") ) {
00333       proj->name = strdup(grid_mapping);
00334       proj->grid_mapping_name = strdup("Latitude_Longitude");
00335       proj->latin1 = 0.0;
00336       proj->latin2 = 0.0;
00337       proj->lonc = 0.0;
00338       proj->lat0 = 0.0;
00339       proj->false_easting = 0.0;
00340       proj->false_northing = 0.0;
00341     }
00342     else if ( !strcmp(grid_mapping, "list") ) {
00343       proj->name = strdup(grid_mapping);
00344       proj->grid_mapping_name = strdup("list");
00345       proj->latin1 = 0.0;
00346       proj->latin2 = 0.0;
00347       proj->lonc = 0.0;
00348       proj->lat0 = 0.0;
00349       proj->false_easting = 0.0;
00350       proj->false_northing = 0.0;
00351     }
00352     else {
00353       (void) fprintf(stderr, "%s: WARNING: No projection parameter available for %s.\n", __FILE__, grid_mapping);
00354       (void) fprintf(stderr, "%s: WARNING: Assuming list of longitude and latitude points.\n", __FILE__);
00355       proj->name = strdup("list");
00356       proj->grid_mapping_name = strdup("list");
00357       proj->latin1 = 0.0;
00358       proj->latin2 = 0.0;
00359       proj->lonc = 0.0;
00360       proj->lat0 = 0.0;
00361       proj->false_easting = 0.0;
00362       proj->false_northing = 0.0;
00363     }
00364     (void) free(grid_mapping);
00365   }
00366   
00367   if (varndims == 2) {
00368     /* Allocate memory and set start and count */
00369     start[0] = 0;
00370     start[1] = 0;
00371     start[2] = 0;
00372     count[0] = (size_t) *nlat;
00373     count[1] = (size_t) *nlon;
00374     count[2] = 0;
00375     /* Allocate memory */
00376     (*buf) = (double *) malloc((*nlat)*(*nlon) * sizeof(double));
00377     if ((*buf) == NULL) alloc_error(__FILE__, __LINE__);
00378   }
00379   else if (varndims == 1) {
00380     /* Allocate memory and set start and count */
00381     start[0] = 0;
00382     start[1] = 0;
00383     start[2] = 0;
00384     count[0] = (size_t) npts;
00385     count[1] = 0;
00386     count[2] = 0;
00387     /* Allocate memory */
00388     (*buf) = (double *) malloc(npts * sizeof(double));
00389     if ((*buf) == NULL) alloc_error(__FILE__, __LINE__);
00390   }
00391 
00392   /* Read values from netCDF variable */
00393   istat = nc_get_vara_double(ncinid, varinid, start, count, *buf);
00394   if (istat != NC_NOERR) { handle_netcdf_error(istat, __FILE__, __LINE__); return -1; }
00395 
00396   /* Close the input netCDF file. */
00397   istat = ncclose(ncinid);
00398   if (istat != NC_NOERR) { handle_netcdf_error(istat, __FILE__, __LINE__); return -1; }
00399 
00400   /* Free memory */
00401   (void) free(tmpstr);
00402 
00403   /* Success status */
00404   return 0;
00405 }

int read_netcdf_var_3d ( double **  buf,
info_field_struct info_field,
proj_struct proj,
char *  filename,
char *  varname,
char *  dimxname,
char *  dimyname,
char *  timename,
int *  nlon,
int *  nlat,
int *  ntime,
int  outinfo 
)

Read a 3D variable in a NetCDF file, and return information in info_field_struct structure and proj_struct.

Parameters:
[out] buf 3D variable
[out] info_field Information about the output variable
[out] proj Information about the horizontal projection of the output variable
[in] filename NetCDF input filename
[in] varname NetCDF variable name
[in] dimxname Longitude dimension name
[in] dimyname Latitude dimension name
[in] timename Time dimension name
[out] nlon Longitude dimension length
[out] nlat Latitude dimension length
[out] ntime Time dimension length
[in] outinfo TRUE if we want information output, FALSE if not
Returns:
Status.

Read data variable

Definition at line 67 of file read_netcdf_var_3d.c.

References alloc_error(), info_field_struct::coordinates, proj_struct::false_easting, proj_struct::false_northing, info_field_struct::fillvalue, get_attribute_str(), info_field_struct::grid_mapping, proj_struct::grid_mapping_name, handle_netcdf_error(), info_field_struct::height, proj_struct::lat0, proj_struct::latin1, proj_struct::latin2, proj_struct::latpole, proj_struct::lonc, info_field_struct::long_name, proj_struct::lonpole, MAXPATH, proj_struct::name, TRUE, and info_field_struct::units.

Referenced by read_large_scale_eof(), read_large_scale_fields(), read_learning_fields(), and remove_clim().

00068                                                                                                                   {
00086   int istat; /* Diagnostic status */
00087 
00088   size_t dimval; /* Variable used to retrieve dimension length */
00089 
00090   int ncinid; /* NetCDF input file handle ID */
00091   int varinid; /* NetCDF variable ID */
00092   int projinid; /* Projection variable ID */
00093   nc_type vartype_main; /* Type of the variable (NC_FLOAT, NC_DOUBLE, etc.) */
00094   int varndims; /* Number of dimensions of variable */
00095   int vardimids[NC_MAX_VAR_DIMS]; /* Variable dimension ids */
00096   int timediminid; /* Time dimension ID */
00097   int londiminid; /* Longitude dimension ID */
00098   int latdiminid; /* Latitude dimension ID */
00099 
00100   size_t start[3]; /* Start position to read */
00101   size_t count[3]; /* Number of elements to read */
00102 
00103   float valf; /* Variable used to retrieve fillvalue */
00104   int vali; /* Variable used to retrieve integer values */
00105   char *tmpstr = NULL; /* Temporary string */
00106   size_t t_len; /* Length of string attribute */
00107 
00108   float *proj_latin = NULL; /* Parallel latitudes of projection */
00109   int npts = 0; /* Number of points for 1D variables */
00110   char *grid_mapping = NULL;
00111 
00112   /* Allocate memory */
00113   tmpstr = (char *) malloc(MAXPATH * sizeof(char));
00114   if (tmpstr == NULL) alloc_error(__FILE__, __LINE__);
00115 
00116   /* Read data in NetCDF file */
00117 
00118   /* Open NetCDF file for reading */
00119   if (outinfo == TRUE)
00120     printf("%s: Opening for reading NetCDF input file %s\n", __FILE__, filename);
00121   istat = nc_open(filename, NC_NOWRITE, &ncinid);  /* open for reading */
00122   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00123 
00124   if (outinfo == TRUE)
00125     printf("%s: READ %s %s\n", __FILE__, varname, filename);
00126 
00127   /* Get dimensions length */
00128   istat = nc_inq_dimid(ncinid, timename, &timediminid);  /* get ID for time dimension */
00129   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00130   istat = nc_inq_dimlen(ncinid, timediminid, &dimval); /* get time length */
00131   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00132   *ntime = (int) dimval;
00133 
00134   istat = nc_inq_dimid(ncinid, dimyname, &latdiminid);  /* get ID for lat dimension */
00135   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00136   istat = nc_inq_dimlen(ncinid, latdiminid, &dimval); /* get lat length */
00137   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00138   *nlat = (int) dimval;
00139 
00140   istat = nc_inq_dimid(ncinid, dimxname, &londiminid);  /* get ID for lon dimension */
00141   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00142   istat = nc_inq_dimlen(ncinid, londiminid, &dimval); /* get lon length */
00143   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00144   *nlon = (int) dimval;
00145   
00146   /* Get main variable ID */
00147   istat = nc_inq_varid(ncinid, varname, &varinid);
00148   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00149 
00152   /* Get variable information */
00153   istat = nc_inq_var(ncinid, varinid, (char *) NULL, &vartype_main, &varndims, vardimids, (int *) NULL);
00154   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00155 
00156   /* Verify that variable is really 3D or 2D */
00157   if (varndims != 3 && varndims != 2) {
00158     (void) fprintf(stderr, "%s: Error NetCDF type and/or dimensions nlon %d nlat %d.\n", __FILE__, *nlon, *nlat);
00159     (void) free(tmpstr);
00160     istat = ncclose(ncinid);
00161     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00162     return -1;
00163   }
00164 
00165   if (varndims == 2) {
00166     if ((*nlat) != (*nlon)) {
00167       (void) fprintf(stderr, "%s: Error NetCDF type and/or dimensions nlon %d nlat %d.\n", __FILE__, *nlon, *nlat);
00168       (void) free(tmpstr);
00169       istat = ncclose(ncinid);
00170       if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00171       return -1;
00172     }
00173     npts = *nlon;
00174     *nlat = 0;
00175   }
00176 
00177   /* If info_field si not NULL, get some information about the read variable */
00178   if (info_field != NULL) {
00179     /* Get missing value */
00180     if (vartype_main == NC_FLOAT) {
00181       istat = nc_get_att_float(ncinid, varinid, "missing_value", &valf);
00182       if (istat != NC_NOERR)
00183         info_field->fillvalue = -9999.0;
00184       else
00185         info_field->fillvalue = (double) valf;
00186     }
00187     else if (vartype_main == NC_DOUBLE) {
00188       istat = nc_get_att_double(ncinid, varinid, "missing_value", &(info_field->fillvalue));
00189       if (istat != NC_NOERR)
00190         info_field->fillvalue = -9999.0;
00191     }
00192 
00193     /* Get coordinates */
00194     istat = nc_inq_attlen(ncinid, varinid, "coordinates", &t_len);
00195     if (istat == NC_NOERR) {
00196       istat = nc_get_att_text(ncinid, varinid, "coordinates", tmpstr);
00197       if (istat == NC_NOERR) {
00198         if (tmpstr[t_len-1] != '\0')
00199           tmpstr[t_len] = '\0';
00200         info_field->coordinates = strdup(tmpstr);
00201       }
00202       else
00203         info_field->coordinates = strdup("lon lat");
00204     }
00205     else
00206       info_field->coordinates = strdup("lon lat");
00207 
00208     /* Get grid projection */
00209     istat = nc_inq_attlen(ncinid, varinid, "grid_mapping", &t_len);
00210     if (istat == NC_NOERR) {
00211       handle_netcdf_error(istat, __FILE__, __LINE__);
00212       istat = nc_get_att_text(ncinid, varinid, "grid_mapping", tmpstr);
00213       if (istat == NC_NOERR) {
00214         if (tmpstr[t_len-1] != '\0')
00215           tmpstr[t_len] = '\0';
00216         info_field->grid_mapping = strdup(tmpstr);
00217       }
00218       else
00219         info_field->grid_mapping = strdup("unknown");
00220     }
00221     else
00222       info_field->grid_mapping = strdup("unknown");
00223 
00224     /* Get units */
00225     istat = nc_inq_attlen(ncinid, varinid, "units", &t_len);
00226     if (istat == NC_NOERR) {
00227       handle_netcdf_error(istat, __FILE__, __LINE__);
00228       istat = nc_get_att_text(ncinid, varinid, "units", tmpstr);
00229       if (istat == NC_NOERR) {
00230         if (tmpstr[t_len-1] != '\0')
00231           tmpstr[t_len] = '\0';
00232         info_field->units = strdup(tmpstr);
00233       }
00234       else
00235         info_field->units = strdup("unknown");
00236     }
00237     else
00238       info_field->units = strdup("unknown");
00239 
00240     /* Get height */
00241     istat = nc_inq_attlen(ncinid, varinid, "height", &t_len);
00242     if (istat == NC_NOERR) {
00243       handle_netcdf_error(istat, __FILE__, __LINE__);
00244       istat = nc_get_att_text(ncinid, varinid, "height", tmpstr);
00245       if (istat == NC_NOERR) {
00246         if (tmpstr[t_len-1] != '\0')
00247           tmpstr[t_len] = '\0';
00248         info_field->height = strdup(tmpstr);
00249       }
00250       else
00251         info_field->height = strdup("unknown");
00252     }
00253     else
00254       info_field->height = strdup("unknown");
00255 
00256     /* Get long name */
00257     istat = nc_inq_attlen(ncinid, varinid, "long_name", &t_len);
00258     if (istat == NC_NOERR) {
00259       handle_netcdf_error(istat, __FILE__, __LINE__);
00260       istat = nc_get_att_text(ncinid, varinid, "long_name", tmpstr);
00261       if (istat == NC_NOERR) {
00262         if (tmpstr[t_len-1] != '\0')
00263           tmpstr[t_len] = '\0';
00264         info_field->long_name = strdup(tmpstr);
00265       }
00266       else
00267         info_field->long_name = strdup(varname);
00268     }
00269     else
00270       info_field->long_name = strdup(varname);
00271   }
00272 
00273   /* if proj is not NULL, retrieve informations about the horizontal projection parameters */
00274   if (proj != NULL) {
00275     if (info_field == NULL)
00276       grid_mapping = strdup("unknown");
00277     else
00278       grid_mapping = strdup(info_field->grid_mapping);
00279     /* Get projection variable ID */
00280     if ( !strcmp(grid_mapping, "Lambert_Conformal") ) {
00281       istat = nc_inq_varid(ncinid, grid_mapping, &projinid); /* get projection variable ID */
00282       if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00283     }
00284     else if ( !strcmp(grid_mapping, "rotated_latitude_longitude") ) {
00285       istat = nc_inq_varid(ncinid, grid_mapping, &projinid); /* get projection variable ID */
00286       if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00287     }
00288     if (proj->name != NULL) {
00289       if ( strcmp(grid_mapping, "Lambert_Conformal") && strcmp(grid_mapping, "Latitude_Longitude") &&
00290            ( !strcmp(proj->name, "Latitude_Longitude") || !strcmp(proj->name, "Lambert_Conformal") ) ) {
00291         (void) free(grid_mapping);
00292         grid_mapping = strdup(proj->name);
00293       }
00294       else if ( strcmp(grid_mapping, "rotated_latitude_longitude") && !strcmp(proj->name, "rotated_pole") ) {
00295         (void) free(grid_mapping);
00296         grid_mapping = strdup("rotated_latitude_longitude");
00297       }
00298     }
00299     if ( !strcmp(grid_mapping, "Lambert_Conformal") ) {
00300       /*              int Lambert_Conformal ;
00301                       Lambert_Conformal:grid_mapping_name = "lambert_conformal_conic" ;
00302                       Lambert_Conformal:standard_parallel = 45.89892f, 47.69601f ;
00303                       Lambert_Conformal:longitude_of_central_meridian = 2.337229f ;
00304                       Lambert_Conformal:latitude_of_projection_origin = 46.8f ;
00305                       Lambert_Conformal:false_easting = 600000.f ;
00306                       Lambert_Conformal:false_northing = 2200000.f ;
00307       */
00308       istat = nc_get_var1_int(ncinid, projinid, 0, &vali);
00309       if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);  
00310       if (proj->name != NULL)
00311         (void) free(proj->name);
00312       proj->name = strdup(grid_mapping);
00313       if (proj->grid_mapping_name != NULL)
00314         (void) free(proj->grid_mapping_name);
00315       istat = get_attribute_str(&(proj->grid_mapping_name), ncinid, projinid, "grid_mapping_name");
00316     
00317       proj_latin = (float *) malloc(2 * sizeof(float));
00318       if (proj_latin == NULL) alloc_error(__FILE__, __LINE__);
00319       istat = nc_get_att_float(ncinid, projinid, "standard_parallel", proj_latin);
00320       proj->latin1 = (double) proj_latin[0];
00321       proj->latin2 = (double) proj_latin[1];
00322       (void) free(proj_latin);
00323     
00324       istat = nc_get_att_double(ncinid, projinid, "longitude_of_central_meridian", &(proj->lonc));
00325       istat = nc_get_att_double(ncinid, projinid, "latitude_of_projection_origin", &(proj->lat0));
00326       istat = nc_get_att_double(ncinid, projinid, "false_easting", &(proj->false_easting));
00327       istat = nc_get_att_double(ncinid, projinid, "false_northing", &(proj->false_northing));
00328     
00329     }
00330     else if ( !strcmp(grid_mapping, "rotated_latitude_longitude") ) {
00331       /*        int rotated_pole ;
00332                 rotated_pole:grid_mapping_name = "rotated_latitude_longitude" ;
00333                 rotated_pole:grid_north_pole_latitude = 39.25 ;
00334                 rotated_pole:grid_north_pole_longitude = -162. ;
00335       */
00336       istat = nc_get_var1_int(ncinid, projinid, 0, &vali);
00337       if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);  
00338       if (proj->name != NULL)
00339         (void) free(proj->name);
00340       proj->name = strdup(grid_mapping);
00341       if (proj->grid_mapping_name != NULL)
00342         (void) free(proj->grid_mapping_name);
00343       istat = get_attribute_str(&(proj->grid_mapping_name), ncinid, projinid, "grid_mapping_name");
00344     
00345       istat = nc_get_att_double(ncinid, projinid, "grid_north_pole_latitude", &(proj->latpole));
00346       istat = nc_get_att_double(ncinid, projinid, "grid_north_pole_longitude", &(proj->lonpole));
00347     
00348     }
00349     else if ( !strcmp(grid_mapping, "Latitude_Longitude") ) {
00350       if (proj->name != NULL)
00351         (void) free(proj->name);
00352       proj->name = strdup(grid_mapping);
00353       if (proj->grid_mapping_name != NULL)
00354         (void) free(proj->grid_mapping_name);
00355       proj->grid_mapping_name = strdup("Latitude_Longitude");
00356       proj->latin1 = 0.0;
00357       proj->latin2 = 0.0;
00358       proj->lonc = 0.0;
00359       proj->lat0 = 0.0;
00360       proj->false_easting = 0.0;
00361       proj->false_northing = 0.0;
00362     }
00363     else if ( !strcmp(grid_mapping, "list") ) {
00364       if (proj->name != NULL)
00365         (void) free(proj->name);
00366       proj->name = strdup(grid_mapping);
00367       if (proj->grid_mapping_name != NULL)
00368         (void) free(proj->grid_mapping_name);
00369       proj->grid_mapping_name = strdup("list");
00370       proj->latin1 = 0.0;
00371       proj->latin2 = 0.0;
00372       proj->lonc = 0.0;
00373       proj->lat0 = 0.0;
00374       proj->false_easting = 0.0;
00375       proj->false_northing = 0.0;
00376     }
00377     else {
00378       (void) fprintf(stderr, "%s: WARNING: No projection parameter available for %s.\n", __FILE__, grid_mapping);
00379       (void) fprintf(stderr, "%s: WARNING: Assuming list of longitude and latitude points.\n", __FILE__);
00380       if (proj->name != NULL)
00381         (void) free(proj->name);
00382       proj->name = strdup("list");
00383       if (proj->grid_mapping_name != NULL)
00384         (void) free(proj->grid_mapping_name);
00385       proj->grid_mapping_name = strdup("list");
00386       proj->latin1 = 0.0;
00387       proj->latin2 = 0.0;
00388       proj->lonc = 0.0;
00389       proj->lat0 = 0.0;
00390       proj->false_easting = 0.0;
00391       proj->false_northing = 0.0;
00392     }
00393     (void) free(grid_mapping);
00394   }
00395 
00396   if (varndims == 3) {
00397     /* Allocate memory and set start and count */
00398     start[0] = 0;
00399     start[1] = 0;
00400     start[2] = 0;
00401     count[0] = (size_t) *ntime;
00402     count[1] = (size_t) *nlat;
00403     count[2] = (size_t) *nlon;
00404     /* Allocate memory */
00405     (*buf) = (double *) malloc((*nlat)*(*nlon)*(*ntime) * sizeof(double));
00406     if ((*buf) == NULL) alloc_error(__FILE__, __LINE__);
00407   }
00408   else if (varndims == 2) {
00409     /* Allocate memory and set start and count */
00410     start[0] = 0;
00411     start[1] = 0;
00412     start[2] = 0;
00413     count[0] = (size_t) *ntime;
00414     count[1] = (size_t) npts;
00415     count[2] = 0;
00416     /* Allocate memory */
00417     (*buf) = (double *) malloc(npts*(*ntime) * sizeof(double));
00418     if ((*buf) == NULL) alloc_error(__FILE__, __LINE__);
00419   }
00420 
00421   /* Read values from netCDF variable */
00422   istat = nc_get_vara_double(ncinid, varinid, start, count, *buf);
00423   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00424 
00425   /* Close the input netCDF file. */
00426   istat = ncclose(ncinid);
00427   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00428 
00429   /* Free memory */
00430   (void) free(tmpstr);
00431 
00432   /* Success status */
00433   return 0;
00434 }

int read_netcdf_var_3d_2d ( double **  buf,
info_field_struct info_field,
proj_struct proj,
char *  filename,
char *  varname,
char *  dimxname,
char *  dimyname,
char *  timename,
int  t,
int *  nlon,
int *  nlat,
int *  ntime,
int  outinfo 
)

Read a 2D field from a 3D variable in a NetCDF file, and return information in info_field_struct structure and proj_struct.

Parameters:
[out] buf 2D variable
[out] info_field Information about the output variable
[out] proj Information about the horizontal projection of the output variable
[in] filename NetCDF input filename
[in] varname NetCDF variable name
[in] dimxname Longitude dimension name
[in] dimyname Latitude dimension name
[in] timename Time dimension name
[in] t Time index to retrieve
[out] nlon Longitude dimension length
[out] nlat Latitude dimension length
[out] ntime Time dimension length
[in] outinfo TRUE if we want information output, FALSE if not
Returns:
Status.

Read data variable

Definition at line 68 of file read_netcdf_var_3d_2d.c.

References alloc_error(), info_field_struct::coordinates, proj_struct::false_easting, proj_struct::false_northing, info_field_struct::fillvalue, get_attribute_str(), info_field_struct::grid_mapping, proj_struct::grid_mapping_name, handle_netcdf_error(), info_field_struct::height, proj_struct::lat0, proj_struct::latin1, proj_struct::latin2, proj_struct::latpole, proj_struct::lonc, info_field_struct::long_name, proj_struct::lonpole, MAXPATH, proj_struct::name, TRUE, and info_field_struct::units.

Referenced by output_downscaled_analog(), read_field_subdomain_period(), and read_obs_period().

00069                                                                                                                             {
00088   int istat; /* Diagnostic status */
00089 
00090   size_t dimval; /* Variable used to retrieve dimension length */
00091 
00092   int ncinid; /* NetCDF input file handle ID */
00093   int varinid; /* NetCDF variable ID */
00094   int projinid; /* Projection variable ID */
00095   nc_type vartype_main; /* Type of the variable (NC_FLOAT, NC_DOUBLE, etc.) */
00096   int varndims; /* Number of dimensions of variable */
00097   int vardimids[NC_MAX_VAR_DIMS]; /* Variable dimension ids */
00098   int timediminid; /* Time dimension ID */
00099   int londiminid; /* Longitude dimension ID */
00100   int latdiminid; /* Latitude dimension ID */
00101 
00102   size_t start[3]; /* Start position to read */
00103   size_t count[3]; /* Number of elements to read */
00104 
00105   float valf; /* Variable used to retrieve fillvalue */
00106   int vali; /* Variable used to retrieve integer values */
00107   char *tmpstr = NULL; /* Temporary string */
00108   size_t t_len; /* Length of string attribute */
00109 
00110   float *proj_latin = NULL; /* Parallel latitudes of projection */
00111   int npts = 0; /* Number of points for 1D variables */
00112   char *grid_mapping = NULL;
00113 
00114   /* Allocate memory */
00115   tmpstr = (char *) malloc(MAXPATH * sizeof(char));
00116   if (tmpstr == NULL) alloc_error(__FILE__, __LINE__);
00117 
00118   /* Read data in NetCDF file */
00119 
00120   /* Open NetCDF file for reading */
00121   if (outinfo == TRUE)
00122     printf("%s: Opening for reading NetCDF input file %s\n", __FILE__, filename);
00123   istat = nc_open(filename, NC_NOWRITE, &ncinid);  /* open for reading */
00124   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00125 
00126   /* Get dimensions length */
00127   istat = nc_inq_dimid(ncinid, timename, &timediminid);  /* get ID for time dimension */
00128   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00129   istat = nc_inq_dimlen(ncinid, timediminid, &dimval); /* get time length */
00130   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00131   *ntime = (int) dimval;
00132   /* Verify timestep provided */
00133   if (t < 0 || t > ((*ntime)-1)) {
00134     (void) free(tmpstr);
00135     istat = ncclose(ncinid);
00136     (void) fprintf(stderr, "%s: Invalid timestep provided: %d. Maximum value is %d\n", __FILE__, t, *ntime);
00137     return -1;
00138   }
00139 
00140   if (outinfo == TRUE)
00141     printf("%s: READ %s %s time=%d %d.\n", __FILE__, varname, filename, t, *ntime);
00142 
00143   istat = nc_inq_dimid(ncinid, dimyname, &latdiminid);  /* get ID for lat dimension */
00144   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00145   istat = nc_inq_dimlen(ncinid, latdiminid, &dimval); /* get lat length */
00146   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00147   *nlat = (int) dimval;
00148 
00149   istat = nc_inq_dimid(ncinid, dimxname, &londiminid);  /* get ID for lon dimension */
00150   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00151   istat = nc_inq_dimlen(ncinid, londiminid, &dimval); /* get lon length */
00152   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00153   *nlon = (int) dimval;
00154 
00155   /* Get main variable ID */
00156   istat = nc_inq_varid(ncinid, varname, &varinid);
00157   if (istat != NC_NOERR) {
00158     (void) fprintf(stderr, "%s: Error with variable %s in file %s\n", __FILE__, varname, filename);
00159     handle_netcdf_error(istat, __FILE__, __LINE__);
00160   }
00161 
00164   /* Get variable information */
00165   istat = nc_inq_var(ncinid, varinid, (char *) NULL, &vartype_main, &varndims, vardimids, (int *) NULL);
00166   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00167 
00168   /* Verify that variable is really 3D or 2D */
00169   if (varndims != 3 && varndims != 2) {
00170     (void) fprintf(stderr, "%s: Error NetCDF type and/or dimensions nlon %d nlat %d.\n", __FILE__, *nlon, *nlat);
00171     (void) free(tmpstr);
00172     istat = ncclose(ncinid);
00173     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00174     return -1;
00175   }
00176 
00177   if (varndims == 2) {
00178     if ((*nlat) != (*nlon)) {
00179       (void) fprintf(stderr, "%s: Error NetCDF type and/or dimensions nlon %d nlat %d.\n", __FILE__, *nlon, *nlat);
00180       (void) free(tmpstr);
00181       istat = ncclose(ncinid);
00182       if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00183       return -1;
00184     }
00185     npts = *nlon;
00186     *nlat = 0;
00187   }
00188 
00189   /* If info_field si not NULL, get some information about the read variable */
00190   if (info_field != NULL) {
00191     /* Get missing value */
00192     if (vartype_main == NC_FLOAT) {
00193       istat = nc_get_att_float(ncinid, varinid, "missing_value", &valf);
00194       if (istat != NC_NOERR)
00195         info_field->fillvalue = -9999.0;
00196       else
00197         info_field->fillvalue = (double) valf;
00198     }
00199     else if (vartype_main == NC_DOUBLE) {
00200       istat = nc_get_att_double(ncinid, varinid, "missing_value", &(info_field->fillvalue));
00201       if (istat != NC_NOERR)
00202         info_field->fillvalue = -9999.0;
00203     }
00204 
00205     /* Get coordinates */
00206     istat = nc_inq_attlen(ncinid, varinid, "coordinates", &t_len);
00207     if (istat == NC_NOERR) {
00208       istat = nc_get_att_text(ncinid, varinid, "coordinates", tmpstr);
00209       if (istat == NC_NOERR) {
00210         if (tmpstr[t_len-1] != '\0')
00211           tmpstr[t_len] = '\0';
00212         info_field->coordinates = strdup(tmpstr);
00213       }
00214       else
00215         info_field->coordinates = strdup("lon lat");
00216     }
00217     else
00218       info_field->coordinates = strdup("lon lat");
00219 
00220     /* Get grid projection */
00221     istat = nc_inq_attlen(ncinid, varinid, "grid_mapping", &t_len);
00222     if (istat == NC_NOERR) {
00223       handle_netcdf_error(istat, __FILE__, __LINE__);
00224       istat = nc_get_att_text(ncinid, varinid, "grid_mapping", tmpstr);
00225       if (istat == NC_NOERR) {
00226         if (tmpstr[t_len-1] != '\0')
00227           tmpstr[t_len] = '\0';
00228         info_field->grid_mapping = strdup(tmpstr);
00229       }
00230       else
00231         info_field->grid_mapping = strdup("unknown");
00232     }
00233     else
00234       info_field->grid_mapping = strdup("unknown");
00235 
00236     /* Get units */
00237     istat = nc_inq_attlen(ncinid, varinid, "units", &t_len);
00238     if (istat == NC_NOERR) {
00239       handle_netcdf_error(istat, __FILE__, __LINE__);
00240       istat = nc_get_att_text(ncinid, varinid, "units", tmpstr);
00241       if (istat == NC_NOERR) {
00242         if (tmpstr[t_len-1] != '\0')
00243           tmpstr[t_len] = '\0';
00244         info_field->units = strdup(tmpstr);
00245       }
00246       else
00247         info_field->units = strdup("unknown");
00248     }
00249     else
00250       info_field->units = strdup("unknown");
00251 
00252     /* Get height */
00253     istat = nc_inq_attlen(ncinid, varinid, "height", &t_len);
00254     if (istat == NC_NOERR) {
00255       handle_netcdf_error(istat, __FILE__, __LINE__);
00256       istat = nc_get_att_text(ncinid, varinid, "height", tmpstr);
00257       if (istat == NC_NOERR) {
00258         if (tmpstr[t_len-1] != '\0')
00259           tmpstr[t_len] = '\0';
00260         info_field->height = strdup(tmpstr);
00261       }
00262       else
00263         info_field->height = strdup("unknown");
00264     }
00265     else
00266       info_field->height = strdup("unknown");
00267 
00268     /* Get long name */
00269     istat = nc_inq_attlen(ncinid, varinid, "long_name", &t_len);
00270     if (istat == NC_NOERR) {
00271       handle_netcdf_error(istat, __FILE__, __LINE__);
00272       istat = nc_get_att_text(ncinid, varinid, "long_name", tmpstr);
00273       if (istat == NC_NOERR) {
00274         if (tmpstr[t_len-1] != '\0')
00275           tmpstr[t_len] = '\0';
00276         info_field->long_name = strdup(tmpstr);
00277       }
00278       else
00279         info_field->long_name = strdup(varname);
00280     }
00281     else
00282       info_field->long_name = strdup(varname);
00283   }
00284 
00285   /* if proj is not NULL, retrieve informations about the horizontal projection parameters */
00286   if (proj != NULL) {
00287     if (info_field == NULL)
00288       grid_mapping = strdup("unknown");
00289     else
00290       grid_mapping = strdup(info_field->grid_mapping);
00291     /* Get projection variable ID */
00292     if ( !strcmp(grid_mapping, "Lambert_Conformal") ) {
00293       istat = nc_inq_varid(ncinid, grid_mapping, &projinid); /* get projection variable ID */
00294       if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00295     }
00296     else if ( !strcmp(grid_mapping, "rotated_latitude_longitude") ) {
00297       istat = nc_inq_varid(ncinid, grid_mapping, &projinid); /* get projection variable ID */
00298       if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00299     }
00300     if (proj->name != NULL) {
00301       if ( strcmp(grid_mapping, "Lambert_Conformal") && strcmp(grid_mapping, "Latitude_Longitude") &&
00302            ( !strcmp(proj->name, "Latitude_Longitude") || !strcmp(proj->name, "Lambert_Conformal") ) ) {
00303         (void) free(grid_mapping);
00304         grid_mapping = strdup(proj->name);
00305       }
00306       else if ( strcmp(grid_mapping, "rotated_latitude_longitude") && !strcmp(proj->name, "rotated_pole") ) {
00307         (void) free(grid_mapping);
00308         grid_mapping = strdup("rotated_latitude_longitude");
00309       }
00310     }
00311     if ( !strcmp(grid_mapping, "Lambert_Conformal") ) {
00312       /*              int Lambert_Conformal ;
00313                       Lambert_Conformal:grid_mapping_name = "lambert_conformal_conic" ;
00314                       Lambert_Conformal:standard_parallel = 45.89892f, 47.69601f ;
00315                       Lambert_Conformal:longitude_of_central_meridian = 2.337229f ;
00316                       Lambert_Conformal:latitude_of_projection_origin = 46.8f ;
00317                       Lambert_Conformal:false_easting = 600000.f ;
00318                       Lambert_Conformal:false_northing = 2200000.f ;
00319       */
00320       istat = nc_get_var1_int(ncinid, projinid, 0, &vali);
00321       if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);  
00322       proj->name = strdup(grid_mapping);
00323       istat = get_attribute_str(&(proj->grid_mapping_name), ncinid, projinid, "grid_mapping_name");
00324     
00325       proj_latin = (float *) malloc(2 * sizeof(float));
00326       if (proj_latin == NULL) alloc_error(__FILE__, __LINE__);
00327       istat = nc_get_att_float(ncinid, projinid, "standard_parallel", proj_latin);
00328       proj->latin1 = (double) proj_latin[0];
00329       proj->latin2 = (double) proj_latin[1];
00330       (void) free(proj_latin);
00331     
00332       istat = nc_get_att_double(ncinid, projinid, "longitude_of_central_meridian", &(proj->lonc));
00333       istat = nc_get_att_double(ncinid, projinid, "latitude_of_projection_origin", &(proj->lat0));
00334       istat = nc_get_att_double(ncinid, projinid, "false_easting", &(proj->false_easting));
00335       istat = nc_get_att_double(ncinid, projinid, "false_northing", &(proj->false_northing));
00336     
00337     }
00338     else if ( !strcmp(grid_mapping, "rotated_latitude_longitude") ) {
00339       /*        char rotated_pole ;
00340                 rotated_pole:grid_mapping_name = "rotated_latitude_longitude" ;
00341                 rotated_pole:grid_north_pole_latitude = 39.25 ;
00342                 rotated_pole:grid_north_pole_longitude = -162. ;
00343       */
00344       istat = nc_get_var1_int(ncinid, projinid, 0, &vali);
00345       if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);  
00346       proj->name = strdup("rotated_pole");
00347       istat = get_attribute_str(&(proj->grid_mapping_name), ncinid, projinid, "grid_mapping_name");
00348     
00349       istat = nc_get_att_double(ncinid, projinid, "grid_north_pole_latitude", &(proj->latpole));
00350       istat = nc_get_att_double(ncinid, projinid, "grid_north_pole_longitude", &(proj->lonpole));
00351     
00352     }
00353     else if ( !strcmp(grid_mapping, "Latitude_Longitude") ) {
00354       proj->name = strdup(grid_mapping);
00355       proj->grid_mapping_name = strdup("Latitude_Longitude");
00356       proj->latin1 = 0.0;
00357       proj->latin2 = 0.0;
00358       proj->lonc = 0.0;
00359       proj->lat0 = 0.0;
00360       proj->false_easting = 0.0;
00361       proj->false_northing = 0.0;
00362     }
00363     else if ( !strcmp(grid_mapping, "list") ) {
00364       proj->name = strdup(grid_mapping);
00365       proj->grid_mapping_name = strdup("list");
00366       proj->latin1 = 0.0;
00367       proj->latin2 = 0.0;
00368       proj->lonc = 0.0;
00369       proj->lat0 = 0.0;
00370       proj->false_easting = 0.0;
00371       proj->false_northing = 0.0;
00372     }
00373     else {
00374       (void) fprintf(stderr, "%s: WARNING: No projection parameter available for %s.\n", __FILE__, grid_mapping);
00375       (void) fprintf(stderr, "%s: WARNING: Assuming list of longitude and latitude points.\n", __FILE__);
00376       proj->name = strdup("list");
00377       proj->grid_mapping_name = strdup("list");
00378       proj->latin1 = 0.0;
00379       proj->latin2 = 0.0;
00380       proj->lonc = 0.0;
00381       proj->lat0 = 0.0;
00382       proj->false_easting = 0.0;
00383       proj->false_northing = 0.0;
00384     }
00385     (void) free(grid_mapping);
00386   }
00387 
00388   if (varndims == 3) {
00389     /* Allocate memory and set start and count */
00390     start[0] = t;
00391     start[1] = 0; /* Timestep to retrieve */
00392     start[2] = 0;
00393     count[0] = 1; /* Only get one timestep */
00394     count[1] = (size_t) *nlat;
00395     count[2] = (size_t) *nlon;
00396     /* Allocate memory */
00397     (*buf) = (double *) malloc((*nlat)*(*nlon) * sizeof(double));
00398     if ((*buf) == NULL) alloc_error(__FILE__, __LINE__);
00399   }
00400   else if (varndims == 2) {
00401     /* Allocate memory and set start and count */
00402     start[0] = t;
00403     start[1] = 0; /* Timestep to retrieve */
00404     start[2] = 0;
00405     count[0] = 1; /* Only get one timestep */
00406     count[1] = (size_t) npts; /* List of latitude+longitude points only */
00407     count[2] = 0;
00408     /* Allocate memory */
00409     (*buf) = (double *) malloc(npts * sizeof(double));
00410     if ((*buf) == NULL) alloc_error(__FILE__, __LINE__);
00411   }
00412 
00413   /* Read values from netCDF variable */
00414   istat = nc_get_vara_double(ncinid, varinid, start, count, *buf);
00415   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00416 
00417   /* Close the input netCDF file. */
00418   istat = ncclose(ncinid);
00419   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00420 
00421   /* Free memory */
00422   (void) free(tmpstr);
00423 
00424   /* Success status */
00425   return 0;
00426 }

int read_netcdf_var_generic_val ( double *  buf,
info_field_struct info_field,
char *  filename,
char *  varname,
int  index 
)

Read a NetCDF variable scalar double at the index position, and return information in info_field_struct structure.

Parameters:
[out] buf Scalar double value
[out] info_field Information about the output variable
[in] filename NetCDF input filename
[in] varname NetCDF variable name
[in] index Index position in the variable to retrieve
Returns:
Status.

Definition at line 66 of file read_netcdf_var_generic_val.c.

References alloc_error(), info_field_struct::fillvalue, handle_netcdf_error(), info_field_struct::height, info_field_struct::long_name, MAXPATH, and info_field_struct::units.

Referenced by read_learning_fields().

00066                                                                                                                   {
00077   int istat; /* Diagnostic status */
00078 
00079   int ncinid; /* NetCDF input file handle ID */
00080   int varinid; /* NetCDF variable ID */
00081   nc_type vartype_main; /* Type of the variable (NC_FLOAT, NC_DOUBLE, etc.) */
00082   int varndims; /* Number of dimensions of variable */
00083   int vardimids[NC_MAX_VAR_DIMS]; /* Variable dimension ids */
00084 
00085   float valf; /* Variable used to retrieve fillvalue */
00086   char *tmpstr = NULL; /* Temporary string */
00087   size_t t_len; /* Length of string attribute */
00088   size_t *idx; /* Index position */
00089 
00090   /* Allocate memory and set index to input parameter value */
00091   idx = (size_t *) malloc(sizeof(size_t));
00092   if (idx == NULL) alloc_error(__FILE__, __LINE__);
00093   idx[0] = (size_t) index;
00094 
00095   /* Allocate memory */
00096   tmpstr = (char *) malloc(MAXPATH * sizeof(char));
00097   if (tmpstr == NULL) alloc_error(__FILE__, __LINE__);
00098 
00099   /* Read data in NetCDF file */
00100 
00101   /* Open NetCDF file for reading */
00102   printf("%s: Opening for reading NetCDF input file %s\n", __FILE__, filename);
00103   istat = nc_open(filename, NC_NOWRITE, &ncinid);  /* open for reading */
00104   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00105 
00106   printf("%s: READ %s %s\n", __FILE__, varname, filename);
00107 
00108   /* Get main variable ID */
00109   istat = nc_inq_varid(ncinid, varname, &varinid);
00110   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00111 
00112   /* Get variable information */
00113   istat = nc_inq_var(ncinid, varinid, (char *) NULL, &vartype_main, &varndims, vardimids, (int *) NULL);
00114   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00115 
00116   /* If info_field si not NULL, get some information about the read variable */
00117   if (info_field != NULL) {
00118     /* Get missing value */
00119     if (vartype_main == NC_FLOAT) {
00120       istat = nc_get_att_float(ncinid, varinid, "missing_value", &valf);
00121       if (istat != NC_NOERR)
00122         info_field->fillvalue = -9999.0;
00123       else
00124         info_field->fillvalue = (double) valf;
00125     }
00126     else if (vartype_main == NC_DOUBLE) {
00127       istat = nc_get_att_double(ncinid, varinid, "missing_value", &(info_field->fillvalue));
00128       if (istat != NC_NOERR)
00129         info_field->fillvalue = -9999.0;
00130     }
00131 
00132     /* Get units */
00133     istat = nc_inq_attlen(ncinid, varinid, "units", &t_len);
00134     if (istat == NC_NOERR) {
00135       handle_netcdf_error(istat, __FILE__, __LINE__);
00136       istat = nc_get_att_text(ncinid, varinid, "units", tmpstr);
00137       if (istat == NC_NOERR) {
00138         if (tmpstr[t_len-1] != '\0')
00139           tmpstr[t_len] = '\0';
00140         info_field->units = strdup(tmpstr);
00141       }
00142       else
00143         info_field->units = strdup("unknown");
00144     }
00145     else
00146       info_field->units = strdup("unknown");
00147 
00148     /* Get height */
00149     istat = nc_inq_attlen(ncinid, varinid, "height", &t_len);
00150     if (istat == NC_NOERR) {
00151       handle_netcdf_error(istat, __FILE__, __LINE__);
00152       istat = nc_get_att_text(ncinid, varinid, "height", tmpstr);
00153       if (istat == NC_NOERR) {
00154         if (tmpstr[t_len-1] != '\0')
00155           tmpstr[t_len] = '\0';
00156         info_field->height = strdup(tmpstr);
00157       }
00158       else
00159         info_field->height = strdup("unknown");
00160     }
00161     else
00162       info_field->height = strdup("unknown");
00163 
00164     /* Get long name */
00165     istat = nc_inq_attlen(ncinid, varinid, "long_name", &t_len);
00166     if (istat == NC_NOERR) {
00167       handle_netcdf_error(istat, __FILE__, __LINE__);
00168       istat = nc_get_att_text(ncinid, varinid, "long_name", tmpstr);
00169       if (istat == NC_NOERR) {
00170         if (tmpstr[t_len-1] != '\0')
00171           tmpstr[t_len] = '\0';
00172         info_field->long_name = strdup(tmpstr);
00173       }
00174       else
00175         info_field->long_name = strdup(varname);
00176     }
00177     else
00178       info_field->long_name = strdup(varname);
00179   }
00180 
00181   /* Read values from netCDF variable */
00182   istat = nc_get_var1_double(ncinid, varinid, idx, buf);
00183   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00184 
00185   /* Close the input netCDF file. */
00186   istat = ncclose(ncinid);
00187   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00188 
00189   /* Free memory */
00190   (void) free(tmpstr);
00191   (void) free(idx);
00192 
00193   /* Success status */
00194   return 0;
00195 }

int read_netcdf_xy ( double **  x,
double **  y,
int *  nx,
int *  ny,
char *  xname,
char *  yname,
char *  dimxname,
char *  dimyname,
char *  filename 
)

Read X and Y dimensions in a NetCDF file.

Parameters:
[out] x X field
[out] y Y field
[out] nx X dimension
[out] ny Y dimension
[in] xname X variable name
[in] yname Y variable name
[in] dimxname X dimension name
[in] dimyname Y dimension name
[in] filename Input NetCDF filename
Returns:
Status.

1D dimensions y and x dimensions

Read dimensions variables

Definition at line 66 of file read_netcdf_xy.c.

References alloc_error(), and handle_netcdf_error().

Referenced by output_downscaled_analog().

00066                                                                                                                                    {
00081   int istat; /* Diagnostic status */
00082 
00083   size_t dimval; /* Variable used to retrieve dimension length */
00084 
00085   int ncinid; /* NetCDF input file handle ID */
00086   int yinid; /* Y variable ID */
00087   int xinid; /* X variable ID */
00088   nc_type vartype; /* Type of the variable (NC_FLOAT, NC_DOUBLE, etc.) */
00089   int varndims; /* Number of dimensions of variable */
00090   int vardimids[NC_MAX_VAR_DIMS]; /* Variable dimension ids */
00091   int xdiminid; /* X dimension ID */
00092   int ydiminid; /* Y dimension ID */
00093 
00094   size_t start[3]; /* Start position to read */
00095   size_t count[3]; /* Number of elements to read */
00096 
00097   int ndims; /* Number of dimensions of y and x variables */
00098 
00099   /* Read data in NetCDF file */
00100 
00101   /* Open NetCDF file for reading */
00102   printf("%s: Reading info from NetCDF input file %s\n", __FILE__, filename);
00103   istat = nc_open(filename, NC_NOWRITE, &ncinid);  /* open for reading */
00104   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00105 
00107   ndims = 1;
00108   
00109   /* Get dimensions length */
00110   istat = nc_inq_dimid(ncinid, dimyname, &ydiminid);  /* get ID for Y dimension */
00111   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00112   istat = nc_inq_dimlen(ncinid, ydiminid, &dimval); /* get y length */
00113   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00114   *ny = (int) dimval;
00115   
00116   istat = nc_inq_dimid(ncinid, dimxname, &xdiminid);  /* get ID for X dimension */
00117   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00118   istat = nc_inq_dimlen(ncinid, xdiminid, &dimval); /* get x length */
00119   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00120   *nx = (int) dimval;
00121   
00122   /* Get dimension ID */
00123   istat = nc_inq_varid(ncinid, yname, &yinid);  /* get ID for y variable */
00124   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00125   /* Get y dimensions and type */
00126   istat = nc_inq_var(ncinid, yinid, (char *) NULL, &vartype, &varndims, vardimids, (int *) NULL); /* get variable information */
00127   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);  
00128   if (varndims != ndims) {
00129     (void) fprintf(stderr, "Error NetCDF type and/or dimensions %d != %d.\n", varndims, ndims);
00130     istat = ncclose(ncinid);
00131     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00132     return -1;
00133   }
00134 
00135   /* Get dimension ID */
00136   istat = nc_inq_varid(ncinid, xname, &xinid);  /* get ID for x variable */
00137   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00138   /* Get x dimensions and type */
00139   istat = nc_inq_var(ncinid, xinid, (char *) NULL, &vartype, &varndims, vardimids, (int *) NULL); /* get variable information */
00140   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);  
00141   if (varndims != ndims) {
00142     (void) fprintf(stderr, "Error NetCDF type and/or dimensions %d != %d.\n", varndims, ndims);
00143     istat = ncclose(ncinid);
00144     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00145     return -1;
00146   }
00147 
00149   /* Allocate memory and set start and count */
00150   start[0] = 0;
00151   start[1] = 0;
00152   start[2] = 0;
00153   count[0] = (size_t) (*ny);
00154   count[1] = 0;
00155   count[2] = 0;
00156   (*y) = (double *) malloc((*ny) * sizeof(double));
00157   if ((*y) == NULL) alloc_error(__FILE__, __LINE__);
00158   
00159   /* Read values from netCDF variable */
00160   istat = nc_get_vara_double(ncinid, yinid, start, count, *y);
00161   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00162   
00163   /* Allocate memory and set start and count */
00164   start[0] = 0;
00165   start[1] = 0;
00166   start[2] = 0;
00167   count[0] = (size_t) (*nx);
00168   count[1] = 0;
00169   count[2] = 0;
00170   (*x) = (double *) malloc((*nx) * sizeof(double));
00171   if ((*x) == NULL) alloc_error(__FILE__, __LINE__);
00172 
00173   /* Read values from netCDF variable */
00174   istat = nc_get_vara_double(ncinid, xinid, start, count, *x);
00175   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00176 
00177   istat = ncclose(ncinid);
00178   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00179 
00180   /* Success status */
00181   return 0;
00182 }

int write_netcdf_dims_3d ( double *  lon,
double *  lat,
double *  x,
double *  y,
double *  alt,
double *  timein,
char *  cal_type,
char *  time_units,
int  nlon,
int  nlat,
int  ntime,
char *  timestep,
char *  gridname,
char *  coords,
char *  grid_mapping_name,
double  latin1,
double  latin2,
double  lonc,
double  lat0,
double  false_easting,
double  false_northing,
double  lonpole,
double  latpole,
char *  lonname,
char *  latname,
char *  timename,
char *  filename,
int  outinfo 
)

Write NetCDF dimensions and create output file.

Parameters:
[in] lon Longitude field
[in] lat Latitude field
[in] x X field
[in] y Y field
[in] alt Altitude field
[in] timein Time field
[in] cal_type Calendar-type (udunits)
[in] time_units Time units (udunits)
[in] nlon Longitude dimension
[in] nlat Latitude dimension
[in] ntime Time dimension
[in] timestep Timestep string (NetCDF attribute)
[in] gridname Grid type name in the NetCDF file (NetCDF attribute)
[in] coords Coordinates type (NetCDF attribute)
[in] grid_mapping_name Grid mapping name in the NetCDF file (CF-1.0 compliant)
[in] latin1 Latin1 projection parameter
[in] latin2 Latin2 projection parameter
[in] lonc Lonc center longitude projection parameter
[in] lat0 Lat0 first latitude projection parameter
[in] false_easting False_easting projection parameter
[in] false_northing False_northing projection parameter
[in] filename Output NetCDF filename
[in] lonname Longitude name dimension in the NetCDF file
[in] latname Latitude name dimension in the NetCDF file
[in] timename Time name dimension in the NetCDF file
[in] outinfo TRUE if we want information output, FALSE if not
Returns:
Status.

Definition at line 68 of file write_netcdf_dims_3d.c.

References alloc_error(), handle_netcdf_error(), MAXPATH, and TRUE.

Referenced by output_downscaled_analog(), and remove_clim().

00074                                                   {
00106   int istat;
00107 
00108   int ncoutid;
00109   int timedimoutid, xdimoutid, ydimoutid;
00110   int timeoutid, latoutid, lonoutid, xoutid, youtid, projoutid, altoutid;
00111   int vardimids[NC_MAX_VAR_DIMS];    /* dimension ids */
00112 
00113   short int *alts = NULL;
00114 
00115   size_t start[3];
00116   size_t count[3];
00117 
00118   int vali;
00119   int i;
00120 
00121   float *proj_latin = NULL;
00122 
00123   char *tmpstr = NULL;
00124   double *tmpd = NULL;
00125   int *tmpi = NULL;
00126 
00127   double minlat;
00128   double maxlat;
00129   double minlon;
00130   double maxlon;
00131 
00132   short int fillvalue = -99;
00133 
00134   /* Change directory to output directory for autofs notification */
00135   tmpstr = strdup(filename);
00136   istat = chdir(dirname(tmpstr));
00137   (void) free(tmpstr);
00138 
00139   tmpstr = (char *) malloc(MAXPATH * sizeof(char));
00140   if (tmpstr == NULL) alloc_error(__FILE__, __LINE__);
00141 
00142   /* Open NetCDF file */
00143   if (outinfo == TRUE)
00144     printf("%s: Writing info from NetCDF output file %s\n", __FILE__, filename);
00145   istat = nc_open(filename, NC_WRITE, &ncoutid);  /* open NetCDF file */
00146   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00147 
00148   istat = nc_redef(ncoutid);
00149   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00150 
00151   /* Set dimensions */
00152   istat = nc_def_dim(ncoutid, timename, NC_UNLIMITED, &timedimoutid);
00153   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00154   if ( !strcmp(gridname, "Latitude_Longitude")) {
00155     istat = nc_def_dim(ncoutid, lonname, nlon, &xdimoutid);
00156     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00157     istat = nc_def_dim(ncoutid, latname, nlat, &ydimoutid);
00158     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00159   }
00160   else if ( !strcmp(gridname, "list")) {
00161     istat = nc_def_dim(ncoutid, lonname, nlon, &xdimoutid);
00162     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00163     istat = nc_def_dim(ncoutid, latname, nlon, &ydimoutid);
00164     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00165   }
00166   else {
00167     istat = nc_def_dim(ncoutid, "x", nlon, &xdimoutid);
00168     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00169     istat = nc_def_dim(ncoutid, "y", nlat, &ydimoutid);
00170     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00171   }
00172 
00173   /* Define dimensions variables */
00174   vardimids[0] = timedimoutid;
00175   vardimids[1] = 0;
00176   istat = nc_def_var(ncoutid, timename, NC_INT, 1, vardimids, &timeoutid);
00177   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00178 
00179   if ( !strcmp(coords, "1D") ) {
00180     vardimids[0] = xdimoutid;
00181     vardimids[1] = 0;
00182     istat = nc_def_var(ncoutid, lonname, NC_DOUBLE, 1, vardimids, &lonoutid);
00183     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00184     vardimids[0] = ydimoutid;
00185     vardimids[1] = 0;
00186     istat = nc_def_var(ncoutid, latname, NC_DOUBLE, 1, vardimids, &latoutid);
00187     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00188   }
00189   else if ( !strcmp(gridname, "Latitude_Longitude") && !strcmp(coords, "2D") ) {
00190     vardimids[0] = ydimoutid;
00191     vardimids[1] = xdimoutid;
00192     istat = nc_def_var(ncoutid, lonname, NC_DOUBLE, 2, vardimids, &lonoutid);
00193     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00194     vardimids[0] = ydimoutid;
00195     vardimids[1] = xdimoutid;
00196     istat = nc_def_var(ncoutid, latname, NC_DOUBLE, 2, vardimids, &latoutid);
00197     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00198   }
00199   else {
00200     vardimids[0] = ydimoutid;
00201     vardimids[1] = xdimoutid;
00202     istat = nc_def_var(ncoutid, lonname, NC_DOUBLE, 2, vardimids, &lonoutid);
00203     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00204     vardimids[0] = xdimoutid;
00205     vardimids[1] = 0;
00206     istat = nc_def_var(ncoutid, "x", NC_INT, 1, vardimids, &xoutid);
00207     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00208     vardimids[0] = ydimoutid;
00209     vardimids[1] = xdimoutid;
00210     istat = nc_def_var(ncoutid, latname, NC_DOUBLE, 2, vardimids, &latoutid);
00211     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00212     vardimids[0] = ydimoutid;
00213     vardimids[1] = 0;
00214     istat = nc_def_var(ncoutid, "y", NC_INT, 1, vardimids, &youtid);
00215     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00216   }
00217 
00218   /* Define projection variable */
00219   if ( !strcmp(gridname, "Lambert_Conformal")) {
00220     istat = nc_def_var(ncoutid, gridname, NC_INT, 0, 0, &projoutid);
00221     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00222   }
00223   else if ( !strcmp(gridname, "rotated_pole")) {
00224     istat = nc_def_var(ncoutid, gridname, NC_INT, 0, 0, &projoutid);
00225     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00226   }
00227 
00228   if (alt != NULL) {
00229     /* Define Altitude variable */
00230     vardimids[0] = ydimoutid;
00231     vardimids[1] = xdimoutid;
00232     istat = nc_def_var(ncoutid, "Altitude", NC_SHORT, 2, vardimids, &altoutid);
00233     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00234   }
00235 
00236   /* Set time attributes */
00237   istat = nc_put_att_text(ncoutid, timeoutid, "units", strlen(time_units), time_units);
00238   istat = nc_put_att_text(ncoutid, timeoutid, "calendar", strlen(cal_type), cal_type);
00239   (void) sprintf(tmpstr, "time in %s", time_units);
00240   istat = nc_put_att_text(ncoutid, timeoutid, "long_name", strlen(tmpstr), tmpstr);
00241 
00242   istat = nc_put_att_text(ncoutid, NC_GLOBAL, "timestep", strlen(timestep), timestep);
00243 
00244   /* Set longitude attributes */
00245   (void) strcpy(tmpstr, "degrees_east");
00246   istat = nc_put_att_text(ncoutid, lonoutid, "units", strlen(tmpstr), tmpstr);
00247   (void) strcpy(tmpstr, "longitude coordinate");
00248   istat = nc_put_att_text(ncoutid, lonoutid, "long_name", strlen(tmpstr), tmpstr);
00249   (void) strcpy(tmpstr, "longitude");
00250   istat = nc_put_att_text(ncoutid, lonoutid, "standard_name", strlen(tmpstr), tmpstr);
00251 
00252   /* Set latitude attributes */
00253   (void) strcpy(tmpstr, "degrees_north");
00254   istat = nc_put_att_text(ncoutid, latoutid, "units", strlen(tmpstr), tmpstr);
00255   (void) strcpy(tmpstr, "latitude coordinate");
00256   istat = nc_put_att_text(ncoutid, latoutid, "long_name", strlen(tmpstr), tmpstr);
00257   (void) strcpy(tmpstr, "latitude");
00258   istat = nc_put_att_text(ncoutid, latoutid, "standard_name", strlen(tmpstr), tmpstr);
00259 
00260   if (alt != NULL) {
00261     /* Set altitude attributes */
00262     (void) strcpy(tmpstr, "meters");
00263     istat = nc_put_att_text(ncoutid, altoutid, "units", strlen(tmpstr), tmpstr);
00264     (void) strcpy(tmpstr, "Altitude");
00265     istat = nc_put_att_text(ncoutid, altoutid, "long_name", strlen(tmpstr), tmpstr);
00266     (void) strcpy(tmpstr, "Altitude");
00267     istat = nc_put_att_text(ncoutid, altoutid, "standard_name", strlen(tmpstr), tmpstr);
00268     (void) strcpy(tmpstr, "lon lat");
00269     istat = nc_put_att_text(ncoutid, altoutid, "coordinates", strlen(tmpstr), tmpstr);
00270     fillvalue = -99;
00271     istat = nc_put_att_short(ncoutid, altoutid, "_FillValue", NC_SHORT, 1, &fillvalue);
00272     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00273     istat = nc_put_att_short(ncoutid, altoutid, "missing_value", NC_SHORT, 1, &fillvalue);
00274     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00275   }
00276 
00277   if ( strcmp(gridname, "Latitude_Longitude") && strcmp(gridname, "list")) {
00278     /* Set x attributes */
00279     /*          int x(x) ;
00280                 x:units = "m" ;
00281                 x:long_name = "x coordinate of projection" ;
00282                 x:standard_name = "projection_x_coordinate" ;*/
00283     (void) strcpy(tmpstr, "m");
00284     istat = nc_put_att_text(ncoutid, xoutid, "units", strlen(tmpstr), tmpstr);
00285     (void) strcpy(tmpstr, "x coordinate of projection");
00286     istat = nc_put_att_text(ncoutid, xoutid, "long_name", strlen(tmpstr), tmpstr);
00287     (void) strcpy(tmpstr, "projection_x_coordinate");
00288     istat = nc_put_att_text(ncoutid, xoutid, "standard_name", strlen(tmpstr), tmpstr);  
00289     
00290     /* Set y attributes */
00291     /*        int y(y) ;
00292               y:units = "m" ;
00293               y:long_name = "y coordinate of projection" ;
00294               y:standard_name = "projection_y_coordinate" ;*/
00295     (void) strcpy(tmpstr, "m");
00296     istat = nc_put_att_text(ncoutid, youtid, "units", strlen(tmpstr), tmpstr);
00297     (void) strcpy(tmpstr, "y coordinate of projection");
00298     istat = nc_put_att_text(ncoutid, youtid, "long_name", strlen(tmpstr), tmpstr);
00299     (void) strcpy(tmpstr, "projection_y_coordinate");
00300     istat = nc_put_att_text(ncoutid, youtid, "standard_name", strlen(tmpstr), tmpstr);  
00301 
00302     /* Set projection attributes */
00303     /*          int Lambert_Conformal ;
00304                 Lambert_Conformal:grid_mapping_name = "lambert_conformal_conic" ;
00305                 Lambert_Conformal:standard_parallel = 45.89892f, 47.69601f ;
00306                 Lambert_Conformal:longitude_of_central_meridian = 2.337229f ;
00307                 Lambert_Conformal:latitude_of_projection_origin = 46.8f ;
00308                 Lambert_Conformal:false_easting = 600000.f ;
00309                 Lambert_Conformal:false_northing = 2200000.f ;
00310     */
00311     /*
00312         int rotated_pole ;
00313                 rotated_pole:grid_mapping_name = "rotated_latitude_longitude" ;
00314                 rotated_pole:grid_north_pole_latitude = 39.25 ;
00315                 rotated_pole:grid_north_pole_longitude = -162. ;
00316     */
00317   }
00318 
00319   if ( !strcmp(gridname, "Lambert_Conformal")) {
00320     
00321     istat = nc_put_att_text(ncoutid, projoutid, "grid_mapping_name", strlen(grid_mapping_name), grid_mapping_name);
00322     if (alt != NULL)
00323       istat = nc_put_att_text(ncoutid, altoutid, "grid_mapping_name", strlen(grid_mapping_name), grid_mapping_name);
00324     
00325     proj_latin = (float *) malloc(2 * sizeof(float));
00326     if (proj_latin == NULL) alloc_error(__FILE__, __LINE__);
00327     proj_latin[0] = (float) latin1;
00328     proj_latin[1] = (float) latin2;
00329     istat = nc_put_att_float(ncoutid, projoutid, "standard_parallel", NC_FLOAT, 2, proj_latin);
00330     (void) free(proj_latin);
00331     
00332     istat = nc_put_att_double(ncoutid, projoutid, "longitude_of_central_meridian", NC_FLOAT, 1, &lonc);
00333     istat = nc_put_att_double(ncoutid, projoutid, "latitude_of_projection_origin", NC_FLOAT, 1, &lat0);
00334     istat = nc_put_att_double(ncoutid, projoutid, "false_easting", NC_FLOAT, 1, &false_easting);
00335     istat = nc_put_att_double(ncoutid, projoutid, "false_northing", NC_FLOAT, 1, &false_northing);
00336   }
00337   else if ( !strcmp(gridname, "rotated_pole")) {
00338     
00339     istat = nc_put_att_text(ncoutid, projoutid, "grid_mapping_name", strlen(grid_mapping_name), grid_mapping_name);
00340     if (alt != NULL)
00341       istat = nc_put_att_text(ncoutid, altoutid, "grid_mapping_name", strlen(grid_mapping_name), grid_mapping_name);
00342     
00343     istat = nc_put_att_double(ncoutid, projoutid, "grid_north_pole_latitude", NC_FLOAT, 1, &latpole);
00344     istat = nc_put_att_double(ncoutid, projoutid, "grid_north_pole_longitude", NC_FLOAT, 1, &lonpole);
00345   }
00346 
00347   (void) strcpy(tmpstr, "Grid");
00348   istat = nc_put_att_text(ncoutid, NC_GLOBAL, "cdm_datatype", strlen(tmpstr), tmpstr);
00349 
00350   /* Geographic global attributes */
00351   maxlat = -9999.9;
00352   minlat = 9999.9;
00353   maxlon = -9999.9;
00354   minlon = 9999.9;
00355   if ( !strcmp(gridname, "list") ) {
00356     for (i=0; i<(nlon); i++) {
00357       if (lat[i] > maxlat) maxlat = lat[i];
00358       if (lat[i] < minlat) minlat = lat[i];
00359     }
00360     for (i=0; i<(nlon); i++) {
00361       if (lon[i] > maxlon) maxlon = lon[i];
00362       if (lon[i] < minlon) minlon = lon[i];
00363     }
00364   }
00365   else {
00366     for (i=0; i<(nlat*nlon); i++) {
00367       if (lat[i] > maxlat) maxlat = lat[i];
00368       if (lat[i] < minlat) minlat = lat[i];
00369     }
00370     for (i=0; i<(nlat*nlon); i++) {
00371       if (lon[i] > maxlon) maxlon = lon[i];
00372       if (lon[i] < minlon) minlon = lon[i];
00373     }
00374   }
00375   istat = nc_put_att_double(ncoutid, NC_GLOBAL, "geospatial_lat_max", NC_DOUBLE, 1, &maxlat);
00376   istat = nc_put_att_double(ncoutid, NC_GLOBAL, "geospatial_lat_min", NC_DOUBLE, 1, &minlat);
00377   istat = nc_put_att_double(ncoutid, NC_GLOBAL, "geospatial_lon_max", NC_DOUBLE, 1, &maxlon);
00378   istat = nc_put_att_double(ncoutid, NC_GLOBAL, "geospatial_lon_min", NC_DOUBLE, 1, &minlon);
00379 
00380   /* End definition mode */
00381   istat = nc_enddef(ncoutid);
00382   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00383 
00384   if ( !strcmp(gridname, "Lambert_Conformal") || !strcmp(gridname, "rotated_pole") ) {
00385     /* Write projection variable */
00386     vali = 1;
00387     istat = nc_put_var1_int(ncoutid, projoutid, 0, &vali);
00388     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);  
00389   }
00390 
00391   /* Write dimensions variables to NetCDF output file */
00392   if (ntime > 0) {
00393     start[0] = 0;
00394     count[0] = (size_t) ntime;
00395     count[1] = 0;
00396     count[2] = 0;
00397     istat = nc_put_vara_double(ncoutid, timeoutid, start, count, timein);
00398     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00399   }
00400 
00401   if ( !strcmp(gridname, "list") ) {
00402     start[0] = 0;
00403     start[1] = 0;
00404     start[2] = 0;
00405     count[0] = (size_t) nlon;
00406     count[1] = 0;
00407     count[2] = 0;
00408     istat = nc_put_vara_double(ncoutid, latoutid, start, count, lat);
00409     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00410     
00411     start[0] = 0;
00412     start[1] = 0;
00413     start[2] = 0;
00414     count[0] = (size_t) nlon;
00415     count[1] = 0;
00416     count[2] = 0;
00417     istat = nc_put_vara_double(ncoutid, lonoutid, start, count, lon);
00418     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00419   }
00420   else if ( !strcmp(coords, "1D") ) {
00421     start[0] = 0;
00422     start[1] = 0;
00423     start[2] = 0;
00424     count[0] = (size_t) nlat;
00425     count[1] = 0;
00426     count[2] = 0;
00427     tmpd = (double *) malloc(nlat * sizeof(double));
00428     if (tmpd == NULL) alloc_error(__FILE__, __LINE__);
00429     for (i=0; i<nlat; i++)
00430       tmpd[i] = lat[i*nlon];
00431     istat = nc_put_vara_double(ncoutid, latoutid, start, count, tmpd);
00432     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00433     
00434     start[0] = 0;
00435     start[1] = 0;
00436     start[2] = 0;
00437     count[0] = (size_t) nlon;
00438     count[1] = 0;
00439     count[2] = 0;
00440     tmpd = (double *) realloc(tmpd, nlon * sizeof(double));
00441     if (tmpd == NULL) alloc_error(__FILE__, __LINE__);
00442     for (i=0; i<nlon; i++)
00443       tmpd[i] = lon[i];
00444     istat = nc_put_vara_double(ncoutid, lonoutid, start, count, tmpd);
00445     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00446     (void) free(tmpd);
00447   }
00448   else {
00449     start[0] = 0;
00450     start[1] = 0;
00451     start[2] = 0;
00452     count[0] = (size_t) nlat;
00453     count[1] = (size_t) nlon;
00454     count[2] = 0;
00455     istat = nc_put_vara_double(ncoutid, latoutid, start, count, lat);
00456     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00457     
00458     start[0] = 0;
00459     start[1] = 0;
00460     start[2] = 0;
00461     count[0] = (size_t) nlat;
00462     count[1] = (size_t) nlon;
00463     count[2] = 0;
00464     istat = nc_put_vara_double(ncoutid, lonoutid, start, count, lon);
00465     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00466   }
00467 
00468   if ( (!strcmp(gridname, "Lambert_Conformal")||!strcmp(gridname, "rotated_pole")) && x != NULL && y != NULL) {
00469     start[0] = 0;
00470     start[1] = 0;
00471     start[2] = 0;
00472     count[0] = (size_t) nlon;
00473     count[1] = 0;
00474     count[2] = 0;
00475     tmpi = (int *) realloc(tmpi, nlon * sizeof(int));
00476     if (tmpi == NULL) alloc_error(__FILE__, __LINE__);
00477     for (i=0; i<nlon; i++)
00478       tmpi[i] = x[i];
00479 
00480     istat = nc_put_vara_int(ncoutid, xoutid, start, count, tmpi);
00481     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00482     start[0] = 0;
00483     start[1] = 0;
00484     start[2] = 0;
00485     count[0] = (size_t) nlat;
00486     count[1] = 0;
00487     count[2] = 0;
00488     tmpi = (int *) realloc(tmpi, nlat * sizeof(int));
00489     if (tmpi == NULL) alloc_error(__FILE__, __LINE__);
00490     for (i=0; i<nlat; i++)
00491       tmpi[i] = y[i];
00492     istat = nc_put_vara_int(ncoutid, youtid, start, count, tmpi);
00493     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00494     (void) free(tmpi);
00495   }
00496 
00497   if (alt != NULL) {
00498     alts = (short int *) malloc(nlat*nlon * sizeof(short int));
00499     if (alts == NULL) alloc_error(__FILE__, __LINE__);
00500     for (i=0; i<(nlat*nlon); i++)
00501       alts[i] = (short int) alt[i];
00502     start[0] = 0;
00503     start[1] = 0;
00504     start[2] = 0;
00505     count[0] = (size_t) nlat;
00506     count[1] = (size_t) nlon;
00507     count[2] = 0;
00508     istat = nc_put_vara_short(ncoutid, altoutid, start, count, alts);
00509     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00510     (void) free(alts);
00511   }
00512 
00513   /* Close the output netCDF file. */
00514   istat = ncclose(ncoutid);
00515   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00516 
00517   (void) free(tmpstr);
00518 
00519   return 0;
00520 }

int write_netcdf_var_3d ( double *  buf,
double  fillvalue,
char *  filename,
char *  varname,
char *  gridname,
char *  lonname,
char *  latname,
char *  timename,
int  format,
int  compression_level,
int  nlon,
int  nlat,
int  ntime,
int  outinfo 
)

Write a 3D field in a NetCDF output file.

Parameters:
[in] buf 3D Field to write
[in] fillvalue Missing value
[in] filename Output NetCDF filename
[in] varname Variable name in the NetCDF file
[in] gridname Grid type name in the NetCDF file
[in] lonname Longitude name dimension in the NetCDF file
[in] latname Latitude name dimension in the NetCDF file
[in] timename Time name dimension in the NetCDF file
[in] format Format of NetCDF file
[in] compression_level Compression level of NetCDF file (only for NetCDF-4: format==4)
[in] nlon Longitude dimension
[in] nlat Latitude dimension
[in] ntime Time dimension
[in] outinfo TRUE if we want information output, FALSE if not
Returns:
Status.

Open already existing output file

Definition at line 67 of file write_netcdf_var_3d.c.

References alloc_error(), handle_netcdf_error(), MAXPATH, and TRUE.

Referenced by remove_clim().

00069                                                                                                    {
00089   int istat; /* Diagnostic status */
00090 
00091   size_t dimval; /* Temporary variable used to get values from dimension lengths */
00092 
00093   int ncoutid; /* NetCDF output file handle ID */
00094   int varoutid; /* NetCDF variable output ID */
00095   int timedimoutid; /* NetCDF time dimension output ID */
00096   int londimoutid; /* NetCDF longitude dimension output ID */
00097   int latdimoutid; /* NetCDF latitude dimension output ID */
00098   int vardimids[NC_MAX_VAR_DIMS]; /* NetCDF dimension IDs */
00099   //  size_t chunksize[NC_MAX_VAR_DIMS]; /* Chunksize */
00100   //  size_t cachesize; /* HDF5 cache size */
00101   //  size_t cache_nelems = 2000; /* HDF5 cache number of elements */
00102   //  float cache_preemp;
00103 
00104   int ntime_file; /* Time dimension in NetCDF output file */
00105   int nlat_file; /* Latitude dimension in NetCDF output file */
00106   int nlon_file; /* Longitude dimension in NetCDF output file */
00107 
00108   size_t start[3]; /* Start element when writing */
00109   size_t count[3]; /* Count of elements to write */
00110 
00111   char *attname = NULL; /* Attribute name */
00112   char *tmpstr = NULL; /* Temporary string */
00113 
00114   /* Change directory to output directory for autofs notification */
00115   tmpstr = strdup(filename);
00116   istat = chdir(dirname(tmpstr));
00117   (void) free(tmpstr);
00118 
00119   /* Allocate memory */
00120   attname = (char *) malloc(MAXPATH * sizeof(char));
00121   if (attname == NULL) alloc_error(__FILE__, __LINE__);
00122 
00123   /* Set cache size */
00124   /*   if (format == 4 && compression_level > 0) {
00125     if ( !strcmp(gridname, "list") )
00126       cachesize = (size_t) nlon*sizeof(float)*cache_nelems;
00127     else
00128       cachesize = (size_t) nlat*nlon*sizeof(float)*cache_nelems;
00129     istat = nc_get_chunk_cache(&cachesize, &cache_nelems, &cache_preemp);
00130     cache_preemp = 0.75;
00131     cachesize=128000000;
00132     printf("%d %d %f\n",(int)cachesize,(int)cache_nelems,cache_preemp);
00133     istat = nc_set_chunk_cache(cachesize, cache_nelems, cache_preemp);
00134     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00135     } */
00136 
00138   istat = nc_open(filename, NC_WRITE, &ncoutid);
00139   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00140   
00141   /* Get dimension lengths */
00142   istat = nc_inq_dimid(ncoutid, timename, &timedimoutid);  /* get ID for time dimension */
00143   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00144   istat = nc_inq_dimlen(ncoutid, timedimoutid, &dimval); /* get time length */
00145   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00146   ntime_file = (int) dimval;
00147 
00148   istat = nc_inq_dimid(ncoutid, latname, &latdimoutid);  /* get ID for lat dimension */
00149   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00150   istat = nc_inq_dimlen(ncoutid, latdimoutid, &dimval); /* get lat length */
00151   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00152   nlat_file = (int) dimval;
00153 
00154   istat = nc_inq_dimid(ncoutid, lonname, &londimoutid);  /* get ID for lon dimension */
00155   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00156   istat = nc_inq_dimlen(ncoutid, londimoutid, &dimval); /* get lon length */
00157   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00158   nlon_file = (int) dimval;
00159 
00160   /* Verify that they match the provided ones in parameters */
00161   if ( !strcmp(gridname, "list") ) {
00162     if ( (ntime_file != ntime) || (nlat_file != nlon) || (nlon_file != nlon) ) {
00163       (void) fprintf(stderr, "%s: Error NetCDF type and/or dimensions.\n", __FILE__);
00164       return -1;
00165     }
00166   }
00167   else {
00168     if ( (ntime_file != ntime) || (nlat_file != nlat) || (nlon_file != nlon) ) {
00169       (void) fprintf(stderr, "%s: Error NetCDF type and/or dimensions.\n", __FILE__);
00170       return -1;
00171     }
00172   }
00173 
00174   /* Go into NetCDF define mode */
00175   istat = nc_redef(ncoutid);
00176   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00177 
00178   /* Define main output variable */
00179   vardimids[0] = timedimoutid;
00180   if ( !strcmp(gridname, "list") ) {
00181     vardimids[1] = londimoutid;
00182     istat = nc_def_var(ncoutid, varname, NC_FLOAT, 2, vardimids, &varoutid);  
00183   }
00184   else {
00185     vardimids[1] = latdimoutid;
00186     vardimids[2] = londimoutid;
00187     istat = nc_def_var(ncoutid, varname, NC_FLOAT, 3, vardimids, &varoutid);  
00188   }
00189   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00190 
00191 #ifdef NC_NETCDF4
00192     if (format == 4 && compression_level > 0) {
00193       istat = nc_def_var_deflate(ncoutid, varoutid, 0, 1, compression_level);
00194       if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00195       /* Set up chunking */
00196       /*      if ( !strcmp(gridname, "list") ) {
00197         chunksize[0] = (size_t) ntime;
00198         chunksize[1] = (size_t) nlon;
00199         chunksize[2] = (size_t) 0;
00200       }
00201       else {
00202         chunksize[0] = (size_t) ntime;
00203         chunksize[1] = (size_t) nlat;
00204         chunksize[2] = (size_t) nlon;
00205       }
00206       istat = nc_def_var_chunking(ncoutid, varoutid, NC_CHUNKED, chunksize);
00207       if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00208       */
00209     }
00210 #endif
00211 
00212   /* Set main variable attributes */
00213   (void) strcpy(attname, "_FillValue");
00214   istat = nc_put_att_double(ncoutid, varoutid, attname, NC_FLOAT, 1, &fillvalue);
00215   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00216 
00217   (void) strcpy(attname, "missing_value");
00218   istat = nc_put_att_double(ncoutid, varoutid, attname, NC_FLOAT, 1, &fillvalue);
00219   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00220 
00221   tmpstr = (char *) malloc(100 * sizeof(char));
00222   if (tmpstr == NULL) alloc_error(__FILE__, __LINE__);
00223   istat = sprintf(tmpstr, "Climatology of %s", varname);
00224   istat = nc_put_att_text(ncoutid, varoutid, "long_name", strlen(tmpstr), tmpstr);
00225   istat = nc_put_att_text(ncoutid, varoutid, "grid_mapping", strlen(gridname), gridname);
00226   istat = sprintf(tmpstr, "lon lat");
00227   istat = nc_put_att_text(ncoutid, varoutid, "coordinates", strlen(tmpstr), tmpstr);
00228   (void) free(tmpstr);
00229 
00230   /* End definition mode */
00231   istat = nc_enddef(ncoutid);
00232   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00233 
00234   /* Write variable to NetCDF output file */
00235   start[0] = 0;
00236   start[1] = 0;
00237   start[2] = 0;
00238   count[0] = (size_t) ntime;
00239   if ( !strcmp(gridname, "list") ) {
00240     count[1] = (size_t) nlon;
00241     count[2] = 0;
00242   }
00243   else {
00244     count[1] = (size_t) nlat;
00245     count[2] = (size_t) nlon;
00246   }
00247   if (outinfo == TRUE)
00248     printf("%s: WRITE %s %s\n", __FILE__, varname, filename);
00249   istat = nc_put_vara_double(ncoutid, varoutid, start, count, buf);
00250   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00251 
00252   /* Close the output netCDF file. */
00253   istat = ncclose(ncoutid);
00254   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00255 
00256   /* Free memory */
00257   (void) free(attname);
00258 
00259   /* Diagnostic status */
00260   return 0;
00261 }

int write_netcdf_var_3d_2d ( double *  buf,
double *  timein,
double  fillvalue,
char *  filename,
char *  varname,
char *  longname,
char *  units,
char *  height,
char *  gridname,
char *  lonname,
char *  latname,
char *  timename,
int  t,
int  newfile,
int  format,
int  compression_level,
int  nlon,
int  nlat,
int  ntime,
int  outinfo 
)

Write a 2D field in a 3D NetCDF variable.

Parameters:
[in] buf 3D Field to write
[in] timein Time dimension value
[in] fillvalue Missing value
[in] filename Output NetCDF filename
[in] varname Variable name in the NetCDF file
[in] longname Variable long name in the NetCDF file
[in] units Variable units in the NetCDF file
[in] height Variable height in the NetCDF file
[in] gridname Grid type name in the NetCDF file
[in] lonname Longitude name dimension in the NetCDF file
[in] latname Latitude name dimension in the NetCDF file
[in] timename Time name dimension in the NetCDF file
[in] t Time index of value to write
[in] newfile TRUE is new NetCDF file, FALSE if not
[in] format Format of NetCDF file
[in] compression_level Compression level of NetCDF file (only for NetCDF-4: format==4)
[in] outinfo TRUE if we want information output, FALSE if not
[in] nlon Longitude dimension
[in] nlat Latitude dimension
[in] ntime Time dimension
Returns:
Status.

Open already existing output file

Definition at line 68 of file write_netcdf_var_3d_2d.c.

References alloc_error(), handle_netcdf_error(), MAXPATH, and TRUE.

Referenced by output_downscaled_analog().

00072                                                                    {
00098   int istat; /* Diagnostic status */
00099 
00100   size_t dimval; /* Temporary variable used to get values from dimension lengths */
00101 
00102   int ncoutid; /* NetCDF output file handle ID */
00103   int varoutid; /* NetCDF variable output ID */
00104   int timedimoutid; /* NetCDF time dimension output ID */
00105   int timeid; /* NetCDF time variable ID */
00106   int londimoutid; /* NetCDF longitude dimension output ID */
00107   int latdimoutid; /* NetCDF latitude dimension output ID */
00108   int vardimids[NC_MAX_VAR_DIMS]; /* NetCDF dimension IDs */
00109   //  size_t chunksize[NC_MAX_VAR_DIMS]; /* Chunksize */
00110   //  size_t cachesize; /* HDF5 cache size */
00111   //  size_t cache_nelems = 2000; /* HDF5 cache number of elements */
00112   //  float cache_preemp;
00113 
00114   int ntime_file; /* Time dimension in NetCDF output file */
00115   int nlat_file; /* Latitude dimension in NetCDF output file */
00116   int nlon_file; /* Longitude dimension in NetCDF output file */
00117 
00118   size_t start[3]; /* Start element when writing */
00119   size_t count[3]; /* Count of elements to write */
00120 
00121   char *attname = NULL; /* Attribute name */
00122   char *tmpstr = NULL; /* Temporary string */
00123 
00124   /* Allocate memory */
00125   attname = (char *) malloc(MAXPATH * sizeof(char));
00126   if (attname == NULL) alloc_error(__FILE__, __LINE__);
00127 
00128   /* Change directory to output directory for autofs notification */
00129   tmpstr = strdup(filename);
00130   istat = chdir(dirname(tmpstr));
00131   (void) free(tmpstr);
00132 
00133   /*  if (format == 4 && compression_level > 0) {
00134     if ( !strcmp(gridname, "list") )
00135       cachesize = (size_t) nlon*sizeof(float)*cache_nelems;
00136     else
00137       cachesize = (size_t) nlat*nlon*sizeof(float)*cache_nelems;
00138     istat = nc_get_chunk_cache(&cachesize, &cache_nelems, &cache_preemp);
00139     cache_preemp = 0.75;
00140     cachesize=128000000;
00141     printf("%d %d %f\n",(int)cachesize,(int)cache_nelems,cache_preemp);
00142     istat = nc_set_chunk_cache(cachesize, cache_nelems, cache_preemp);
00143     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00144     } */
00145 
00147   istat = nc_open(filename, NC_WRITE, &ncoutid);
00148   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);  
00149 
00150   /* Get dimension lengths */
00151   istat = nc_inq_dimid(ncoutid, timename, &timedimoutid);  /* get ID for time dimension */
00152   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00153   istat = nc_inq_dimlen(ncoutid, timedimoutid, &dimval); /* get time length */
00154   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00155   ntime_file = (int) dimval;
00156 
00157   istat = nc_inq_varid(ncoutid, timename, &timeid);  /* get ID for time variable */
00158   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00159 
00160   istat = nc_inq_dimid(ncoutid, latname, &latdimoutid);  /* get ID for lat dimension */
00161   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00162   istat = nc_inq_dimlen(ncoutid, latdimoutid, &dimval); /* get lat length */
00163   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00164   nlat_file = (int) dimval;
00165 
00166   istat = nc_inq_dimid(ncoutid, lonname, &londimoutid);  /* get ID for lon dimension */
00167   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00168   istat = nc_inq_dimlen(ncoutid, londimoutid, &dimval); /* get lon length */
00169   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00170   nlon_file = (int) dimval;
00171 
00172   /* Verify that they match the provided ones in parameters */
00173   if ( !strcmp(gridname, "list") ) {
00174     if ( ((nlat_file != nlon) || (nlon_file != nlon) )) {
00175       (void) fprintf(stderr, "%s: Error NetCDF type and/or dimensions.\n", __FILE__);
00176       return -1;
00177     }
00178   }
00179   else {
00180     if ( ((nlat_file != nlat) || (nlon_file != nlon) )) {
00181       (void) fprintf(stderr, "%s: Error NetCDF type and/or dimensions.\n", __FILE__);
00182       return -1;
00183     }
00184   }
00185 
00186   /* Go into NetCDF define mode only if first element */
00187   if (newfile == TRUE) {
00188     istat = nc_redef(ncoutid);
00189     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00190     
00191     /* Define main output variable */
00192     vardimids[0] = timedimoutid;
00193     if ( !strcmp(gridname, "list") ) {
00194       vardimids[1] = londimoutid;
00195       istat = nc_def_var(ncoutid, varname, NC_FLOAT, 2, vardimids, &varoutid);  
00196     }
00197     else {
00198       vardimids[1] = latdimoutid;
00199       vardimids[2] = londimoutid;
00200       istat = nc_def_var(ncoutid, varname, NC_FLOAT, 3, vardimids, &varoutid);  
00201     }
00202     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00203 
00204 #ifdef NC_NETCDF4
00205     if (format == 4 && compression_level > 0) {
00206       /* Set up compression level */
00207       istat = nc_def_var_deflate(ncoutid, varoutid, 0, 1, compression_level);
00208       if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00209       /* Set up chunking */
00210       /*      if ( !strcmp(gridname, "list") ) {
00211         chunksize[0] = (size_t) 1;
00212         chunksize[1] = (size_t) nlon;
00213         chunksize[2] = (size_t) 0;
00214       }
00215       else {
00216         chunksize[0] = (size_t) 1;
00217         chunksize[1] = (size_t) nlat;
00218         chunksize[2] = (size_t) nlon;
00219       }
00220       istat = nc_def_var_chunking(ncoutid, varoutid, NC_CHUNKED, chunksize);
00221       if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);*/
00222     }
00223 #endif
00224 
00225     /* Set main variable attributes */
00226     (void) strcpy(attname, "_FillValue");
00227     istat = nc_put_att_double(ncoutid, varoutid, attname, NC_FLOAT, 1, &fillvalue);
00228     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00229     
00230     (void) strcpy(attname, "missing_value");
00231     istat = nc_put_att_double(ncoutid, varoutid, attname, NC_FLOAT, 1, &fillvalue);
00232     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00233     
00234     tmpstr = (char *) malloc(100 * sizeof(char));
00235     if (tmpstr == NULL) alloc_error(__FILE__, __LINE__);
00236     istat = nc_put_att_text(ncoutid, varoutid, "long_name", strlen(longname), longname);
00237     istat = nc_put_att_text(ncoutid, varoutid, "grid_mapping", strlen(gridname), gridname);
00238     istat = nc_put_att_text(ncoutid, varoutid, "units", strlen(units), units);
00239     istat = nc_put_att_text(ncoutid, varoutid, "height", strlen(height), height);
00240     istat = sprintf(tmpstr, "lon lat");
00241     istat = nc_put_att_text(ncoutid, varoutid, "coordinates", strlen(tmpstr), tmpstr);
00242     (void) free(tmpstr);
00243     
00244     /* End definition mode */
00245     istat = nc_enddef(ncoutid);
00246     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00247   }
00248   else {
00249     istat = nc_inq_varid(ncoutid, varname, &varoutid);
00250     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00251   }
00252 
00253   /* Write time dimension variable to NetCDF output file */
00254   start[0] = ntime_file;
00255   start[1] = 0;
00256   start[2] = 0;
00257   count[0] = (size_t) 1;
00258   count[1] = 0;
00259   count[2] = 0;
00260   istat = nc_put_vara_double(ncoutid, timeid, start, count, &(timein[t]));
00261   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00262 
00263   /* Write variable to NetCDF output file */
00264   start[0] = ntime_file;
00265   start[1] = 0;
00266   start[2] = 0;
00267   count[0] = (size_t) 1;
00268   if ( !strcmp(gridname, "list") ) {
00269     count[1] = (size_t) nlon;
00270     count[2] = 0;
00271   }
00272   else {
00273     count[1] = (size_t) nlat;
00274     count[2] = (size_t) nlon;
00275   }
00276   if (outinfo == TRUE)
00277     printf("%s: WRITE %s %s\n", __FILE__, varname, filename);
00278   istat = nc_put_vara_double(ncoutid, varoutid, start, count, buf);
00279   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00280 
00281   /* Close the output netCDF file. */
00282   istat = ncclose(ncoutid);
00283   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00284 
00285   /* Free memory */
00286   (void) free(attname);
00287 
00288   /* Diagnostic status */
00289   return 0;
00290 }


Generated on 12 May 2016 for DSCLIM by  doxygen 1.6.1