CRIS/SIS c Utilities Library

Last Modified Wednesday, 21-Jan-1998 13:42:06 PST.

Here are descriptions of some of the utilities available for working with CRIS or SIS data retrieval. These routines will be available to you in c when you include the utilities library as shown in the sample Makefile.


Creating a new directory:
    char directoryPath[] = "/home/idunn1/rgr/dataStorage";
    createDirectory(directoryPath);
createDirectory is used primarily by the put data routines to create new locations for archiving the data. If createDirectory fails to create the directory, the program will exit.

Checking on the existance of a directory:

    char directoryPath[] = "/home/idunn1/rgr/dataStorage";
    if (directoryExists(directoryPath))
	printf("%s exists.\n", directoryPath);
    else
	printf("%s does not exist.\n", directoryPath);
directoryExists is used primarily by the put data routines to check if an archive directory already exists (before createDirectory is called). Returns 1 (true) if the directory does exist, and 0 (false) if not.

Checking on the existance of a file:

    char filePath[] = "/home/idunn1/rgr/dataStorage/data1.out";
    if (fileExists(filePath))
        printf("%s exists.\n", filePath);
    else
        printf("%s does not exist.\n", filePath);
fileExists is used primarily by the put data routines to check if an archive data file exists already or needs to be created and initialized. Returns 1 (true) if the file does exist, and 0 (false) if not.

Construct the path to a data archive file:
generateDataFilePath is a lower level routine who's use requires some knowledge of the underlying data archive structure. It is included here in case it is needed for debugging...

    char pathBuffer[256];
    char instrumentID = 'c';
    char dataFileSuffix[] = "evt";
    struct tm *pTimeStruct;

    pTimeStruct = getDateFromDay1996(633);	/* sept 24, 1997 */
    generateDataFilePath(pathBuffer, pTimeStruct, instrumentID, dataFileSuffix);
    printf("path to data file is %s\n", pathBuffer);
generateDataFilePath takes a struct tm (date), an instrument ID ('c' for CRIS, 's' for SIS), and a suffix (bp0, ct, de, do0, do1, evt, hp, hsk, lp, sub, or sum) dependent on the data type involved, and also uses the environment variables L1_CRIS_DATA_BASE_DIRECTORY or L1_SIS_DATA_BASE_DIRECTORY to construct the path to the particular archive data file. The resulting path is placed into pathBuffer.

Get a date (struct tm format) from the day of 1996:

    struct tm *pTimeStruct;
    int day1996 = 633;		/* sept 24, 1997 (day1996 = 1 on 1/1/96) */

    pTimeStruct = getDateFromDay1996(day1996);
    printf("year = %d\n", pTimeStruct->tm_year + 1900);
    printf("month = %d\n", pTimeStruct->tm_mon);	/* 0 - 11 */
    printf("mday = %d\n", pTimeStruct->tm_mday);	/* 1 - 31 */
    printf("hour = %d\n", pTimeStruct->tm_hour);	/* 0 - 23 */
    printf("minute = %d\n", pTimeStruct->tm_min);	/* 0 - 59 */
    printf("second = %d\n", pTimeStruct->tm_sec);	/* 0 - 59 */
getDateFromDay1996 returns the struct tm (see time.h) which corresponds to the beginning of the day1996. Beware that day1996 = 1 on Jan. 1, 1996. These day numbers are printed on the ACE calendars in blue.

Get a date (struct tm format) from seconds since start of 1996:

    struct tm *pTimeStruct;
    unsigned long secondsSince1996 = 54605304;

    pTimeStruct = getDateFromSecondsSince1996(secondsSince1996);
    printf("year = %d\n", pTimeStruct->tm_year + 1900);
    printf("month = %d\n", pTimeStruct->tm_mon);        /* 0 - 11 */
    printf("mday = %d\n", pTimeStruct->tm_mday);        /* 1 - 31 */
    printf("hour = %d\n", pTimeStruct->tm_hour);        /* 0 - 23 */
    printf("minute = %d\n", pTimeStruct->tm_min);       /* 0 - 59 */
    printf("second = %d\n", pTimeStruct->tm_sec);       /* 0 - 59 */
getDateFromSecondsSince1996 returns the struct tm (see time.h) which corresponds to the number of seconds since Jan 1, 1996, 00:00:00.

Get the day1996 in which secondsSince1996 occurs:

    int day1996;
    unsigned long secondsSince1996 = 54605304;
    day1996 = getDay1996FromSecondsSince1996(secondsSince1996);
    printf("day1996 = %d\n", day1996);
getDay1996FromSecondsSince1996 returns the day of 1996 (a count of days since Jan 1, 1996 where day1996 = 1 on Jan 1, 1996) in which the second secondsSince1996 occurs.

Determine the date of the first data in an archive:

    struct tm timeStruct;
    struct tm *pTimeStruct;
    char instrumentID = 'c';

    pTimeStruct = &timeStruct;
    getFirstDateOfCurrentArchivesData(instrumentID, pTimeStruct);
    printf("year = %d\n", pTimeStruct->tm_year);
	.
	.
	.
    printf("sec = %d\n", pTimeStruct->tm_sec);
getFirstDateOfCurrentArchivesData is used to determine when the last day of data in the archive is. getFirstDateOfCurrentArchivesData fills in a struct tm with this date. getFirstDateOfCurrentArchivesData takes as arguments a char, either 'c' or 's' to signify the CRIS or SIS archive, and a pointer to an already allocated struct tm.

Determine the date of the last data in an archive:

    struct tm timeStruct;
    struct tm *pTimeStruct;
    char instrumentID = 's';

    pTimeStruct = &timeStruct;
    getLastDateOfCurrentArchivesData(instrumentID, pTimeStruct);
    printf("year = %d\n", pTimeStruct->tm_year);
	.
	.
	.
    printf("sec = %d\n", pTimeStruct->tm_sec);
getLastDateOfCurrentArchivesData is used to determine when the last day of data in the archive is. getLastDateOfCurrentArchivesData fills in a struct tm with this date. getLastDateOfCurrentArchivesData takes as arguments a char, either 'c' or 's' to signify the CRIS or SIS archive, and a pointer to an already allocated struct tm.

Transform a date (tm struct) into the number of seconds since 1996:

    unsigned long secondsSince1996;
    struct tm timeStruct, *pTimeStruct;

    timeStruct.tm_year = 97;	/* 1997 */
    timeStruct.tm_mon = 8;	/* sept. */
    timeStruct.tm_mday = 24;	/* the 24th */
    timeStruct.tm_hour = 15;	/* 3 pm */
    timeStruct.tm_min = 34;
    timeStruct.tm_sec = 30;
    timeStruct.tm_isdst = 0;	/*  no daylight savings time */
    pTimeStruct = &timeStruct;
    
    secondsSince1996 = getSecondsSince1996FromDate(pTimeStruct);
    printf("secondsSince1996 = %ld\n", secondsSince1996);
getSecondsSince1996FromDate takes a date (struct tm format, see time.h) and returns the number of seconds since 1996.

Open a file (or create it if it doesn't exist) with read/write capability:

    int bytesToRead = 1024;
    char buffer[1024];
    int fileDescriptor;
    char *path =
	"/home/idunn1/rgr/prototype/data/sis_prelim/1997.09/1997.09.24/sis_prelim.1997.09.24.hsk";

    fileDescriptor = openOrCreateFileToReadWrite(path);
    readFromFile(fileDescriptor, buffer, bytesToRead);
    close(fileDescriptor);
openOrCreateFileToReadWrite is a low level routine used internally in the CRIS/SIS routines. It is used mostly by the put routines to write data to an archive file. If the file does not exist, it is created, initialized, and re-opened with read and write capability. openOrCreateFileToReadWrite also does some error checking and exits if the file cannot be created or opened as expected.

Read from a file:

    int bytesToRead = 1024;
    char buffer[1024];
    int fileDescriptor;
    char *path =
	"/home/idunn1/rgr/prototype/data/sis_prelim/1997.09/1997.09.24/sis_prelim.1997.09.24.hsk";
    
    fileDescriptor = openOrCreateFileToReadWrite(path);
    readFromFile(fileDescriptor, buffer, bytesToRead);
    close(fileDescriptor);
readFromFile is a low level routine used internally in the CRIS/SIS routines. Primarily it just adds a little error checking to the usual read calls. If the routine does not read the expected number of bytes into the buffer, it will exit.

Write to a file:

    int bytesToRead = 1024;
    char buffer[1024];
    int fileDescriptor;
    char *path =
        "/home/idunn1/rgr/prototype/data/sis_prelim/1997.09/1997.09.24/sis_prelim.1997.09.24.hsk";
    
    fileDescriptor = openOrCreateFileToReadWrite(path);
    writeToFile(fileDescriptor, buffer, bytesToRead);
    close(fileDescriptor);
writeToFile is a low level routine used internally in the CRIS/SIS routines. Primarily it just adds a little error checking to the usual write calls. If writeToFile does not write the expected number of bytes from the buffer into the file, it will exit.

bruce