diff --git a/csst_dfs_commons/convert/date_utils.py b/csst_dfs_commons/convert/date_utils.py index 13872dfaf9cd655040c7049b344c7fb1fe439a30..03a1d716bc8bf0cd5fe2ca07ab7a0b380439f187 100644 --- a/csst_dfs_commons/convert/date_utils.py +++ b/csst_dfs_commons/convert/date_utils.py @@ -17,4 +17,7 @@ def format_time_ms(float_time): local_time = time.localtime(float_time) data_head = time.strftime("%Y-%m-%d %H:%M:%S", local_time) data_secs = (float_time - int(float_time)) * 1000 - return "%s.%03d" % (data_head, data_secs) \ No newline at end of file + return "%s.%03d" % (data_head, data_secs) + +def str_to_datetime(str_time): + return datetime.strptime(str_time.split('.')[0], '%Y-%m-%d %H:%M:%S') \ No newline at end of file diff --git a/csst_dfs_commons/models/facility.py b/csst_dfs_commons/models/facility.py index a22b20e39c2d774378b735ceb7fe76d9c268753e..b5bbf242fcc9b87ba3bddf033623a8fa5df50e50 100644 --- a/csst_dfs_commons/models/facility.py +++ b/csst_dfs_commons/models/facility.py @@ -2,6 +2,20 @@ import dataclasses from typing import List,Dict from .common import BaseModel, default_field +@dataclasses.dataclass +class UserRecord(BaseModel): + id: int = 0 + user_name: str="" + true_name: str="" + email: str="" + passwd: str="" + organization: str="" + create_time: str="" + status: int = 0 + last_login_ip: str="" + last_login_time: str="" + role: str="" + @dataclasses.dataclass class Observation(BaseModel): id: int = 0 diff --git a/csst_dfs_commons/models/level2.py b/csst_dfs_commons/models/level2.py new file mode 100644 index 0000000000000000000000000000000000000000..bc2c635543bad76f246aea3ea1bed964fe5d3f42 --- /dev/null +++ b/csst_dfs_commons/models/level2.py @@ -0,0 +1,40 @@ +import dataclasses +from .common import BaseModel + +@dataclasses.dataclass +class Level2Record(BaseModel): + id: int = 0 + level0_id: str = '' + level1_id: int = 0 + brick_id: int = 0 + module_id: str = '' + object_name : str="" + data_type: str="" + filename : str="" + file_path: str="" + qc2_status: int = 0 + qc2_time: str="" + prc_status: int = 0 + prc_time: str="" + create_time: str="" + update_time: str="" + pipeline_id: str="" + import_status: int = 0 + +@dataclasses.dataclass +class Level2TypeRecord(BaseModel): + data_type: str="" + module_id: str = '' + key_column : str="" + hdu_index: int=0 + demo_filename : str="" + demo_file_path: str="" + ra_column: str="" + dec_column: str="" + update_time: str="" + create_time: str="" + import_status: int = 0 + +def filter_table_name(data_type): + return '_'.join(data_type.split('-')) + diff --git a/csst_dfs_commons/utils/fits.py b/csst_dfs_commons/utils/fits.py index fe7344204bb8e794aed1cc33fae5812ab4988298..c64eb43fe7dd2c17eaa3b809b5cd35989ecea21e 100644 --- a/csst_dfs_commons/utils/fits.py +++ b/csst_dfs_commons/utils/fits.py @@ -6,6 +6,7 @@ from astropy.io import fits from astropy import wcs import numpy as np import healpy as hp +from csst_dfs_commons.models.constants import PI, DEFAULT_NSIDE def get_header_value(key: str, headers, default_value = None): try: @@ -74,3 +75,34 @@ def hdul_of_healpix_ids(hdulist, nside=256): healpixids = hp.query_polygon(nside, xyzpoly.T) return healpixids + +def heapix_sql_condition(ra, dec, radius, ra_col = 'ra', dec_col = 'dec', nside = DEFAULT_NSIDE): + arcDec = (PI / 180) * dec + whereSql = f"abs((180./{PI}) * ACOS(SIN({PI} * {dec_col}/180) * SIN({arcDec}) + COS({PI} * {dec_col}/180) * COS({arcDec}) * COS(({PI}/180) * ({ra_col} - {ra})))) < {radius}" + + heapix_ids = get_healpix_ids(ra, dec, radius, nside) + nside_column = "NS%dHIdx" % (nside,) + whereZoneSql = "%s in (%s)" % \ + (nside_column, ','.join([str(i) for i in heapix_ids])) + + return whereZoneSql, whereSql + +def level2_heapix_sql_condition(ra, dec, radius, ra_col = 'ra', dec_col = 'dec', nside = DEFAULT_NSIDE): + x2 = f"cos({dec_col})*cos({ra_col})" + y2 = f"cos({dec_col})*sin({ra_col})" + z2 = f"sin({dec_col})" + + x1 = f"cos({dec})*cos({ra})" + y1 = f"cos({dec})*sin({ra})" + z1 = f"sin({dec})" + + distance = f"pow(({x2}-{x1}),2)+pow(({y2}-{y1}),2)+pow(({z2}-{z1}),2)" + + heapix_ids = get_healpix_ids(ra, dec, radius, nside) + nside_column = "brick_id" + whereZoneSql = "%s in (%s)" % \ + (nside_column, ','.join([str(i) for i in heapix_ids])) + + whereSql = f"{distance} <= 4*pow({radius}/2, 2) and {whereZoneSql}" + + return whereSql \ No newline at end of file