#include #include #include #include #include static char vcid[] = "$Id: vicNl.c,v 5.14.2.13 2009/09/28 21:46:58 vicadmin Exp $"; /** Main Program **/ int main(int argc, char *argv[]) /********************************************************************** vicNl.c Dag Lohmann January 1996 This program controls file I/O and variable initialization as well as being the primary driver for the model. For details about variables, input files and subroutines check: http://ce.washington.edu/~hydro/Lettenmaier/Models/VIC/VIC_home.html UNITS: unless otherwise marked: all water balance components are in mm all energy balance components are in mks depths, and lengths are in m modifications: 1997-98 Model was updated from simple 2 layer water balance to an extension of the full energy and water balance 3 layer model. KAC 02-27-01 added controls for lake model KAC 11-18-02 Updated storage of lake water for water balance calculations. LCB 03-12-03 Modifed to add AboveTreeLine to soil_con_struct so that the model can make use of the computed treeline. KAC 04-10-03 Modified to initialize storm parameters using the state file. KAC 04-10-03 Modified to start the model by skipping records until the state file date is found. This replaces the previous method of modifying the global file start date, which can change the interpolation of atmospheric forcing data. KAC 04-15-03 Modified to store wet and dry fractions when intializing water balance storage. This accounts for changes in model state initialization, which now stores wet and dry fractions rather than just averagedvalues. KAC 29-Oct-03 Modified the version display banner to print the version string defined in global.h. TJB 01-Nov-04 Updated arglist for make_dist_prcp(), as part of fix for QUICK_FLUX state file compatibility. TJB 02-Nov-04 Updated arglist for read_lakeparam(), as part of fix for lake fraction readjustment. TJB 2005-Apr-13 OUTPUT_FORCE option now calls close_files(). TJB 2006-Sep-23 Implemented flexible output configuration; uses the new out_data, out_data_files, and save_data structures. TJB 2006-Oct-16 Merged infiles and outfiles structs into filep_struct; This included merging builtnames into filenames. TJB 2006-Nov-07 Removed LAKE_MODEL option. TJB 2006-Nov-07 Changed statefile to init_state in call to check_state_file(). TJB 2007-Jan-15 Added PRT_HEADER option; added call to write_header(). TJB 2007-Apr-04 Added option to continue run after a cell fails. GCT/KAC 2007-Apr-21 Added calls to free_dmy(), free_out_data_files(), free_out_data(), and free_veglib(). Added closing of all parameter files. TJB 2007-Aug-21 Return ErrorFlag from initialize_model_state. JCA 2007-Sep-14 Excluded calls to free_veglib() and closing of parameter files other than the soil param file for the case when OUTPUT_FORCE=TRUE. TJB 2007-Nov-06 Moved computation of cell_area from read_lakeparam() to read_soilparam() and read_soilparam_arc(). TJB 2008-May-05 Added prcp fraction (mu) to initial water storage computation. This solves water balance errors for the case where DIST_PRCP is TRUE. TJB 2009-Jan-16 Added soil_con.avgJulyAirTemp to argument list of initialize_atmos(). TJB 2009-Jun-09 Modified to use extension of veg_lib structure to contain bare soil information. TJB 2009-Jul-07 Added soil_con.BandElev[] to read_snowband() arg list. TJB 2009-Jul-31 Replaced references to N+1st veg tile with references to index of lake/wetland tile. TJB 2009-Sep-28 Replaced initial water/energy storage computations and calls to calc_water_balance_error/calc_energy_balance_error with an initial call to put_data. Modified the call to read_snowband(). TJB **********************************************************************/ { extern veg_lib_struct *veg_lib; extern option_struct options; #if LINK_DEBUG extern debug_struct debug; #endif // LINK_DEBUG extern Error_struct Error; extern global_param_struct global_param; /** Variable Declarations **/ char NEWCELL; char LASTREC; char MODEL_DONE; char *init_STILL_STORM; char ErrStr[MAXSTRING]; int rec, i, j; int veg; int dist; int band; int Ndist; int Nveg_type; int cellnum; int index; int *init_DRY_TIME; int RUN_MODEL; int Ncells; int cell_cnt; int startrec; int ErrorFlag; float mu; double storage; double veg_fract; double band_fract; double Clake; dmy_struct *dmy; atmos_data_struct *atmos; veg_con_struct *veg_con; soil_con_struct soil_con; dist_prcp_struct prcp; /* stores information about distributed precipitation */ filenames_struct filenames; filep_struct filep; lake_con_struct lake_con; out_data_file_struct *out_data_files; out_data_struct *out_data; save_data_struct save_data; /** Read Model Options **/ initialize_global(); filenames = cmd_proc(argc, argv); #if VERBOSE display_current_settings(DISP_VERSION,(filenames_struct*)NULL,(global_param_struct*)NULL); #endif /** Read Global Control File **/ filep.globalparam = open_file(filenames.global,"r"); global_param = get_global_param(&filenames, filep.globalparam); /** Set up output data structures **/ out_data = create_output_list(); out_data_files = set_output_defaults(out_data); fclose(filep.globalparam); filep.globalparam = open_file(filenames.global,"r"); parse_output_info(&filenames, filep.globalparam, &out_data_files, out_data); /** Check and Open Files **/ check_files(&filep, &filenames); #if !OUTPUT_FORCE /** Check and Open Debugging Files **/ #if LINK_DEBUG open_debug(); #endif /** Read Vegetation Library File **/ veg_lib = read_veglib(filep.veglib,&Nveg_type); #endif // !OUTPUT_FORCE /** Initialize Parameters **/ if(options.DIST_PRCP) Ndist = 2; else Ndist = 1; cellnum = -1; /** Make Date Data Structure **/ dmy = make_dmy(&global_param); /** allocate memory for the atmos_data_struct **/ alloc_atmos(global_param.nrecs, &atmos); /** Initial state **/ startrec = 0; #if !OUTPUT_FORCE if ( options.INIT_STATE ) filep.init_state = check_state_file(filenames.init_state, dmy, &global_param, options.Nlayer, options.Nnode, &startrec); /** open state file if model state is to be saved **/ if ( options.SAVE_STATE && strcmp( filenames.statefile, "NONE" ) != 0 ) filep.statefile = open_state_file(&global_param, filenames, options.Nlayer, options.Nnode); else filep.statefile = NULL; #endif // !OUTPUT_FORCE /************************************ Run Model for all Active Grid Cells ************************************/ MODEL_DONE = FALSE; cell_cnt=0; while(!MODEL_DONE) { if(!options.ARC_SOIL) { if((fscanf(filep.soilparam, "%d", &flag))!=EOF) { if(flag) RUN_MODEL=TRUE; else RUN_MODEL=FALSE; } else { MODEL_DONE = TRUE; RUN_MODEL = FALSE; } if(!MODEL_DONE) soil_con = read_soilparam(filep.soilparam, RUN_MODEL); } else { soil_con = read_soilparam_arc(filep.soilparam, filenames.soil_dir, &Ncells, &RUN_MODEL, cell_cnt); cell_cnt++; if(cell_cnt==Ncells) MODEL_DONE = TRUE; } if(RUN_MODEL) { #if LINK_DEBUG if(debug.PRT_SOIL) write_soilparam(&soil_con); #endif #if QUICK_FS /** Allocate Unfrozen Water Content Table **/ if(options.FROZEN_SOIL) { for(i=0;i1) fclose(filep.snowband); if (options.LAKES) fclose(filep.lakeparam); #endif /* !OUTPUT_FORCE */ return EXIT_SUCCESS; } /* End Main Program */