import datetime
import os.path as path
import numpy as np
import numpy.ma as ma
import volcat

class RaikokeVolcat:
    """
    for easy retrieval of data for Raikoke eruption 
    """

    def __init__(self):     
        # location of data
        self.directory="/pub/ECMWF/JPSS/VOLCAT/Raikoke/Ash/"
        # naming of data
        self.basename = 'SCOPE_NWC_ASH-L2-ASH_PRODUCTS-HIMAWARI8_NOAA-RAIKOKE-'
        self.suffix =  '00-fv2.nc'
        self.strfmt = "%Y%m%d-%H%M"
        self.year = [2019]
        self.month = [6]
        # days available
        self.day = [22,23,24]
        # hours available
        self.hour = np.arange(0,24)
        # minutes available
        self.minute = np.arange(0,60,10)

    def create_name(self, date):
        midname= date.strftime(self.strfmt)
        fname =self.basename + midname + self.suffix
        return path.join(self.directory, fname)

    def get_dset(self, date):
        """
        date : datetime object
        returns DataArray generated by volcat.open_dataset
        """
        fname = self.create_name(date)
        dset = volcat.open_dataset(fname)
        return dset

    def generate_dsets(self, daylist = None, hlist=None, mlist=None):
        if not hlist: hlist = self.hour
        if not mlist: mlist = self.minute
        if not daylist: daylist = self.day
        for year in self.year:
            for month in self.month:
                for day in daylist:
                  for hr in hlist:
                    for mm in mlist:
                        date = datetime.datetime(year,month,day,hr,mm)
                        try:
                          dset = self.get_dset(date)
                        except:
                          print('FILE cannot be opened ', self.create_name(date))
                          continue
                        yield dset


