00001 /* ***************************************************** */ 00002 /* compute_time_info Compute time info from NetCDF time. */ 00003 /* compute_time_info.c */ 00004 /* ***************************************************** */ 00005 /* Author: Christian Page, CERFACS, Toulouse, France. */ 00006 /* ***************************************************** */ 00007 /* Date of creation: oct 2008 */ 00008 /* Last date of modification: jul 2011 */ 00009 /* ***************************************************** */ 00010 /* Original version: 1.0 */ 00011 /* Current revision: 1.1 */ 00012 /* ***************************************************** */ 00013 /* Revisions */ 00014 /* 1.1: Updated for utCalendar2_cal (udunits2) */ 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 <io.h> 00064 00066 int 00067 compute_time_info(time_vect_struct *time_s, double *timeval, char *time_units, char *cal_type, int ntime) { 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 }