dsclim.h

Go to the documentation of this file.
00001 
00005 /* LICENSE BEGIN
00006 
00007 Copyright Cerfacs (Christian Page) (2015)
00008 
00009 christian.page@cerfacs.fr
00010 
00011 This software is a computer program whose purpose is to downscale climate
00012 scenarios using a statistical methodology based on weather regimes.
00013 
00014 This software is governed by the CeCILL license under French law and
00015 abiding by the rules of distribution of free software. You can use, 
00016 modify and/ or redistribute the software under the terms of the CeCILL
00017 license as circulated by CEA, CNRS and INRIA at the following URL
00018 "http://www.cecill.info". 
00019 
00020 As a counterpart to the access to the source code and rights to copy,
00021 modify and redistribute granted by the license, users are provided only
00022 with a limited warranty and the software's author, the holder of the
00023 economic rights, and the successive licensors have only limited
00024 liability. 
00025 
00026 In this respect, the user's attention is drawn to the risks associated
00027 with loading, using, modifying and/or developing or reproducing the
00028 software by the user in light of its specific status of free software,
00029 that may mean that it is complicated to manipulate, and that also
00030 therefore means that it is reserved for developers and experienced
00031 professionals having in-depth computer knowledge. Users are therefore
00032 encouraged to load and test the software's suitability as regards their
00033 requirements in conditions enabling the security of their systems and/or 
00034 data to be ensured and, more generally, to use and operate it in the 
00035 same conditions as regards security. 
00036 
00037 The fact that you are presently reading this means that you have had
00038 knowledge of the CeCILL license and that you accept its terms.
00039 
00040 LICENSE END */
00041 
00042 
00043 
00044 
00045 
00046 
00047 #ifndef DSCLIM_H
00048 #define DSCLIM_H
00049 
00050 #ifdef HAVE_CONFIG_H
00051 #include <config.h>
00052 #endif
00053 
00055 #define _GNU_SOURCE
00056 
00057 /* C standard includes */
00058 #ifdef HAVE_UNISTD_H
00059 #include <unistd.h>
00060 #endif
00061 #ifdef HAVE_STRING_H
00062 #include <string.h>
00063 #endif
00064 #ifdef HAVE_STDLIB_H
00065 #include <stdlib.h>
00066 #endif
00067 #ifdef HAVE_STDIO_H
00068 #include <stdio.h>
00069 #endif
00070 #ifdef HAVE_ERRNO_H
00071 #include <errno.h>
00072 #endif
00073 #ifdef HAVE_LIBGEN_H
00074 #include <libgen.h>
00075 #endif
00076 
00077 /* NetCDF-related includes */
00078 #include <zlib.h>
00079 #include <hdf5.h>
00080 #include <netcdf.h>
00081 
00082 #ifdef HAVE_UDUNITS2
00083 #include <udunits.h>
00084 #endif
00085 
00086 /* GNU GSL includes */
00087 #include <gsl/gsl_sort.h>
00088 
00090 #define PAGESIZE sysconf(_SC_PAGESIZE)
00091 
00093 #define NCAT 4
00094 
00095 #define FIELD_LS 0
00096 
00097 #define CTRL_FIELD_LS 1
00098 
00099 #define SEC_FIELD_LS 2
00100 
00101 #define CTRL_SEC_FIELD_LS 3
00102 
00104 #define MAXPATH 5000
00105 
00107 #define DEFLATE_LEVEL 6
00108 
00109 /* Local C includes. */
00110 #include <utils.h>
00111 #include <clim.h>
00112 #include <classif.h>
00113 #include <pceof.h>
00114 #include <io.h>
00115 #include <regress.h>
00116 
00118 typedef struct {
00119   int nobs_var; 
00120   char *lonname; 
00121   char *latname; 
00122   char *timename; 
00123   proj_struct *proj; 
00124   char *dimxname; 
00125   char *dimyname; 
00126   char *dimcoords; 
00127   char *frequency; 
00128   char *path; 
00129   int month_begin; 
00130   int year_digits; 
00131   char *altitude; 
00132   char *altitudename; 
00133   char *template; 
00134   char **acronym; 
00135   char **netcdfname; 
00136   char **name; 
00137   char **post; 
00138   char **clim; 
00139   char **output; 
00140   char **units; 
00141   char **height; 
00142   double *delta; 
00143   double *factor; 
00144 } var_struct;
00145 
00147 typedef struct {
00148   int *tindex; 
00149   int *tindex_all; 
00150   int *time; 
00151   int *year; 
00152   int *month; 
00153   int *day; 
00154   int *ndayschoice; 
00155   tstruct **analog_dayschoice; 
00156   int **tindex_dayschoice; 
00157   float **metric_norm; 
00158   int ntime; 
00159   int *tindex_s_all; 
00160   int *year_s; 
00161   int *month_s; 
00162   int *day_s; 
00163 } analog_day_struct;
00164 
00166 typedef struct {
00167 /* The dimension should be for each independent field, for all categories */
00168 /* Can be NULL for non-appropriate category. */
00169   double *eof_ls; 
00170   double *sing_ls; 
00171   char *eof_nomvar_ls; 
00172   char *sing_nomvar_ls; 
00173 } eof_data_struct;
00174 
00176 typedef struct {
00177 /* The dimension should be for each independent field, for all categories */
00178 /* Can be NULL for non-appropriate category. */
00179   int eof_project; 
00180   double eof_scale; 
00181   int eof_weight; 
00182   char *eof_filein_ls; 
00183   info_field_struct *info; 
00184   int neof_ls; 
00185   char *eof_coords; 
00186 } eof_info_struct;
00187 
00189 typedef struct {
00190 /* The should be for each independent field, for all categories */
00191 /* Can be NULL for non-appropriate category.. */
00192   int clim_remove; 
00193   int clim_provided; 
00194   int clim_save; 
00195   char *clim_nomvar_ls; 
00196   char *clim_filein_ls; 
00197   char *clim_fileout_ls; 
00198 } clim_info_struct;
00199 
00201 typedef struct {
00202   double **mean_dist; 
00203   double **var_dist; 
00204   double **dist; 
00205   double *dist_all; 
00206   double **smean_norm; 
00207   double **sup_val_norm; 
00208   int **days_class_clusters; 
00209   int *days_class_clusters_all; 
00210   double *smean; 
00211   double **smean_2d; 
00212   double **svar_2d; 
00213   double *mean; 
00214   double *var; 
00215   double *var_pc_norm; 
00216   int *ndayschoice; 
00217   double **delta; 
00218   double *delta_all; 
00219   double ***delta_dayschoice; 
00220   double **delta_dayschoice_all; 
00221 } downscale_struct;
00222 
00224 typedef struct {
00225 /* The dimension should be for each independent field, for all categories. */
00226   char *nomvar_ls; 
00227   double *field_ls; 
00228   char *filename_ls; 
00229   double *field_eof_ls; 
00230   char *dimxname; 
00231   char *dimyname; 
00232   char *lonname; 
00233   char *latname; 
00234   char *timename; 
00235   info_field_struct *info; 
00236   clim_info_struct *clim_info; 
00237   eof_info_struct *eof_info; 
00238   eof_data_struct *eof_data; 
00239   downscale_struct *down; 
00240   double first_variance; 
00241 } field_data_struct;
00242 
00244 typedef struct {
00245 /* The dimension should be : 
00246  * Large-scale fields (0)
00247  * Large-scale Control Run fields (1)
00248  * Secondary large-scale fields (2)
00249  * Secondary large-scale Control Run fields (3)
00250  *
00251  * These variables are thus different for each of these categories
00252  */
00253   int n_ls; 
00254   int ntime_ls; 
00255   double *time_ls; 
00256   time_vect_struct *time_s; 
00257   double *lon_ls; 
00258   double *lat_ls; 
00259   int nlon_ls; 
00260   int nlat_ls; 
00261   double *lon_eof_ls; 
00262   double *lat_eof_ls; 
00263   int nlon_eof_ls; 
00264   int nlat_eof_ls; 
00265   double **precip_index; 
00266   analog_day_struct *analog_days; 
00267   analog_day_struct analog_days_year; 
00268   proj_struct *proj; 
00269   field_data_struct *data; 
00270 } field_struct;
00271 
00273 typedef struct {
00274   int ntime; 
00275   time_vect_struct *time_s; 
00276   double *time; 
00277   double *weight; 
00278   int *class_clusters; 
00279   double *precip_reg; 
00280   double *precip_reg_cst; 
00281   double *precip_reg_rsq; 
00282   double *precip_reg_err; 
00283   double *precip_reg_vif; 
00284   double *precip_reg_autocor; 
00285   double *precip_reg_dist; 
00286   double *precip_index; 
00287   double *precip_index_obs; 
00288   double *sup_index; 
00289   double sup_index_mean; 
00290   double sup_index_var; 
00291   double *sup_val; 
00292 } learning_data_struct;
00293 
00295 typedef struct {
00296   double *eof; 
00297   double *sing; 
00298   time_vect_struct *time_s; 
00299   int ntime; 
00300   char *filename_eof; 
00301   char *nomvar_eof; 
00302   char *nomvar_sing; 
00303 } learning_eof_struct;
00304 
00306 typedef struct {
00307   int learning_provided; 
00308   int learning_save; 
00309   char *filename_open_weight; 
00310   char *filename_open_learn; 
00311   char *filename_open_clust_learn; 
00312   char *filename_save_weight; 
00313   char *filename_save_learn; 
00314   char *filename_save_clust_learn; 
00315   char *filename_rea_sup; 
00316   char *nomvar_weight; 
00317   char *nomvar_class_clusters; 
00318   char *nomvar_precip_reg; 
00319   char *nomvar_precip_reg_cst; 
00320   char *nomvar_precip_index; 
00321   char *nomvar_precip_index_obs; 
00322   char *nomvar_precip_reg_dist; 
00323   char *nomvar_precip_reg_rsq; 
00324   char *nomvar_precip_reg_err; 
00325   char *nomvar_precip_reg_acor; 
00326   char *nomvar_precip_reg_vif; 
00327   char *nomvar_sup_index; 
00328   char *nomvar_sup_val; 
00329   char *nomvar_sup_index_mean; 
00330   char *nomvar_sup_index_var; 
00331   char *nomvar_pc_normalized_var; 
00332   char *nomvar_time; 
00333   char *nomvar_rea_sup; 
00334   double *pc_normalized_var; 
00335   double *lon; 
00336   double *lat; 
00337   int nlon; 
00338   int nlat; 
00339   int ntime; 
00340   double *sup_lon; 
00341   double *sup_lat; 
00342   int sup_nlon; 
00343   int sup_nlat; 
00344   char *sup_lonname; 
00345   char *sup_latname; 
00346   time_vect_struct *time_s; 
00347   int obs_neof; 
00348   int rea_neof; 
00349   char *rea_coords; 
00350   char *rea_gridname; 
00351   char *rea_dimxname; 
00352   char *rea_dimyname; 
00353   char *rea_lonname; 
00354   char *rea_latname; 
00355   char *rea_timename; 
00356   char *obs_dimxname; 
00357   char *obs_dimyname; 
00358   char *obs_lonname; 
00359   char *obs_latname; 
00360   char *obs_timename; 
00361   char *obs_eofname; 
00362   learning_eof_struct *obs; 
00363   learning_eof_struct *rea; 
00364   learning_data_struct *data; 
00365 } learning_struct;
00366 
00368 typedef struct {
00369   char *filename; 
00370   char *dimxname; 
00371   char *dimyname; 
00372   char *lonname; 
00373   char *latname; 
00374   char *ptsname; 
00375   double *lon; 
00376   double *lat; 
00377   int npts; 
00378   double dist; 
00379   int reg_save; 
00380   char *filename_save_ctrl_reg; 
00381   char *filename_save_other_reg; 
00382   char *timename; 
00383   char *dist_clusters_name; 
00384 } reg_struct;
00385 
00387 typedef struct {
00388   int use_mask; 
00389   double *field; 
00390   char *filename; 
00391   char *maskname; 
00392   char *lonname; 
00393   char *latname; 
00394   char *coords; 
00395   char *proj; 
00396   char *dimxname; 
00397   char *dimyname; 
00398   char *dimcoords; 
00399   double *lon; 
00400   double *lat; 
00401   int nlon; 
00402   int nlat; 
00403 } mask_struct;
00404 
00406 typedef struct {
00407   int nmonths; 
00408   int *month; 
00409   int nclusters; 
00410   int nreg; 
00411   int shuffle; 
00412   int secondary_choice; 
00413   int secondary_main_choice; 
00414   int secondary_cov; 
00415   int ndayschoices; 
00416   int ndayschoices_min; 
00417   int ndays; 
00418 } season_struct;
00419 
00421 typedef struct {
00422   int year_begin; 
00423   int month_begin; 
00424   int day_begin; 
00425   int year_end; 
00426   int month_end; 
00427   int day_end; 
00428   int downscale; 
00429 } period_struct;
00430 
00432 typedef struct {
00433   int debug; 
00434   int format; 
00435   int compression; 
00436   int compression_level; 
00437   int fixtime; 
00438   int year_begin_ctrl; 
00439   int year_begin_other; 
00440   char *config; 
00441   int clim_filter_width; 
00442   char *clim_filter_type; 
00443   char *cal_type; 
00444   char *time_units; 
00445   char *dimxname_eof; 
00446   char *dimyname_eof; 
00447   char *lonname_eof; 
00448   char *latname_eof; 
00449   char *eofname; 
00450   char *ptsname; 
00451   char *clustname; 
00452   double longitude_min; 
00453   double longitude_max; 
00454   double latitude_min; 
00455   double latitude_max; 
00456   double secondary_longitude_min; 
00457   double secondary_longitude_max; 
00458   double secondary_latitude_min; 
00459   double secondary_latitude_max; 
00460   double learning_mask_longitude_min; 
00461   double learning_mask_longitude_max; 
00462   double learning_mask_latitude_min; 
00463   double learning_mask_latitude_max; 
00464   mask_struct *learning_maskfile; 
00465   char *output_path; 
00466   int output_month_begin; 
00467   int nseasons; 
00468   season_struct *season; 
00469   int nperiods; 
00470   period_struct *period; 
00471   period_struct *period_ctrl; 
00472   int downscale; 
00473   char *classif_type; 
00474   int nclassifications; 
00475   int npartitions; 
00476   var_struct *obs_var; 
00477   int analog_save; 
00478   int output_only; 
00479   int output; 
00480   char *analog_file_ctrl; 
00481   char *analog_file_other; 
00482   int use_downscaled_year; 
00483   int only_wt; 
00484   double deltat; 
00485 } conf_struct;
00486 
00488 typedef struct {
00489   info_struct *info; 
00490   conf_struct *conf; 
00491   field_struct *field; 
00492   learning_struct *learning; 
00493   reg_struct *reg; 
00494   mask_struct *secondary_mask; 
00495 } data_struct;
00496 
00497 /* Prototypes */
00498 int load_conf(data_struct *data, char *fileconf);
00499 int wt_downscaling(data_struct *data);
00500 int wt_learning(data_struct *data);
00501 int read_large_scale_fields(data_struct *data);
00502 int read_large_scale_eof(data_struct *data);
00503 int read_learning_obs_eof(data_struct *data);
00504 int read_learning_rea_eof(data_struct *data);
00505 int read_learning_fields(data_struct *data);
00506 int read_obs_period(double **buffer, double **lon, double **lat, double *missing_value, data_struct *data, char *varname,
00507                     int *year, int *month, int *day, int *nlon, int *nlat, int ntime);
00508 int read_field_subdomain_period(double **buffer, double **lon, double **lat, double *missing_value, char *varname,
00509                                 int *year, int *month, int *day, double lonmin, double lonmax, double latmin, double latmax,
00510                                 char *coords, char *gridname, char *lonname, char *latname, char *dimxname, char *dimyname, 
00511                                 char *timename, char *filename, int *nlon, int *nlat, int ntime);
00512 int remove_clim(data_struct *data);
00513 int read_regression_points(reg_struct *reg);
00514 int read_mask(mask_struct *mask);
00515 int find_the_days(analog_day_struct analog_days, double *precip_index, double *precip_index_learn,
00516                   double *sup_field_index, double *sup_field_index_learn, double *sup_field, double *sup_field_learn, short int *mask,
00517                   int *class_clusters, int *class_clusters_learn, int *year, int *month, int *day,
00518                   int *year_learn, int *month_learn, int *day_learn, char *time_units,
00519                   int ntime, int ntime_learn, int *months, int nmonths, int ndays, int ndayschoices, int npts, int shuffle, int sup,
00520                   int sup_choice, int sup_cov, int use_downscaled_year, int only_wt, int nlon, int nlat, int sup_nlon, int sup_nlat);
00521 void compute_secondary_large_scale_diff(double *delta, double **delta_dayschoice, analog_day_struct analog_days, double *sup_field_index,
00522                                         double *sup_field_index_learn, double sup_field_var, double sup_field_var_learn, int ntimes);
00523 int merge_seasons(analog_day_struct analog_days_merged, analog_day_struct analog_days, int *merged_itimes, int ntimes_merged, int ntimes);
00524 int merge_seasonal_data(double *buf_merged, double *buf, analog_day_struct analog_days, int *merged_itimes, int dimx, int dimy,
00525                         int ntimes_merged, int ntimes);
00526 int merge_seasonal_data_i(int *buf_merged, int *buf, analog_day_struct analog_days, int *merged_itimes, int dimx, int dimy,
00527                           int ntimes_merged, int ntimes);
00528 int merge_seasonal_data_2d(double **buf_merged, double **buf, analog_day_struct analog_days, int *merged_itimes, int dimx, int dimy,
00529                            int supdim, int ntimes_merged, int ntimes);
00530 int output_downscaled_analog(analog_day_struct analog_days, double *delta, int output_month_begin, char *output_path,
00531                              char *config, char *time_units, char *cal_type, double deltat,
00532                              int file_format, int file_compression, int file_compression_level,
00533                              int debug,
00534                              info_struct *info, var_struct *obs_var, period_struct *period,
00535                              double *time_ls, int ntime);
00536 int write_learning_fields(data_struct *data);
00537 int write_regression_fields(data_struct *data, char *filename, double **timeval, int *ntime, double **precip_index, double **distclust,
00538                             double **sup_index);
00539 void read_analog_data(analog_day_struct *analog_days, double **delta, double **time_ls, char *filename, char *timename);
00540 void save_analog_data(analog_day_struct analog_days, double *delta, double **delta_dayschoice, double *dist, int *cluster,
00541                       double *time_ls, char *filename, data_struct *data);
00542 void free_main_data(data_struct *data);
00543 const char *get_filename_ext(const char *filename);
00544 
00545 #endif

Generated on 12 May 2016 for DSCLIM by  doxygen 1.6.1