Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/logging #170

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ compiler: gcc
before_install:
- sudo apt-get update -qq
- sudo apt-get install -y libnetcdf-dev
script:
- make -C drivers/classic/
- make -C drivers/image/
script:
- make full -C drivers/classic/
- make full -C drivers/image/
4 changes: 3 additions & 1 deletion drivers/classic/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ HDRS = ${VICPATH}/include/vic_def.h \
${DRIVERPATH}/include/vic_driver_classic.h \
${DRIVERPATH}/include/mtclim.h \
${SHAREDPATH}/include/vic_driver_shared.h \
${VICPATH}/include/vic_physical_constants.h
${VICPATH}/include/vic_physical_constants.h \
${VICPATH}/include/vic_log.h

OBJS = \
${DRIVERPATH}/src/alloc_atmos.o \
Expand Down Expand Up @@ -131,6 +132,7 @@ OBJS = \
${SHAREDPATH}/src/make_veg_var.o \
${SHAREDPATH}/src/open_file.o \
${SHAREDPATH}/src/print_library_shared.o \
${SHAREDPATH}/src/vic_log.o \
${VICPATH}/src/CalcAerodynamic.o \
${VICPATH}/src/CalcBlowingSnow.o \
${VICPATH}/src/IceEnergyBalance.o \
Expand Down
1 change: 0 additions & 1 deletion drivers/classic/include/vic_driver_classic.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
#ifndef VIC_DRIVER_CLASSIC_H
#define VIC_DRIVER_CLASSIC_H

#include <stdio.h>
#include <vic_driver_shared.h>

#define VIC_DRIVER "Classic"
Expand Down
6 changes: 6 additions & 0 deletions drivers/classic/src/get_global_param.c
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,12 @@ get_global_param(FILE *gp)
options.RC_MODE = RC_JARVIS;
}
}
/*************************************
Define log directory
*************************************/
else if (strcasecmp("LOG_DIR", optstr) == 0) {
sscanf(cmdstr, "%*s %s", filenames.log_path_pfx);
}
/*************************************
Define state files
*************************************/
Expand Down
22 changes: 21 additions & 1 deletion drivers/classic/src/vic_classic.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,13 @@ main(int argc,
char *argv[])
{
/** Variable Declarations **/
extern FILE *LOG_DEST;

char MODEL_DONE;
char RUN_MODEL;
char ErrStr[MAXSTRING];
char write_flag;
char *logfilename;
size_t rec;
size_t Nveg_type;
int cellnum;
Expand Down Expand Up @@ -94,9 +96,27 @@ main(int argc,
filep.globalparam = open_file(filenames.global, "r");
get_global_param(filep.globalparam);

// Initialize Log Destination
if (strcmp(filenames.log_path, "MISSING") != 0) {
// Create logfile name
logfilename = get_logname(filenames.log_path);

// Open Logfile
filep.logfile = open_file(logfilename, "w");

LOG_DEST = filep.logfile;

log_info("Initialized Log File: %s", logfilename);
}
else {
// Set global log destination
LOG_DEST = stderr;

log_info("Logging to stderr");
}

/** Set model constants **/
if (strcmp(filenames.constants, "MISSING") != 0) {
fprintf(stderr, "reading constants for some reason\n");
filep.constants = open_file(filenames.constants, "r");
get_parameters(filep.constants);
}
Expand Down
4 changes: 3 additions & 1 deletion drivers/image/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ HDRS = ${VICPATH}/include/vic_def.h \
${VICPATH}/include/vic_run.h \
${DRIVERPATH}/include/vic_driver_image.h \
${SHAREDPATH}/include/vic_driver_shared.h \
${VICPATH}/include/vic_physical_constants.h
${VICPATH}/include/vic_physical_constants.h \
${VICPATH}/include/vic_log.h

OBJS = \
${DRIVERPATH}/src/alloc_atmos.o \
Expand Down Expand Up @@ -132,6 +133,7 @@ OBJS = \
${SHAREDPATH}/src/open_file.o \
${SHAREDPATH}/src/print_library_shared.o \
${SHAREDPATH}/src/soil_moisture_from_water_table.o \
${SHAREDPATH}/src/vic_log.o \
${VICPATH}/src/CalcAerodynamic.o \
${VICPATH}/src/CalcBlowingSnow.o \
${VICPATH}/src/IceEnergyBalance.o \
Expand Down
2 changes: 0 additions & 2 deletions drivers/image/include/vic_driver_image.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@
#ifndef VIC_DRIVER_IMAGE_H
#define VIC_DRIVER_IMAGE_H

#include <stdbool.h>
#include <netcdf.h>
#include <stdio.h>
#include <vic_driver_shared.h>

#define DRIVER "Image"
Expand Down
6 changes: 6 additions & 0 deletions drivers/image/src/get_global_param.c
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,12 @@ get_global_param(FILE *gp)
options.RC_MODE = RC_JARVIS;
}
}
/*************************************
Define log directory
*************************************/
else if (strcasecmp("LOG_DIR", optstr) == 0) {
sscanf(cmdstr, "%*s %s", filenames.log_path_pfx);
}
/*************************************
Define state files
*************************************/
Expand Down
23 changes: 22 additions & 1 deletion drivers/image/src/vic_start.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ vic_start(void)
extern filep_struct filep;
extern domain_struct global_domain;
extern option_struct options;
extern FILE *LOG_DEST;

char *logfilename;

// Initialize global structures
initialize_options();
Expand All @@ -49,12 +52,30 @@ vic_start(void)
filep.globalparam = open_file(filenames.global, "r");
get_global_param(filep.globalparam);

// Initialize Log Destination
if (strcmp(filenames.log_path, "MISSING") != 0) {
// Create logfile name
logfilename = get_logname(filenames.log_path);

// Open Logfile
filep.logfile = open_file(logfilename, "w");

LOG_DEST = filep.logfile;

log_info("Initialized Log File: %s", logfilename);
}
else {
// Set global log destination
LOG_DEST = stderr;

log_info("Logging to stderr");
}

// set model constants
if (!strcasecmp(filenames.constants, "MISSING")) {
filep.constants = open_file(filenames.constants, "r");
get_parameters(filep.constants);
}
;

// read domain info
get_global_domain(filenames.domain, &global_domain);
Expand Down
3 changes: 2 additions & 1 deletion drivers/shared/include/vic_driver_shared.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,13 @@ void compute_treeline(atmos_data_struct *, dmy_struct *, double, double *,
bool *);
void cmd_proc(int argc, char **argv, char *globalfilename);
void compress_files(char string[]);
char* get_current_datetime(void);
void display_current_settings(int);
void free_all_vars(all_vars_struct *all_vars, int Nveg);
void free_dmy(dmy_struct **dmy);
void free_vegcon(veg_con_struct **veg_con);
double get_dist(double lat1, double long1, double lat2, double long2);
char* get_logname(const char *pathtofile);
void get_next_time_step(unsigned short *, unsigned short *, unsigned short *,
unsigned short *, unsigned short *, unsigned short);
void get_parameters(FILE *paramfile);
Expand Down Expand Up @@ -115,5 +117,4 @@ void print_veg_lib(veg_lib_struct *vlib, char carbon);
void print_veg_var(veg_var_struct *vvar, size_t ncanopy);
void usage(char *);
void soil_moisture_from_water_table(soil_con_struct *soil_con, size_t nlayers);

#endif
1 change: 1 addition & 0 deletions drivers/shared/src/initialize_filenames.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ initialize_filenames()
strcpy(filenames.snowband, "MISSING");
strcpy(filenames.lakeparam, "MISSING");
strcpy(filenames.result_dir, "MISSING");
strcpy(filenames.log_path, "MISSING");
for (i = 0; i < 2; i++) {
strcpy(filenames.f_path_pfx[i], "MISSING");
}
Expand Down
7 changes: 6 additions & 1 deletion drivers/shared/src/print_library_shared.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ print_dmy(dmy_struct *dmy)
}

/******************************************************************************
* @brief Print filenames structure.
* @brief Print energy balance structure.
*****************************************************************************/
void
print_energy_bal(energy_bal_struct *eb,
Expand Down Expand Up @@ -209,6 +209,9 @@ print_energy_bal(energy_bal_struct *eb,
printf("\tsnow_flux : %.4lf\n", eb->snow_flux);
}

/******************************************************************************
* @brief Print filenames structure.
*****************************************************************************/
void
print_filenames(filenames_struct *fnames)
{
Expand All @@ -228,6 +231,7 @@ print_filenames(filenames_struct *fnames)
printf("\tstatefile : %s\n", fnames->statefile);
printf("\tveg : %s\n", fnames->veg);
printf("\tveglib : %s\n", fnames->veglib);
printf("\tlog_path : %s\n", fnames->log_path);
}

/******************************************************************************
Expand All @@ -249,6 +253,7 @@ print_filep(filep_struct *fp)
printf("\tstatefile : %p\n", fp->statefile);
printf("\tveglib : %p\n", fp->veglib);
printf("\tvegparam : %p\n", fp->vegparam);
printf("\tlogfile : %p\n", fp->logfile);
}

/******************************************************************************
Expand Down
103 changes: 103 additions & 0 deletions drivers/shared/src/vic_log.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/******************************************************************************
* @section DESCRIPTION
*
* calculates nitrogen scaling factors for all canopy layers, following eqns
* 106 and 107 in Knorr 1997.
*
* Note: this should only be applied to veg types that have a canopy, e.g.
* trees and shrubs, but not grass or tundra vegetation.
*
* @section LICENSE
*
* The Variable Infiltration Capacity (VIC) macroscale hydrological model
* Copyright (C) 2014 The Land Surface Hydrology Group, Department of Civil
* and Environmental Engineering, University of Washington.
*
* The VIC model is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*****************************************************************************/

#include <time.h>

#include <vic_def.h>
#include <vic_run.h>
#include <vic_driver_shared.h>

/******************************************************************************
* @brief Get string of current date and time. Format YYMMDD-SSSSS.
*****************************************************************************/
char*
get_current_datetime()
{
const int size = 20;
char *cdt = (char*)malloc(sizeof(char)*size);
char ymd[8];
unsigned seconds_since_midnight;

if(cdt == NULL)
{
return NULL;
}

memset (cdt, 0, size);

time_t currDateTime;
currDateTime = time(NULL);

if(currDateTime == -1)
{
return NULL;
}

struct tm *timeinfo = localtime (&currDateTime);

seconds_since_midnight = (unsigned)currDateTime % 86400;

if(strftime(ymd, 7, "%y%m%d", timeinfo) == 0)
{
return NULL;
}

sprintf(cdt, "%s-%05d", ymd, seconds_since_midnight);

return cdt;
}

/******************************************************************************
* @brief Make logfile name string.
*****************************************************************************/
char*
get_logname(const char *path)
{
char *timestamp = get_current_datetime();
char *prefix = "vic.log.";
char *ext = ".txt";
int size = (strlen(path) + strlen(prefix) + strlen(ext) +
strlen(timestamp) + 1);
char *filename = (char*)malloc(sizeof(char)*size);

if(filename == NULL)
{
return NULL;
}

memset (filename, 0, size);
strcpy(filename, path);
strcpy(filename + strlen(path), prefix);
strcpy(filename + strlen(path) + strlen(prefix), timestamp);
strcpy(filename + strlen(path) + strlen(prefix) + strlen(timestamp),
ext);

return filename;
}
2 changes: 2 additions & 0 deletions samples/global.param.sample
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ ENDYEAR 2000 # year model simulation ends
ENDMONTH 12 # month model simulation ends
ENDDAY 31 # day model simulation ends

# LOG_DIR (put the log directory path here) # Log directory. Default log output to stderr

#######################################################################
# Simulation Parameters Namelist
#######################################################################
Expand Down
3 changes: 3 additions & 0 deletions vic_run/include/vic_def.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include <stdbool.h>

#include <vic_physical_constants.h>
#include <vic_log.h>

/***** Model Constants *****/
#define MAXSTRING 2048
Expand Down Expand Up @@ -416,6 +417,7 @@ typedef struct {
FILE *statefile; /**< output model state file */
FILE *veglib; /**< vegetation parameters for all vege types */
FILE *vegparam; /**< fractional coverage info for grid cell */
FILE *logfile; /**< log file */
} filep_struct;

/******************************************************************************
Expand All @@ -435,6 +437,7 @@ typedef struct {
char statefile[MAXSTRING]; /**< name of file in which to store model state */
char veg[MAXSTRING]; /**< vegetation grid coverage file */
char veglib[MAXSTRING]; /**< vegetation parameter library file */
char log_path[MAXSTRING]; /**< Location to write log file to*/
} filenames_struct;

/******************************************************************************
Expand Down
Loading