00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
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;
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;
00105 time_vect_struct *time_s = NULL;
00106 char *cal_type = NULL;
00107 char *time_units = NULL;
00108 double *lon_total = NULL;
00109 double *lat_total = NULL;
00110 int ntime_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
00136 info->title = strdup("none");
00137
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
00144 istat = compute_time_info(time_s, time_ls, time_units, cal_type, ntime_file);
00145
00146
00147 ntime_sub = 0;
00148 for (nt=0; nt<ntime; nt++) {
00149
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
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
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
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
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
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
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
00247 return 0;
00248 }