]> Pileus Git - ~andy/rsl/blob - examples/kwaj_subtract_one_day.c
65acfaa07705c693980d2e5ae0b05e21fc611991
[~andy/rsl] / examples / kwaj_subtract_one_day.c
1 #include "rsl.h"
2
3 /**********************************************************************/
4 /*                                                                    */
5 /*                           usage                                    */
6 /*                                                                    */
7 /**********************************************************************/
8 void usage(char **argv)
9 {
10   fprintf(stderr, "Usage: %s in out.uf\n", argv[0]);
11   fprintf(stderr, "\n");
12   fprintf(stderr, "Subtract one day from all time fields in all headers,\n");
13   fprintf(stderr, "all the way down to the ray.  Output to uf.\n");
14   return;
15 }
16
17 /**********************************************************************/
18 /*                                                                    */
19 /*                        subtract_one_day_ray                        */
20 /*                                                                    */
21 /**********************************************************************/
22 #include<time.h>
23 void    *subtract_one_day(int month, int day, int year,
24                                                   int *m, int *d, int *y)
25 {
26   /* Connocialize and subtract. */
27   struct tm *t;
28   time_t the_time;
29
30   t = (struct tm *)calloc(1, sizeof(struct tm));
31   t->tm_mon  = month-1;   /* 0 - 11 */
32   t->tm_mday = day-1;       /* 1 - 31 */  /* And, subtract one day. */
33   t->tm_year = year-1900; /* since 1900 */
34   the_time = mktime(t);
35   t = localtime(&the_time);
36   *m = t->tm_mon+1;
37   *d = t->tm_mday;
38   *y = t->tm_year+1900;
39   return;
40 }
41
42 /**********************************************************************/
43 /*                                                                    */
44 /*                        subtract_one_day_ray                        */
45 /*                                                                    */
46 /**********************************************************************/
47 Ray    *subtract_one_day_ray(Ray *x)
48 {
49   if (x == NULL) return x;
50   subtract_one_day(x->h.month, x->h.day, x->h.year,
51                                    &x->h.month, &x->h.day, &x->h.year);
52   return x;
53 }
54 /**********************************************************************/
55 /*                                                                    */
56 /*                        subtract_one_day_sweep                      */
57 /*                                                                    */
58 /**********************************************************************/
59 Sweep  *subtract_one_day_sweep(Sweep *x)
60 {
61   int i;
62
63   if (x == NULL) return x;
64   for(i=0; i<x->h.nrays; i++)
65         x->ray[i] = subtract_one_day_ray(x->ray[i]);
66   return x;
67 }
68 /**********************************************************************/
69 /*                                                                    */
70 /*                        subtract_one_day_volume                     */
71 /*                                                                    */
72 /**********************************************************************/
73 Volume *subtract_one_day_volume(Volume *x)
74 {
75   int i;
76
77   if (x == NULL) return x;
78   for(i=0; i<x->h.nsweeps; i++)
79         x->sweep[i] = subtract_one_day_sweep(x->sweep[i]);
80   return x;
81 }
82 /**********************************************************************/
83 /*                                                                    */
84 /*                        subtract_one_day_radar                      */
85 /*                                                                    */
86 /**********************************************************************/
87 Radar  *subtract_one_day_radar(Radar *x)
88 {
89   int i;
90
91   if (x == NULL) return x;
92   for(i=0; i<x->h.nvolumes; i++)
93         x->v[i] = subtract_one_day_volume(x->v[i]);
94   return x;
95 }
96   
97
98
99 /**********************************************************************/
100 /*                                                                    */
101 /*                             m a i n                                */
102 /*                                                                    */
103 /**********************************************************************/
104 int main(int argc, char **argv)
105 {
106
107   Radar *radar;
108
109   if (argc != 3) {
110         usage(argv);
111         exit(-1);
112   }
113
114   radar = RSL_anyformat_to_radar(argv[1]);
115   radar = subtract_one_day_radar(radar);
116   subtract_one_day(radar->h.month, radar->h.day, radar->h.year,
117                                    &radar->h.month, &radar->h.day, &radar->h.year);
118   RSL_radar_to_uf_gzip(radar, argv[2]);
119
120   exit(0);
121 }