/***************************************************************************** * * write_grhdr * * LAST UPDATE: 31 December 1997 * * DESCRIPTION: * Writes output header file in CLIMSAT (GrADS-like) format. * * MODIFICATIONS: * Catherine Cormack 11/16/01 localtime returns years as number of * years since 1900. Modified sprintf to * add 1900 to years. * Don Anderson 12/08/97 Corrected error in TDEF month handling: * strcat ( tdefdate, mon[month] ) ; changed to * strcat ( tdefdate, mon[month-1] ) ; * * Wesley Berg 12/31/97 Modified inputs for grid_data routine * *****************************************************************************/ #include #include #include "common_structures" extern FILE *inf, *opf, *opf2, *stf, *hdf; int write_grhdr ( char *header_file, char *data_file, long stime, long etime, double xstart, double xend, int xdim, double ystart, double yend, int ydim, struct std_hdr hdr ) { char title[80] ; char subtitle[80] ; char cdate[20] ; char datatype_str[8] ; char error_flag ; char sdate[20] ; time_t st; char tdeftime[6] ; char tdefdate[10] ; char tinc[5] ; int tval ; int tdif ; char varstr[80] ; char xdefstr[60] ; double xstartcen ; double xinc ; char xunit[10] ; char ydefstr[60] ; char yearstr[3] ; double ystartcen ; double yinc ; char yunit[10] ; int i ; static char *mon[]={"JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG", "SEP","OCT","NOV","DEC"} ; struct tm *lt; /** Massage all the date data **/ strcpy ( title, hdr.sensor ) ; strcpy ( subtitle, hdr.filename ) ; error_flag = 0 ; date(tdefdate, tdeftime, stime) ; /* printf("tdefdate = %s, tdeftime = %s\n", tdefdate,tdeftime); */ tdif = etime - stime; if (tdif < 0) tdif = stime - etime; if (tdif >= 31536000 ) { strcpy(tinc,"YR"); tval=(int)(tdif/31536000); } else if (tdif >= 2592000 ) { strcpy(tinc,"MO"); tval=(int)(tdif/2592000) ; } else if (tdif >= 86400 ) { strcpy(tinc,"DY"); tval=(int)(tdif/86400) ; } else if (tdif >= 3600 ) { strcpy(tinc,"HR"); tval=(int)(tdif/3600) ; } else { strcpy(tinc,"MN"); tval=(int)(tdif/60) ; } /** Determine lower left pixel center x,y **/ xinc = ( xend - xstart ) / xdim ; yinc = ( yend - ystart ) / ydim ; xstartcen = xstart + ( xinc / 2.0 ) ; ystartcen = yend - ( yinc / 2.0 ) ; /** Specify x, y units **/ strcpy ( xunit, "DD" ) ; strcpy ( yunit, "DD" ) ; /** Get current time **/ st = time ( NULL ) ; lt = localtime ( &st ) ; /** Specify data type **/ strcpy ( datatype_str, "INTEGER" ) ; /** Write out the dang file **/ sprintf ( cdate, "%2.2d%3s%4d",lt->tm_mday,mon[lt->tm_mon], lt->tm_year+1900 ) ; sprintf ( xdefstr, "%d LINEAR %10.4f %7.4f %s", xdim, xstartcen, xinc, xunit ) ; sprintf ( ydefstr, "%d LINEAR %10.4f %7.4f %s", ydim, ystartcen, -yinc, yunit ) ; fprintf ( hdf, "DSET %s\n", data_file ) ; fprintf ( hdf, "TITLE %s\n", title ) ; fprintf ( hdf, "SUBTITLE %s\n", subtitle ) ; fprintf ( hdf, "CREATED %s NOAA-CIRES/CDC\n", cdate ) ; fprintf ( hdf, "UNDEF -1\n" ) ; fprintf ( hdf, "DTYPE %s\n", datatype_str ) ; fprintf ( hdf, "XDEF %s\n", xdefstr ) ; fprintf ( hdf, "YDEF %s\n", ydefstr ) ; fprintf ( hdf, "ZDEF 1 LINEAR 0 1\n" ) ; fprintf ( hdf, "TDEF 1 LINEAR %sZ%s %3d%2s\n", tdeftime, tdefdate, tval, tinc ) ; fprintf ( hdf, "VARS %2d\n",hdr.fields+hdr.hifields ) ; for (i=0; i