Пример #1
0
 def setup(self):
     """Setup function run before each test method to setup files needed
        to run the test effectively
     """
     self.format = True
     self.ref_file = 'ref_f_sami2py-1.00.namelist'
     self.model_path = generate_path(tag='test', lon=0, year=2012, day=211,
                                     test=True)
     if not os.path.exists(self.model_path):
         os.makedirs(self.model_path)
     self.filelist = ['glonf.dat', 'glatf.dat', 'zaltf.dat', 'denif.dat',
                      'dennf.dat', 'u4f.dat', 'vsif.dat', 'tif.dat',
                      'tef.dat', 'time.dat']
     for filename in self.filelist:
         open(os.path.join(fortran_dir, filename), 'w').close()
Пример #2
0
 def setup(self):
     """Setup function run before each test method to setup files needed
        to run the test effectively
     """
     self.format = False
     self.ref_file = 'ref_u_sami2py-1.00.namelist'
     self.model_path = generate_path('test', 0, 2012, 211, True)
     if not os.path.exists(self.model_path):
         os.makedirs(self.model_path)
     self.filelist = [
         'glonu.dat', 'glatu.dat', 'zaltu.dat', 'deniu.dat', 'vsiu.dat',
         'tiu.dat', 'teu.dat', 'time.dat'
     ]
     for filename in self.filelist:
         open(os.path.join(fortran_dir, filename), 'w').close()
Пример #3
0
    def _generate_metadata(self, namelist):
        """Reads the namelist and generates MetaData based on Parameters

        Parameters
        -----------
        namelist : (list)
            variable namelist from SAMI2 model

        Returns
        -------
        void
            Model object modified in place
        """

        import re

        def find_float(name, ind):
            """regular expression search for float vals"""
            return float(re.findall(r"\d*\.\d+|\d+", name)[ind])

        def find_int(name, ind):
            """regular expression search for int vals"""
            return int(re.findall(r"\d+", name)[ind])

        self.MetaData['fmtout'] = ('.true.' in namelist[1])

        self.MetaData['F10.7A'] = find_float(namelist[14], 0)
        self.MetaData['F10.7'] = find_float(namelist[15], 2)
        self.MetaData['ap'] = find_int(namelist[16], 0)

        self.MetaData['Neutral Atmosphere Model'] = 'NRLMSISe-2000'
        self.MetaData['EUV Model'] = 'EUVAC'

        # Ions Used
        nion1 = wind_model = find_int(namelist[20], 1) - 1
        nion2 = wind_model = find_int(namelist[21], 1) - 1
        ions = ['H+', 'O+', 'NO+', 'O2+', 'He+', 'N2+', 'N+']
        self.MetaData['Ions Used'] = ', '.join(ions[nion1:nion2])

        # Multipliers
        neutral_scalars = re.findall(r"\d*\.\d+|\d+", namelist[28])
        self.MetaData['H Multiplier'] = float(neutral_scalars[0])
        self.MetaData['O Multiplier'] = float(neutral_scalars[1])
        self.MetaData['NO Multiplier'] = float(neutral_scalars[2])
        self.MetaData['O2 Multiplier'] = float(neutral_scalars[3])
        self.MetaData['He Multiplier'] = float(neutral_scalars[4])
        self.MetaData['N2 Multiplier'] = float(neutral_scalars[5])
        self.MetaData['N Multiplier'] = float(neutral_scalars[6])
        self.MetaData['T_exo Multiplier'] = find_float(namelist[33], 0)
        self.MetaData['T_n Multiplier'] = find_float(namelist[29], 0)
        self.MetaData['EUV Multiplier'] = find_float(namelist[34], 0)
        self.MetaData['ExB Drift Multiplier'] = find_float(namelist[24], 1)
        self.MetaData['Wind Multiplier'] = find_float(namelist[23], 1)

        if '.true.' in namelist[10]:
            self.MetaData['ExB model'] = 'Fejer-Scherliess'
        else:
            model_path = generate_path(self.tag, self.lon0, self.year,
                                       self.day, self.test)
            self.MetaData['ExB model'] = 'Fourier Series'
            self.MetaData['Fourier Coeffs'] = np.loadtxt(path.join(model_path,
                                                                   'exb.inp'))

        wind_model = find_int(namelist[35], 0)
        self.MetaData['Wind Model'] = ('HWM-{:02d}').format(wind_model)

        # Model Geometry
        self.MetaData['rmin'] = find_float(namelist[11], 0)
        self.MetaData['rmax'] = find_float(namelist[12], 0)
        self.MetaData['gams'] = find_int(namelist[26], 0)
        self.MetaData['gamp'] = find_int(namelist[27], 0)
        self.MetaData['altmin'] = find_float(namelist[13], 0)

        # Model runtime
        self.MetaData['dthr'] = find_float(namelist[5], 0)
        self.MetaData['hrinit'] = find_float(namelist[22], 0)
        self.MetaData['hrpr'] = find_float(namelist[6], 0)
        self.MetaData['hrmax'] = find_float(namelist[3], 0)
        self.MetaData['dt0'] = find_float(namelist[4], 0)
        self.MetaData['maxstep'] = find_int(namelist[2], 0)
        self.MetaData['denmin'] = find_float(namelist[30], 0)
Пример #4
0
    def _load_model(self):
        """Loads model results

        Returns
        -------
        void
            Model object modified in place
        """

        nf = 98
        nz = 101
        ni = 7
        
        def return_fourier(x, coeffs):
            """
            Returns a Fourier series up to NumF coefficients
            """
            def cos_a(x, n):
                """simple cosine"""
                return np.cos(n * np.pi * x / 12.0)
        
            def sin_a(x, n):
                """simple sine"""
                return np.sin(n * np.pi * x / 12.0)
        
            NumF = int((len(coeffs) - 1)/2)
        
            y = coeffs[0]
            for i in range(1, NumF+1):
                y = y + coeffs[2*i-1]*cos_a(x, i) + coeffs[2*i]*sin_a(x, i)
        
            return y
        
        model_path = generate_path(self.tag, self.lon0, self.year, self.day,
                                   self.test)

        # Get NameList
        namelist_file = open(path.join(model_path, 'sami2py-1.00.namelist'))
        self.namelist = namelist_file.readlines()
        namelist_file.close()

        self.MetaData = dict()
        self._generate_metadata(self.namelist)

        # Get times
        time = np.loadtxt(path.join(model_path, 'time.dat'))
        self.ut = time[:, 1] + time[:, 2] / 60 + time[:, 3] / 3600

        self._calculate_slt()
        nt = len(self.ut)

        if self.MetaData['fmtout']:
            # Get Location
            glat = np.loadtxt(path.join(model_path, 'glatf.dat'))
            glon = np.loadtxt(path.join(model_path, 'glonf.dat'))
            zalt = np.loadtxt(path.join(model_path, 'zaltf.dat'))

            # Get plasma values
            deni = np.loadtxt(path.join(model_path, 'denif.dat'))
            vsi = np.loadtxt(path.join(model_path, 'vsif.dat'))
            ti = np.loadtxt(path.join(model_path, 'tif.dat'))
            te = np.loadtxt(path.join(model_path, 'tef.dat'))

            # get neutral values
            if self.outn:
                denn = np.loadtxt(path.join(model_path, 'dennf.dat'))
                u4 = np.loadtxt(path.join(model_path, 'u4f.dat'))
        else:
            # Get Location
            glat = get_unformatted_data(model_path, 'glat')
            glon = get_unformatted_data(model_path, 'glon')
            zalt = get_unformatted_data(model_path, 'zalt')

            # Get plasma values
            dim0 = nz * nf * ni + 2
            dim1 = nt
            deni = get_unformatted_data(model_path, 'deni',
                                        dim=(dim0, dim1), reshape=True)
            vsi = get_unformatted_data(model_path, 'vsi',
                                       dim=(dim0, dim1), reshape=True)
            ti = get_unformatted_data(model_path, 'ti',
                                      dim=(dim0, dim1), reshape=True)

            # Electron Temperatures have only one species
            dim0 = nz * nf + 2
            te = get_unformatted_data(model_path, 'te',
                                      dim=(dim0, dim1), reshape=True)
            if self.outn:
                # Multiple neutral species
                dim0 = nz * nf * ni + 2
                denn = get_unformatted_data(model_path, 'denn',
                                            dim=(dim0, dim1), reshape=True)
                # Only one wind
                dim0 = nz * nf + 2
                u4 = get_unformatted_data(model_path, 'u4',
                                          dim=(dim0, dim1), reshape=True)

        glat = np.reshape(glat, (nz, nf), order="F")
        glon = np.reshape(glon, (nz, nf), order="F")
        zalt = np.reshape(zalt, (nz, nf), order="F")
        deni = np.reshape(deni, (nz, nf, ni, nt), order="F")
        vsi = np.reshape(vsi, (nz, nf, ni, nt), order="F")
        ti = np.reshape(ti, (nz, nf, ni, nt), order="F")
        te = np.reshape(te, (nz, nf, nt), order="F")
        self.data = xr.Dataset({'deni': (['z', 'f', 'ion', 'ut'], deni),
                                'vsi': (['z', 'f', 'ion', 'ut'], vsi),
                                'ti': (['z', 'f', 'ion', 'ut'], ti),
                                'te': (['z', 'f', 'ut'], te),
                                'slt': (['ut'], self.slt)},
                               coords={'glat': (['z', 'f'], glat),
                                       'glon': (['z', 'f'], glon),
                                       'zalt': (['z', 'f'], zalt),
                                       'ut': self.ut})
        if self.outn:
            denn = np.reshape(denn, (nz, nf, ni, nt), order="F")
            self.data['denn'] = (('z', 'f', 'ion', 'ut'), denn)
            u4 = np.reshape(u4, (nz, nf, nt), order="F")
            self.data['u4'] = (('z', 'f', 'ut'), u4)
        
        # Add drifts
        if self.MetaData['ExB model'] == 'Fourier Series': 
            self.data['ExB'] = return_fourier(self.data['slt'],
                                              self.MetaData['Fourier Coeffs'])
Пример #5
0
    def _load_model(self):
        """Loads model results

        Returns
        -------
        void
            Model object modified in place
        """

        nf = 98
        nz = 101
        ni = 7

        model_path = generate_path(self.tag, self.lon0, self.year, self.day,
                                   self.test)

        # Get NameList
        namelist_file = open(path.join(model_path, 'sami2py-1.00.namelist'))
        self.namelist = namelist_file.readlines()
        namelist_file.close()

        self.MetaData = dict()
        self._generate_metadata(self.namelist)

        # Get times
        time = np.loadtxt(path.join(model_path, 'time.dat'))
        self.ut = time[:, 1] + time[:, 2] / 60 + time[:, 3] / 3600

        self._calculate_slt()
        nt = len(self.ut)

        if self.MetaData['fmtout']:
            # Get Location
            glat = np.loadtxt(path.join(model_path, 'glatf.dat'))
            glon = np.loadtxt(path.join(model_path, 'glonf.dat'))
            zalt = np.loadtxt(path.join(model_path, 'zaltf.dat'))

            # Get plasma values
            deni = np.loadtxt(path.join(model_path, 'denif.dat'))
            vsi = np.loadtxt(path.join(model_path, 'vsif.dat'))
            ti = np.loadtxt(path.join(model_path, 'tif.dat'))
            te = np.loadtxt(path.join(model_path, 'tef.dat'))

            # get neutral values
            if self.outn:
                denn = np.loadtxt(path.join(model_path, 'dennf.dat'))
                u4 = np.loadtxt(path.join(model_path, 'u4f.dat'))
        else:
            # Get Location
            glat = get_unformatted_data(model_path, 'glat')
            glon = get_unformatted_data(model_path, 'glon')
            zalt = get_unformatted_data(model_path, 'zalt')

            # Get plasma values
            dim0 = nz * nf * ni + 2
            dim1 = nt
            deni = get_unformatted_data(model_path,
                                        'deni',
                                        dim=(dim0, dim1),
                                        reshape=True)
            vsi = get_unformatted_data(model_path,
                                       'vsi',
                                       dim=(dim0, dim1),
                                       reshape=True)
            ti = get_unformatted_data(model_path,
                                      'ti',
                                      dim=(dim0, dim1),
                                      reshape=True)

            # Electron Temperatures have only one species
            dim0 = nz * nf + 2
            te = get_unformatted_data(model_path,
                                      'te',
                                      dim=(dim0, dim1),
                                      reshape=True)
            if self.outn:
                # Multiple neutral species
                dim0 = nz * nf * ni + 2
                denn = get_unformatted_data(model_path,
                                            'denn',
                                            dim=(dim0, dim1),
                                            reshape=True)
                # Only one wind
                dim0 = nz * nf + 2
                u4 = get_unformatted_data(model_path,
                                          'u4',
                                          dim=(dim0, dim1),
                                          reshape=True)

        glat = np.reshape(glat, (nz, nf), order="F")
        glon = np.reshape(glon, (nz, nf), order="F")
        zalt = np.reshape(zalt, (nz, nf), order="F")
        deni = np.reshape(deni, (nz, nf, ni, nt), order="F")
        vsi = np.reshape(vsi, (nz, nf, ni, nt), order="F")
        ti = np.reshape(ti, (nz, nf, ni, nt), order="F")
        te = np.reshape(te, (nz, nf, nt), order="F")
        self.data = xr.Dataset(
            {
                'deni': (['z', 'f', 'ion', 'ut'], deni),
                'vsi': (['z', 'f', 'ion', 'ut'], vsi),
                'ti': (['z', 'f', 'ion', 'ut'], ti),
                'te': (['z', 'f', 'ut'], te),
                'slt': (['ut'], self.slt)
            },
            coords={
                'glat': (['z', 'f'], glat),
                'glon': (['z', 'f'], glon),
                'zalt': (['z', 'f'], zalt),
                'ut': self.ut
            })
        if self.outn:
            denn = np.reshape(denn, (nz, nf, ni, nt), order="F")
            self.data['denn'] = (('z', 'f', 'ion', 'ut'), denn)
            u4 = np.reshape(u4, (nz, nf, nt), order="F")
            self.data['u4'] = (('z', 'f', 'ut'), u4)