Mapper
create_rsel2_file.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <sys/types.h>
3 #include <sys/stat.h>
4 #include <X11/Xos.h>
5 #include <ctype.h>
6 #include "mm2rfs.h"
7 
8 #include <Xm/Text.h>
9 #include <Xm/TextF.h>
10 
11 extern struct time_cntl tc;
12 extern struct fgroup_info fg[20];
13 extern fgroup_num;
14 extern Widget text_edit;
15 extern Widget text_output;
16 extern int dur_toggle_item_set;
17 extern int snow_toggle_item_set;
18 
19 int create_rsel2_file(int remove)
20 {
21 FILE *fp;
22 char *text;
23 char fg_last[9];
24 char line[80];
25 char tmp_file[80];
26 int i,ii,j,k,kk,last_per;
27 int y,m,d,h;
28 int vy,vm,vd,vh;
29 int nhz;
30 int rsel;
31 int len,len_actual;
32 int hz2rsel=-1000;
33 float fhz;
34 struct fmaz_struct fmaz[100];
35 
36  last_per=3+(4*(dur_toggle_item_set-1));
37 
38  /* load structure with fmaz informaiton */
39  i=fmaz_load(&fmaz[0]);
40 
41 
42  /* identify snow level offset */
43  if(snow_toggle_item_set < 1 || snow_toggle_item_set >= 7)
44  hz2rsel=-1000;
45  else
46  hz2rsel=1000-500*snow_toggle_item_set;
47 
48  /* make sure that there are postable fmazs */
49  ii=0;
50  if(i)
51  {
52  for(j=0;j<i;j++)
53  if(strlen(fmaz[j].fmazid))
54  ii=1;
55  }
56 
57  if(i && ii)
58  {
59  /* set time control */
60  adj_time(-6,tc.year,tc.month,tc.day,tc.hour,&y,&m,&d,&h);
61  if(tc.period==1) h=10;
62  if(tc.period==2) h=16;
63  if(tc.period==3) h=22;
64  if(tc.period==4) h=4;
65  /* mods are valid for the next 3 computational periods */
66  /* mods are valid for the current period only */
67  adj_time(0,y,m,d,h,&vy,&vm,&vd,&vh);
68  y=y%100;
69  vy=vy%100;
70 
71  /* create tmp filename */
72  sprintf(tmp_file,"rsel_fg.%d%d%d.%d%d%d",
73  tc.year,tc.month,tc.day,tc.hour,tc.minute,tc.second);
74 
75  if((fp = fopen (tmp_file, "w")))
76  {
77  strcpy(fg_last,"\0");
78  for(j=0;j<i;j++)
79  {
80  if( strlen(fmaz[j].fmazid) &&
81  (!strcmp(fmaz[j].pos,"O") || !strcmp(fmaz[j].pos,"U"))
82  )
83  {
84  if(strcmp(fg_last,fmaz[j].fgroupid))
85  {
86  if(j)
87  fprintf(fp,"$\n");
88  for(k=0;k<fgroup_num;k++)
89  if(!strcmp(fg[k].id,fmaz[j].fgroupid))
90  fprintf(fp,"$%s\n.TSCHNG "
91  "%02d%02d%02d%02dPST %02d%02d%02d%02dPST\n",
92  fg[k].desc,m,d,y,h,vm,vd,vy,vh);
93  }
94  strcpy(fg_last,fmaz[j].fgroupid);
95 
96  sprintf(line,"%s ",fmaz[j].segid);
97  *(line+8)='\0';
98  fprintf(fp,"%s ",line);
99 
100  sprintf(line,"%s ",fmaz[j].fmazid);
101  *(line+8)='\0';
102  fprintf(fp,"%s ",line);
103 
104 /*
105  sprintf(line,"%s ",rsel[j].ts_dt);
106 */
107 
108  sprintf(line,"RSEL ");
109  *(line+4)='\0';
110  fprintf(fp,"%s 6 ",line);
111 
112  for(k=0;k<=last_per;k++)
113  {
114  if(k<tc.period-1)
115  strcpy(line," ");
116  else
117  {
118 
119  fhz=0.;nhz=0;
120  for(kk=k;kk<=k+1;kk++)
121  {
122  if(strcmp(fmaz[j].per[kk].qual,"M"))
123  {
124  fhz=fhz+fmaz[j].per[kk].amt;
125  nhz++;
126  }
127  }
128  if(nhz)
129  {
130  fhz=fhz/(float)nhz;
131  rsel=100*((int)(10*(fhz+.0499)))+hz2rsel;
132  if(rsel<0) rsel=0;
133  sprintf(line,"%5d",rsel);
134  }
135  else
136  sprintf(line,"-9999");
137  }
138  fprintf(fp," %s",line);
139 
140  if(!((k+1)%4) && k != last_per)
141  fprintf(fp," &\n ");
142  }
143  fprintf(fp," SNOW-17\n");
144 /*
145  fprintf(fp," %s\n",rsel[j].oper);
146 */
147  }
148  }
149 
150  fclose(fp);
151  if(remove)
152  {
153  if((fp = fopen (tmp_file, "r")))
154  {
155  len=10000;
156  if (!(text = XtMalloc ((unsigned)(len+1)))) /* +1 for NULL */
157  {
158  sprintf (line, "%s: XtMalloc(%ld) failed", len, tmp_file);
159  if(text_output)
160  XmTextFieldSetString (text_output, line);
161  return(0);
162  }
163  else
164  {
165  len_actual=fread (text, sizeof (char), len, fp);
166  text[len_actual] = 0; /* NULL-terminate */
167  if(text_edit);
168  XmTextSetString (text_edit, text);
169  sprintf (line, "Click 'Save as *' to store as '* File'");
170  if(text_output);
171  XmTextFieldSetString (text_output, line);
172  }
173  /* close and rm file */
174  fclose(fp);
175  sprintf(line,"rm %s",tmp_file);
176  system(line);
177  }
178  else
179  {
180  sprintf(line,"Couldn't open temp file %s",tmp_file);
181  if(text_output)
182  XmTextFieldSetString (text_output, line);
183  return(0);
184  }
185  }
186  }
187  else
188  {
189  sprintf(line,"Couldn't open temp file %s",tmp_file);
190  if(text_output)
191  XmTextFieldSetString (text_output, line);
192  return(0);
193  }
194  }
195  else
196  {
197  sprintf(line,"No Freezing Level forecasts in table fmaqtrly to read.");
198  if(text_output)
199  XmTextFieldSetString (text_output, line);
200  return(0);
201  }
202 
203  return(1);
204 }
int adj_time(int adj, int y, int m, int d, int h, int *adj_y, int *adj_m, int *adj_d, int *adj_h)
Definition: adj_time.c:10
static int i
struct fgroup_info fg[20]
Widget text_edit
fgroup_num
Widget text_output
int snow_toggle_item_set
int create_rsel2_file(int remove)
int dur_toggle_item_set
struct time_cntl tc
char qual[10]
Definition: subdisplay.c:33
Widget text
Definition: display_rec.c:5
int fmaz_load(struct fmaz_struct *fmaz)
Definition: fmaz_load.c:36
fclose(fp)
sprintf(fbuf,"/usr/mapper/nexrad/ngrid.%02d-%02d-%02d-%02d", year, month, day, hour)
fp
Definition: make_NEXRAD.c:339
system(tarbuf)
fprintf(fp,"%d %d %d %d 1\n", iminx, iminy, maxi, maxj)
int j
Definition: mapp2h.h:48
int k
Definition: mapp2h.h:48