Пример #1
0
int safssi_stdat(fmgeopos gpos, fmprojspec myproj,
	osihdf *safssi, safssi_data *ssi) {

    char *where="safssi_stdat";
    int dx, dy, i, j, k, l, m;
    int nodata = 0;
    fmtime timeid;
    fmindex xyp, tgin;
    fmucspos tgxy;
    fmgeopos tgll;
    fmucsref rim;

    /*
     * Convert from image header to useable data structures, first
     * satellite id.
     */
    sprintf((*ssi).source,"%s", safssi->h.source);

    /*
     * The UCS info is converted
     */
    rim.Ax = safssi->h.Ax;
    rim.Ay = safssi->h.Ay;
    rim.Bx = safssi->h.Bx;
    rim.By = safssi->h.By;
    rim.iw = safssi->h.iw;
    rim.ih = safssi->h.ih;

    /*
    printf(" lat: %.2f lon: %.2f\n",gpos.lat,gpos.lon);
    printf(" Bx: %.2f By: %.2f Ax: %.2f Ay: %.2f\n",
	    rim.Bx,rim.By,rim.Ax,rim.Ay);
    */

    (*ssi).nav.Ax = rim.Ax;
    (*ssi).nav.Ay = rim.Ay;

    /*
     * Set UNIX time for product
     */
    timeid.fm_min = safssi->h.minute;
    timeid.fm_hour = safssi->h.hour;
    timeid.fm_mday = safssi->h.day;
    timeid.fm_mon = safssi->h.month;
    timeid.fm_year = safssi->h.year;
    (*ssi).vtime = tofmsec1970(timeid);;

    /*
     * Get UCS position of the requested geographical position within the
     * image.
     */
    tgxy = fmgeo2ucs(gpos, myproj);
    tgin = fmucs2ind(rim,tgxy);
    if (tgin.col < 0 || tgin.row < 0) {
	fmerrmsg(where, "This is out of image...");
	fprintf(stdout," lat: %.2f lon: %.2f\n",tgll.lat,tgll.lon);
	fprintf(stdout," northings: %d eastings: %d\n",tgxy.northings,tgxy.eastings);
	fprintf(stdout," myproj: %d\n", myproj);
	return(FM_SYNTAX_ERR);
    }
    (*ssi).nav.Bx = (float) tgxy.eastings;
    (*ssi).nav.By = (float) tgxy.northings;

    /*
     * Check bounding box size, these parameters are set by the init
     * function...
     */
    if ((*ssi).nav.iw%2 == 0 || (*ssi).nav.ih%2 == 0) {
	fmerrmsg("avhrr_stdat","area required must be odd\n");
	return(FM_SYNTAX_ERR); 
    }

    /*
     * Collect the actual data, l is used for pixel count within the
     * actual image, and k within the storage tile.
     */
    dx = (int) floorf((float) (*ssi).nav.iw/2.);
    dy = (int) floorf((float) (*ssi).nav.ih/2.);
    k = 0;
    for (i=(tgin.row-dy); i<=(tgin.row+dy); i++) {
	for (j=(tgin.col-dx); j<=(tgin.col+dx); j++) {
	    /*
	     * Check if within image coverage...
	     */
	    if (i < 0 || j < 0 || i >= rim.ih || j >= rim.iw) continue;
	    /*
	     * Navigation is performed in 2D while data is stored in 1D...
	     */
	    l = fmivec(j,i,rim.iw);
	    /*
	     * Collect data...
	     */
	    (*ssi).data[k] = ((float *) safssi->d[0].data)[l];
	    (*ssi).qflg[k] = ((unsigned short *) safssi->d[1].data)[l];
	    
	    /*
	     * Check number of unprocessed pixels
	     */
	    if ((*ssi).data[k] < -100.) nodata++;
	
	    k++;
	}
    }

    /*
     * If all pixels are unprocessed this is likely to be out of coverage,
     * return code to indicate this...
     */
    if (nodata == k) {
	fmlogmsg(where,"This request contains all unprocessed pixels.");
	return(FM_OTHER_ERR);
    }

    return(FM_OK);
}
Пример #2
0
int nwpice_read(char *fpath, char **filenames, int nrf, int nruns, fmtime
	reqtime, fmucsref refucs, nwpice *nwp) {

    char *where="nwpice_read";
    int i, imgsize;
    /* HIRLAM/NWP variables */
    int nfiles=FFNS, iunit=10, interp=1, itime[5];
    int nparam1=NOFIELDS1, nparam2=NOFIELDS2;
    int ierror, iundef, len1, len2;
    float satgrid[10], *nwpfield1, *nwpfield2;
    char **fnpl, **fnml, **fnsf;
    /*
    enum nwp_par {T0M,T2M,T950,T800,T750,T500,MSLP,PW,TOPO};
    */
    int iparam1[NOFIELDS1][4]={ /* Spec of what to get */
	{ 30, 2, 1000, 0},  /* Temp. at 0m */
    }; 
    /*
    int iparam2[NOFIELDS2][4]={ 
	{  0, 0,  0, 7}   
    }; 
    */
    int icontrol[6]={ /* Accep. spec */
	    88,  /* Producer 88 -> MI */
	-32767,  /* Model grid -32767-> first found */
	     3,  /* Min allowed forecast length in hours */
	   +24,  /* Max allowed forecast length in hours */
	    -3,  /* Min allowed offset in hours from image time */
	    +3   /* Max allowed offset in hours from image time */
    };
    fmtime nwptime;

    /* 
     * Create the grid specification used by feltfiles and libmi. 
     * Requested grid description, only one supported yet.
     */
    satgrid[0] = 60.;
    satgrid[1] = 0.;
    satgrid[2] = 1000.;
    satgrid[3] = 1000.;
    satgrid[4] = 0.;
    satgrid[5] = 0.;
    satgrid[6] = refucs.Ax;
    satgrid[7] = refucs.Ay;
    satgrid[8] = refucs.Bx;
    satgrid[9] = refucs.By;
    /* Requested valid time */
    itime[0] = reqtime.fm_year;
    itime[1] = reqtime.fm_mon;
    itime[2] = reqtime.fm_mday;
    itime[3] = reqtime.fm_hour;
    itime[4] = reqtime.fm_min;

    /*
     * Create the filenames to use. Currently only one level is required
     * for this software, implying that only one set of files containing
     * the surface parameters need to be read by this software. Memory
     * must be allocated contigous.
     */
    if (strstr(fpath,"/opdata")) {
	len1 =  strlen(fpath)+1+strlen(filenames[0])+6+1;
    } else if (strstr(fpath,"/starc")) {
	len1 =  strlen(fpath)+1+11+strlen(filenames[0])+6+1+9;
    } else {
	fmerrmsg(where,"Could not determine source for NWP data.");
	return(FM_IO_ERR);
    }
    if (fmalloc_byte_2d_contiguous(&fnsf,nruns,len1)){
	fmerrmsg(where,"Could not allocate fnsf");
	exit(FM_MEMALL_ERR);
    }
    for (i=0;i<nruns;i++) {
	if (strstr(fpath,"/opdata")) {
	    sprintf(fnsf[i],"%s/%s%02d.dat",fpath,filenames[0],(i*6));
	} else if (strstr(fpath,"/starc")) {
	    sprintf(fnsf[i],"%s/%4d/%02d/%02d/%s%02d.dat_%4d%02d%02d",
		    fpath,
		    reqtime.fm_year, reqtime.fm_mon,reqtime.fm_mday,
		    filenames[0],(i*6),
		    reqtime.fm_year, reqtime.fm_mon,reqtime.fm_mday);
	}
    }
    fmlogmsg(where,"Collecting HIRLAM data from %s",fpath);

    /*
     * The fields are returned in the vector "field[MAXIMGSIZE]" which is
     * organized like this: "field[nx*ny*nparam]", where nx and ny are
     * the numbers of gridpoints in x and y direction and nparam is the
     * number of fields/parameters collected. 
     */
    imgsize = refucs.iw*refucs.ih;
    if (imgsize > FMIO_MAXIMGSIZE) {
	fmerrmsg(where,"Data container for NWP is too small, the required size if %d while only %d is available.", imgsize, FMIO_MAXIMGSIZE);
	return(FM_IO_ERR);
    }
    nwpfield1 = (float *) malloc(NOFIELDS1*imgsize*sizeof(float));
    if (!nwpfield1) {
	fmerrmsg(where,"Could not allocate nwpfield1.");
	return(FM_MEMALL_ERR);
    }

    /*
     * Collect data from the files containing surface data.
     */
    /*
    getfield_(&nfiles, feltfilepresl[0], &iunit, &interp, 
	    satgrid, &refucs.iw, &refucs.ih, itime, 
	    &nparam1, iparam1, icontrol, nwpfield1, &iundef, &ierror, len1);
    */
    getfield_(&nfiles, fnsf[0], &iunit, &interp, 
	    satgrid, &refucs.iw, &refucs.ih, itime, 
	    &nparam1, iparam1, icontrol, nwpfield1, &iundef, &ierror, len1);

    if (ierror) {
	fmerrmsg(where,"error reading surface and parameter fields");
	return(FM_IO_ERR);
    }

    /*
     * Collect data from the files containing model level fields
     */
    /*
    for (i=0;i<FFNS;i++) {
	printf(" feltfilemodl %s\n",feltfilemodl[i]);
    }
    */
    /* Not needed...
    printf(" Then model fields are searched...\n");
    getfield_(&nfiles, feltfilemodl[0], &iunit, &interp, 
	    satgrid, &refucs.iw, &refucs.ih, itime, 
	    &nparam2, iparam2, icontrol, nwpfield2, &iundef, &ierror, len2);

    if (ierror) {
	errmsg(where,"error reading model level fields");
	return(2);
    }
    */

    /*
     * Print status information about the model field which was found.
     * if "iundef" is different from 0, undefined values were found
     * in the HIRLAM fields. The code for undefined is +1.e+35.
     */
    printf(" Date: %02d/%02d/%d", 
	    itime[2], itime[1], itime[0]);
    printf(" and time: %02d:00 UTC\n", itime[3]);
    printf(" Forecast length: %d\n", itime[4]);
    printf(" Status of 'getfield':");
    printf(" ierror=%d iundef=%d\n\n", ierror, iundef);

    /*
     * Transfer the time information to the nwpice structure
     */
    nwp->leadtime = itime[4];
    nwptime.fm_year = itime[0];
    nwptime.fm_mon = itime[1];
    nwptime.fm_mday = itime[2];
    nwptime.fm_hour = itime[3];
    nwptime.fm_min = 0;
    nwptime.fm_sec = 0;
    nwp->validtime = tofmsec1970(nwptime);

    /*
     * Transfer the UCS information
     */
    nwp->refucs = refucs;

    /*
     * Allocate the data structure that will contain the NWP data
     */
    /*
    if (!nwp->t950hpa) {
	nwp->t950hpa = (float *) malloc(imgsize*sizeof(float));
	if (!nwp->t950hpa) return(FM_MEMALL_ERR);
    }
    if (!nwp->t800hpa) {
	nwp->t800hpa = (float *) malloc(imgsize*sizeof(float));
	if (!nwp->t800hpa) return(FM_MEMALL_ERR);
    }
    if (!nwp->t700hpa) {
	nwp->t700hpa = (float *) malloc(imgsize*sizeof(float));
	if (!nwp->t700hpa) return(FM_MEMALL_ERR);
    }
    if (!nwp->t500hpa) {
	nwp->t500hpa = (float *) malloc(imgsize*sizeof(float));
	if (!nwp->t500hpa) return(FM_MEMALL_ERR);
    }
    if (!nwp->t2m) {
	nwp->t2m = (float *) malloc(imgsize*sizeof(float));
	if (!nwp->t2m) return(FM_MEMALL_ERR);
    }
    */
    if (!nwp->t0m) {
	nwp->t0m = (float *) malloc(imgsize*sizeof(float));
	if (!nwp->t0m) return(FM_MEMALL_ERR);
    }
    /*
    if (!nwp->ps) {
	nwp->ps = (float *) malloc(imgsize*sizeof(float));
	if (!nwp->ps) return(FM_MEMALL_ERR);
    }
    if (!nwp->pw) {
	nwp->pw = (float *) malloc(imgsize*sizeof(float));
	if (!nwp->pw) return(FM_MEMALL_ERR);
    }
    if (!nwp->rh) {
	nwp->rh = (float *) malloc(imgsize*sizeof(float));
	if (!nwp->rh) return(FM_MEMALL_ERR);
    }
    if (!nwp->topo) {
	nwp->topo = (float *) malloc(imgsize*sizeof(float));
	if (!nwp->topo) return(FM_MEMALL_ERR);
    }
    */

    /*
     * Transfer the NWP data from the local temporary array to the nwpice
     * structure
     */
    for (i=0;i<imgsize;i++) {
	/*
	 * Surface, parameter and pressure fields
	 */
	nwp->t0m[i] = nwpfield1[0*imgsize+i];
	/*
	nwp->t2m[i] = nwpfield1[1*imgsize+i];
	nwp->t950hpa[i] = nwpfield1[2*imgsize+i];
	nwp->t800hpa[i] = nwpfield1[3*imgsize+i];
	nwp->t700hpa[i] = nwpfield1[4*imgsize+i];
	nwp->t500hpa[i] = nwpfield1[5*imgsize+i];
	nwp->ps[i] = nwpfield1[6*imgsize+i];
	nwp->topo[i] = nwpfield1[7*imgsize+i];
	nwp->rh[i] = nwpfield1[8*imgsize+i];
	*/
	
	/*
	 * Model level fields
	 */
	/*
	nwp->pw[i] = nwpfield2[0*imgsize+i];
	*/
    }

    /*
     * Free allocated memory
     */
    free(nwpfield1);
    /*
    free(nwpfield2);
    */

    return(FM_OK);
}
Пример #3
0
fmsec1970 ymdhms2fmsec1970(char *str, int mode) {

    char *where="ymdhms2fmsec1970";
    fmsec1970 secs;
    fmtime t;
    int i;
    char *dummy;

    /*
     * Check the input.
     */
    if (strlen(str) < 14) {
	fmerrmsg(where,"Input datetime string is too short");
	return(-1);
    }
    
    dummy = (char *) malloc(5*sizeof(char));
    if (!dummy) return(2);

    /*
     * Initialize
     */
    for (i=0; i<5; i++) {
	dummy[i] = '\0';
    }

    /*
     * Get year
     */
    for (i=0;i<4;i++) {
	dummy[i] = str[i];
    }
    t.fm_year = atoi(dummy);
    dummy[2] = dummy[3] = '\0';

    /*
     * Get month
     */
    for (i=0;i<2;i++) {
	dummy[i] = str[4+i];
    }
    t.fm_mon = atoi(dummy);
    if (t.fm_mon < 1 || t.fm_mon > 12) {
	t.fm_mon = -9; 
    } 

    /*
     * Get day
     */
    for (i=0;i<2;i++) {
	dummy[i] = str[6+i];
    }
    t.fm_mday = atoi(dummy);
    if (t.fm_mday < 1 || t.fm_mday > 31) t.fm_mday = -9; 

    /*
     * Get hour
     */
    for (i=0;i<2;i++) {
	dummy[i] = str[8+i];
    }
    t.fm_hour = atoi(dummy);
    if (t.fm_hour < 0 || t.fm_hour > 23) t.fm_hour = -9; 

    /*
     * Get minute
     */
    for (i=0;i<2;i++) {
	dummy[i] = str[10+i];
    }
    t.fm_min = atoi(dummy);
    if (t.fm_min < 0 || t.fm_min > 59) t.fm_min = -9; 

    /*
     * Get seconds
     */
    for (i=0;i<2;i++) {
	dummy[i] = str[12+i];
    }
    t.fm_sec = atoi(dummy);
    if (t.fm_sec < 0 || t.fm_sec > 59) t.fm_sec = -9; 

    /*
     * Release resiources
     */
    free(dummy);

    if (mode > 0) {
	printf(" %4d-%02d-%02d %02d:%02d:%02d\n",
		t.fm_year,t.fm_mon,t.fm_mday,
		t.fm_hour,t.fm_min,t.fm_sec);
    }
    if (t.fm_sec < 0 || t.fm_min < 0 || t.fm_hour < 0 || t.fm_mday < 0 ||
	t.fm_mon < 0 || t.fm_year < 0) {
	fmerrmsg(where,"Decoding of datetime string failed");
	fmlogmsg(where,"Decoding of datetime string failed");
    }
    
    secs = tofmsec1970(t);

    return(secs);
}