-
Notifications
You must be signed in to change notification settings - Fork 0
/
timing.c
501 lines (413 loc) · 11.8 KB
/
timing.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
/**************************************************************************
* timing.c: timing functions
*
* The routines with a _ suffix are called from Fortran
*
* The routines without the suffix are called from C
*
* $Id: timing.c 4496 2008-07-24 15:26:29Z rpayn $
*
$Revision: 4496 $
$Log: timing.c,v $
Revision 1.7 1997/04/18 16:44:15 markstro
(1) Commented out errno problem with opening files from fortran.
(2) Put in checks for saving parameter file when loading new one.
(3) Changes to runcontrol.c and timing.c unknown
Revision 1.6 1996/02/19 20:01:20 markstro
Now lints pretty clean
Revision 1.5 1994/11/22 17:20:38 markstro
(1) Cleaned up dimensions and parameters.
(2) Some changes due to use of malloc_dbg.
* Revision 1.4 1994/11/08 16:17:52 markstro
* (1) More proto type fine tuning
* (2) fixed up data file reading
*
* Revision 1.3 1994/09/30 14:55:26 markstro
* Initial work on function prototypes.
*
* Revision 1.2 1994/01/31 20:17:43 markstro
* Make sure that all source files have CVS log.
*
**************************************************************************/
#define TIMING_C
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "mms.h"
/**************************************************************************
* dattim and dattim_ : get start, end or current data date and time
*
* args - dwhen : string, "start", "end", and "now"
* timearray: integer or long array which accepts the time
* and date
*
*/
/*--------------------------------------------------------------------*\
| FUNCTION : dattim_
| COMMENT : called from Fortran, sorts out args and calls dattim()
| get start, end or current data date and time
| PARAMETERS :
| RETURN VALUE :
| RESTRICTIONS :
\*--------------------------------------------------------------------*/
void dattim_ (char *dwhen, ftnint *timearray, ftnlen dwhenlen) {
// char *when;
char when[80];
long ta[6];
/*
* copy when and terminate
*/
// when = (char *) umalloc(dwhenlen + 1);
// strncpy(when, dwhen, dwhenlen);
// when[dwhenlen] = '\0';
strncpy (when, dwhen, dwhenlen);
*(when + dwhenlen) = '\0';
/*
* call C version of dattim()
*/
dattim(when, ta);
timearray[0] = ta[0];
timearray[1] = ta[1];
timearray[2] = ta[2];
timearray[3] = ta[3];
timearray[4] = ta[4];
timearray[5] = ta[5];
/*
* free up array
*/
//ufree(when);
}
/**************************************************************************
*/
/*--------------------------------------------------------------------*\
| FUNCTION : dattim
| COMMENT : called from C
| PARAMETERS :
| RETURN VALUE :
| RESTRICTIONS :
\*--------------------------------------------------------------------*/
void dattim (char *when, long *timearray) {
DATETIME *time;
/*
* set time according to when argument
*/
if (!strcmp(when, "start"))
time = Mstrttime;
else if(!strcmp(when, "end"))
time = Mendtime;
else if (!strcmp(when, "now"))
time = Mnowtime;
else {
(void)fprintf(stderr,
"ERROR - dattim - illegal argument '%s'.\n", when);
exit(1);
}
/*
* load up time array
*/
timearray[0] = time->year;
timearray[1] = time->month;
timearray[2] = time->day;
timearray[3] = time->hour;
timearray[4] = time->min;
timearray[5] = time->sec;
}
/*--------------------------------------------------------------------*\
| FUNCTION : nowjt_
| COMMENT :
| PARAMETERS :
| RETURN VALUE :
| RESTRICTIONS :
\*--------------------------------------------------------------------*/
double nowjt_ () {
return Mnowtime->jt;
}
/**************************************************************************
* julian_ and julian : returns the julian date of the data stream relative
* to calendar, solar and water year start dates.
* (1 JAN) (22 DEC) (1 OCT)
*
* args - when : string, "start", "end", "now"
* type : string, "calendar", "solar", "water", "absolute"
*
* julian_() is called from Fortran, sorts out args and calls julian()
*/
/*--------------------------------------------------------------------*\
| FUNCTION : julian_
| COMMENT :
| PARAMETERS :
| RETURN VALUE :
| RESTRICTIONS :
\*--------------------------------------------------------------------*/
long julian_ (char *jwhen, char *jtype, ftnlen jwhenlen, ftnlen jtypelen) {
// char *when, *type;
char when[80], type[80];
long retval;
/*
* copy strings and terminate
*/
// when = (char *) umalloc(jwhenlen + 1);
// strncpy(when, jwhen, jwhenlen);
// when[jwhenlen] = '\0';
strncpy (when, jwhen, jwhenlen);
*(when + jwhenlen) = '\0';
// type = (char *) umalloc(jtypelen + 1);
// strncpy(type, jtype, jtypelen);
// type[jtypelen] = '\0';
strncpy (type, jtype, jtypelen);
*(type + jtypelen) = '\0';
/*
* call C version of julian()
*/
retval = julian(when, type);
/*
* free up arrays
*/
//ufree(when);
//ufree(type);
return retval;
}
/**************************************************************************
* julian() is called from C
*/
/*--------------------------------------------------------------------*\
| FUNCTION : julian
| COMMENT :
| PARAMETERS :
| RETURN VALUE :
| RESTRICTIONS :
\*--------------------------------------------------------------------*/
long julian (char *when, char *type) {
DATETIME *time, reftime;
/*
* set time according to when argument
*/
if (!strcmp(when, "start"))
time = Mstrttime;
else if(!strcmp(when, "end"))
time = Mendtime;
else if (!strcmp(when, "now"))
time = Mnowtime;
else {
(void)fprintf(stderr,
"ERROR - julian - illegal argument '%s'.\n", when);
exit(1);
}
/*
* set reftime depending on type arg
*/
if (!strcmp(type, "calendar")) {
reftime.year = time->year - 1;
reftime.month = 12;
reftime.day = 31;
} else if(!strcmp(type, "solar")) {
if ((time->month == 12) && (time->day > 21))
reftime.year = time->year;
else
reftime.year = time->year - 1;
reftime.month = 12;
reftime.day = 21;
} else if(!strcmp(type, "spring")) {
if ((time->month > 3) || (time->month == 3 && time->day > 20)) {
reftime.year = time->year;
} else {
reftime.year = time->year - 1;
}
reftime.month = 3;
reftime.day = 20;
} else if (!strcmp(type, "water")) {
if (time->month > 9)
reftime.year = time->year;
else
reftime.year = time->year - 1;
reftime.month = 9;
reftime.day = 30;
} else if(!strcmp(type, "absolute")) {
julday(time);
return (time->jd);
} else {
(void)fprintf(stderr,
"ERROR - julian - illegal argument '%s'.\n", type);
exit(1);
}
reftime.hour = 0;
reftime.min = 0;
reftime.sec = 0;
/*
* compute the julian dates
*/
julday(time);
julday(&reftime);
return (time->jd - reftime.jd);
}
/**************************************************************************
* deltim_() is called from Fortran, deltim()
*/
double deltim_(void) {
/* printf ("from deltim: %f\n", deltim()); */
return deltim();
}
/**************************************************************************
* deltim() is called from C
*/
/*--------------------------------------------------------------------*\
| FUNCTION : deltim
| COMMENT :
| PARAMETERS :
| RETURN VALUE :
| RESTRICTIONS :
\*--------------------------------------------------------------------*/
double deltim (void) {
return (double) Mdeltat * 24.0;
}
/**************************************************************************
* getstep_() is called from Fortran, getstep()
*/
/*--------------------------------------------------------------------*\
| FUNCTION : getstep_
| COMMENT :
| PARAMETERS :
| RETURN VALUE :
| RESTRICTIONS :
\*--------------------------------------------------------------------*/
long getstep_ (void) {
return getstep();
}
/**************************************************************************
* getstep() is called from C
*/
/*--------------------------------------------------------------------*\
| FUNCTION : getstep
| COMMENT :
| PARAMETERS :
| RETURN VALUE :
| RESTRICTIONS :
\*--------------------------------------------------------------------*/
long getstep (void) {
return Mnsteps;
}
/**************************************************************************
* djulian_ and djulian : returns the double julian date of the data stream
* relative to calendar, solar and water year start dates.
* (1 JAN) (22 DEC) (1 OCT)
*
* args - when : string, "start", "end", "now"
* type : string, "calendar", "solar", "water", "absolute"
*
* julian_() is called from Fortran, sorts out args and calls julian()
*/
/*--------------------------------------------------------------------*\
| FUNCTION : djulian_
| COMMENT :
| PARAMETERS :
| RETURN VALUE :
| RESTRICTIONS :
\*--------------------------------------------------------------------*/
double djulian_ (char *jwhen, char *jtype, ftnlen jwhenlen, ftnlen jtypelen) {
char *when, *type;
double retval;
/*
* copy strings and terminate
*/
when = (char *) umalloc(jwhenlen + 1);
strncpy(when, jwhen, jwhenlen);
when[jwhenlen] = '\0';
type = (char *) umalloc(jtypelen + 1);
strncpy(type, jtype, jtypelen);
type[jtypelen] = '\0';
/*
* call C version of djulian()
*/
retval = djulian(when, type);
/*
* free up arrays
*/
//ufree(when);
//ufree(type);
return retval;
}
/**************************************************************************
* julian() is called from C
*/
/*--------------------------------------------------------------------*\
| FUNCTION : djulian
| COMMENT :
| PARAMETERS :
| RETURN VALUE :
| RESTRICTIONS :
\*--------------------------------------------------------------------*/
double djulian (char *when, char *type) {
DATETIME *time, reftime;
/*
* set time according to when argument
*/
if (!strcmp(when, "start"))
time = Mstrttime;
else if(!strcmp(when, "end"))
time = Mendtime;
else if (!strcmp(when, "now"))
time = Mnowtime;
else {
(void)fprintf(stderr,
"ERROR - julian - illegal argument '%s'.\n", when);
exit(1);
}
/*
* set reftime depending on type arg
*/
if (!strcmp(type, "calendar")) {
reftime.year = time->year - 1;
reftime.month = 12;
reftime.day = 31;
} else if(!strcmp(type, "solar")) {
if ((time->month == 12) && (time->day > 21))
reftime.year = time->year;
else
reftime.year = time->year - 1;
reftime.month = 12;
reftime.day = 21;
} else if (!strcmp(type, "water")) {
if (time->month > 9)
reftime.year = time->year;
else
reftime.year = time->year - 1;
reftime.month = 9;
reftime.day = 30;
} else if(!strcmp(type, "absolute")) {
julday(time);
return (time->jt);
} else {
(void)fprintf(stderr,
"ERROR - julian - illegal argument '%s'.\n", type);
exit(1);
}
reftime.hour = 0;
reftime.min = 0;
reftime.sec = 0;
/*
* compute the julian dates
*/
julday(time);
julday(&reftime);
return (time->jt - reftime.jt);
}
/**************************************************************************
* delnex_() is called from Fortran, delnex()
*/
double delnex_(void) {
/* printf ("from deltim: %f\n", deltim()); */
return delnex();
}
/**************************************************************************
* delnex() is called from C
*/
/*--------------------------------------------------------------------*\
| FUNCTION : delnex
| COMMENT :
| PARAMETERS :
| RETURN VALUE :
| RESTRICTIONS :
\*--------------------------------------------------------------------*/
double delnex (void) {
return (double) Mdeltanext * 24.0;
}