read_field_subdomain_period.c File Reference

Read NetCDF field and extract subdomain and subperiod. More...

#include <dsclim.h>
Include dependency graph for read_field_subdomain_period.c:

Go to the source code of this file.

Functions

int read_field_subdomain_period (double **buffer, double **lon, double **lat, double *missing_value, char *varname, int *year, int *month, int *day, double lonmin, double lonmax, double latmin, double latmax, char *coords, char *gridname, char *lonname, char *latname, char *dimxname, char *dimyname, char *timename, char *filename, int *nlon, int *nlat, int ntime)
 Read NetCDF field and extract subdomain and subperiod.

Detailed Description

Read NetCDF field and extract subdomain and subperiod.

Definition in file read_field_subdomain_period.c.


Function Documentation

int read_field_subdomain_period ( double **  buffer,
double **  lon,
double **  lat,
double *  missing_value,
char *  varname,
int *  year,
int *  month,
int *  day,
double  lonmin,
double  lonmax,
double  latmin,
double  latmax,
char *  coords,
char *  gridname,
char *  lonname,
char *  latname,
char *  dimxname,
char *  dimyname,
char *  timename,
char *  filename,
int *  nlon,
int *  nlat,
int  ntime 
)

Read NetCDF field and extract subdomain and subperiod.

Parameters:
[out] buffer Output field 3D array
[out] lon Longitude 2D array
[out] lat Latitude 2D array
[out] missing_value Missing value
[in] varname Variable name to read
[in] year Year vector for subperiod
[in] month Month vector for subperiod
[in] day Day vector for subperiod
[in] lonmin Minimum longitude for subdomain
[in] lonmax Maximum longitude for subdomain
[in] latmin Minimum latitude for subdomain
[in] latmax Maximum latitude for subdomain
[in] coords Coordinates dimensions (1D or 2D)
[in] gridname Projection name
[in] lonname Longitude field name
[in] latname Latitude field name
[in] dimxname X Dimension name
[in] dimyname Y Dimension name
[in] timename Time dimension name
[in] filename Input filename
[out] nlon Longitude dimension
[out] nlat Latitude dimension
[in] ntime Time dimension
Returns:
Status.

Definition at line 67 of file read_field_subdomain_period.c.

References alloc_error(), compute_time_info(), info_field_struct::coordinates, time_vect_struct::day, extract_subdomain(), FALSE, info_field_struct::fillvalue, info_field_struct::grid_mapping, info_field_struct::height, time_vect_struct::hour, info_field_struct::long_name, time_vect_struct::minutes, time_vect_struct::month, read_netcdf_dims_3d(), read_netcdf_var_3d_2d(), time_vect_struct::seconds, info_struct::title, info_field_struct::units, and time_vect_struct::year.

Referenced by wt_learning().

00070                                                                                              {
00099   int istat; /* Diagnostic status */
00100   info_struct *info;
00101   info_field_struct *info_field;
00102   double *buf_total = NULL;
00103   double *buf_sub = NULL;
00104   double *time_ls = NULL; /* Temporary time information buffer */
00105   time_vect_struct *time_s = NULL;
00106   char *cal_type = NULL; /* Calendar type (udunits) */
00107   char *time_units = NULL; /* Time units (udunits) */
00108   double *lon_total = NULL;
00109   double *lat_total = NULL;
00110   int ntime_file; /* Number of times dimension in input file */
00111   int nlon_file;
00112   int nlat_file;
00113   int ntime_sub;
00114 
00115   int nt;
00116   int tt;
00117   int i;
00118   int j;
00119 
00120   *lon = NULL;
00121   *lat = NULL;
00122   *buffer = NULL;
00123 
00124   *nlon = *nlat = -1;
00125 
00126   info = (info_struct *) malloc(sizeof(info_struct));
00127   if (info == NULL) alloc_error(__FILE__, __LINE__);
00128 
00129   info_field = (info_field_struct *) malloc(sizeof(info_field_struct));
00130   if (info_field == NULL) alloc_error(__FILE__, __LINE__);
00131 
00132   time_s = (time_vect_struct *) malloc(sizeof(time_vect_struct));
00133   if (time_s == NULL) alloc_error(__FILE__, __LINE__);
00134 
00135   /* To prevent fetching of not needed info attributes */
00136   info->title = strdup("none");
00137   /* Read dimensions */
00138   istat = read_netcdf_dims_3d(&lon_total, &lat_total, &time_ls, &cal_type, &time_units, &nlon_file, &nlat_file, &ntime_file,
00139                               info, coords, gridname, lonname, latname, dimxname, dimyname, timename, filename);
00140   (void) free(info->title);
00141   (void) free(info);
00142 
00143   /* Compute time information */
00144   istat = compute_time_info(time_s, time_ls, time_units, cal_type, ntime_file);
00145 
00146   /* Loop over time */
00147   ntime_sub = 0;
00148   for (nt=0; nt<ntime; nt++) {
00149     /* Search in all second time vector times for matching date */
00150     for (tt=0; tt<ntime_file; tt++) {
00151       if (year[nt]  == time_s->year[tt] &&
00152           month[nt] == time_s->month[tt] &&
00153           day[nt]   == time_s->day[tt]) {
00154         /* Found common date, process it. */
00155         istat = read_netcdf_var_3d_2d(&buf_total, info_field, (proj_struct *) NULL, filename, varname, dimxname, dimyname, timename,
00156                                       tt, nlon, nlat, &ntime_file, FALSE);
00157         /* Free non-needed variables */
00158         (void) free(info_field->coordinates);
00159         (void) free(info_field->grid_mapping);
00160         (void) free(info_field->units);
00161         (void) free(info_field->height);
00162         (void) free(info_field->long_name);
00163         if (istat != 0) {
00164           /* In case of failure */
00165           (void) free(buf_total);
00166           (void) free(lon_total);
00167           (void) free(lat_total);
00168           (void) free(time_ls);
00169           (void) free(time_units);
00170           (void) free(cal_type);
00171           (void) free(time_s->year);
00172           (void) free(time_s->month);
00173           (void) free(time_s->day);
00174           (void) free(time_s->hour);
00175           (void) free(time_s->minutes);
00176           (void) free(time_s->seconds);
00177           (void) free(time_s);
00178           (void) free(info_field);
00179           return istat;
00180         }
00181         *missing_value = info_field->fillvalue;
00182         
00183         /* Extract subdomain */
00184         if ((*lat) != NULL)
00185           (void) free(*lat);
00186         if ((*lon) != NULL)
00187           (void) free(*lon);
00188         (void) extract_subdomain(&buf_sub, lon, lat, nlon, nlat, buf_total, lon_total, lat_total,
00189                                  lonmin, lonmax, latmin, latmax, nlon_file, nlat_file, 1);
00190         (void) free(buf_total);
00191 
00192         /* Store into output field */
00193         (*buffer) = realloc((*buffer), (*nlon)*(*nlat)*(ntime_sub+1) * sizeof(double));
00194         if ((*buffer) == NULL) alloc_error(__FILE__, __LINE__);
00195         for (j=0; j<(*nlat); j++)
00196           for (i=0; i<(*nlon); i++)
00197             (*buffer)[i+j*(*nlon)+(ntime_sub)*(*nlon)*(*nlat)] = buf_sub[i+j*(*nlon)];
00198 
00199         (void) free(buf_sub);
00200         
00201         ntime_sub++;
00202 
00203         break;
00204       }
00205     }
00206   }
00207 
00208   if (*nlat == -1 || *nlon == -1) {
00209     /* In case of failure */
00210     (void) free(lon_total);
00211     (void) free(lat_total);
00212     (void) free(time_ls);
00213     (void) free(time_units);
00214     (void) free(cal_type);
00215     (void) free(time_s->year);
00216     (void) free(time_s->month);
00217     (void) free(time_s->day);
00218     (void) free(time_s->hour);
00219     (void) free(time_s->minutes);
00220     (void) free(time_s->seconds);
00221     (void) free(time_s);
00222     (void) free(info_field);
00223     
00224     (void) fprintf(stderr, "%s: Cannot find any date!! Dates we try to find:: At index 0: %d %d %d, at last index: %d %d %d. Dates we are searching in (in the file):: At index 0: %d %d %d, at last index: %d %d %d. \n", __FILE__, year[0], month[0], day[0], year[ntime-1], month[ntime-1], day[ntime-1], time_s->year[0], time_s->month[0], time_s->day[0], time_s->year[ntime_file-1], time_s->month[ntime_file-1], time_s->day[ntime_file-1]);
00225 
00226     return -1;
00227   }
00228 
00229   (void) free(lon_total);
00230   (void) free(lat_total);
00231 
00232   (void) free(time_s->year);
00233   (void) free(time_s->month);
00234   (void) free(time_s->day);
00235   (void) free(time_s->hour);
00236   (void) free(time_s->minutes);
00237   (void) free(time_s->seconds);
00238   (void) free(time_s);
00239 
00240   (void) free(time_ls);
00241   (void) free(time_units);
00242   (void) free(cal_type);
00243 
00244   (void) free(info_field);
00245 
00246   /* Diagnostic status */
00247   return 0;
00248 }


Generated on 12 May 2016 for DSCLIM by  doxygen 1.6.1