Exemple #1
0
static int parse_metar(char *file){
    FILE *fp;
    char *s, *c;
    int ovector[ovecsize];
    int ovalue;
    int len;
    float f;
    int i, j;

    reset_current(&current);
    if((fp=fopen(file, "r"))==NULL) return 0;
    len=fread(bigbuf, sizeof(char), BIGBUF_LEN-2, fp);
    fclose(fp);
    if(len<1) return 0;
    for(i=0; i<len; i++){
        if(isspace(bigbuf[i])) bigbuf[i]=' ';
    }
    c=strstr(bigbuf, " RMK");
    if(c!=NULL) *(c+1)='\0';
    c=strstr(bigbuf, " TEMPO");
    s=strstr(bigbuf, " BECMG");
    if(c!=NULL) *(c+1)='\0';
    if(s!=NULL) *(s+1)='\0';
    /* XXX: parse trend forecast data? */

    len=strlen(bigbuf);
    if(bigbuf[len-1]!=' '){
        bigbuf[len++]=' ';
        bigbuf[len]='\0';
    }

    /* Look for something like a METAR coded report */
    ovalue=pcre_exec(station_time, NULL, bigbuf, len, 0, 0, ovector, ovecsize);
    if(ovalue<=0) return 0;
    if(pcre_get_substring(bigbuf, ovector, ovalue, 1, (const char **)&c)<0) return 0;
    if(c[0]!='\0') current.date=atoi(c);
    pcre_free_substring(c);
    if(pcre_get_substring(bigbuf, ovector, ovalue, 2, (const char **)&c)<0) return 0;
    current.time=atoi(c);
    pcre_free_substring(c);

    /* Chop off extraneous stuff */
    if(pcre_get_substring(bigbuf, ovector, ovalue, 3, (const char **)&s)<0) return 0;

    /* windspeed, winddir */
    ovalue=pcre_exec(wind, NULL, s, len, 0, 0, ovector, ovecsize);
    if(ovalue>0){
        get_substr(4, c);
        if(c[0]!='\0'){
            current.winddir=0;
        } else {
            pcre_free_substring(c);
            get_substr(1, c);
            if(c[0]=='V') current.winddir=0;
            else current.winddir=((int)((atoi(c)+11.25)/22.5))%16+1;
        }
        pcre_free_substring(c);
        get_substr(2, c);
        current.windspeed=atoi(c);
        pcre_free_substring(c);
        get_substr(3, c);
        if(c[0]=='M'){ /* MPS */
            current.windspeed=mps2knots(current.windspeed);
        } else if(c[0]=='K' && c[1]=='M'){ /* KMH */
            current.windspeed=kph2knots(current.windspeed);
        }
    }

    /* vis */
    f=99;
    c=strstr(s, " M1/4SM ");
    if(c!=NULL){
        f=0;
        goto wind_done;
    }
    ovalue=pcre_exec(vis[2], NULL, s, len, 0, 0, ovector, ovecsize);
    if(ovalue>0){
        get_substr(2, c);
        i=atoi(c);
        pcre_free_substring(c);
        get_substr(3, c);
        j=atoi(c);
        pcre_free_substring(c);
        get_substr(1, c);
        f=atoi(c)+(float)i/j;
        pcre_free_substring(c);
        goto wind_done;
    }
    ovalue=pcre_exec(vis[1], NULL, s, len, 0, 0, ovector, ovecsize);
    if(ovalue>0){
        get_substr(2, c);
        i=atoi(c);
        pcre_free_substring(c);
        get_substr(1, c);
        f=(float)atoi(c)/i;
        pcre_free_substring(c);
        goto wind_done;
    }
    ovalue=pcre_exec(vis[0], NULL, s, len, 0, 0, ovector, ovecsize);
    if(ovalue>0){
        get_substr(1, c);
        f=atoi(c);
        pcre_free_substring(c);
        goto wind_done;
    }
    c=strstr(s, " CAVOK ");
    if(c!=NULL){
        f=99;
        current.sky=0;
        goto wind_done;
    }
    ovalue=pcre_exec(vis[3], NULL, s, len, 0, 0, ovector, ovecsize);
    if(ovalue>0){
        get_substr(1, c);
        f=m2mi(atoi(c));
        pcre_free_substring(c);
        goto wind_done;
    }
wind_done:
    if(f<=6) current.vis=6;
    if(f<=5) current.vis=5;
    if(f<3) current.vis=4;
    if(f<1) current.vis=3;
    if(f<=.5) current.vis=2;
    if(f<=.25) current.vis=1;

    /* temp, rh */
    ovalue=pcre_exec(temp, NULL, s, len, 0, 0, ovector, ovecsize);
    if(ovalue>0){
        get_substr(1, c);
        if(c[0]=='M') c[0]='-';
        current.temp=atoi(c);
        pcre_free_substring(c);
        get_substr(2, c);
        if(c[0]!='\0'){
            if(c[0]=='M') c[0]='-';
            current.rh=rh_C(current.temp, atoi(c));
        }
        pcre_free_substring(c);
    }

    /* pressure */
    ovalue=pcre_exec(pressure, NULL, s, len, 0, 0, ovector, ovecsize);
    if(ovalue>0){
        get_substr(2, c);
        i=atoi(c);
        pcre_free_substring(c);
        get_substr(1, c);
        if(c[0]=='Q'){
            current.pressure=hPa2inHg(i);
        } else {
            current.pressure=i/100.0;
        }
        pcre_free_substring(c);
    }

    /* sky */
    if(strstr(s, " SKC")!=NULL || strstr(s, " CLR")!=NULL) current.sky=0;
    if(strstr(s, " FEW")!=NULL) current.sky=1;
    if(strstr(s, " SCT")!=NULL) current.sky=2;
    if(strstr(s, " BKN")!=NULL) current.sky=3;
    if(strstr(s, " OVC")!=NULL || strstr(s, " VV")!=NULL) current.sky=4;

    /* obs, frz, snow, rain, tstorm */
    /* There can be multiple weather chunks, so we while loop */
    j=0;
    while((ovalue=pcre_exec(weather, NULL, s, len, j, 0, ovector, ovecsize))>0){{
        char *in, *de, *pp, *ob, *ot;

        j=ovector[0]+1;
        get_substr(0, c);
        i=(c[1]=='\0');
        pcre_free_substring(c);
        if(i) continue;


        get_substr(1, in);
        get_substr(2, de);
        get_substr(3, pp);
        get_substr(4, ob);
        get_substr(5, ot);

#define IN(haystack, needle) ((needle[0]=='\0')?0:strstr(haystack, needle))
        if(current.obs<1 && strcmp(de, "FZ") && IN("BR|FG", ob))
            current.obs=1;
        if(current.obs<2 && IN("FU|VA|DU|SA|HZ|PY", ob))
            current.obs=2;
        if(current.obs<3 && IN("PO|SS|DS", ot))
            current.obs=3;
        if(current.obs<3 && IN("DR|BL", de)
           && (strstr(pp, "SN") || IN("DU|SA|PY", ob)))
            current.obs=3;
        if(!strcmp(ot, "FC")){
            current.sky=5;
            current.obs=99;
            current.vis=7;
        }
#undef IN

        i=66;
        if(in[0]=='-' || in[0]=='V') i=33;
        if(in[0]=='+') i=99;
        if(!strcmp(de, "SH")) i=33;
        if(current.frz<i
           && ((!strcmp(de, "FZ") && (strstr(pp, "DZ") || strstr(pp, "RA")))
               || strstr(pp, "IC") || strstr(pp, "PE") || strstr(pp, "PL")
               || strstr(pp, "GR") || strstr(pp, "GS")))
                current.frz=i;
        if(current.snow<i && strcmp(de, "BL")
           && (strstr(pp, "SN") || strstr(pp, "SG")))
            current.snow=i;
        if(current.rain<i && (strstr(pp, "UP")
                              || (strcmp(de, "FZ")
                                  && (strstr(pp, "DZ") || strstr(pp, "RA")))))
            current.rain=i;
        if(current.tstorm<i && !strcmp(de, "TS"))
            current.tstorm=i;

        pcre_free_substring(in);
        pcre_free_substring(de);
        pcre_free_substring(pp);
        pcre_free_substring(ob);
        pcre_free_substring(ot);
    }}
    if(current.obs==99) current.obs=0;

    pcre_free_substring(s); /* Done parsing! Just a few final calculations... */

    current.heatindex=heatindex_C(current.temp, current.rh);
    current.windchill=windchill_C(current.temp, current.windspeed);

    /* Figure out the proper month... */
    {
        int mon, day, year, time2; /* holds UTC */
        int y; /* with current.*, holds local time */
        time_t t=time(NULL);
        struct tm *tm=gmtime(&t);
        current.month=tm->tm_mon+1;
        if(tm->tm_mday<current.date) current.month--;
        if(current.month<1){ current.month+=12; tm->tm_year--; }
        y=year=tm->tm_year;
        mon=current.month;
        day=current.date;
        time2=current.time;
        current.time=utc2local((int)current.time, &current.month, &current.date, &y, NULL);

        if(latitude!=999 && calcSolarZenith(latitude, longitude, year, mon, day, hm2min(time2))>90)
            current.moon=calc_moon(current.month, current.date, y, current.time);
    }
    return 1;
}
Exemple #2
0
int edit_image(char filename[], char newfilename[], double **ppix )
{
 fitsfile *fptr, *newfptr;
 int status, nfound, day, month, year, timeref;
 int naxis = 2;			/* 2-dimensional image */
 int bitpix = DOUBLE_IMG;		/* Output image type is double */
 int datatype = TDOUBLE;
 long naxes[2];			/* dimensions of the image */
 long fpixel, npixels;
 char date[FLEN_VALUE], time[FLEN_VALUE];
 char *nullcomment;
 char outfilename[MAXLENGTH];

 nullcomment=0;

  /***********************************************************************/
  status = 0;		/* Initialize status before calling fitsio routines */
  /***********************************************************************/

 /* Copy newfilename to outfilename.  'newfilename' is passed has a reference,
  * therefore any change to it will be passed back to the calling routine
  * which is not really what one wants.  (as I discovered...)
  * The addition of the '!' is meaningful here but not necessarly useful in
  * the calling routine. */
 if (!strcmp(newfilename,filename)) {
   strcpy(outfilename,"!");
   strcat(outfilename,filename);
 }
 else {
   strcpy(outfilename,newfilename);
 }
 printf("Writing %s ... ",newfilename);
 fflush(stdout);

/* Open the existing FITS file */
 if ( fits_open_file(&fptr, filename, READONLY, &status) )
 	printerror( status );

/* Create the new FITS file  (should I use a template?)*/
 if ( fits_create_file(&newfptr, outfilename, &status) )
 	printerror( status );

 /* No need to create an image in the new FITS file because the primary
  * array is automatically created when the header is copied */

/* Copy the header from the existing FITS file to the new FITS file */
 if ( fits_copy_header(fptr, newfptr, &status) )
 	printerror( status );
 
/* Get the length of the axes */
 if ( fits_read_keys_lng(newfptr, "NAXIS", 1, 2, naxes, &nfound, &status) )
 	printerror( status );

/* Write the image (array of pixel values) in the new FITS file */
 fpixel = 1;				/* first pixel to write */
 npixels = naxes[0] * naxes[1];	/* number of pixels to write */

 if ( fits_write_img(newfptr, datatype, fpixel, npixels, ppix[0], &status) )
 	printerror( status );

/* Edit header */
 if ( fits_get_system_date(&day, &month, &year, &status) )
 	printerror( status );
 if ( fits_date2str(year, month, day, date, &status) )
 	printerror( status );
 if ( fits_update_key(newfptr, TSTRING, "DATE", date, "", &status) )
 	printerror( status );

 if ( fits_get_system_time(time, &timeref, &status) )
 	printerror( status );
 if ( timeref == 0 )		/* Convert UTC/GMT to Local time */
 	if ( utc2local(time, FITS_FMT) ) {
		printf("Error : Unable to convert time.\n");
		exit(1);
	} 
 if ( status = cnvtimefmt(time, FITS_FMT, IRAF_FMT))
 	printerror( status ); 	/* Stupid IRAF cannot read the new FITS
 					 *  date-time Y2K format */
 if ( fits_update_key(newfptr, TSTRING, "IRAF-TLM", time, nullcomment, &status) )
 	printerror( status );
 
/* Close the existing FITS file */
 if ( fits_close_file(fptr, &status) )
 	printerror( status );

/* Close the new FITS file */
 if ( fits_close_file(newfptr, &status) )
 	printerror( status );

 printf("done\n");

 return(0);
}