extract_subdomain.c File Reference

Extract subdomain in a variable given latitudes and longitudes. More...

#include <utils.h>
Include dependency graph for extract_subdomain.c:

Go to the source code of this file.

Functions

void extract_subdomain (double **buf_sub, double **lon_sub, double **lat_sub, int *nlon_sub, int *nlat_sub, double *buf, double *lon, double *lat, double minlon, double maxlon, double minlat, double maxlat, int nlon, int nlat, int ndim)
 Extract subdomain in a variable given latitudes and longitudes.

Detailed Description

Extract subdomain in a variable given latitudes and longitudes.

Definition in file extract_subdomain.c.


Function Documentation

void extract_subdomain ( double **  buf_sub,
double **  lon_sub,
double **  lat_sub,
int *  nlon_sub,
int *  nlat_sub,
double *  buf,
double *  lon,
double *  lat,
double  minlon,
double  maxlon,
double  minlat,
double  maxlat,
int  nlon,
int  nlat,
int  ndim 
)

Extract subdomain in a variable given latitudes and longitudes.

Parameters:
[out] buf_sub 3D buffer spanning only subdomain
[out] lon_sub Longitude array spanning only subdomain
[out] lat_sub Latitude array spanning only subdomain
[out] nlon_sub Longitude dimension length spanning only subdomain
[out] nlat_sub Latitude dimension length spanning only subdomain
[in] buf 3D input buffer
[in] lon Longitude array
[in] lat Latitude array
[in] minlon Subdomain bounds: minimum longitude
[in] maxlon Subdomain bounds: maximum longitude
[in] minlat Subdomain bounds: minimum latitude
[in] maxlat Subdomain bounds: maximum latitude
[in] nlon Longitude dimension length
[in] nlat Latitude dimension length
[in] ndim Third dimension length

Definition at line 59 of file extract_subdomain.c.

References alloc_error().

Referenced by main(), read_field_subdomain_period(), read_large_scale_eof(), read_large_scale_fields(), wt_downscaling(), and wt_learning().

00061                                                 {
00080   /* Compute subdomain and apply to arrays */
00081   
00082   int i; /* Loop counter */
00083   int j; /* Loop counter */
00084   int t; /* Time loop counter */
00085   int ii; /* Subdomain loop counter */
00086   int jj; /* Subdomain loop counter */
00087   double curlon; /* Current longitude */
00088   double curlat; /* Current latitude */
00089 
00090   /* Initializing */
00091   *nlon_sub = *nlat_sub = 0;
00092 
00093   /* Count latitude dimension length */
00094   for (i=0; i<nlat; i++)
00095     if (lat[i*nlon] >= minlat && lat[i*nlon] <= maxlat)
00096       (*nlat_sub)++;
00097 
00098   /* Count longitude dimension length */
00099   /* Adjust to span -180 to +180 */
00100   for (i=0; i<nlon; i++) {
00101     if (lon[i] > 180.0)
00102       curlon = lon[i] - 360.0;
00103     else
00104       curlon = lon[i];
00105     if (curlon >= minlon && curlon <= maxlon)
00106       (*nlon_sub)++;
00107   }
00108 
00109   /* Allocate memory with dimension lengths */
00110   (*buf_sub) = (double *) malloc((*nlon_sub)*(*nlat_sub)*ndim * sizeof(double));
00111   if ((*buf_sub) == NULL) alloc_error(__FILE__, __LINE__);
00112   (*lon_sub) = (double *) malloc((*nlon_sub)*(*nlat_sub) * sizeof(double));
00113   if ((*lon_sub) == NULL) alloc_error(__FILE__, __LINE__);
00114   (*lat_sub) = (double *) malloc((*nlon_sub)*(*nlat_sub) * sizeof(double));
00115   if ((*lat_sub) == NULL) alloc_error(__FILE__, __LINE__);
00116 
00117   /* Loop over all gridpoints and construct new buffer array spanning only subdomain */
00118   ii = 0;
00119   jj = 0;
00120   /* Loop over latitudes */
00121   for (j=0; j<nlat; j++) {
00122     if (ii > 0)
00123       jj++;
00124     ii = 0;
00125     /* Loop over longitudes */
00126     for (i=0; i<nlon; i++) {
00127       /* Adjust longitude to span -180 to +180 */
00128       if (lon[i] > 180.0)
00129         curlon = lon[i] - 360.0;
00130       else
00131         curlon = lon[i+j*nlon];
00132       curlat = lat[i+j*nlon];
00133       /* Retrieve only gridpoints within bounds */
00134       if (curlon >= minlon && curlon <= maxlon && curlat >= minlat && curlat <= maxlat) {
00135         /* Loop over last dimension to assign all values for this gridpoint */
00136         for (t=0; t<ndim; t++)
00137           (*buf_sub)[ii+jj*(*nlon_sub)+t*(*nlon_sub)*(*nlat_sub)] = buf[i+j*nlon+t*nlon*nlat];
00138         /* Create also latitude and longitude arrays */
00139         (*lon_sub)[ii+jj*(*nlon_sub)] = lon[i+j*nlon];
00140         (*lat_sub)[ii+jj*(*nlon_sub)] = lat[i+j*nlon];
00141         ii++;
00142       }
00143     }
00144   }
00145 }


Generated on 12 May 2016 for DSCLIM by  doxygen 1.6.1