Example #1
0
    def get_tasks_for_base_plots_in_files(cls, file_ids):
        task_list = []

        for fid in file_ids:
            datafile = DataFile.objects.get(pk=fid)

            #Wavewatch and SST/currents files use a separate Plot function.
            if datafile.file.name.startswith("OuterGrid"):
                #plotter = WaveWatchPlotter(datafile.file.name)
                for t in xrange(0, 85):
                    # The unchopped file's index starts at noon: index = 0 and progresses throgh 85 forecasts, one per hour,
                    # for the next 85 hours.
                    # Only plot every 4th index to match up with the SST forecast.
                    # WaveWatch has forecasts for every hour but at this time we don't need them all.
                    if t % 4 == 0:
                        task_list.append(cls.make_wave_watch_plot.subtask(args=(4, t, fid), immutable=True))
                        task_list.append(cls.make_wave_watch_plot.subtask(args=(6, t, fid), immutable=True))
            elif datafile.file.name.startswith("WIND"):
                plotter = WindPlotter(datafile.file.name)
                number_of_times = plotter.get_number_of_model_times()
                for t in xrange(number_of_times):
                    task_list.append(cls.make_plot.subtask(args=(5, t, fid), immutable=True))
            else:
                plotter = Plotter(datafile.file.name)
                number_of_times = plotter.get_number_of_model_times()

                for t in xrange(number_of_times):
                    #SST Now has values every 2 hours, but we only want every 4
                    #This only adds the task for every other time stamp
                    if t % 2 != 0:
                        #using EXTEND because we are adding multiple items: might also be able to use APPEND
                        task_list.extend(cls.make_plot.subtask(args=(od_id, t, fid), immutable=True) for od_id in [1, 3])
        return task_list
Example #2
0
    def make_plot(overlay_definition_id, time_index=0, file_id=None):
        zoom_levels_for_currents = [('2-7', 8),  ('8-12', 4)]
        zoom_levels_for_others = [(None, None)]
        zoom_levels_for_winds = [('1-10', 2), ('11-12', 1)]
        if file_id is None:
            datafile = DataFile.objects.latest('model_date')

        #If plotting winds grab the latest wind file
        #-------------------------------------------------------------------------
        elif overlay_definition_id == 5:
            datafile = DataFile.objects.filter(type='WIND').latest('model_date')
        else:
            datafile = DataFile.objects.get(pk=file_id)

        #Wind has its own plotter if plotting winds use WindPlotter
        #-------------------------------------------------------------------------
        if overlay_definition_id == 5:
            plotter = WindPlotter(datafile.file.name)
        else:
            plotter = Plotter(datafile.file.name)

        overlay_definition = OverlayDefinition.objects.get(pk=overlay_definition_id)

        if overlay_definition_id == 3:
            zoom_levels = zoom_levels_for_currents
        elif overlay_definition_id == 5:
            zoom_levels = zoom_levels_for_winds
        else:
            zoom_levels = zoom_levels_for_others

        tile_dir = "tiles_{0}_{1}".format(overlay_definition.function_name, uuid4())
        overlay_ids = []
        for zoom_level in zoom_levels:
            # Make a plot with downsampling of 4, and with 2
            plot_filename, key_filename = plotter.make_plot(getattr(plot_functions, overlay_definition.function_name),
                                                            time_index=time_index, downsample_ratio=zoom_level[1], zoom_levels=zoom_level[0])

            overlay = Overlay(
                file=os.path.join(settings.UNCHOPPED_STORAGE_DIR, plot_filename),
                key=os.path.join(settings.KEY_STORAGE_DIR, key_filename),
                created_datetime=timezone.now(),
                definition_id=overlay_definition_id,
                applies_at_datetime=plotter.get_time_at_oceantime_index(time_index),
                zoom_levels=zoom_level[0],
                tile_dir=tile_dir,
                is_tiled=False
            )
            overlay.save()
            overlay_ids.append(overlay.id)
        return overlay_ids
Example #3
0
    def get_tasks_for_base_plots_in_files(cls, file_ids):
        """ Using a list of the file_ids this function creates a list of tasks to be used by celery. Celery takes
        these tasks and will generate plots automagically!

        :param file_ids: The list of file_ids that plot tasks need to be generated for!
        :return: The task list of plots that need to be done for the future.
        """
        task_list = []

        for fid in file_ids:
            datafile = DataFile.objects.get(pk=fid)
            # OSU WW3
            if datafile.file.name.startswith(settings.OSU_WW3_DF_FN):
                # The unchopped file's index starts at noon: index = 0 and progresses throgh 85 forecasts, one per hour,
                # for the next 85 hours.
                # Only plot every 4th index to match up with the SST forecast.
                # WaveWatch has forecasts for every hour but at this time we don't need them all.
                for t in xrange(0, 85):
                    if t % 4 == 0:
                        task_list.append(cls.make_wave_watch_plot.subtask(args=(settings.OSU_WW3_HI, t, fid), immutable=True))
                        task_list.append(cls.make_wave_watch_plot.subtask(args=(settings.OSU_WW3_DIR, t, fid), immutable=True))

            # Wind
            elif datafile.file.name.startswith(settings.NAMS_WIND_DF_FN):
                plotter = WindPlotter(datafile.file.name)
                number_of_times = plotter.get_number_of_model_times()
                wind_values = plotter.get_wind_indices()
                begin = wind_values['begin']
                swap = wind_values['swap']
                three_hour_indices = wind_values['indices']
                for t in range(begin, swap, 4):
                    task_list.append(cls.make_plot.subtask(args=(settings.NAMS_WIND, t, fid), immutable=True))

                for t in range(swap, number_of_times, 1):
                    if t in three_hour_indices:
                        task_list.append(cls.make_plot.subtask(args=(settings.NAMS_WIND, t, fid), immutable=True))

            # NCEP WW3
            elif datafile.file.name.startswith(settings.NCEP_WW3_DF_FN):
                print "NCEP"
                plotter = NcepWW3Plotter(datafile.file.name)
                for t in xrange(plotter.get_number_of_model_times()):
                    task_list.append(cls.make_wave_watch_plot.subtask(args=(settings.NCEP_WW3_DIR, t, fid), immutable=True))
                    task_list.append(cls.make_wave_watch_plot.subtask(args=(settings.NCEP_WW3_HI, t, fid), immutable=True))

            # NAVY Hycom
            elif datafile.file.name.startswith(settings.NAVY_HYCOM_DF_FN):
                plotter = HycomPlotter(datafile.file.name)
                t = plotter.get_number_of_model_times()

                for t in range(plotter.get_number_of_model_times()):
                    # TODO: Need to update this to match what each file is saved for
                    task_list.append(cls.make_wave_watch_plot.subtask(args=(settings.NAVY_HYCOM_SST, t, fid), immutable=True))
                    task_list.append(cls.make_wave_watch_plot.subtask(args=(settings.NAVY_SUR_CUR, t, fid), immutable=True))

            # OSU_ROMS
            elif datafile.file.name.startswith(settings.OSU_ROMS_DF_FN):
                plotter = Plotter(datafile.file.name)
                number_of_times = plotter.get_number_of_model_times()

                for t in xrange(number_of_times):
                    #SST Now has values every 2 hours, but we only want every 4
                    #This only adds the task for every other time stamp
                    if t % 2 != 0:
                        #using EXTEND because we are adding multiple items: might also be able to use APPEND
                        task_list.extend(cls.make_plot.subtask(args=(od_id, t, fid),
                                                               immutable=True) for od_id in [settings.OSU_ROMS_SST,
                                                                                             settings.OSU_ROMS_SUR_SAL,
                                                                                             settings.OSU_ROMS_SUR_CUR,
                                                                                             settings.OSU_ROMS_BOT_SAL,
                                                                                             settings.OSU_ROMS_BOT_TEMP,
                                                                                             settings.OSU_ROMS_SSH])
            # OSU T-Cline
            elif datafile.file.name.startswith(settings.OSU_TCLINE_DF_FN):
                plotter = TClinePlotter(datafile.file.name)
                number_of_times = plotter.get_number_of_model_times()
                for t in range(0, number_of_times, 1):
                    task_list.append(cls.make_plot.subtask(args=(settings.OSU_TCLINE, t, fid), immutable=True))
            else:
                print "OverlayManager.get_task_for_base_plots_in_files: NOT A FORECAST I RECOGNIZE"

        return task_list
Example #4
0
    def make_plot(overlay__id, time_index=0, file_id=None):
        """ Creates an unchopped (un-tiled), plain png, plot for the specified file at the
        specified time_index and the specify overlay definition as well as a database entry for
        that unchopped file and a new tile directory for that particular unchopped png image.

        Here is a list of the current models that this function is able to plot:

         id |        Name             | Model Name | Type
         -- |-------------------------|------------|-----
         1  | Sea Surface Tempearture | OSU ROMS   | Contour
         2  | Surface Salinity        | OSU ROMS   | Contour
         3  | Sea Surface Currents    | OSU ROMS   | Vector
         5  | Sea Surface Winds       | NAMS       | Vector (Barbs)
         7  | Bottom Sea Tempearture  | OSU ROMS   | Contour
         8  | Bottom Salinity         | OSU ROMS   | Contour
         9  | Sea Surface Height      | OSU ROMS   | Contour
         12 | HYCOM Sea Surface Temp  | NCEP       | Contour
         13 | HYCOM Sea Surface Cur   | NCEP       | Vector


        :param overlay__id: The definition of the function to be plotted. See the table above.
        Ensure overlay_definition_id's corrospond to the correct file type.
        :param time_index: The desired time slice of the file given
        :param file_id: The file id according to the database
        :return: The id of the generated overlay
        """

        extend_bool = False
        overlay_definition = OverlayDefinition.objects.get(pk=overlay__id)

        if overlay__id in settings.OSU_ROMS:
            if file_id is None:
                datafile = DataFile.objects.filter(
                    type='NCDF').latest('model_date')
            else:
                datafile = DataFile.objects.get(pk=file_id)
            plotter = Plotter(datafile.file.name)
            zoom_levels = plotter.get_zoom_level(overlay__id)

        elif overlay__id in settings.HYCOM:
            print "RTOFS HYCOM"
            if file_id is None:
                datafile = DataFile.objects.filter(
                    type='HYCOM').latest('model_date')
            else:
                datafile = DataFile.objects.get(pk=file_id)
            plotter = HycomPlotter(datafile.file.name)
            zoom_levels = plotter.get_zoom_level(overlay__id)

        elif overlay__id == settings.NAMS_WIND:
            if file_id is None:
                datafile = DataFile.objects.filter(
                    type='WIND').latest('model_date')
            else:
                datafile = DataFile.objects.get(pk=file_id)
            plotter = WindPlotter(datafile.file.name)
            zoom_levels = plotter.get_zoom_level(overlay__id)

        elif overlay__id in settings.NAVY_HYCOM:
            print "NAVY HYCOM"
            if file_id is None:
                datafile = DataFile.objects.filter(
                    type='HYCOM').latest('model_date')
            else:
                datafile = DataFile.objects.get(pk=file_id)
            plotter = NavyPlotter(datafile.file.name)
            zoom_levels = plotter.get_zoom_level(overlay__id)
            extend_bool = True

        elif overlay__id == settings.OSU_TCLINE:
            if file_id is None:
                datafile = DataFile.objects.filter(
                    type='T-CLINE').latest('model_date')
            else:
                datafile = DataFile.objects.get(pk=file_id)
            plotter = TClinePlotter(datafile.file.name)
            zoom_levels = plotter.get_zoom_level(overlay__id)

        tile_dir = "tiles_{0}_{1}".format(overlay_definition.function_name,
                                          uuid4())
        overlay_ids = []

        for zoom_level in zoom_levels:
            plot_filename, key_filename = plotter.make_plot(
                getattr(plot_functions, overlay_definition.function_name),
                time_index=time_index,
                downsample_ratio=zoom_level[1],
                zoom_levels=zoom_level[0])
            ''' Here we are changing the overlay_id number of forecasted models to be that of the corresponding
                base foreacast overlay_id.
                
                That way when SharkEyesCore.views creates the list of overlays, it grabs the base forecast and
                appends the extended one to the end as if the extended forecasts were part of the base forecast. '''
            # Extended Forecasts
            if settings.EXTEND:
                if overlay__id == settings.NAVY_HYCOM_SST:
                    overlay__id = settings.OSU_ROMS_SST
                elif overlay__id == settings.NAVY_HYCOM_SUR_CUR:
                    overlay__id = settings.OSU_ROMS_SUR_CUR
            if not settings.EXTEND:  # Nice for testing the views of the tiled models
                extend_bool = False

            overlay = Overlay(
                file=os.path.join(settings.UNCHOPPED_STORAGE_DIR,
                                  plot_filename),
                key=os.path.join(settings.KEY_STORAGE_DIR, key_filename),
                created_datetime=timezone.now(),
                definition_id=overlay__id,
                applies_at_datetime=plotter.get_time_at_oceantime_index(
                    time_index),
                zoom_levels=zoom_level[0],
                tile_dir=tile_dir,
                is_tiled=False,
                is_extend=extend_bool)
            overlay.save()
            overlay_ids.append(overlay.id)
        return overlay_ids
Example #5
0
    def get_tasks_for_base_plots_in_files(cls, file_ids):
        """ Using a list of the file_ids this function creates a list of tasks to be used by celery. Celery takes
        these tasks and will generate plots automagically!

        :param file_ids: The list of file_ids that plot tasks need to be generated for!
        :return: The task list of plots that need to be done for the future.
        """
        task_list = []

        for fid in file_ids:
            datafile = DataFile.objects.get(pk=fid)
            # OSU WW3
            if datafile.file.name.startswith(settings.OSU_WW3_DF_FN):
                # The unchopped file's index starts at noon: index = 0 and progresses throgh 85 forecasts, one per hour,
                # for the next 85 hours.
                # Only plot every 4th index to match up with the SST forecast.
                # WaveWatch has forecasts for every hour but at this time we don't need them all.
                for t in xrange(0, 85):
                    if t % 4 == 0:
                        task_list.append(
                            cls.make_wave_watch_plot.subtask(
                                args=(settings.OSU_WW3_HI, t, fid),
                                immutable=True))
                        task_list.append(
                            cls.make_wave_watch_plot.subtask(
                                args=(settings.OSU_WW3_DIR, t, fid),
                                immutable=True))

            # Wind
            elif datafile.file.name.startswith(settings.NAMS_WIND_DF_FN):
                plotter = WindPlotter(datafile.file.name)
                number_of_times = plotter.get_number_of_model_times()
                wind_values = plotter.get_wind_indices()
                begin = wind_values['begin']
                swap = wind_values['swap']
                three_hour_indices = wind_values['indices']
                for t in range(begin, swap, 4):
                    task_list.append(
                        cls.make_plot.subtask(args=(settings.NAMS_WIND, t,
                                                    fid),
                                              immutable=True))

                for t in range(swap, number_of_times, 1):
                    if t in three_hour_indices:
                        task_list.append(
                            cls.make_plot.subtask(args=(settings.NAMS_WIND, t,
                                                        fid),
                                                  immutable=True))

            # NCEP WW3
            elif datafile.file.name.startswith(settings.NCEP_WW3_DF_FN):
                print "NCEP"
                plotter = NcepWW3Plotter(datafile.file.name)
                for t in xrange(plotter.get_number_of_model_times()):
                    task_list.append(
                        cls.make_wave_watch_plot.subtask(
                            args=(settings.NCEP_WW3_DIR, t, fid),
                            immutable=True))
                    task_list.append(
                        cls.make_wave_watch_plot.subtask(
                            args=(settings.NCEP_WW3_HI, t, fid),
                            immutable=True))

            # NAVY Hycom
            elif datafile.file.name.startswith(settings.NAVY_HYCOM_DF_FN):
                plotter = HycomPlotter(datafile.file.name)
                t = plotter.get_number_of_model_times()

                for t in range(plotter.get_number_of_model_times()):
                    # TODO: Need to update this to match what each file is saved for
                    task_list.append(
                        cls.make_wave_watch_plot.subtask(
                            args=(settings.NAVY_HYCOM_SST, t, fid),
                            immutable=True))
                    task_list.append(
                        cls.make_wave_watch_plot.subtask(
                            args=(settings.NAVY_SUR_CUR, t, fid),
                            immutable=True))

            # OSU_ROMS
            elif datafile.file.name.startswith(settings.OSU_ROMS_DF_FN):
                plotter = Plotter(datafile.file.name)
                number_of_times = plotter.get_number_of_model_times()

                for t in xrange(number_of_times):
                    #SST Now has values every 2 hours, but we only want every 4
                    #This only adds the task for every other time stamp
                    if t % 2 != 0:
                        #using EXTEND because we are adding multiple items: might also be able to use APPEND
                        task_list.extend(
                            cls.make_plot.subtask(args=(od_id, t, fid),
                                                  immutable=True)
                            for od_id in [
                                settings.OSU_ROMS_SST, settings.
                                OSU_ROMS_SUR_SAL, settings.OSU_ROMS_SUR_CUR,
                                settings.OSU_ROMS_BOT_SAL, settings.
                                OSU_ROMS_BOT_TEMP, settings.OSU_ROMS_SSH
                            ])
            # OSU T-Cline
            elif datafile.file.name.startswith(settings.OSU_TCLINE_DF_FN):
                plotter = TClinePlotter(datafile.file.name)
                number_of_times = plotter.get_number_of_model_times()
                for t in range(0, number_of_times, 1):
                    task_list.append(
                        cls.make_plot.subtask(args=(settings.OSU_TCLINE, t,
                                                    fid),
                                              immutable=True))
            else:
                print "OverlayManager.get_task_for_base_plots_in_files: NOT A FORECAST I RECOGNIZE"

        return task_list