#include #include /* Sample program to read AMeDAS data by Naoki Sato, June 11, 2001 revised by Naoki Sato, November 1, 2010 revised by Naoki Sato, September 15, 2011 revised by Naoki Sato, July 11, 2012 ======================================== Subroutine amdhr reads AMeDAS data from the data files. Output of subroutine amdhr itype : Type of the station. 1: Precipitation only. 4: Precipitation, wind, sunshine duration and temperature. ir : Precipitation during the last one hour [mm/hr] id : Wind direction 1: NNE, 2: NE, ... , 16: N iw : Wind speed [m/s] s : Sunshine duration [hr/hr] t : Temperature [C] ir and s are the total amounts during the last one hour. Note that id, iw, s and t are available only when itype = 4. Input of subroutine amdhr ihh : Time [o'clock]. e.g. ihh = 12 : 12:00 JST. idd, imm, iyy : Date. e.g. idd = 15, imm = 8, iyy = 1995 : August 15, 1995. ihmax, idmax : The maximum values of ihh and idd. ihmax and idmax must be set as ihmax = 24 and idmax = 31. nmax : The number of stations where you want to investigate the data. ista : Station No. e.g. ista = 44126 : Setagaya. e.g. If you want to look into the data at Setagaya (44126), Tokyo (44131) and Sagamihara (46046), set nmax and ista as follows. nmax = 3, ista[0] = 44126, ista[1] = 44131, ista[2] = 46046. In this sample program, nmax is set as nmax = 1, intending to look data at only one station. An example of the usage Assume that you want to know the precipitation from 1100 to 1200 on August 15, 1995 at Setagaya (44126) and Tokyo (44131). First, please assign the proper values to ihmax (=24) and idmax (=31), and set nmax as nmax = 2. Then, please set the values of ista, iyy and imm as ista[0] = 44126, ista[1] = 44131, iyy = 1995, imm = 8. After calling subroutine amdhr, you will get the values at the two stations as those of ir[(12-1)+(15-1)*24+(1-1)*31*24] and ir[(12-1)+(15-1)*24+(2-1)*31*24]. */ int amdhr( int ihmax, int idmax, int nmax, int *ista, int iyy, int imm, int imiss, float rmiss, int *itype, int *ir, int *id, int *iw, float *s, float *t ); int amdid( int nmax, int *ista, float *xx, float *yy, float *hh ); char *cmonth( int im ); char *cdirec( int i ); int print( int ihmax, int idmax, int nmax, int *ista, int n, int iyy, int imm, int idd, int imiss, float rmiss, int *itype, int *ir, int *id, int *iw, float *s, float *t ); char *cmonth( int im ) { char *cmonth; if (im == 1) cmonth = "Jan."; if (im == 2) cmonth = "Feb."; if (im == 3) cmonth = "Mar."; if (im == 4) cmonth = "Apr."; if (im == 5) cmonth = "May "; if (im == 6) cmonth = "Jun."; if (im == 7) cmonth = "Jul."; if (im == 8) cmonth = "Aug."; if (im == 9) cmonth = "Sep."; if (im == 10) cmonth = "Oct."; if (im == 11) cmonth = "Nov."; if (im == 12) cmonth = "Dec."; return cmonth; } char *cdirec( int i ) { char *cdirec; cdirec = " "; if (i == 1) cdirec = "NNE"; if (i == 2) cdirec = "NE "; if (i == 3) cdirec = "ENE"; if (i == 4) cdirec = "E "; if (i == 5) cdirec = "ESE"; if (i == 6) cdirec = "SE "; if (i == 7) cdirec = "SSE"; if (i == 8) cdirec = "S "; if (i == 9) cdirec = "SSW"; if (i == 10) cdirec = "SW "; if (i == 11) cdirec = "WSW"; if (i == 12) cdirec = "W "; if (i == 13) cdirec = "WNW"; if (i == 14) cdirec = "NW "; if (i == 15) cdirec = "NNW"; if (i == 16) cdirec = "N "; if (i == 0) cdirec = "C "; return cdirec; } int print( int ihmax, int idmax, int nmax, int *ista, int n, int iyy, int imm, int idd, int imiss, float rmiss, int *itype, int *ir, int *id, int *iw, float *s, float *t ){ int ihh, ir1, iw1; float rs1, rt1; char cd1[4]; FILE *fp; if (itype[(idd-1)+(n-1)*idmax]==0){ printf( "%s\n", "Data not found." ); return 0; } fp = fopen( "output.txt", "w" ); if (fp == NULL){ printf( "%s\n", "Can not write data to file 'output.txt'." ); return 0; } fprintf( fp, " Station No. = %5d,\n", ista[n-1] ); fprintf( fp, " Date = %s %2d, %4d\n", cmonth(imm), idd, iyy ); if (itype[(idd-1)+(n-1)*idmax] == 1){ fprintf( fp, " Time Precip.\n" ); fprintf( fp, " hr mm\n" ); for (ihh=1; ihh<=ihmax; ihh++) { if (ir[(ihh-1)+(idd-1)*ihmax+(n-1)*idmax*ihmax] != imiss){ ir1 = ir[(ihh-1)+(idd-1)*ihmax+(n-1)*idmax*ihmax]; }else{ ir1 = 999999; } fprintf( fp, " %2d %3d\n", ihh, ir1 ); } } if (itype[(idd-1)+(n-1)*idmax] == 4){ fprintf( fp, " Time Precip. Dir. Speed S.D. Temp.\n" ); fprintf( fp, " hr mm m/s hr C\n" ); for (ihh=1; ihh<=ihmax; ihh++) { if (ir[(ihh-1)+(idd-1)*ihmax+(n-1)*idmax*ihmax] != imiss){ ir1 = ir[(ihh-1)+(idd-1)*ihmax+(n-1)*idmax*ihmax]; }else{ ir1 = 999999; } if (id[(ihh-1)+(idd-1)*ihmax+(n-1)*idmax*ihmax] != imiss){ strcpy( cd1, cdirec(id[(ihh-1)+(idd-1)*ihmax+(n-1)*idmax*ihmax]) ); }else{ strcpy( cd1, "***" ); } if (iw[(ihh-1)+(idd-1)*ihmax+(n-1)*idmax*ihmax] != imiss){ iw1 = iw[(ihh-1)+(idd-1)*ihmax+(n-1)*idmax*ihmax]; }else{ iw1 = 999999; } if (s[(ihh-1)+(idd-1)*ihmax+(n-1)*idmax*ihmax] != rmiss){ rs1 = s[(ihh-1)+(idd-1)*ihmax+(n-1)*idmax*ihmax]; }else{ rs1 = 1.0e9; } if (t[(ihh-1)+(idd-1)*ihmax+(n-1)*idmax*ihmax] != rmiss){ rt1 = t[(ihh-1)+(idd-1)*ihmax+(n-1)*idmax*ihmax]; }else{ rt1 = 1.0e9; } fprintf( fp, " %2d %3d %s %2d %3.1f %5.1f\n", ihh, ir1, cd1, iw1, rs1, rt1 ); } } fclose( fp ); printf( "%s\n", "Output was written to file 'output.txt'." ); return 0; } int main( void ) { int ihmax=24, idmax=31, nmax=1; static int ista[1], itype[31*1], id[24*31*1], ir[24*31*1], iw[24*31*1]; static float s[24*31*1], t[24*31*1]; int idd, imm, iyy, status; int imiss = 999999; float rmiss = 1000000000.; printf( "%s\n", "Station No. ?" ); scanf( "%d", ista ); printf( "%s\n", "Year, Month, Day ?" ); scanf( "%d,%d,%d", &iyy, &imm, &idd ); printf( "%s\n", "Reading data ..." ); status = amdhr( ihmax, idmax, nmax, ista, iyy, imm, imiss, rmiss, itype, ir, id, iw, s, t ); status = print( ihmax, idmax, nmax, ista, 1, iyy, imm, idd, imiss, rmiss, itype, ir, id, iw, s, t ); return 0; }