예제 #1
0
    def read_variable(self, geo, validtime, cache, geo_in=None):

        self.validtime = validtime
        if self.open_new_file(int(self.var_dict["fcint"]),
                              int(self.var_dict["offset"]),
                              int(self.var_dict["file_inc"])):

            if cache is not None and cache.file_open(self.filename):
                self.file_handler = cache.get_file_handler(self.filename)
            else:
                var_dict = self.var_dict
                var_dict = {"set": var_dict}
                self.file_handler = surfex.get_datasources(
                    validtime, var_dict)[0]

                if cache is not None:
                    cache.set_file_handler(self.filename, self.file_handler)

        if self.file_handler is None:
            warning("No file handler exist for this time step")
            field = np.array([geo.nlons])
            field = field.fill(np.nan)
        else:
            if "varname" in self.var_dict:
                varname = self.var_dict["varname"]
            else:
                varname = None

            accumulated = False
            if "accumulated" in self.var_dict:
                accumulated = self.var_dict["accumulated"]

            # Re-read field
            previous_field = None
            if accumulated:
                print(self.basetime, self.initialtime, self.previoustime)
                can_read = True
                if self.basetime <= self.initialtime:
                    if self.previoustime < self.basetime:
                        can_read = False

                if not can_read:
                    surfex.warning(
                        "Can not read previous time step for this time. Setting it to 0."
                    )
                    print(self.basetime, self.initialtime, self.previoustime)
                    previous_field = np.zeros([geo.npoints])
                else:
                    id_str = cache.generate_obs_id(varname,
                                                   self.previousfilename,
                                                   self.previoustime)
                    if cache.is_saved(id_str):
                        previous_field = cache.saved_fields[id_str]
                    else:
                        fname = self.filename
                        if self.debug:
                            print("Re-read ", self.previoustime, " from ",
                                  self.previousfilename)
                        self.file_handler.fname = self.previousfilename

                        times, previous_field, stids = self.file_handler.points(
                            geo)

                        # Change filename back in handler. Ready to read this time step
                        self.file_handler.fname = fname
                        cache.save_field(id_str, previous_field)

            # Read field
            id_str = None
            if cache is not None:
                id_str = cache.generate_obs_id(varname, self.filename,
                                               self.validtime)
            if cache is not None and cache.is_saved(id_str):
                field = cache.saved_fields[id_str]
            else:
                times, field, stids = self.file_handler.points(geo)

            # Deaccumulate
            if accumulated:
                instant = [(validtime - self.previoustime).total_seconds()]
                if "instant" in self.var_dict:
                    instant = [self.var_dict["instant"]]
                field = self.deaccumulate(field, previous_field,
                                          float(instant[0]))

        self.previoustime = validtime
        return field
예제 #2
0
    def read_variable(self, geo, validtime, cache, geo_in=None):

        self.validtime = validtime
        if self.open_new_file(int(self.var_dict["fcint"]),
                              int(self.var_dict["offset"]),
                              int(self.var_dict["file_inc"])):

            # print "Updating filehandler for "+self.print_variable_info()
            if cache is not None and cache.file_open(self.filename):
                self.file_handler = cache.get_file_handler(self.filename)
            else:
                fileformat = None
                if "fileformat" in self.var_dict:
                    fileformat = self.var_dict["fileformat"]
                filetype = None
                if "filetype" in self.var_dict:
                    filetype = self.var_dict["filetype"]
                self.file_handler = surfex.file.get_surfex_io_object(
                    self.filename,
                    fileformat=fileformat,
                    filetype=filetype,
                    geo=geo_in)
                if cache is not None:
                    cache.set_file_handler(self.filename, self.file_handler)

        if self.file_handler is None:
            warning("No file handler exist for this time step")
            field = np.array([len(geo.lons)])
            field = field.fill(np.nan)
        else:
            varname = self.var_dict["varname"]
            layers = None
            if "layers" in self.var_dict:
                layers = self.var_dict["layers"]
            patches = None
            if "patches" in self.var_dict:
                patches = self.var_dict["patches"]
            accumulated = False
            if "accumulated" in self.var_dict:
                accumulated = self.var_dict["accumulated"]
            interval = None
            if "interval" in self.var_dict:
                interval = self.var_dict["interval"]
            datatype = None
            if "datatype" in self.var_dict:
                datatype = self.var_dict["datatype"]

            var = surfex.file.SurfexFileVariable(varname,
                                                 validtime=validtime,
                                                 patches=patches,
                                                 layers=layers,
                                                 basetime=self.basetime,
                                                 interval=interval,
                                                 datatype=datatype)
            int_type = "nearest"
            if "interpolator" in self.var_dict:
                int_type = self.var_dict["interpolator"]

            # Re-read field
            previous_field = None
            if accumulated:
                print(self.basetime, self.initialtime, self.previoustime)
                can_read = True
                if self.basetime <= self.initialtime:
                    if self.previoustime < self.basetime:
                        can_read = False

                if not can_read:
                    surfex.warning(
                        "Can not read previous time step for this time. Setting it to 0."
                    )
                    print(self.basetime, self.initialtime, self.previoustime)
                    previous_field = np.zeros([geo.npoints])
                else:
                    id_str = cache.generate_surfex_id(varname, patches, layers,
                                                      self.previousfilename,
                                                      self.previoustime)
                    if cache.is_saved(id_str):
                        previous_field = cache.saved_fields[id_str]
                    else:
                        fname = self.filename
                        if self.debug:
                            print("Re-read ", self.previoustime, " from ",
                                  self.previousfilename)
                        self.file_handler.fname = self.previousfilename
                        previous_field, intp = self.file_handler.points(
                            var,
                            geo,
                            validime=self.previoustime,
                            interpolation=int_type,
                            cache=cache)

                        # Change filename back in handler. Ready to read this time step
                        self.file_handler.fname = fname
                        cache.save_field(id_str, previous_field)

            # Read field
            id_str = None
            if cache is not None:
                id_str = cache.generate_surfex_id(varname, patches, layers,
                                                  self.filename,
                                                  self.validtime)
            if cache is not None and cache.is_saved(id_str):
                field = cache.saved_fields[id_str]
            else:
                print(validtime)
                field, interpolator = self.file_handler.points(
                    var,
                    geo,
                    validtime=validtime,
                    interpolation=int_type,
                    cache=cache)
                # Rotate wind to geographic if requested
                field = self.rotate_geographic_wind(field, interpolator)
                if cache is not None:
                    cache.save_field(id_str, field)

            # Deaccumulate
            if accumulated:
                instant = [(validtime - self.previoustime).total_seconds()]
                if "instant" in self.var_dict:
                    instant = [self.var_dict["instant"]]
                field = self.deaccumulate(field, previous_field,
                                          float(instant[0]))

        self.previoustime = validtime
        return field
예제 #3
0
    def read_variable(self, geo, validtime, cache, geo_in=None):

        self.validtime = validtime
        if self.open_new_file(int(self.var_dict["fcint"]),
                              int(self.var_dict["offset"]),
                              int(self.var_dict["file_inc"])):
            # print "Updating filehandler for "+self.print_variable_info()
            if cache.file_open(self.filename):
                self.file_handler = cache.get_file_handler(self.filename)
            else:
                self.file_handler = surfex.fa.Fa(self.filename)
                cache.set_file_handler(self.filename, self.file_handler)

        if self.file_handler is None:
            surfex.util.warning("No file handler exist for this time step")
            field = np.array([len(geo.lons)])
            field = field.fill(np.nan)
        else:
            var_name = self.var_dict["name"]
            accumulated = False
            if "accumulated" in self.var_dict:
                accumulated = self.var_dict["accumulated"]
            int_type = "nearest"
            if "interpolator" in self.var_dict:
                int_type = self.var_dict["interpolator"]

            # Re-read field
            previous_field = None
            if accumulated:
                print(self.basetime, self.initialtime, self.previoustime)
                can_read = True
                if self.basetime <= self.initialtime:
                    if self.previoustime < self.basetime:
                        can_read = False

                if not can_read:
                    surfex.warning(
                        "Can not read previous time step for this time. Setting it to 0."
                    )
                    print(self.basetime, self.initialtime, self.previoustime)
                    previous_field = np.zeros([geo.npoints])
                else:
                    # Re-read field
                    id_str = cache.generate_netcdf_id(var_name,
                                                      self.previousfilename,
                                                      self.previoustime)
                    if cache.is_saved(id_str):
                        print("Updating cached value ", id_str)
                        previous_field = cache.saved_fields[id_str]
                    else:
                        # Modify filename in handler
                        fname = self.filename
                        if self.debug:
                            print("Re-read ", self.previoustime, " from ",
                                  self.previousfilename)
                        self.file_handler.fname = self.previousfilename
                        previous_field, intp = self.file_handler.points(
                            var_name,
                            geo,
                            validtime=self.previoustime,
                            interpolation=int_type,
                            cache=cache)
                        cache.save_field(id_str, previous_field)
                        # Change filename back in handler. Ready to read this time step
                        self.file_handler.fname = fname

            id_str = cache.generate_netcdf_id(var_name, self.filename,
                                              validtime)
            field, interpolator = self.file_handler.points(
                var_name,
                geo,
                validtime=validtime,
                interpolation=int_type,
                cache=cache)
            # Rotate wind to geographic if requested
            field = self.rotate_geographic_wind(field, interpolator)
            cache.save_field(id_str, field)

            if accumulated:
                print("accumulated variable ", self.var_dict)
                print("field", field)
                print("prevous", previous_field)
                print("deccumulated",
                      self.deaccumulate(field, previous_field, 0))
            if accumulated:
                instant = [(validtime - self.previoustime).total_seconds()]
                if "instant" in self.var_dict:
                    instant = [self.var_dict["instant"]]
                field = self.deaccumulate(field, previous_field,
                                          float(instant[0]))
                print("instant", field)

        self.previoustime = validtime
        return field
예제 #4
0
    def read_variable(self, geo, validtime, cache, geo_in=None):
        self.validtime = validtime
        if self.open_new_file(int(self.var_dict["fcint"]),
                              int(self.var_dict["offset"]),
                              int(self.var_dict["file_inc"])):

            # print "Updating filehandler for "+self.print_variable_info()
            if cache.file_open(self.filename):
                self.file_handler = cache.get_file_handler(self.filename)
            else:
                self.file_handler = surfex.grib.Grib(self.filename)
                cache.set_file_handler(self.filename, self.file_handler)

        if self.file_handler is None:
            warning("No file handler exist for this time step")
            field = np.array([len(geo.lons)])
            field = field.fill(np.nan)
        else:
            if self.grib_type == "grib1":
                par = self.var_dict["parameter"]
                typ = self.var_dict["type"]
                level = self.var_dict["level"]
                tri = self.var_dict["tri"]
                gribvar = surfex.grib.Grib1Variable(par, typ, level, tri)
            elif self.grib_type == "grib2":
                discipline = self.var_dict["discipline"]
                pc = self.var_dict["parameterCategory"]
                pn = self.var_dict["parameterNumber"]
                lt = self.var_dict["levelType"]
                lev = self.var_dict["level"]
                tsp = -1
                if "typeOfStatisticalProcessing" in self.var_dict:
                    tsp = self.var_dict["typeOfStatisticalProcessing"]
                print("Trygve ", discipline, pc, pn, lt, lev, tsp)
                gribvar = surfex.grib.Grib2Variable(discipline, pc, pn, lt,
                                                    lev, tsp)
            else:
                raise NotImplementedError

            int_type = "nearest"
            if "interpolator" in self.var_dict:
                int_type = self.var_dict["interpolator"]

            # Re-read field
            previous_field = None
            if gribvar.is_accumulated():
                print(self.basetime, self.initialtime, self.previoustime)
                can_read = True
                if self.basetime <= self.initialtime:
                    if self.previoustime < self.basetime:
                        can_read = False

                if not can_read:
                    surfex.warning(
                        "Can not read previous time step for this time. Setting it to 0."
                    )
                    print(self.basetime, self.initialtime, self.previoustime)
                    previous_field = np.zeros([geo.npoints])
                else:

                    id_str = cache.generate_grib_id(gribvar,
                                                    self.previousfilename,
                                                    self.previoustime)
                    if cache.is_saved(id_str):
                        previous_field = cache.saved_fields[id_str]
                    else:
                        # Modify filename in handler
                        fname = self.filename
                        if self.debug:
                            print("Re-read ", self.previoustime, " from ",
                                  self.previousfilename)
                        self.file_handler.fname = self.previousfilename
                        previous_field, intp = self.file_handler.points(
                            gribvar,
                            geo,
                            self.previoustime,
                            interpolation=int_type,
                            cache=cache)

                        # Change filename back in handler. Ready to read this time step
                        self.file_handler.fname = fname
                        cache.save_field(id_str, previous_field)

            # Read field
            id_str = cache.generate_grib_id(gribvar, self.filename,
                                            self.validtime)
            if cache.is_saved(id_str):
                field = cache.saved_fields[id_str]
            else:
                field, interpolator = self.file_handler.points(
                    gribvar,
                    geo,
                    validtime,
                    interpolation=int_type,
                    cache=cache)
                # Rotate wind to geographic if requested
                field = self.rotate_geographic_wind(field, interpolator)
                cache.save_field(id_str, field)

            # Deaccumulate
            if gribvar.is_accumulated():
                instant = [(validtime - self.previoustime).total_seconds()]
                if "instant" in self.var_dict:
                    instant = [self.var_dict["instant"]]
                field = self.deaccumulate(field, previous_field,
                                          float(instant[0]))

        self.previoustime = validtime
        return field