/* cpp wrappers for the functions contained in the sir_ez, sir_geom and sir_io libraries. written 18 May 2002 by DGL at BYU (c) 2002,2003 by BYU MERS */ #include #include #include #include #include "libsir.h" /* print SIR file header from sirheader structure */ void print_sir_header(struct sirheader *h) { fprintf(stdout,"SIR file header: \n"); print_sir_head(stdout, h); } /* given open file pointer, read SIR file header into sirheader structure */ int read_sir_header(FILE *imf, struct sirheader *h) { return get_sir_head_file(imf, h); } /* Given open file pointer and previously read sirheader structure, read SIR file image data into previously allocated memory. The user is responsible for allocating storage space before calling this function! Size required is header.nsx*header.nsy*sizeof(float). */ int read_sir_data(FILE *imf, float *data, struct sirheader *h) { return get_sir_data(imf, data, h); } /* loads a SIR file into memory. Returns pointer to allocated image array and sirheader structure. */ float *LoadSIR(char *filename, struct sirheader *h) { FILE *file; long size; /* size of the image in bytes. */ float *data; /* make sure the file is there. */ if ((file = fopen(filename, "rb"))==NULL) { fprintf(stderr, "File Not Found : %s\n",filename); return NULL; } sir_init_head(h); read_sir_header(file, h); size = (long)h->nsx * (long)h->nsy * (long)sizeof(float); data = (float*)malloc(size); if(data==NULL) { fprintf(stderr, "Error allocating SIR data memory\n"); return NULL; } read_sir_data(file, data, h); return data; } /* given an x,y pixel location, returns lat,lon values */ void pixtolatlon(struct sirheader *sh, float px, float py, float *lon, float *lat) { sir_pix2latlon(px, py, lon, lat, sh); } /* given an x,y pixel location as integers, returns lat,lon values */ void ipixtolatlon(struct sirheader *sh, int px, int py, float *lon, float *lat) { sir_pix2latlon((float) px, (float) py, lon, lat, sh); } /* given lat,lon value, returns x,y pixel location (may be outside of image). returns -1 if x,y is outside of image, otherwise returns lexicographic index (zero based for C) for corresponding pixel */ int latlon2pix(struct sirheader *sh, float lon, float lat, float *px, float *py) { int ix, iy; sir_latlon2pix(lon, lat, px, py, sh); f2ipix(*px, *py, &ix, &iy, sh->nsx, sh->nsy); if (ix == 0 || iy == 0) return(-1); else return((iy-1) * sh->nsx + (ix-1)); } /* given lat,lon value, returns quantized x,y pixel location within image. zero x or y values indicate pixel is out of image */ void ilatlon2pix(struct sirheader *sh, float lon, float lat, int *px, int *py) { float fx, fy; sir_latlon2pix(lon, lat, &fx, &fy, sh); sir_pix(fx, fy, px, py, sh); } /* read a selected rectangular block of data from SIR file */ int sir_datablock(FILE *imf, float *stval, struct sirheader *sh, int x1, int y1, int x2, int y2) { return(get_sir_data_block(imf, stval, sh, x1, y1, x2, y2)); } /* writes a SIR file. returns 0 if no error, negative value for an erro (see write_sir3 for error codes) */ int PutSIR(char *filename, struct sirheader *h, float *image) { return(put_sir(filename, h, image)); } /* returns the lexicographic array index (zero-based for use with C) for the x,y th pixel. Returns -1 if x,y is outside of the image x,y are 1-based. */ int SIRlex(int x, int y, struct sirheader *head) { if (x < 1 || x > head->nsx || y < 1 || y > head->nsy) return(-1); else return((y-1) * head->nsx + (x-1)); } /* given the lexicographic array index (zero-based for use with C) returns the corresponding x,y pixel values. x,y are set to zero if index is outside of image. Returns 0 if index is inside image and -1 if outside. x,y are 1-based. */ int iSIRlex(int *x, int *y, int i, struct sirheader *head) { if (i < 0 || i >= head->nsx * head->nsy) { *x = 0; *y = 0; return(-1); } else { *x = 1 + (i % head->nsx); *y = 1 + (i / head->nsx); return(0); } }