00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <ctype.h>
00025 #include <errno.h>
00026 #include <string.h>
00027 #include <unistd.h>
00028 #include <utime.h>
00029 #include <sys/stat.h>
00030
00031 #include "dslibc/version.h"
00032 #include "ingest_lib/ingest_datachunk.h"
00033 #include "ingest_lib/ingest_util.h"
00034 #include "ingest_lib/ingest_stats.h"
00035 #include "ingest_lib/sds_util.h"
00036 #include "ingest_lib/sds_status.h"
00037 #include "ingest_lib/sds_log.h"
00038 #include "ingest_lib/sds_tdb.h"
00039 #include "ingest_lib/version.h"
00040
00041
00042
00043
00044
00045 static DataChunk *prev_dc = NULL;
00046
00047 static struct {
00048 char dsc_name[32];
00049 char dsc_level[8];
00050 char base_name[32];
00051 } gOutDirMap[16];
00052
00053 static int gOutDirMapCount = 0;
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078 int append_global(DataChunk *dc, char *attname, char *attvalue)
00079 {
00080 char newvalue[MAXSTRLEN];
00081 char *oldvalue;
00082
00083 if ((oldvalue = dc_GetGlobalAttr(dc, attname)) == NULL) {
00084 strcpy(newvalue, attvalue);
00085 }
00086 else {
00087 sprintf(newvalue, "%s%s", oldvalue, attvalue);
00088 }
00089 return(change_global(dc, attname, newvalue));
00090 }
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114 int get_dc_platform_ds_class(
00115 DataChunk *dc,
00116 char *dsc_name,
00117 char *dsc_level)
00118 {
00119 const char *site;
00120 const char *facility;
00121 char dc_platform[MAXPLATNAME];
00122 char *chrp;
00123
00124 site = get_process_site();
00125 facility = get_process_facility();
00126
00127
00128
00129 sprintf(dc_platform, "%s", ds_PlatformName(dc->dc_Platform));
00130
00131
00132
00133 chrp = (char *)strrchr(dc_platform, '.');
00134 strcpy(dsc_level, ++chrp);
00135
00136
00137
00138 chrp = strstr(dc_platform, facility);
00139
00140 if (chrp == NULL) {
00141 append_log_msg(__FILE__, __LINE__,
00142 "Could not find facility (%s) in platform name: %s\n",
00143 facility, dc_platform);
00144 current_status(STATUS_BADPLATNAME);
00145 return(FAILURE);
00146 }
00147
00148 *chrp = '\0';
00149
00150 sprintf(dsc_name, dc_platform + strlen(site));
00151
00152 return(SUCCESS);
00153 }
00154
00155 int get_tdb_plat_names(
00156 DataChunk *dc,
00157 char *tdb_plat_base,
00158 char *tdb_plat_name,
00159 char *tdb_alias_base,
00160 char *tdb_alias_name)
00161 {
00162 int status;
00163 char dsc_name[MAXPLATNAME];
00164 char dsc_level[MAXPLATNAME];
00165 int tdb_inst_num;
00166 char *alias;
00167
00168 status = get_dc_platform_ds_class(dc, dsc_name, dsc_level);
00169 if (status == FAILURE) {
00170 return(FAILURE);
00171 }
00172
00173 tdb_inst_num = get_tdb_inst_num();
00174
00175 if (tdb_plat_base) {
00176 strcpy(tdb_plat_base, dsc_name);
00177 }
00178
00179 if (tdb_plat_name) {
00180 sprintf(tdb_plat_name, "%s%d", dsc_name, tdb_inst_num);
00181 }
00182
00183 if (tdb_alias_base || tdb_alias_name) {
00184 if ((alias = get_tdb_alias(tdb_plat_base)) != NULL) {
00185
00186 if (tdb_alias_base) {
00187 strcpy(tdb_alias_base, alias);
00188 }
00189
00190 if (tdb_alias_name) {
00191 sprintf(tdb_alias_name, "%s%d", alias, tdb_inst_num);
00192 }
00193 }
00194 else {
00195 if (tdb_alias_base) {
00196 strcpy(tdb_alias_base, tdb_plat_base);
00197 }
00198
00199 if (tdb_alias_name) {
00200 strcpy(tdb_alias_name, tdb_plat_name);
00201 }
00202 }
00203 }
00204
00205 return(SUCCESS);
00206 }
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231 int change_flattr (DataChunk *dc, char *field_name, char *flattr_name, char *flattr_value)
00232 {
00233 char message[MAXSTRLEN];
00234 FieldId fid;
00235 char *lastvalue;
00236
00237
00238
00239 if ((fid = F_Declared(field_name)) < 0) {
00240 append_log_msg(__FILE__, __LINE__,
00241 "Field %s is not in the TDB Platform Dictionary!", field_name);
00242 }
00243 else {
00244
00245
00246
00247 dc_SetFieldAttr(dc, fid, flattr_name, flattr_value);
00248
00249
00250
00251 lastvalue = get_previous_flattr(dc, fid, flattr_name);
00252
00253 if (strcmp(lastvalue, flattr_value) != 0) {
00254 append_log_msg(__FILE__, __LINE__,
00255 "Field attribute changed: %s:%s\n\tfrom:%s\n\tto: %s\n",
00256 field_name, flattr_name, lastvalue, flattr_value);
00257 return (1);
00258 }
00259 else {
00260 print_debug(__FILE__, __LINE__,
00261 "flatter %s for field %s has not changed\n",
00262 flattr_name, field_name);
00263 }
00264 }
00265
00266 return (0);
00267 }
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290 int change_global(DataChunk *dc, char *glattr_name, char *glattr_value)
00291 {
00292 char message[MAXSTRLEN];
00293 char *lastvalue;
00294
00295
00296
00297 if (!dc_GetGlobalAttr(dc, glattr_name)) {
00298 print_debug(__FILE__, __LINE__,
00299 "Metadata for %s was not previously defined.\n", glattr_name);
00300 }
00301
00302
00303
00304 dc_SetGlobalAttr(dc, glattr_name, glattr_value);
00305
00306
00307
00308 lastvalue = get_previous_glattr(dc, glattr_name);
00309
00310 if (strcmp(lastvalue, glattr_value) != 0) {
00311 append_log_msg(__FILE__, __LINE__,
00312 "Global attribute changed: %s\n\tfrom:%s\n\tto: %s\n",
00313 glattr_name, lastvalue, glattr_value);
00314 return (1);
00315 }
00316 else {
00317 print_debug(__FILE__, __LINE__,
00318 "glattr %s has not changed\n", glattr_name);
00319 }
00320
00321 return (0);
00322 }
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346 int check_for_std_metadata_changes(DataChunk *dc, int *new_file)
00347 {
00348 char message[MAXSTRLEN];
00349 char *newvalue;
00350 char *oldvalue;
00351 int index;
00352 int status;
00353 static char *std_glattrs[] = {
00354
00355 "ingest_software",
00356 "proc_level" ,
00357 "site_id" ,
00358 "facility_id" ,
00359 "sample_int" ,
00360 "averaging_int" ,
00361 "serial_number" ,
00362 "comment" ,
00363 "resolution_description",
00364 (char *)NULL
00365 };
00366
00367 status = 0;
00368
00369 prev_dc = get_previous_dc(dc);
00370
00371 if (prev_dc != (DataChunk *)NULL) {
00372
00373 for(index = 0; std_glattrs[index] != NULL; index ++) {
00374 newvalue = dc_GetGlobalAttr(dc, std_glattrs[index]);
00375 oldvalue = dc_GetGlobalAttr(prev_dc, std_glattrs[index]);
00376
00377 if (strcmp(newvalue, oldvalue) != 0) {
00378 append_log_msg(__FILE__, __LINE__,
00379 "Global attribute changed: %s\n\tfrom:%s\n\tto: %s\n",
00380 std_glattrs[index], oldvalue, newvalue);
00381 status = 1;
00382 *new_file = 1;
00383 }
00384 }
00385 }
00386 return(status);
00387 }
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411 int check_for_plat_metadata_changes(DataChunk *dc, int *newfile)
00412 {
00413 char message[MAXSTRLEN];
00414 char tdb_plat_base[SHORTSTRLEN];
00415 char tdb_plat_name[SHORTSTRLEN];
00416 char tdb_alias_base[SHORTSTRLEN];
00417 char tdb_alias_name[SHORTSTRLEN];
00418 int attnum;
00419 char *attname;
00420 char *newvalue;
00421 char *oldvalue;
00422 int ndiffs = 0;
00423
00424 prev_dc = get_previous_dc(dc);
00425
00426 if (prev_dc != (DataChunk *)NULL) {
00427
00428 if (!db_reconnect()) {
00429 return(0);
00430 }
00431
00432 get_tdb_plat_names(
00433 dc,
00434 tdb_plat_base,
00435 tdb_plat_name,
00436 tdb_alias_base,
00437 tdb_alias_name);
00438
00439 for(attnum = 1; attnum < MAX_METADATA; attnum++) {
00440
00441 attname = get_glattr_name(
00442 attnum,
00443 tdb_plat_name,
00444 tdb_plat_base,
00445 tdb_alias_name,
00446 tdb_alias_base);
00447
00448 if (attname == NULL) {
00449 break;
00450 }
00451
00452 if (strcmp(attname, "end_global_attributes") == NULL) {
00453 shared_tdb_free(attname);
00454 break;
00455 }
00456
00457 newvalue = dc_GetGlobalAttr(dc, attname);
00458 oldvalue = dc_GetGlobalAttr(prev_dc, attname);
00459
00460 if (oldvalue == (char *) NULL) {
00461 append_log_msg(__FILE__, __LINE__,
00462 "Global attribute added: %s\n", attname);
00463 ndiffs++;
00464 *newfile = 1;
00465 }
00466 else if (strcmp(newvalue, oldvalue) != 0) {
00467 append_log_msg(__FILE__, __LINE__,
00468 "Global attribute changed: %s\n\tfrom:%s\n\tto: %s\n",
00469 attname, oldvalue, newvalue);
00470 ndiffs++;
00471 *newfile = 1;
00472 }
00473 shared_tdb_free(attname);
00474 }
00475
00476 db_disconnect();
00477 }
00478
00479 return(ndiffs);
00480 }
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503 int check_for_location_changes(DataChunk *dc, int *newfile)
00504 {
00505 char message[MAXSTRLEN];
00506 Location curr_loc;
00507 Location prev_loc;
00508
00509 prev_dc = get_previous_dc(dc);
00510
00511 if (prev_dc != (DataChunk *)NULL) {
00512
00513 dc_GetLoc(dc, 0, &curr_loc);
00514 dc_GetLoc(prev_dc, 0, &prev_loc);
00515
00516 if ((curr_loc.l_alt != prev_loc.l_alt) ||
00517 (curr_loc.l_lat != prev_loc.l_lat) ||
00518 (curr_loc.l_lon != prev_loc.l_lon)) {
00519
00520 append_log_msg(__FILE__, __LINE__,
00521 "Location changed: %s\n", ds_PlatformName(dc->dc_Platform));
00522 *newfile = 1;
00523 return(1);
00524 }
00525 }
00526 return(0);
00527 }
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547
00548 void check_for_metadata_changes(DataChunk *dc, int *newfile)
00549 {
00550 check_for_std_metadata_changes(dc, newfile);
00551 check_for_plat_metadata_changes(dc, newfile);
00552 check_for_location_changes(dc, newfile);
00553 }
00554
00555
00556
00557
00558
00559
00560
00561
00562
00563
00564
00565
00566
00567
00568
00569
00570
00571
00572
00573
00574
00575
00576 int define_dimsizes(DataChunk *dc, int ndims, int *dimsizes)
00577 {
00578 char tdb_plat_name[SHORTSTRLEN];
00579 char datalevel[8];
00580 int dimnum;
00581 char *chrp;
00582 int retval;
00583
00584 get_tdb_plat_names(dc, NULL, tdb_plat_name, NULL, NULL);
00585
00586 get_dc_platform_datalevel(dc, datalevel);
00587
00588
00589
00590 for (dimnum = 1; dimnum < ndims + 1; dimnum++) {
00591 if ((dimsizes[dimnum - 1] < 1) || (dimsizes[dimnum - 1] > MAX_DIMSIZE)) {
00592 append_log_msg(__FILE__, __LINE__,
00593 "Dimension size (%d) is out of range for platform %s, dimnum %d.\n",
00594 dimsizes[dimnum - 1], tdb_plat_name, dimnum);
00595 return(FAILURE);
00596 }
00597 }
00598
00599 if (!db_reconnect()) {
00600 return(FAILURE);
00601 }
00602
00603 retval = SUCCESS;
00604
00605 for (dimnum = 1; dimnum < ndims + 1; dimnum++) {
00606 if (!set_dim_size(tdb_plat_name, datalevel, dimnum, dimsizes[dimnum - 1])) {
00607 retval = FAILURE;
00608 break;
00609 }
00610 }
00611
00612 db_disconnect();
00613
00614 return(retval);
00615 }
00616
00617
00618
00619
00620
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635
00636 int set_dc_platform(DataChunk *dc, char *platbase, char *datalevel)
00637 {
00638 const char *site;
00639 const char *facility;
00640
00641 char platform[SHORTSTRLEN];
00642
00643 site = get_process_site();
00644 facility = get_process_facility();
00645
00646 if (dc) {
00647 sprintf (platform, "%s%s%s.%s", site, platbase, facility, datalevel);
00648
00649 print_debug(__FILE__, __LINE__,
00650 "Looking up zebra platform: %s\n", platform);
00651
00652 if ((dc->dc_Platform = ds_LookupPlatform(platform)) == BadPlatform) {
00653 current_status(STATUS_INITDC);
00654 append_log_msg(__FILE__, __LINE__,
00655 "ds_LookupPlatform failed for platform %s\n", platform);
00656 return(FAILURE);
00657 }
00658 }
00659 else {
00660 current_status(STATUS_INITDC);
00661 append_log_msg(__FILE__, __LINE__,
00662 "Datachunk sent to do_lookup is NULL\n");
00663 return(FAILURE);
00664 }
00665
00666 return(SUCCESS);
00667 }
00668
00669
00670
00671
00672
00673
00674
00675
00676
00677
00678
00679
00680
00681
00682
00683
00684 DataChunk *freeDC(DataChunk *dc)
00685 {
00686 if (dc) {
00687 dc_DestroyDC(dc);
00688 }
00689 return((DataChunk *)NULL);
00690 }
00691
00692
00693
00694
00695
00696
00697
00698
00699
00700
00701
00702
00703
00704
00705
00706
00707
00708
00709 int get_dc_platform_datalevel(DataChunk *dc, char *datalevel)
00710 {
00711 char *chrp;
00712
00713
00714
00715 if ((chrp = (char *)strrchr(ds_PlatformName(dc->dc_Platform), '.')) != NULL) {
00716 sprintf(datalevel, ++chrp);
00717 }
00718 else {
00719 datalevel[0] = '\0';
00720 return(0);
00721 }
00722
00723 return(1);
00724 }
00725
00726
00727
00728
00729
00730
00731
00732
00733
00734
00735
00736
00737
00738
00739
00740
00741
00742
00743
00744
00745
00746
00747
00748
00749
00750
00751
00752
00753
00754
00755
00756 DC_ElemType get_field_type(char *field_type)
00757 {
00758 int field_type_num;
00759
00760 field_type_num = (int)DCT_Unknown;
00761
00762 while (strcmp(DC_ElemTypeNames[field_type_num++], field_type)) {
00763 if (field_type_num == (int)DCT_Element) {
00764 field_type_num = (int)DCT_Unknown;
00765 print_debug(__FILE__, __LINE__,
00766 "Field type %s not found, using float instead.\n", field_type);
00767 break;
00768 }
00769 }
00770
00771 if (field_type_num == (int)DCT_Unknown) {
00772 while (strcmp(DC_ElemTypeNames[field_type_num++], "float"))
00773 { };
00774 }
00775 field_type_num--;
00776
00777 return((DC_ElemType)field_type_num);
00778 }
00779
00780
00781
00782
00783
00784
00785
00786
00787
00788
00789
00790
00791
00792
00793
00794
00795
00796 DataChunk *get_previous_dc(DataChunk *dc)
00797 {
00798 ZebTime curtime;
00799 ZebTime when;
00800 ZebTime start;
00801 ZebTime end;
00802 FieldId fields[MAX_FIELDS];
00803 int nflds = 0;
00804 int nfield;
00805
00806
00807
00808
00809
00810 if (prev_dc != (DataChunk *)NULL ) {
00811 if (prev_dc->dc_Platform != dc->dc_Platform) {
00812 prev_dc = freeDC(prev_dc);
00813 }
00814 }
00815
00816
00817
00818
00819 if (prev_dc == (DataChunk *)NULL) {
00820
00821
00822
00823 dc_GetTime(dc, 0, &curtime);
00824
00825 when.zt_Sec = when.zt_MicroSec = 0;
00826
00827 ds_DataTimes(dc->dc_Platform, &curtime, 1, DsBefore, &when);
00828
00829
00830
00831
00832
00833
00834
00835
00836
00837 start = end = when;
00838
00839 start.zt_MicroSec -= 2;
00840 end.zt_MicroSec += 2;
00841
00842 if (start.zt_MicroSec < 0) {
00843 start.zt_Sec -= 1;
00844 start.zt_MicroSec += 1e6;
00845 }
00846
00847 if (end.zt_MicroSec >= 1e6) {
00848 end.zt_Sec += 1;
00849 end.zt_MicroSec -= 1e6;
00850 }
00851
00852
00853
00854 nfield = MAX_FIELDS;
00855
00856
00857
00858 nflds = ds_GetFields(dc->dc_Platform, &when, &nfield, fields);
00859
00860 if (nflds) {
00861
00862
00863
00864 prev_dc = ds_Fetch(dc->dc_Platform, dc->dc_Class, &start,
00865 &end, fields, nfield, 0, 0);
00866 }
00867 else {
00868 append_log_msg(__FILE__, __LINE__,
00869 "Could not get fields from Data Store in get_previous_dc()!\n");
00870 }
00871 }
00872 return(prev_dc);
00873 }
00874
00875
00876
00877
00878
00879
00880
00881
00882
00883
00884
00885
00886
00887
00888
00889
00890
00891
00892
00893
00894
00895 char *get_previous_flattr(DataChunk *dc, FieldId fid, char *flattr_name)
00896 {
00897 static char flattr_value[MAXSTRLEN];
00898 char *previous_flattr;
00899
00900 flattr_value[0] = '\0';
00901
00902
00903
00904 prev_dc = get_previous_dc(dc);
00905
00906 if (prev_dc != (DataChunk *)NULL) {
00907
00908
00909
00910 previous_flattr = dc_GetFieldAttr(prev_dc, fid, flattr_name);
00911 if (previous_flattr) {
00912 sprintf(flattr_value, "%s", previous_flattr);
00913 }
00914 else {
00915 flattr_value[0] = '\0';
00916 }
00917 }
00918 return(flattr_value);
00919 }
00920
00921
00922
00923
00924
00925
00926
00927
00928
00929
00930
00931
00932
00933
00934
00935
00936
00937
00938
00939 char *get_previous_glattr(DataChunk *dc, char *glattr_name)
00940 {
00941 static char glattr_value[MAXSTRLEN];
00942 char *previous_glattr;
00943
00944 glattr_value[0] = '\0';
00945
00946
00947
00948 prev_dc = get_previous_dc(dc);
00949
00950 if (prev_dc != (DataChunk *)NULL) {
00951
00952
00953
00954 previous_glattr = dc_GetGlobalAttr(prev_dc, glattr_name);
00955 if (previous_glattr) {
00956 sprintf(glattr_value, "%s", previous_glattr);
00957 }
00958 else {
00959 glattr_value[0] = '\0';
00960 }
00961 }
00962 return(glattr_value);
00963 }
00964
00965 void set_rename_raw_map_dir(char *dsc_name, char *dsc_level, char *base_name)
00966 {
00967 strcpy(gOutDirMap[gOutDirMapCount].dsc_name, dsc_name);
00968 strcpy(gOutDirMap[gOutDirMapCount].dsc_level, dsc_name);
00969 strcpy(gOutDirMap[gOutDirMapCount].base_name, base_name);
00970
00971 gOutDirMapCount++;
00972 }
00973
00974 char *get_rename_raw_map_dir(char *dsc_name, char *dsc_level)
00975 {
00976 int i;
00977
00978 for (i = 0; i < gOutDirMapCount; i++) {
00979 if ((strcmp(gOutDirMap[i].dsc_name, dsc_name) == 0) &&
00980 (strcmp(gOutDirMap[i].dsc_level, dsc_level) == 0) ) {
00981
00982 return(gOutDirMap[i].base_name);
00983 }
00984 }
00985
00986 return((char *)NULL);
00987 }
00988
00989
00990
00991
00992
00993
00994
00995
00996
00997
00998
00999
01000
01001
01002
01003
01004
01005
01006
01007
01008
01009
01010
01011
01012
01013
01014
01015
01016
01017
01018
01019
01020
01021
01022
01023 int rename_raw(
01024 char *filename,
01025 time_t start_time,
01026 time_t end_time,
01027 char *dsc_name,
01028 char *dsc_level,
01029 char *extension,
01030 int preserve_dots)
01031 {
01032 char *datastream_home;
01033 const char *site;
01034 const char *facility;
01035 struct tm *start_tm;
01036 char *chrp;
01037 char out_dir[MAXPATHNAME];
01038 char time_string[32];
01039 char orig_name[MAXPATHNAME];
01040 char dest_file[MAXPATHNAME];
01041 int dot_count;
01042 char *dir_base_name;
01043
01044
01045
01046
01047
01048 if (!validate_ingest_output_dsc(dsc_name, dsc_level)) {
01049 return(FAILURE);
01050 }
01051
01052 site = get_process_site();
01053 facility = get_process_facility();
01054
01055
01056
01057
01058 if (start_time > 0) {
01059 start_tm = gmtime(&start_time);
01060 }
01061 else {
01062 append_log_msg(__FILE__, __LINE__,
01063 "Bad start time specified for: %s\n", filename);
01064 current_status(STATUS_NOMOVERAW);
01065 return(FAILURE);
01066 }
01067
01068 strftime(time_string, 32, "%Y%m%d.%H%M%S", start_tm);
01069
01070
01071
01072
01073 datastream_home = get_datastream_home();
01074 if (!datastream_home) {
01075 return(FAILURE);
01076 }
01077
01078 dir_base_name = get_rename_raw_map_dir(dsc_name, dsc_level);
01079 if (!dir_base_name) {
01080 dir_base_name = dsc_name;
01081 }
01082
01083 sprintf(out_dir, "%s/%s/%s%s%s.%s",
01084 datastream_home, site, site, dsc_name, facility, dsc_level);
01085
01086 if (!check_path(out_dir)) {
01087
01088 print_debug(__FILE__, __LINE__,
01089 "Creating datastream directory: %s\n", out_dir);
01090
01091 if (mkdir(out_dir, 00775) != 0) {
01092 append_log_msg(__FILE__, __LINE__,
01093 "Error #%i creating datastream directory: %s\n -> %s\n",
01094 errno, out_dir, strerror(errno));
01095 current_status(STATUS_NOMOVERAW);
01096 return(FAILURE);
01097 }
01098 }
01099
01100
01101
01102
01103 sprintf(dest_file, "%s/%s%s%s.%s.%s",
01104 out_dir, site, dsc_name, facility, dsc_level, time_string);
01105
01106 if (extension) {
01107 strcat(dest_file, ".");
01108 strcat(dest_file, extension);
01109 }
01110
01111 if (preserve_dots > 0) {
01112
01113 strcat(dest_file, ".");
01114
01115 chrp = strrchr(filename, '/');
01116 strcpy(orig_name, ++chrp);
01117
01118 chrp = orig_name + strlen(orig_name) - 1;
01119 for (dot_count = 0;; chrp--) {
01120
01121 if (*chrp == '.') dot_count++;
01122
01123 if (dot_count == preserve_dots) {
01124 strcat(dest_file, ++chrp);
01125 break;
01126 }
01127 else if (chrp == orig_name) {
01128 strcat(dest_file, orig_name);
01129 break;
01130 }
01131 }
01132 }
01133
01134
01135
01136
01137 if (!file_move(filename, dest_file)) {
01138 current_status(STATUS_NOMOVERAW);
01139 return(FAILURE);
01140 }
01141
01142
01143
01144
01145 set_file_stats(dest_file);
01146
01147 if (!end_time) {
01148 end_time = start_time;
01149 }
01150
01151 update_datastream_times(dsc_name, dsc_level, start_time, end_time);
01152
01153
01154
01155
01156
01157 if(utime(dest_file, NULL) < 0) {
01158 append_log_msg(__FILE__, __LINE__,
01159 "Error #%i updating file time for: %s\n -> %s\n",
01160 errno, dest_file, strerror(errno));
01161 current_status(STATUS_NOUTIME);
01162 return(FAILURE);
01163 }
01164
01165 return(SUCCESS);
01166 }
01167
01168
01169
01170
01171
01172
01173
01174
01175
01176
01177
01178
01179
01180
01181
01182
01183
01184
01185
01186
01187
01188
01189
01190
01191
01192
01193
01194
01195
01196
01197 int move_ingested_raw(
01198 DataChunk *dc,
01199 char *dsc_name,
01200 char *filename,
01201 time_t badtime)
01202 {
01203 const char *site;
01204 const char *facility;
01205 char *extension;
01206 char dc_platform[MAXPLATNAME];
01207 char *chrp;
01208 int nsamples;
01209 ZebTime zeb_time;
01210 time_t start_time;
01211 time_t end_time;
01212 int status;
01213
01214
01215
01216
01217 if (dsc_name == NULL) {
01218
01219 site = get_process_site();
01220 facility = get_process_facility();
01221
01222 if (dc == NULL) {
01223 append_log_msg(__FILE__, __LINE__,
01224 "Null Datachunk and NULL dsc_name while trying to move file: %s\n", filename);
01225 current_status(STATUS_NOMOVERAW);
01226 return(FAILURE);
01227 }
01228
01229 sprintf(dc_platform, "%s", ds_PlatformName(dc->dc_Platform));
01230
01231 dsc_name = dc_platform + strlen(site);
01232
01233 chrp = strstr(dsc_name, facility);
01234
01235 if (chrp == NULL) {
01236 append_log_msg(__FILE__, __LINE__,
01237 "Could not find facility (%s) in platform name: %s\n",
01238 facility, dc_platform);
01239 current_status(STATUS_NOMOVERAW);
01240 return(FAILURE);
01241 }
01242
01243 *chrp = '\0';
01244 }
01245
01246
01247
01248
01249 if (dc) {
01250 nsamples = dc_GetNSample(dc);
01251 }
01252 else {
01253 nsamples = 0;
01254 }
01255
01256 if (nsamples > 0) {
01257
01258 dc_GetTime(dc, 0, &zeb_time);
01259 start_time = zeb_time.zt_Sec;
01260
01261 dc_GetTime(dc, nsamples - 1, &zeb_time);
01262 end_time = zeb_time.zt_Sec;
01263
01264 extension = "raw";
01265 }
01266 else {
01267 if (badtime > 0) {
01268 start_time = end_time = badtime;
01269 }
01270 else {
01271 append_log_msg(__FILE__, __LINE__,
01272 "Could not determine time for bad file: %s\n", filename);
01273 current_status(STATUS_NOMOVERAW);
01274 return(FAILURE);
01275 }
01276
01277 extension = "bad";
01278
01279 append_log_msg(__FILE__, __LINE__,
01280 "Null DataChunk after processing file: %s\n", filename);
01281
01282
01283
01284
01285
01286
01287 current_status(STATUS_NULLDC);
01288 }
01289
01290 status = rename_raw(
01291 filename, start_time, end_time, dsc_name, "00", extension, 2);
01292
01293 return(status);
01294 }
01295
01296
01297
01298
01299
01300
01301
01302
01303
01304
01305
01306
01307
01308
01309
01310
01311
01312
01313
01314
01315
01316 int parse_dimension_string(char *parse_string, int *dim_num, int *is_static)
01317 {
01318 int i;
01319 int num;
01320 int pstrlen;
01321 char c;
01322
01323 pstrlen = strlen(parse_string);
01324
01325 for(i = 0, num = 0; i < pstrlen; num++) {
01326
01327 while(!isdigit(parse_string[i])) {
01328 if(i > pstrlen) break;
01329 i++;
01330 }
01331
01332 dim_num[num] = 0;
01333 while(isdigit(parse_string[i])) {
01334 c = parse_string[i];
01335 dim_num[num] = 10 * dim_num[num] + atoi(&c);
01336 i++;
01337 }
01338
01339
01340
01341 if(dim_num[num] == 0) {
01342 *is_static = 1;
01343
01344
01345
01346 num--;
01347 }
01348 }
01349
01350 return(num);
01351 }
01352
01353
01354
01355
01356
01357
01358
01359
01360
01361
01362
01363
01364
01365
01366
01367
01368
01369
01370
01371
01372
01373
01374
01375
01376
01377
01378
01379
01380
01381
01382
01383
01384
01385
01386
01387
01388
01389
01390
01391
01392 int set_fields(DataChunk *dc,
01393 FieldId *fields,
01394 int *num_dims,
01395 int *dimsizes,
01396 int make_dims_fields)
01397 {
01398 char tdb_plat_base[SHORTSTRLEN];
01399 char tdb_plat_name[SHORTSTRLEN];
01400 char tdb_plat[SHORTSTRLEN];
01401 char datalevel[8];
01402 int pindex;
01403 char *chrp;
01404 int ndims;
01405 int ndimatts;
01406 int nflds;
01407 int nflds_total;
01408 int natts;
01409 char *tdbvalue;
01410 char *tdbdimsize;
01411 char *datatype;
01412 unsigned long dimsize;
01413 FieldId dimids[DC_MaxDimension];
01414 FieldId vardims[DC_MaxDimension];
01415 int dim_nums[DC_MaxDimension];
01416 int ndim_fields;
01417 int fielddims;
01418 int is_static;
01419 int i;
01420 int j;
01421
01422 int make_qc_field;
01423 FieldId qc_fid;
01424 FieldId qc_time_fid;
01425
01426 char qc_name[MAX_QC_NAME_LENGTH];
01427 char qc_descrip[MAX_QC_DESCRIP_LENGTH];
01428 char qc_units[9];
01429 DC_ElemType qc_fieldtype;
01430
01431
01432
01433
01434
01435
01436
01437
01438
01439
01440
01441 static DC_ElemType dimtype[MAX_DCS][MAX_FIELDS];
01442 static char *dimname[MAX_DCS][MAX_DIMS];
01443 static char *dimdescrip[MAX_DCS][MAX_DIMS];
01444 static char *dimunits[MAX_DCS][MAX_DIMS];
01445 static char *dimres[MAX_DCS][MAX_DIMS];
01446 static char *dimattname[MAX_DCS][MAX_DIMS][MAX_FLDATTS];
01447 static char *dimattvalue[MAX_DCS][MAX_DIMS][MAX_FLDATTS];
01448
01449
01450
01451 static DC_ElemType fieldtype[MAX_DCS][MAX_FIELDS];
01452 static char *name[MAX_DCS][MAX_FIELDS];
01453 static char *dim[MAX_DCS][MAX_FIELDS];
01454 static char *descrip[MAX_DCS][MAX_FIELDS];
01455 static char *units[MAX_DCS][MAX_FIELDS];
01456
01457 static int qc_fields[MAX_DCS][MAX_FIELDS];
01458
01459
01460
01461 static char *min[MAX_DCS][MAX_FIELDS];
01462 static char *max[MAX_DCS][MAX_FIELDS];
01463 static char *delta[MAX_DCS][MAX_FIELDS];
01464 static char *missing[MAX_DCS][MAX_FIELDS];
01465 static char *res[MAX_DCS][MAX_FIELDS];
01466
01467
01468
01469 static char *attname[MAX_DCS][MAX_FIELDS][MAX_FLDATTS];
01470 static char *attvalue[MAX_DCS][MAX_FIELDS][MAX_FLDATTS];
01471 static int fields_done[MAX_DCS];
01472 static char qc_comm[1800];
01473 static char qc_min_delta[64];
01474 static char qc_check_prior[64];
01475 static char qc_max_delta[64];
01476 int status;
01477 double d_min_expected;
01478 double d_max_expected;
01479 double d_check_prior;
01480
01481 print_debug(__FILE__, __LINE__,
01482 "Entering set_fields for platform: %s\n", ds_PlatformName(dc->dc_Platform));
01483
01484 if (!db_reconnect()) {
01485 return(0);
01486 }
01487
01488 pindex = set_pindex(dc);
01489
01490
01491
01492
01493 get_tdb_plat_names(
01494 dc,
01495 tdb_plat_base,
01496 tdb_plat_name,
01497 NULL, NULL);
01498
01499
01500
01501
01502 get_dc_platform_datalevel(dc, datalevel);
01503
01504 print_debug(__FILE__, __LINE__,
01505 "TDB lookup info: tdb_plat_base = '%s', tdb_plat_name = '%s', datalevel = '%s'\n",
01506 tdb_plat_base, tdb_plat_name, datalevel);
01507
01508
01509
01510
01511
01512 tdbvalue = get_field_name(tdb_plat_name, datalevel, 1);
01513 if (tdbvalue) {
01514 strcpy(tdb_plat, tdb_plat_name);
01515 shared_tdb_free(tdbvalue);
01516
01517 print_debug(__FILE__, __LINE__,
01518 "Using tdb_plat_name to set fields\n");
01519 }
01520 else if (is_db_error()) {
01521 db_disconnect();
01522 return(0);
01523 }
01524 else {
01525 strcpy(tdb_plat, tdb_plat_base);
01526
01527 print_debug(__FILE__, __LINE__,
01528 "Using tdb_plat_base to set fields\n");
01529 }
01530
01531
01532
01533
01534 if (strcmp (datalevel, "b1") == 0) {
01535
01536
01537
01538
01539
01540
01541 status = get_qc_time_interval(tdb_plat_base, tdb_plat_name,
01542 &d_min_expected, &d_max_expected, &d_check_prior);
01543
01544 if (status) {
01545
01546 strcpy (qc_name, "qc_time");
01547 strcpy (qc_descrip, "Results of quality checks on sample time");
01548 strcpy (qc_units, "unitless");
01549
01550 qc_time_fid = F_DeclareField(qc_name, qc_descrip, qc_units);
01551
01552 print_debug(__FILE__, __LINE__,
01553 "Making QC field named: %s fid=%d\n", qc_name, qc_time_fid);
01554
01555 dc_SetFieldAttr(dc, qc_time_fid, "long_name", qc_descrip);
01556 dc_SetFieldAttr(dc, qc_time_fid, "units", qc_units);
01557
01558 qc_comm[0] = (char) NULL;
01559
01560
01561
01562 strcat(qc_comm, "The qc_time values are calculated by comparing each sample\n");
01563 strcat(qc_comm, "time with the previous time (i.e. delta_t = t[n] - t[n-1]).\n");
01564 strcat(qc_comm, "If the 'qc_check_prior' flag is set the first sample time\n");
01565 strcat(qc_comm, "from a new raw file will be compared against the time just\n");
01566 strcat(qc_comm, "previous to it in the stored data. If the 'qc_check_prior'\n");
01567 strcat(qc_comm, "flag is not set the qc_time value for the first sample time\n");
01568 strcat(qc_comm, "will be set to 0\n");
01569 strcat(qc_comm, "\n");
01570 strcat(qc_comm, "The qc_time bit values are as follows:\n");
01571 strcat(qc_comm, "=========================================================\n");
01572 strcat(qc_comm, "0x0 = delta time is within the specified range\n");
01573 strcat(qc_comm, "0x1 = delta time is equal to 0, duplicate sample times\n");
01574 strcat(qc_comm, "0x2 = delta time is less than the 'delta_t_lower_limit'\n");
01575 strcat(qc_comm, "0x4 = delta time is greater than the 'delta_t_upper_limit'\n");
01576 strcat(qc_comm, "\n");
01577
01578 dc_SetFieldAttr(dc, qc_time_fid, "description", qc_comm);
01579
01580 sprintf (qc_min_delta, "%lf", d_min_expected);
01581 sprintf (qc_max_delta, "%lf", d_max_expected);
01582 sprintf (qc_check_prior,"%lf", d_check_prior);
01583
01584 set_flattr(dc, qc_time_fid,
01585 "delta_t_lower_limit", DCT_Integer, 1, qc_min_delta);
01586 set_flattr(dc, qc_time_fid,
01587 "delta_t_upper_limit", DCT_Integer, 1, qc_max_delta);
01588
01589 set_flattr(dc, qc_time_fid,
01590 "prior_sample_flag", DCT_Integer, 1, qc_check_prior);
01591
01592 fielddims = 0;
01593 is_static = 0;
01594
01595 dc_NSDefineVariable (dc,
01596 qc_time_fid,
01597 fielddims,
01598 vardims,
01599 is_static);
01600
01601 }
01602 }
01603
01604
01605
01606
01607 print_debug(__FILE__, __LINE__,
01608 "Setting up the dimensions\n", ds_PlatformName(dc->dc_Platform));
01609
01610 ndim_fields = 0;
01611 for(ndims = 1; ndims < DC_MaxDimension; ndims++) {
01612
01613 if (fields_done[pindex]) {
01614 if (!dimname[pindex][ndims]) {
01615 break;
01616 }
01617 }
01618 else {
01619 if ((dimname[pindex][ndims]
01620 = get_dimension_name(tdb_plat, datalevel, ndims)) == NULL) {
01621 break;
01622 }
01623
01624 if (strcmp(dimname[pindex][ndims], "end_dimensions") == 0) {
01625 shared_tdb_free(dimname[pindex][ndims]);
01626 dimname[pindex][ndims] = NULL;
01627 break;
01628 }
01629
01630 if ((datatype = get_dimension_key(TDBFLDTYPE, tdb_plat, datalevel, ndims)) == NULL) {
01631 dimtype[pindex][ndims] = get_field_type("float");
01632 }
01633 else {
01634 dimtype[pindex][ndims] = get_field_type(datatype);
01635 shared_tdb_free(datatype);
01636 }
01637
01638 if ((dimdescrip[pindex][ndims]
01639 = get_dimension_key(TDBFLDDESCRIP, tdb_plat, datalevel, ndims)) == NULL) {
01640 dimdescrip[pindex][ndims] = (char *)malloc(2 * sizeof(char));
01641 strcpy(dimdescrip[pindex][ndims], " ");
01642 }
01643 if ((dimunits[pindex][ndims]
01644 = get_dimension_key(TDBFLDUNITS, tdb_plat, datalevel, ndims)) == NULL) {
01645 dimunits[pindex][ndims] = (char *)malloc(2 * sizeof(char));
01646 strcpy(dimunits[pindex][ndims], " ");
01647 }
01648
01649 dimres[pindex][ndims] = get_dlattr_key(TDBFLDRES, tdb_plat, datalevel, ndims);
01650 }
01651
01652
01653
01654
01655
01656
01657 if ((tdbdimsize = get_dlattr_key(TDBDIMSIZE, tdb_plat_name, datalevel, ndims)) == NULL) {
01658 if ((tdbdimsize = get_dlattr_key(TDBDIMSIZE, tdb_plat_base, datalevel, ndims)) == NULL) {
01659 break;
01660 }
01661 }
01662
01663 sscanf(tdbdimsize, "%d", &dimsize);
01664 shared_tdb_free(tdbdimsize);
01665
01666 dimids[ndims - 1] = F_DeclareField(dimname[pindex][ndims],
01667 dimdescrip[pindex][ndims], dimunits[pindex][ndims]);
01668
01669 dc_NSDefineDimension(dc, dimids[ndims - 1], dimsize);
01670 if (dimsizes != NULL) dimsizes[ndims - 1] = dimsize;
01671
01672
01673
01674
01675 if (make_dims_fields) {
01676
01677 print_debug(__FILE__, __LINE__,
01678 "Making the dimensions fields\n");
01679
01680 ndim_fields++;
01681 fields[ndims - 1] = dimids[ndims - 1];
01682
01683 dc_NSDefineVariable(dc, fields[ndims - 1], 1, &dimids[ndims - 1], TRUE);
01684
01685 if (dimres[pindex][ndims]) {
01686 set_flattr(dc, dimids[ndims - 1], "resolution", dimtype[pindex][ndims], 1, dimres[pindex][ndims]);
01687 }
01688
01689
01690
01691 for (ndimatts = 1; ndimatts < MAX_FLDATTS; ndimatts++) {
01692
01693 if (fields_done[pindex]) {
01694 if (!dimattname[pindex][ndims][ndimatts]) {
01695 break;
01696 }
01697 }
01698 else {
01699 if ((dimattname[pindex][ndims][ndimatts]
01700 = get_dlattr_name(tdb_plat, datalevel, ndims, ndimatts)) == NULL) {
01701 break;
01702 }
01703 dimattvalue[pindex][ndims][ndimatts]
01704 = get_dlattr_value(tdb_plat, datalevel, ndims, ndimatts);
01705 }
01706
01707 if (dimattvalue[pindex][ndims][ndimatts]) {
01708 dc_SetFieldAttr(dc, dimids[ndims - 1],
01709 dimattname[pindex][ndims][ndimatts], dimattvalue[pindex][ndims][ndimatts]);
01710 }
01711 }
01712 }
01713 }
01714 ndims--;
01715
01716 if (num_dims != NULL) *num_dims = ndims;
01717
01718 print_debug(__FILE__, __LINE__,
01719 "Setup %i dimensions\n", ndims);
01720
01721
01722
01723
01724 print_debug(__FILE__, __LINE__,
01725 "Setting up the fields\n", ds_PlatformName(dc->dc_Platform));
01726
01727 for (nflds = 1; nflds < MAX_FIELDS - ndim_fields; nflds++) {
01728
01729 make_qc_field = 0;
01730
01731 print_debug (__FILE__, __LINE__,
01732 "Starting out with make_qc_field = 0 for nflds=%d\n", nflds);
01733
01734 if (fields_done[pindex]) {
01735 if (!name[pindex][nflds]) {
01736 break;
01737 }
01738 }
01739 else {
01740 if ((name[pindex][nflds] = get_field_name(tdb_plat, datalevel, nflds)) == NULL) {
01741 break;
01742 }
01743 if (strcmp(name[pindex][nflds], "end_fields") == 0) {
01744 shared_tdb_free(name[pindex][nflds]);
01745 name[pindex][nflds] = NULL;
01746 break;
01747 }
01748
01749 if ((descrip[pindex][nflds] = get_field_key(TDBFLDDESCRIP, tdb_plat, datalevel, nflds)) == NULL) {
01750 descrip[pindex][nflds] = (char *)malloc(2 * sizeof(char));
01751 strcpy(descrip[pindex][nflds], " ");
01752 }
01753 if ((units[pindex][nflds] = get_field_key(TDBFLDUNITS, tdb_plat, datalevel, nflds)) == NULL) {
01754 units[pindex][nflds] = (char *)malloc(2 * sizeof(char));
01755 strcpy(units[pindex][nflds], " ");
01756 }
01757
01758 if ((datatype = get_field_key(TDBFLDTYPE, tdb_plat, datalevel, nflds)) == NULL) {
01759 fieldtype[pindex][nflds] = get_field_type("float");
01760 }
01761 else {
01762 fieldtype[pindex][nflds] = get_field_type(datatype);
01763 shared_tdb_free(datatype);
01764 }
01765
01766 dim[pindex][nflds] = get_field_key(TDBFLDDIMS, tdb_plat, datalevel, nflds);
01767 missing[pindex][nflds] = get_field_key(TDBFLDMISSING, tdb_plat, datalevel, nflds);
01768
01769 min[pindex][nflds] = get_flattr_key(TDBFLDMIN, tdb_plat, datalevel, nflds);
01770 max[pindex][nflds] = get_flattr_key(TDBFLDMAX, tdb_plat, datalevel, nflds);
01771 delta[pindex][nflds] = get_flattr_key(TDBFLDDELTA, tdb_plat, datalevel, nflds);
01772 res[pindex][nflds] = get_flattr_key(TDBFLDRES, tdb_plat, datalevel, nflds);
01773
01774 for (natts = 1; natts < MAX_FLDATTS; natts++) {
01775 if ((attname[pindex][nflds][natts] = get_flattr_name(tdb_plat, datalevel, nflds, natts)) == NULL) {
01776 break;
01777 }
01778 if (strcmp(attname[pindex][nflds][natts], "end_field_attributes") == 0) {
01779 shared_tdb_free(attname[pindex][nflds][natts]);
01780 attname[pindex][nflds][natts] = NULL;
01781 break;
01782 }
01783
01784 attvalue[pindex][nflds][natts] = get_flattr_value(tdb_plat, datalevel, nflds, natts);
01785 }
01786 }
01787
01788 fields[nflds + ndim_fields - 1] = F_DeclareField(name[pindex][nflds],
01789 descrip[pindex][nflds],
01790 units[pindex][nflds]);
01791
01792 print_debug (__FILE__, __LINE__,
01793 "Making non-QC field named %s with fid=%d\n",
01794 name[pindex][nflds], fields[nflds + ndim_fields - 1]);
01795
01796
01797
01798
01799
01800 dc_SetFieldAttr(dc, fields[nflds + ndim_fields - 1],
01801 "long_name", descrip[pindex][nflds]);
01802 dc_SetFieldAttr(dc, fields[nflds + ndim_fields - 1],
01803 "units", units[pindex][nflds]);
01804
01805 print_debug(__FILE__, __LINE__,
01806 "Setting standard attributes for field: %s\n", name[pindex][nflds]);
01807
01808 if (min[pindex][nflds]) {
01809 set_flattr(dc, fields[nflds + ndim_fields - 1],
01810 "valid_min", fieldtype[pindex][nflds], 1, min[pindex][nflds]);
01811
01812 make_qc_field = 1;
01813 print_debug (__FILE__, __LINE__,
01814 "set make_qc_field=1 because we have min=%s\n", min[pindex][nflds]);
01815 }
01816 if (max[pindex][nflds]) {
01817 set_flattr(dc, fields[nflds + ndim_fields - 1],
01818 "valid_max", fieldtype[pindex][nflds], 1, max[pindex][nflds]);
01819
01820 make_qc_field = 1;
01821 print_debug (__FILE__, __LINE__,
01822 "set make_qc_field=1 because we have max=%s\n", max[pindex][nflds]);
01823 }
01824 if (delta[pindex][nflds]) {
01825 set_flattr(dc, fields[nflds + ndim_fields - 1],
01826 "valid_delta", fieldtype[pindex][nflds], 1, delta[pindex][nflds]);
01827
01828 make_qc_field = 1;
01829 print_debug (__FILE__, __LINE__,
01830 "set make_qc_field=1 because we have delta=%s\n", delta[pindex][nflds]);
01831 }
01832 if (res[pindex][nflds]) {
01833 set_flattr(dc, fields[nflds + ndim_fields - 1],
01834 "resolution", fieldtype[pindex][nflds], 1, res[pindex][nflds]);
01835 }
01836 if (missing[pindex][nflds]) {
01837 if (strcmp(missing[pindex][nflds], "NA") != 0) {
01838 set_flattr(dc, fields[nflds + ndim_fields - 1],
01839 "missing_value", fieldtype[pindex][nflds], 1, missing[pindex][nflds]);
01840 }
01841 }
01842 else {
01843 set_flattr(dc, fields[nflds + ndim_fields - 1],
01844 "missing_value", fieldtype[pindex][nflds], 1, DEFAULT_MISSING);
01845 }
01846
01847
01848
01849 print_debug(__FILE__, __LINE__,
01850 "Setting non-standard attributes for field: %s\n", name[pindex][nflds]);
01851
01852 for (natts = 1; natts < MAX_FLDATTS; natts++) {
01853
01854 if (attname[pindex][nflds][natts] == NULL) {
01855 break;
01856 }
01857
01858 if (attvalue[pindex][nflds][natts] != NULL) {
01859 dc_SetFieldAttr(dc, fields[nflds + ndim_fields - 1],
01860 attname[pindex][nflds][natts], attvalue[pindex][nflds][natts]);
01861 }
01862 }
01863
01864
01865
01866
01867
01868
01869
01870
01871
01872 fielddims = 0;
01873 is_static = 0;
01874 if(dim[pindex][nflds]) {
01875 fielddims = parse_dimension_string(dim[pindex][nflds], dim_nums, &is_static);
01876 for(i = 0; i < fielddims; i++) {
01877 vardims[i] = dimids[dim_nums[i] - 1];
01878 }
01879 }
01880 dc_NSDefineVariable(dc, fields[nflds + ndim_fields - 1],
01881 fielddims, vardims, is_static);
01882
01883 print_debug(__FILE__, __LINE__,
01884 "Finished declaring field: %s\n", name[pindex][nflds]);
01885
01886 if (make_qc_field == 1 && strcmp(datalevel, "b1") == 0) {
01887
01888 print_debug (__FILE__, __LINE__,
01889 "make_qc_field is 1 for %s\n", name[pindex][nflds]);
01890
01891
01892 sprintf (qc_name, "qc_%s", name[pindex][nflds]);
01893 sprintf (qc_descrip, "Quality check results on field: %s", descrip[pindex][nflds]);
01894 sprintf (qc_units, "unitless");
01895
01896 qc_fid = F_DeclareField(qc_name,
01897 qc_descrip,
01898 qc_units);
01899
01900 print_debug(__FILE__, __LINE__,
01901 "Making QC field named: %s fid=%d\n", qc_name, qc_fid);
01902
01903 dc_NSDefineVariable(dc,
01904 qc_fid,
01905 fielddims,
01906 vardims,
01907 is_static);
01908
01909
01910
01911
01912
01913
01914 qc_fields[pindex][qc_fid] = 1;
01915 }
01916 }
01917 nflds--;
01918
01919 nflds_total = nflds;
01920
01921
01922
01923
01924 db_disconnect();
01925
01926
01927
01928 dc_NSDefineComplete (dc);
01929
01930 for(i = 1; i <= ndim_fields; i++) {
01931 dc_SetType(dc, fields[i - 1], dimtype[pindex][i]);
01932 }
01933
01934 if (strcmp (datalevel, "b1") == 0) {
01935
01936
01937
01938 qc_fieldtype = get_field_type("int");
01939 dc_SetType (dc, qc_time_fid, qc_fieldtype);
01940 }
01941
01942 for(nflds = 1; nflds <= nflds_total; nflds++) {
01943
01944 dc_SetType(dc,
01945 fields[nflds + ndim_fields - 1],
01946 fieldtype[pindex][nflds]);
01947
01948 print_debug(__FILE__, __LINE__,
01949 "Set field type for fid=%d\n",
01950 fields[nflds + ndim_fields - 1]);
01951 }
01952
01953 for (i=1; i < MAX_FIELDS; i++) {
01954
01955 if (qc_fields[pindex][i] == 1) {
01956 dc_SetType (dc, i, DCT_Integer);
01957 print_debug(__FILE__, __LINE__,
01958 "Set field type for fid=%d (QC field) \n", i);
01959 }
01960 }
01961
01962 if (nflds_total) {
01963 fields_done[pindex] = 1;
01964 print_debug(__FILE__, __LINE__,
01965 "Set %d fields for platform %s\n", nflds_total, tdb_plat);
01966 }
01967 else {
01968 append_log_msg(__FILE__, __LINE__,
01969 "Failed to set fields for: %s\n", tdb_plat);
01970 current_status(STATUS_INITDC);
01971 return(0);
01972 }
01973
01974 return(nflds_total);
01975 }
01976
01977
01978
01979
01980
01981
01982
01983
01984
01985
01986
01987
01988
01989
01990
01991
01992
01993
01994
01995
01996
01997
01998
01999
02000
02001 void set_flattr(DataChunk *dc, FieldId field, char *attname,
02002 DC_ElemType atttype, int nelems, char *attvalue)
02003 {
02004 float fdata;
02005 double ddata;
02006 long double lddata;
02007 unsigned char bytes;
02008 short sdata;
02009 unsigned short usdata;
02010 int idata;
02011 unsigned int uidata;
02012 long ldata;
02013 unsigned long uldata;
02014
02015 switch (atttype) {
02016 case DCT_Float:
02017 fdata = (float)atof(attvalue);
02018 dc_SetFieldAttrArray(dc, field, attname, DCT_Float, 1, (void *)&fdata);
02019 break;
02020 case DCT_Double:
02021 ddata = atof(attvalue);
02022 dc_SetFieldAttrArray(dc, field, attname, DCT_Double, 1, (void *)&ddata);
02023 break;
02024 case DCT_LongDouble:
02025 lddata = (long double)atof(attvalue);
02026 dc_SetFieldAttrArray(dc, field, attname, DCT_LongDouble, 1, (void *)&lddata);
02027 break;
02028 case DCT_Char:
02029 dc_SetFieldAttrArray(dc, field, attname, DCT_Char, strlen(attvalue), (void *)attvalue);
02030 break;
02031 case DCT_UnsignedChar:
02032 bytes = (unsigned char)atoi(attvalue);
02033 dc_SetFieldAttrArray(dc, field, attname, DCT_UnsignedChar, 1, (void *)&bytes);
02034 break;
02035 case DCT_ShortInt:
02036 sdata = (short)atoi(attvalue);
02037 dc_SetFieldAttrArray(dc, field, attname, DCT_ShortInt, 1, (void *)&sdata);
02038 break;
02039 case DCT_UnsignedShort:
02040 usdata = (unsigned short)atoi(attvalue);
02041 dc_SetFieldAttrArray(dc, field, attname, DCT_UnsignedShort, 1, (void *)&usdata);
02042 break;
02043 case DCT_Integer:
02044 idata = atoi(attvalue);
02045 dc_SetFieldAttrArray(dc, field, attname, DCT_Integer, 1, (void *)&idata);
02046 break;
02047 case DCT_UnsignedInt:
02048 uidata = (unsigned int)atoi(attvalue);
02049 dc_SetFieldAttrArray(dc, field, attname, DCT_UnsignedInt, 1, (void *)&uidata);
02050 break;
02051 case DCT_LongInt:
02052 ldata = atol(attvalue);
02053 dc_SetFieldAttrArray(dc, field, attname, DCT_LongInt, 1, (void *)&ldata);
02054 break;
02055 case DCT_UnsignedLong:
02056 uldata = (unsigned long)atol(attvalue);
02057 dc_SetFieldAttrArray(dc, field, attname, DCT_UnsignedLong, 1, (void *)&uldata);
02058 break;
02059 default:
02060 dc_SetFieldAttrArray(dc, field, attname, DCT_String, 1, (void *)attvalue);
02061 break;
02062 }
02063 }
02064
02065
02066
02067
02068
02069
02070
02071
02072
02073
02074
02075
02076
02077
02078
02079
02080
02081
02082
02083
02084
02085
02086
02087
02088 int set_flattr_value (DataChunk *dc, char *field_name, char *flattr_name, char *flattr_value)
02089 {
02090 FieldId fid;
02091
02092
02093
02094 if ((fid = F_Declared(field_name)) < 0) {
02095 append_log_msg(__FILE__, __LINE__,
02096 "Field %s is not in the specified datachunk!", field_name);
02097 return(0);
02098 }
02099
02100
02101
02102 dc_SetFieldAttr(dc, fid, flattr_name, flattr_value);
02103 print_debug(__FILE__, __LINE__,
02104 "Set flatter value, %s:%s = '%s'\n",
02105 field_name, flattr_name, flattr_value);
02106
02107 return (1);
02108 }
02109
02110
02111
02112
02113
02114
02115
02116
02117
02118
02119
02120
02121
02122
02123
02124
02125
02126
02127
02128 int set_global(DataChunk *dc, char *filename)
02129 {
02130 char ingest_version[128];
02131 char tdb_plat_base[SHORTSTRLEN];
02132 char tdb_plat_name[SHORTSTRLEN];
02133 char tdb_alias_base[SHORTSTRLEN];
02134 char tdb_alias_name[SHORTSTRLEN];
02135 char datalevel[8];
02136 char input_source[MAXSTRLEN];
02137 char facility_loc[SHORTSTRLEN];
02138
02139 const char *rcsid;
02140 const char *rcsstate;
02141 const char *site_name;
02142 const char *facility;
02143 ProcLoc *proc_loc;
02144
02145 char *chrp;
02146 char *fld_ind_descrip;
02147 char *res_descrip;
02148 char *tdbvalue;
02149
02150 int attnum;
02151 char *attname;
02152 char *attvalue;
02153
02154 print_debug(__FILE__, __LINE__,
02155 "Setting global attributes for platform: %s\n", ds_PlatformName(dc->dc_Platform));
02156
02157 if (!db_reconnect()) {
02158 return(FAILURE);
02159 }
02160
02161
02162
02163
02164 get_tdb_plat_names(
02165 dc,
02166 tdb_plat_base,
02167 tdb_plat_name,
02168 tdb_alias_base,
02169 tdb_alias_name);
02170
02171 rcsid = get_ingest_rcsid();
02172 rcsstate = get_ingest_rcsstate();
02173 site_name = get_process_site();
02174 facility = get_process_facility();
02175 proc_loc = get_ingest_location();
02176
02177
02178
02179
02180
02181
02182
02183
02184
02185
02186
02187
02188
02189
02190
02191
02192 dslib_trim_rcs_string(rcsstate, ingest_version, 128);
02193 dc_SetGlobalAttr(dc, "ingest_version", ingest_version);
02194
02195
02196
02197 dc_SetGlobalAttr(dc, "libingest_version", get_ingest_lib_version());
02198
02199
02200
02201 dc_SetGlobalAttr(dc, "libdslibc_version", dslib_get_version());
02202
02203
02204
02205 dc_SetGlobalAttr(dc, "libdsdb_version", dsdb_get_version());
02206
02207
02208
02209 dc_SetGlobalAttr(dc, "ingest_software", (char *)&rcsid[4]);
02210
02211
02212
02213 get_dc_platform_datalevel(dc, datalevel);
02214 dc_SetGlobalAttr(dc, "proc_level", datalevel);
02215
02216
02217
02218 sprintf(input_source, "%s", filename);
02219 dc_SetGlobalAttr(dc, "input_source", input_source);
02220
02221
02222
02223 dc_SetGlobalAttr(dc,"site_id", (char *)site_name);
02224
02225
02226
02227
02228 sprintf(facility_loc, "%s: %s", facility, proc_loc->name);
02229
02230 dc_SetGlobalAttr(dc,"facility_id", facility_loc);
02231
02232
02233
02234
02235
02236
02237
02238
02239
02240
02241
02242
02243 tdbvalue = get_glattr_key("sample",
02244 tdb_plat_name, tdb_plat_base,
02245 tdb_alias_name, tdb_alias_base);
02246
02247 if (tdbvalue != NULL) {
02248 dc_SetGlobalAttr(dc, "sample_int", tdbvalue);
02249 shared_tdb_free(tdbvalue);
02250 }
02251 else {
02252 dc_SetGlobalAttr(dc, "sample_int", "Not Specified");
02253 }
02254
02255
02256
02257
02258
02259
02260
02261 tdbvalue = get_glattr_key("averaging",
02262 tdb_plat_name, tdb_plat_base,
02263 tdb_alias_name, tdb_alias_base);
02264
02265 if (tdbvalue != NULL) {
02266 dc_SetGlobalAttr(dc, "averaging_int", tdbvalue);
02267 shared_tdb_free(tdbvalue);
02268 }
02269 else {
02270 dc_SetGlobalAttr(dc, "averaging_int", "None");
02271 }
02272
02273
02274
02275 tdbvalue = get_glattr_key("serial",
02276 tdb_plat_name, tdb_plat_base,
02277 tdb_alias_name, tdb_alias_base);
02278
02279 if (tdbvalue != NULL) {
02280 dc_SetGlobalAttr(dc, "serial_number", tdbvalue);
02281 shared_tdb_free(tdbvalue);
02282 }
02283 else {
02284 dc_SetGlobalAttr(dc,"serial_number", "Not Specified");
02285 }
02286
02287
02288
02289
02290
02291
02292
02293
02294
02295
02296
02297
02298
02299
02300
02301
02302
02303
02304
02305 tdbvalue = get_glattr_key("comment",
02306 tdb_plat_name, tdb_plat_base,
02307 tdb_alias_name, tdb_alias_base);
02308
02309 if (tdbvalue != NULL) {
02310 dc_SetGlobalAttr(dc, "comment", tdbvalue);
02311 shared_tdb_free(tdbvalue);
02312 }
02313 else {
02314 dc_SetGlobalAttr(dc, "comment", "");
02315 }
02316
02317
02318
02319
02320
02321
02322
02323
02324
02325
02326
02327
02328
02329 if ((res_descrip = get_resolution_descrip()) == NULL) {
02330 dc_SetGlobalAttr(dc, "resolution_description", "Not Specified");
02331 }
02332 else {
02333 dc_SetGlobalAttr(dc,"resolution_description", res_descrip);
02334 }
02335
02336
02337
02338 for(attnum = 1; attnum < MAX_METADATA; attnum++) {
02339
02340 attname = get_glattr_name(attnum,
02341 tdb_plat_name, tdb_plat_base,
02342 tdb_alias_name, tdb_alias_base);
02343
02344 if (attname == NULL) {
02345 break;
02346 }
02347 if (strcmp(attname, "end_global_attributes") == 0) {
02348 shared_tdb_free(attname);
02349 break;
02350 }
02351
02352 attvalue = get_glattr_value(attnum,
02353 tdb_plat_name, tdb_plat_base,
02354 tdb_alias_name, tdb_alias_base);
02355
02356 if (attvalue != NULL) {
02357 dc_SetGlobalAttr(dc, attname, attvalue);
02358 shared_tdb_free(attvalue);
02359 }
02360 else {
02361 dc_SetGlobalAttr(dc, attname, "");
02362 }
02363 shared_tdb_free(attname);
02364 }
02365
02366 db_disconnect();
02367
02368 return(SUCCESS);
02369 }
02370
02371
02372
02373
02374
02375
02376
02377
02378
02379
02380
02381
02382
02383
02384
02385
02386
02387 ProcLoc *set_location(DataChunk *dc)
02388 {
02389 static Location loc;
02390 ProcLoc *proc_loc;
02391
02392 print_debug(__FILE__, __LINE__,
02393 "Setting location for platform: %s\n", ds_PlatformName(dc->dc_Platform));
02394
02395 proc_loc = get_ingest_location();
02396
02397 if (proc_loc) {
02398 loc.l_alt = proc_loc->alt;
02399 loc.l_lat = proc_loc->lat;
02400 loc.l_lon = proc_loc->lon;
02401 }
02402 else {
02403 loc.l_alt = 0;
02404 loc.l_lat = 0;
02405 loc.l_lon = 0;
02406 }
02407
02408 dc_SetStaticLoc(dc, &loc);
02409
02410 return(proc_loc);
02411 }
02412
02413
02414
02415
02416
02417
02418
02419
02420
02421
02422
02423
02424
02425
02426
02427
02428
02429
02430
02431
02432
02433
02434
02435 void set_location_from_data(DataChunk *dc, float *lat, float *lon, float *alt)
02436 {
02437 Location loc;
02438
02439 print_debug(__FILE__, __LINE__,
02440 "Setting location for platform: %s\n", ds_PlatformName(dc->dc_Platform));
02441
02442 loc.l_lat = *lat;
02443 loc.l_lon = *lon;
02444 loc.l_alt = *alt;
02445
02446 dc_SetStaticLoc(dc, &loc);
02447
02448 }
02449
02450
02451
02452
02453
02454
02455
02456
02457
02458
02459
02460
02461
02462
02463
02464
02465
02466
02467
02468
02469 int set_pindex(DataChunk *dc)
02470 {
02471 static char platform_names[MAX_DCS][MAXSTRLEN];
02472 int pindex = 0;
02473
02474
02475
02476 while (platform_names[pindex][0] != '\0') {
02477 if (strcmp(platform_names[pindex], ds_PlatformName(dc->dc_Platform)) == 0) {
02478 break;
02479 }
02480 pindex++;
02481 }
02482
02483 if (platform_names[pindex][0] == '\0') {
02484
02485
02486
02487 sprintf(platform_names[pindex], ds_PlatformName(dc->dc_Platform));
02488 }
02489
02490 return(pindex);
02491 }
02492
02493
02494
02495
02496
02497
02498
02499
02500
02501
02502
02503
02504
02505
02506
02507
02508
02509
02510
02511
02512
02513
02514
02515
02516
02517 void set_mobile_location_from_data(DataChunk *dc, int nlocs,
02518 float *lat, float *lon, float *alt)
02519 {
02520 int i;
02521 Location loc;
02522
02523 print_debug(__FILE__, __LINE__,
02524 "Setting location for platform: %s\n", ds_PlatformName(dc->dc_Platform));
02525
02526 for(i = 0; i < nlocs; i++) {
02527 loc.l_lat = lat[i];
02528 loc.l_lon = lon[i];
02529 loc.l_alt = alt[i];
02530
02531 dc_SetLoc(dc, i, &loc);
02532 }
02533
02534 }
02535
02536
02537
02538
02539
02540
02541
02542
02543
02544
02545
02546
02547
02548
02549
02550
02551
02552
02553
02554
02555
02556
02557
02558 int set_sample_time(int hourmin, int jday, int year, ZebTime *dotime)
02559 {
02560 int total_days;
02561
02562
02563
02564 year = four_digit_year(year);
02565 year -= 1900;
02566
02567
02568
02569
02570 if (year < 90 ) {
02571 print_debug(__FILE__, __LINE__,
02572 "In set_sample_time the year is less than 90.\n");
02573 return (FAILURE);
02574 }
02575
02576
02577
02578
02579 if ((jday < 1) || (jday > 366)) {
02580 print_debug(__FILE__, __LINE__,
02581 "In set_sample_time the jday is not in the range 1-366.\n");
02582 return (FAILURE);
02583 }
02584
02585
02586
02587
02588 if ((hourmin < 0) || (hourmin > 2400)) {
02589 print_debug(__FILE__, __LINE__,
02590 "In set_sample_time the hourmin is not in the range 0-2400.\n");
02591 return (FAILURE);
02592 }
02593
02594 total_days = days_since1970(year) + jday - 1;
02595 dotime->zt_Sec =
02596 (total_days * 86400L)
02597 + (((int)(hourmin * .01)) * 3600)
02598 + (((int)(hourmin % 100)) * 60);
02599 dotime->zt_MicroSec = 0;
02600
02601
02602
02603 if (dotime->zt_Sec > time(NULL)) {
02604 print_debug(__FILE__, __LINE__,
02605 "In set_sample_time the calculated time is in the future.\n");
02606 dotime->zt_Sec = 0;
02607 }
02608
02609 return (SUCCESS);
02610 }
02611
02612
02613
02614
02615
02616
02617
02618
02619
02620
02621
02622
02623
02624
02625
02626
02627
02628
02629
02630
02631
02632 void store_datachunk(DataChunk *dc, int newfile)
02633 {
02634
02635
02636
02637
02638 alarm(30*60);
02639
02640
02641
02642 set_dc_stats(dc);
02643
02644
02645
02646 ds_StoreBlocks(dc, newfile, NULL, 0);
02647
02648
02649
02650 alarm(0);
02651
02652
02653
02654 prev_dc = freeDC(prev_dc);
02655 }