read_field_subdomain_period.c

Go to the documentation of this file.
00001 /* ***************************************************** */
00002 /* read_field_subdomain_period Read NetCDF field and     */
00003 /* extract subdomain and subperiod.                      */
00004 /* read_field_subdomain_period.c                         */
00005 /* ***************************************************** */
00006 /* Author: Christian Page, CERFACS, Toulouse, France.    */
00007 /* ***************************************************** */
00008 /* Date of creation: jan 2009                            */
00009 /* Last date of modification: jan 2009                   */
00010 /* ***************************************************** */
00011 /* Original version: 1.0                                 */
00012 /* Current revision:                                     */
00013 /* ***************************************************** */
00014 /* Revisions                                             */
00015 /* ***************************************************** */
00020 /* LICENSE BEGIN
00021 
00022 Copyright Cerfacs (Christian Page) (2015)
00023 
00024 christian.page@cerfacs.fr
00025 
00026 This software is a computer program whose purpose is to downscale climate
00027 scenarios using a statistical methodology based on weather regimes.
00028 
00029 This software is governed by the CeCILL license under French law and
00030 abiding by the rules of distribution of free software. You can use, 
00031 modify and/ or redistribute the software under the terms of the CeCILL
00032 license as circulated by CEA, CNRS and INRIA at the following URL
00033 "http://www.cecill.info". 
00034 
00035 As a counterpart to the access to the source code and rights to copy,
00036 modify and redistribute granted by the license, users are provided only
00037 with a limited warranty and the software's author, the holder of the
00038 economic rights, and the successive licensors have only limited
00039 liability. 
00040 
00041 In this respect, the user's attention is drawn to the risks associated
00042 with loading, using, modifying and/or developing or reproducing the
00043 software by the user in light of its specific status of free software,
00044 that may mean that it is complicated to manipulate, and that also
00045 therefore means that it is reserved for developers and experienced
00046 professionals having in-depth computer knowledge. Users are therefore
00047 encouraged to load and test the software's suitability as regards their
00048 requirements in conditions enabling the security of their systems and/or 
00049 data to be ensured and, more generally, to use and operate it in the 
00050 same conditions as regards security. 
00051 
00052 The fact that you are presently reading this means that you have had
00053 knowledge of the CeCILL license and that you accept its terms.
00054 
00055 LICENSE END */
00056 
00057 
00058 
00059 
00060 
00061 
00062 
00063 #include <dsclim.h>
00064 
00066 int
00067 read_field_subdomain_period(double **buffer, double **lon, double **lat, double *missing_value, char *varname,
00068                             int *year, int *month, int *day, double lonmin, double lonmax, double latmin, double latmax,
00069                             char *coords, char *gridname, char *lonname, char *latname, char *dimxname, char *dimyname,
00070                             char *timename, char *filename, int *nlon, int *nlat, int ntime) {
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