def delete_old_watershed_geoserver_files(watershed):
    """
    Removes old watershed geoserver files from system
    """
    #initialize session
    session = mainSessionMaker()
    main_settings  = session.query(MainSettings).order_by(MainSettings.id).first()
    
    #initialize geoserver manager
    geoserver_manager = GeoServerDatasetManager(engine_url=watershed.geoserver.url,
                                                username=watershed.geoserver.username,
                                                password=watershed.geoserver.password,
                                                app_instance_id=main_settings.app_instance_id)

    session.close()
    
    #delete layers which need to be deleted
    if watershed.geoserver_drainage_line_layer:
        if watershed.geoserver_drainage_line_layer.uploaded:
            geoserver_manager.purge_remove_geoserver_layer(watershed.geoserver_drainage_line_layer.name)
                                     
    if watershed.geoserver_boundary_layer:
        if watershed.geoserver_boundary_layer.uploaded:
            geoserver_manager.purge_remove_geoserver_layer(watershed.geoserver_boundary_layer.name)
                                     
    if watershed.geoserver_gage_layer:
        if watershed.geoserver_gage_layer.uploaded:
            geoserver_manager.purge_remove_geoserver_layer(watershed.geoserver_gage_layer.name)

    if watershed.geoserver_ahps_station_layer:
        if watershed.geoserver_ahps_station_layer.uploaded:
            geoserver_manager.purge_remove_geoserver_layer(watershed.geoserver_ahps_station_layer.name)
def delete_old_watershed_geoserver_files(watershed):
    """
    Removes old watershed geoserver files from system
    """
    #initialize session
    session = mainSessionMaker()
    main_settings  = session.query(MainSettings).order_by(MainSettings.id).first()
    
    #initialize geoserver manager
    geoserver_manager = GeoServerDatasetManager(engine_url=watershed.geoserver.url,
                                                username=watershed.geoserver.username,
                                                password=watershed.geoserver.password,
                                                app_instance_id=main_settings.app_instance_id)

    session.close()
    
    #delete layers which need to be deleted
    if watershed.geoserver_drainage_line_layer:
        if watershed.geoserver_drainage_line_layer.uploaded:
            geoserver_manager.purge_remove_geoserver_layer(watershed.geoserver_drainage_line_layer.name)
                                     
    if watershed.geoserver_boundary_layer:
        if watershed.geoserver_boundary_layer.uploaded:
            geoserver_manager.purge_remove_geoserver_layer(watershed.geoserver_boundary_layer.name)
                                     
    if watershed.geoserver_gage_layer:
        if watershed.geoserver_gage_layer.uploaded:
            geoserver_manager.purge_remove_geoserver_layer(watershed.geoserver_gage_layer.name)

    if watershed.geoserver_ahps_station_layer:
        if watershed.geoserver_ahps_station_layer.uploaded:
            geoserver_manager.purge_remove_geoserver_layer(watershed.geoserver_ahps_station_layer.name)
def delete_old_watershed_prediction_files(watershed, forecast="all"):
    """
    Removes old watershed prediction files from system if no other watershed has them
    """
    def delete_prediciton_files(main_folder_name, sub_folder_name, local_prediction_files_location):
        """
        Removes predicitons from folder and folder if not empty
        """
        prediciton_folder = os.path.join(local_prediction_files_location, 
                                         main_folder_name,
                                         sub_folder_name)
        #remove watersheds subbsasins folders/files
        if main_folder_name and sub_folder_name and \
        local_prediction_files_location and os.path.exists(prediciton_folder):
            
            #remove all prediction files from watershed/subbasin
            try:
                rmtree(prediciton_folder)
            except OSError:
                pass
            
            #remove watershed folder if no other subbasins exist
            try:
                os.rmdir(os.path.join(local_prediction_files_location, 
                                      main_folder_name))
            except OSError:
                pass
        
    #initialize session
    session = mainSessionMaker()
    main_settings  = session.query(MainSettings).order_by(MainSettings.id).first()
    forecast = forecast.lower()
    
    #Remove ECMWF Forecasta
    if forecast == "all" or forecast == "ecmwf":
        #Make sure that you don't delete if another watershed is using the
        #same predictions
        num_ecmwf_watersheds_with_forecast  = session.query(Watershed) \
            .filter(
                and_(
                    Watershed.ecmwf_data_store_watershed_name == watershed.ecmwf_data_store_watershed_name, 
                    Watershed.ecmwf_data_store_subbasin_name == watershed.ecmwf_data_store_subbasin_name
                )
            ) \
            .filter(Watershed.id != watershed.id) \
            .count()
        if num_ecmwf_watersheds_with_forecast <= 0:
            delete_prediciton_files(watershed.ecmwf_data_store_watershed_name, 
                                    watershed.ecmwf_data_store_subbasin_name, 
                                    main_settings.ecmwf_rapid_prediction_directory)
    
    #Remove WRF-Hydro Forecasts
    if forecast == "all" or forecast == "wrf_hydro":
        #Make sure that you don't delete if another watershed is using the
        #same predictions
        num_wrf_hydro_watersheds_with_forecast  = session.query(Watershed) \
            .filter(
                and_(
                    Watershed.wrf_hydro_data_store_watershed_name == watershed.wrf_hydro_data_store_watershed_name, 
                    Watershed.wrf_hydro_data_store_subbasin_name == watershed.wrf_hydro_data_store_subbasin_name
                )
            ) \
            .filter(Watershed.id != watershed.id) \
            .count()
        if num_wrf_hydro_watersheds_with_forecast <= 0:
            delete_prediciton_files(watershed.wrf_hydro_data_store_watershed_name, 
                                    watershed.wrf_hydro_data_store_subbasin_name, 
                                    main_settings.wrf_hydro_rapid_prediction_directory)
    
    session.close()
def delete_old_watershed_prediction_files(watershed, forecast="all"):
    """
    Removes old watershed prediction files from system if no other watershed has them
    """
    def delete_prediciton_files(watershed_folder_name, local_prediction_files_location):
        """
        Removes predicitons from folder and folder if not empty
        """
        prediciton_folder = os.path.join(local_prediction_files_location, 
                                         watershed_folder_name)
        #remove watersheds subbsasins folders/files
        if watershed_folder_name and \
        local_prediction_files_location and os.path.exists(prediciton_folder):
            
            #remove all prediction files from watershed/subbasin
            try:
                rmtree(prediciton_folder)
            except OSError:
                pass
            
            #remove watershed folder if no other subbasins exist
            try:
                os.rmdir(os.path.join(local_prediction_files_location, 
                                      watershed_folder_name))
            except OSError:
                pass
        
    #initialize session
    session = mainSessionMaker()
    main_settings  = session.query(MainSettings).order_by(MainSettings.id).first()
    forecast = forecast.lower()
    
    #Remove ECMWF Forecasta
    if forecast == "all" or forecast == "ecmwf":
        #Make sure that you don't delete if another watershed is using the
        #same predictions
        num_ecmwf_watersheds_with_forecast  = session.query(Watershed) \
            .filter(
                and_(
                    Watershed.ecmwf_data_store_watershed_name == watershed.ecmwf_data_store_watershed_name, 
                    Watershed.ecmwf_data_store_subbasin_name == watershed.ecmwf_data_store_subbasin_name
                )
            ) \
            .filter(Watershed.id != watershed.id) \
            .count()
        if num_ecmwf_watersheds_with_forecast <= 0:
            delete_prediciton_files("{0}-{1}".format(watershed.ecmwf_data_store_watershed_name, 
                                                     watershed.ecmwf_data_store_subbasin_name), 
                                    main_settings.ecmwf_rapid_prediction_directory)
    
    #Remove WRF-Hydro Forecasts
    if forecast == "all" or forecast == "wrf_hydro":
        #Make sure that you don't delete if another watershed is using the
        #same predictions
        num_wrf_hydro_watersheds_with_forecast  = session.query(Watershed) \
            .filter(
                and_(
                    Watershed.wrf_hydro_data_store_watershed_name == watershed.wrf_hydro_data_store_watershed_name, 
                    Watershed.wrf_hydro_data_store_subbasin_name == watershed.wrf_hydro_data_store_subbasin_name
                )
            ) \
            .filter(Watershed.id != watershed.id) \
            .count()
        if num_wrf_hydro_watersheds_with_forecast <= 0:
            delete_prediciton_files("{0}-{1}".format(watershed.wrf_hydro_data_store_watershed_name, 
                                                     watershed.wrf_hydro_data_store_subbasin_name), 
                                    main_settings.wrf_hydro_rapid_prediction_directory)
    
    session.close()