read_large_scale_eof.c

Go to the documentation of this file.
00001 /* ***************************************************** */
00002 /* read_large_scale_eof Read large scale EOFs.           */
00003 /* read_large_scale_eof.c                                */
00004 /* ***************************************************** */
00005 /* Author: Christian Page, CERFACS, Toulouse, France.    */
00006 /* ***************************************************** */
00007 /* Date of creation: oct 2008                            */
00008 /* Last date of modification: oct 2008                   */
00009 /* ***************************************************** */
00010 /* Original version: 1.0                                 */
00011 /* Current revision:                                     */
00012 /* ***************************************************** */
00013 /* Revisions                                             */
00014 /* ***************************************************** */
00019 /* LICENSE BEGIN
00020 
00021 Copyright Cerfacs (Christian Page) (2015)
00022 
00023 christian.page@cerfacs.fr
00024 
00025 This software is a computer program whose purpose is to downscale climate
00026 scenarios using a statistical methodology based on weather regimes.
00027 
00028 This software is governed by the CeCILL license under French law and
00029 abiding by the rules of distribution of free software. You can use, 
00030 modify and/ or redistribute the software under the terms of the CeCILL
00031 license as circulated by CEA, CNRS and INRIA at the following URL
00032 "http://www.cecill.info". 
00033 
00034 As a counterpart to the access to the source code and rights to copy,
00035 modify and redistribute granted by the license, users are provided only
00036 with a limited warranty and the software's author, the holder of the
00037 economic rights, and the successive licensors have only limited
00038 liability. 
00039 
00040 In this respect, the user's attention is drawn to the risks associated
00041 with loading, using, modifying and/or developing or reproducing the
00042 software by the user in light of its specific status of free software,
00043 that may mean that it is complicated to manipulate, and that also
00044 therefore means that it is reserved for developers and experienced
00045 professionals having in-depth computer knowledge. Users are therefore
00046 encouraged to load and test the software's suitability as regards their
00047 requirements in conditions enabling the security of their systems and/or 
00048 data to be ensured and, more generally, to use and operate it in the 
00049 same conditions as regards security. 
00050 
00051 The fact that you are presently reading this means that you have had
00052 knowledge of the CeCILL license and that you accept its terms.
00053 
00054 LICENSE END */
00055 
00056 
00057 
00058 
00059 
00060 
00061 
00062 #include <dsclim.h>
00063 
00065 int
00066 read_large_scale_eof(data_struct *data) {
00073   int istat; /* Diagnostic status */
00074   int i; /* Loop counter */
00075   int cat; /* Field category loop counter */
00076   double *buf = NULL; /* Temporary buffer for reading */
00077   double *lat = NULL; /* Temporary buffer for latitudes */
00078   double *lon = NULL; /* Temporary buffer for longitudes */
00079   int nlon; /* Longitude dimension */
00080   int nlat; /* Latitude dimension */
00081   int nlon_file; /* Longitude dimension for main large-scale fields in input file */
00082   int nlat_file; /* Latitude dimension for main large-scale fields in input file */
00083   int neof_file; /* EOF dimension for main large-scale fields in input file */
00084   proj_struct proj_eof; /* EOF spatial projection structure */
00085 
00086   proj_eof.eof_coords = (char *) NULL;
00087   proj_eof.name = (char *) NULL;
00088   proj_eof.grid_mapping_name = (char *) NULL;
00089 
00090   /* Loop over large-scale field categories (control and model run) */
00091   for (cat=0; cat<2; cat++) {
00092     /* Loop over large-scale fields */
00093     for (i=0; i<data->field[cat].n_ls; i++) {
00094 
00095       /* Verify that we need to project field onto EOF */
00096       if (data->field[cat].data[i].eof_info->eof_project == TRUE) {
00097       
00098         if (data->field[cat].lon_eof_ls == NULL) {
00099           /* Read dimensions for EOF */
00100           istat = read_netcdf_dims_eof(&lon, &lat, &nlon, &nlat, &neof_file,
00101                                        data->field[cat].data[i].eof_info->eof_coords,
00102                                        data->conf->lonname_eof, data->conf->latname_eof,
00103                                        data->conf->dimxname_eof, data->conf->dimyname_eof,
00104                                        data->conf->eofname,
00105                                        data->field[cat].data[i].eof_info->eof_filein_ls);
00106           if (istat < 0) {
00107             /* In case of failure */
00108             (void) free(lon);
00109             (void) free(lat);
00110             return istat;
00111           }
00112         
00113           /* Transfer data into proper data structure */
00114           proj_eof.eof_coords = strdup(data->field[cat].proj[i].coords);
00115           proj_eof.name = strdup(data->field[cat].proj[i].name);
00116           proj_eof.grid_mapping_name = strdup(data->field[cat].proj[i].grid_mapping_name);
00117         }
00118       
00119         /* Read EOF */
00120         istat = read_netcdf_var_3d(&buf, data->field[cat].data[i].eof_info->info, &proj_eof,
00121                                    data->field[cat].data[i].eof_info->eof_filein_ls,
00122                                    data->field[cat].data[i].eof_data->eof_nomvar_ls,
00123                                    data->conf->dimxname_eof, data->conf->dimyname_eof,
00124                                    data->conf->eofname, &nlon_file, &nlat_file, &neof_file, TRUE);
00125         if (nlon != nlon_file || nlat != nlat_file) {
00126           (void) fprintf(stderr, "%s: Problems in dimensions! nlat=%d nlat_file=%d nlon=%d nlon_file=%d\n",
00127                          __FILE__, nlat, nlat_file, nlon, nlon_file);
00128           istat = -1;
00129         }
00130         if (data->field[cat].data[i].eof_info->neof_ls != neof_file) {
00131           (void) fprintf(stderr, "%s: ERROR: Number of EOFs (%d) for %s field from large-scale fields file (%s) is not equal to number of EOFs specified in XML configuration file for large-scale fields (%d)!\n", __FILE__, neof_file,
00132                          data->field[cat].data[i].eof_data->eof_nomvar_ls,
00133                          data->field[cat].data[i].eof_info->eof_filein_ls,
00134                          data->field[cat].data[i].eof_info->neof_ls);
00135           istat = -1;
00136         }
00137         if (istat != 0) {
00138           /* In case of failure */
00139           (void) free(buf);
00140           (void) free(lon);
00141           (void) free(lat);
00142           if (proj_eof.eof_coords != NULL)
00143             (void) free(proj_eof.eof_coords);
00144           if (proj_eof.name != NULL)
00145             (void) free(proj_eof.name);
00146           if (proj_eof.grid_mapping_name != NULL)
00147             (void) free(proj_eof.grid_mapping_name);
00148           return istat;
00149         }
00150       
00152         /* Free memory if needed because of loop. Set pointers to NULL for realloc. */
00153         if (data->field[cat].lon_eof_ls != NULL) {
00154           (void) free(data->field[cat].lon_eof_ls);
00155           data->field[cat].lon_eof_ls = NULL;
00156         }
00157         if (data->field[cat].lat_eof_ls != NULL) {
00158           (void) free(data->field[cat].lat_eof_ls);
00159           data->field[cat].lat_eof_ls = NULL;
00160         }
00161         if (data->field[cat].data[i].eof_data->eof_ls != NULL) {
00162           (void) free(data->field[cat].data[i].eof_data->eof_ls);
00163           data->field[cat].data[i].eof_data->eof_ls = NULL;
00164         }
00165         /* Extraction */
00166         (void) extract_subdomain(&(data->field[cat].data[i].eof_data->eof_ls), &(data->field[cat].lon_eof_ls),
00167                                  &(data->field[cat].lat_eof_ls),
00168                                  &(data->field[cat].nlon_eof_ls), &(data->field[cat].nlat_eof_ls), buf, lon, lat,
00169                                  data->conf->longitude_min, data->conf->longitude_max, data->conf->latitude_min, data->conf->latitude_max,
00170                                  nlon, nlat, data->field[cat].data[i].eof_info->neof_ls);
00171         (void) free(buf);
00172         
00173         /* Print missing value */
00174         printf("%s: EOF missing_value = %lf\n", __FILE__, (double) data->field[cat].data[i].eof_info->info->fillvalue);
00175       
00176         /* Read Singular Values */
00177         istat = read_netcdf_var_1d(&(data->field[cat].data[i].eof_data->sing_ls), (info_field_struct *) NULL,
00178                                    data->field[cat].data[i].eof_info->eof_filein_ls, data->field[cat].data[i].eof_data->sing_nomvar_ls,
00179                                    data->conf->eofname, &neof_file, TRUE);
00180         if (data->field[cat].data[i].eof_info->neof_ls != neof_file) {
00181           (void) fprintf(stderr, "%s: ERROR: Number of EOFs (%d) for %s singular values from large-scale fields file (%s) is not equal to number of EOFs specified in XML configuration file for large-scale fields (%d)!\n", __FILE__, neof_file,
00182                           data->field[cat].data[i].eof_data->sing_nomvar_ls, data->field[cat].data[i].eof_info->eof_filein_ls,
00183                          data->field[cat].data[i].eof_info->neof_ls);
00184           istat = -1;
00185         }
00186         if (istat != 0) {
00187           /* In case of failure */
00188           (void) free(lon);
00189           (void) free(lat);
00190           if (proj_eof.eof_coords != NULL)
00191             (void) free(proj_eof.eof_coords);
00192           if (proj_eof.name != NULL)
00193             (void) free(proj_eof.name);
00194           if (proj_eof.grid_mapping_name != NULL)
00195             (void) free(proj_eof.grid_mapping_name);
00196           return istat;
00197         }
00198       }
00199       /* Free memory if needed */
00200       if (lon != NULL) (void) free(lon);
00201       lon = NULL;
00202       if (lat != NULL) (void) free(lat);
00203       lat = NULL;
00204 
00205       if (proj_eof.eof_coords != NULL) {
00206         (void) free(proj_eof.eof_coords);
00207         proj_eof.eof_coords = (char *) NULL;
00208       }
00209       if (proj_eof.name != NULL) {
00210         (void) free(proj_eof.name);
00211         proj_eof.name = (char *) NULL;
00212       }
00213       if (proj_eof.grid_mapping_name != NULL) {
00214         (void) free(proj_eof.grid_mapping_name);
00215         proj_eof.grid_mapping_name = NULL;
00216       }
00217     }
00218   }
00219   
00220   /* Diagnostic status */
00221   return 0;
00222 }

Generated on 12 May 2016 for DSCLIM by  doxygen 1.6.1