Package: moc-set
moc-setA command-line tool to build, update and query apersistentset of
Multi-Order Coverages maps (MOCs), seethis IVOA standard.AboutQuery a set of S-MOCs pre-saved in a single large binary file.The file can be seen as a persistent cache preventing
from having to open/read/parse a possible large set of FITS files.Amoc-setcan be updated by adding or removing MOCs from it, without having
to re-build the full binary file (except from time to time using thepurgecommand).
It is built in such a way that read operations are allowed while updating (add, remove, change status, purge)
the binary file, but a write operation immediately fails (return status != 0) if another write operation
is already ongoing.Used byMOC set in used:in production inVizieRwhen querying a cone
on the> 16_000cataloguesin an ongoing project leading by@lmichelto look for XMM FOVs intersecting a user define region.Similar to theCDS MOC Server.TheCDS MOC Serveradditionally stores metadata information associated
to each MOC, allows queries with constraints on those metadata, and supports T-MOCs and ST-MOCs in addition to S-MOCs.So,moc-setoffers functionalities similar toa sub-part ofthe MOC Server functionalities
(except for T-MOCs and ST-MOCs, one could build a MOC Server like service based onmoc-set,
using an additional mechanism to store and filter metadata).The MOC Server is implemented in Java. All information used by the MOC Server are loaded
in memory at service startup. To speed up the starting time, the MOC Server also resort on a binary dump,
needing synchronisation mechanisms with the running server data when the list of MOCs is updated.moc-sethas primarily been developed for VizieR needs: lighter/simpler version of the MOC server, with
less information needed (VizieR already store catalogues metadata), system programming language with no runtime,
possibly less memory available, ...InstallFrom pypi for python usersMOCSet is available inpypi,
you can thus install themocsetexecutable usingpip:pipinstall-Umoc-set
mocset--helpDebian packageDownload the lastmocset_vxx_yyy.debcorresponding to your architecture
(x86_64_muslhas the most chances to fit your needs)
from thegithub release page.Install the.debby clicking on it or using the command line:sudodpkg-imocset_vxx_yyy.deb
sudoapt-getinstall-fThen you can use the tool:mocset
manmocsetYou can uninstall using, e.g.:sudodpkg-r$(dpkg-fmocset_vxx_yyy.debPackage)Pre-compile binaries for MacOS, Linux and WindowsDownload the lastmocset-vxx_yyy.tar.gzcorresponding to your architecture
from thegithub release page.
You probably want ot use:Linux:mocset-vxx-x86_64-unknown-linux-musl.tar.gzMacOS:mocset-vxx-x86_64-apple-darwin.tar.gzWindows:mocset-vxx-.zipWARNING: for linux, usemuslinstead ofgnu(high chances of incompatibility in the latter case)The tar contains a single executable binary file.tarxzvfmocset-vxx-yyy.tar.gz
./mocsetCompile from source codeInstall rust(and check that~/.cargo/bin/is in your path),
or update the Rust compiler with:rustupupdateClone themoc lib rustproject:gitclonehttps://github.com/cds-astro/cds-moc-rustInstall from usingcargo:cargoinstall--pathcrates/setorRUSTFLAGS='-C target-cpu=native' cargo install --path crates/setUsageExecmocset --helpto get the help message:> mocset --help
Command-line tool to build, update and query a persistent set of HEALPix Multi-Order Coverages maps (MOCs)

Usage: mocset <COMMAND>

Commands:
  make       Make a new mocset
  append     Append the given MOCs to an existing mocset
  chgstatus  Change the status flag of the given MOCs identifiers (valid, deprecated, removed)
  purge      Purge the mocset removing physically the MOCs flagged as 'removed'
  list       Provide the list of the MOCs in the mocset and the associated flags
  query      Query the mocset
  union      Union of all MOCs in the moc-set matching a given region
  extract    Extracts a MOC from the given moc-set
  help       Print this message or the help of the given subcommand(s)

Options:
  -h, --help     Print help
  -V, --version  Print versionMOC set creationSimply list in a csv file a set of identifier (integer in[0, 281_474_976_710_655]),
and MOC FITS file path. See e.g the filemoclist.txt:1,data/CDS_B_bax_bax.fits2,data/CDS_I_172_irs.fits3,data/CDS_I_274_ccdm.fits4,data/CDS_I_42_catalog.fits5,data/CDS_II_203A_faint.fits6,data/CDS_II_261_GOODS_S.fits7,data/CDS_II_339_summary.fits8,data/CDS_II_97_ans.fits9,data/CDS_III_206_orion.fits10,data/CDS_III_43_main.fits
...Then executemoc-setfrom themocsetcommand:>mocsetmake--n1283--moc-listmoclist.txt--delimiter,mocset.binor from the source code,installingthe Rust compiler (including Cargo):>cargorun--release--make--n1283--moc-listmoclist.txt--delimiter,mocset.binTo build the moc-set binary filemocset.bin.The total number of MOCs that can be stored in the file equals `n128 * 128 - 1 (so 383 in the example above, since n128=3).See the help message for more details:> mocset make --help
Make a new mocset

Usage: mocset make [OPTIONS] <FILE>

Arguments:
  <FILE>  Output file, storing the MOC set

Options:
  -l, --moc-list <FILE>        Input file containing the 'moc_id moc_path' list (default: read from stdin) 'moc_id' must be a positive integer smaller than 281_474_976_710_655 (can be stored on 6 bytes). Use a negative value to flag as deprecated
  -d, --delimiter <SEPARATOR>  Delimiter used to separate the moc identifier from the moc path [default: " "]
  -n, --n128 <N128>            n x 128 - 1 = number of MOCs that can be stored in this moc set [default: 1]
  -h, --help                   Print helpList the content of a MOC setSimply usemocset list:> mocset list mocset.bin
id,status,depth,n_ranges,byte_size
1,valid,11,1673,13384
2,valid,11,36022,288176
3,valid,11,55470,443760
4,valid,11,587,4696
5,valid,11,534,4272
6,valid,11,17,136
...status: can bevalid,deprecatedorremovedn_ranges: number of ranges in the MOCbyte_size: size of the MOC stored in the MOC set (in bytes)See the help message:> mocset list --help
Provide the list of the MOCs in the mocset and the associated flags

Usage: mocset list [OPTIONS] <FILE>

Arguments:
  <FILE>  The moc-set to be read

Options:
  -r, --ranges  Print byte ranges instead of byte_size
  -h, --help    Print helpQuerying a MOC setSee the help to get the different query modes:> mocset query --help
Query the mocset

Usage: mocset query [OPTIONS] <FILE> <COMMAND>

Commands:
  pos   Single position
  cone  A cone, i.e. a position with a small area around (approximated by a MOC)
  moc   The given MOC (you create a moc using moc-cli and pipe it into moc-set)
  help  Print this message or the help of the given subcommand(s)

Arguments:
  <FILE>  The moc-set to be read

Options:
  -d, --add-deprecated       Also selects MOCs flagged as deprecated
  -c, --print-coverage       Print in output the sky fraction (in '[0.0, 1.0]') covered by each selected MOC
  -p, --parallel <PARALLEL>  Switch on multi-threading, with 'parallel' threads (made for SSD, not HDD)
  -h, --help                 Print helpQuery for a single positionTo obtain the list of the MOCs containing a the position 90.0 +0.0 (in decimal degrees), use:>mocsetquerymoclist.binpos90.0+0.0
id161The result is the list of the identifier of the MOC covering the input position (only one MOC, with identifier 161 in our example).See the full help:> mocset query pos --help
Single position

Usage: mocset query <FILE> pos <LON_DEG> <LAT_DEG>

Arguments:
  <LON_DEG>  Longitude of the cone center (in degrees)
  <LAT_DEG>  Latitude of the cone center (in degrees)

Options:
  -h, --help  Print helpQuery for a coneTo obtain the list of the MOCs overlapping the cone of center 90.0 +0.0 and having a radius of 320 arcsec, use:>mocsetquerymocset.bincone90.0+0.0320.0--precision5id2335161--precisionis optional, its default value is 2:at aprecisionof 0,mocsettransforms the input cone in a MOC
containing 1 to 9 values.at aprecisionof 1,mocsettransforms the input cone in a MOC
containing 1 to9x4values.at aprecisionofn,mocsettransforms the input cone in a MOC
containing 1 to9x4^nvalues.See the help:> mocset query cone --help
A cone, i.e. a position with a small area around (approximated by a MOC)

Usage: mocset query <FILE> cone [OPTIONS] <LON_DEG> <LAT_DEG> <R_ARCSEC>

Arguments:
  <LON_DEG>   Longitude of the cone center (in degrees)
  <LAT_DEG>   Latitude of the cone center (in degrees)
  <R_ARCSEC>  Radius of the cone (in arcseconds)

Options:
  -p, --precision <PREC>  MOC precision; 0: depth 'd' at which the cone is overlapped by 1 to max 9 cells; 1: depth 'd' + 1; n: depth 'd' + n [default: 2]
  -i, --included          Returns MOCs containing the whole cone MOC (instead of overlapping only)
  -h, --help              Print helpQuery for a MOCTo obtain the list of the MOCs overlapping the MOC stored in a given FITs file, e.g the MOC of XMM4 DR9, use:> mocset query mocset.bin moc YOUR_PATH/CDS_IX_59_xmm4dr9s.fits
id
1
2
3
4
5
6
7
8
9
10
...You may also use a MOC generate on the fly usingmoc-cli> moc from polygon 5 "(0.0,0.0),(10.0,0.0),(0.0,10.0)" ascii | mocset query mocset.bin moc - --format ascii
id
1
2
3
4
7
8
13
26
27
35
36
60
63
...See the help> mocset query moc --help
The given MOC (you create a moc using moc-cli and pipe it into moc-set)

Usage: mocset query <FILE> moc [OPTIONS] <FILE>

Arguments:
  <FILE>  Path of the input MOC file (or stdin if equals "-")

Options:
  -f, --format <INPUT_FMT>  Format of the input MOC ('ascii', 'json' or 'fits') [default: guess from the file extension]
  -i, --included            Returns MOCs containing the whole given MOC (instead of overlapping)
  -h, --help                Print helpQuery from a STC-S regionYou can query from a STC-S region usingmoc-cli:echo"Intersection ICRS (Polygon 272.536719 -19.461249 272.542612 -19.476380 272.537389 -19.491509 272.540192 -19.499823272.535455 -19.505218 272.528024 -19.505216 272.523437 -19.500298 272.514082 -19.503376272.502271 -19.500966 272.488647 -19.490390  272.481932 -19.490913 272.476737 -19.486589272.487633 -19.455645 272.500386 -19.444996 272.503003 -19.437557 272.512303 -19.432436272.514132 -19.423973 272.522103 -19.421523 272.524511 -19.413250 272.541021 -19.400024272.566264 -19.397500 272.564202 -19.389111 272.569055 -19.383210 272.588186 -19.386539272.593376 -19.381832 272.596327 -19.370541 272.624911 -19.358915 272.629256 -19.347842272.642277 -19.341020 272.651322 -19.330424 272.653174 -19.325079 272.648903 -19.313708272.639616 -19.311098 272.638128 -19.303083 272.632705 -19.299839 272.627971 -19.289408272.628226 -19.276293 272.633750 -19.270590 272.615109 -19.241810 272.614704 -19.221196272.618224 -19.215572 272.630809 -19.209945 272.633540 -19.198681 272.640711 -19.195292272.643028 -19.186751 272.651477 -19.182729 272.649821 -19.174859 272.656782 -19.169272272.658933 -19.161883 272.678012 -19.159481 272.689173 -19.176982 272.689395 -19.183512272.678006 -19.204016 272.671112 -19.206598 272.664854 -19.203523 272.662760 -19.211156272.654435 -19.214434 272.652969 -19.222085 272.656724 -19.242136 272.650071 -19.265092272.652868 -19.274296 272.660871 -19.249462 272.670041 -19.247807 272.675533 -19.254935272.673291 -19.273917 272.668710 -19.279245 272.671460 -19.287043 272.667507 -19.293933272.669261 -19.300601 272.663969 -19.307130 272.672626 -19.308954 272.675225 -19.316490272.657188 -19.349105 272.657638 -19.367455 272.662447 -19.372035 272.662232 -19.378566272.652479 -19.386871 272.645819 -19.387933 272.642279 -19.398277 272.629282 -19.402739272.621487 -19.398197 272.611782 -19.405716 272.603367 -19.404667 272.586162 -19.422703272.561792 -19.420008 272.555815 -19.413012 272.546500 -19.415611 272.537427 -19.424213272.533081 -19.441402Not (Polygon 272.511081 -19.487278 272.515300 -19.486595 272.517029 -19.471442272.511714 -19.458837 272.506430 -19.459001 272.496401 -19.474322 272.504821 -19.484924)Not (Polygon 272.630446 -19.234210 272.637274 -19.248542 272.638942 -19.231476 272.630868 -19.226364))"|mocfromstcs14-ascii|mocsetquerymocset.binmoc---formatasciiGeneric parameterFor all queries, the option--add-deprecated(or-d) add the MOCs flagged as deprecated in
the output, e.g.:mocsetquery--add-deprecatedmoclist.binpos90.0+0.0--precision5Updating a MOC setYou can:chgstatuschange the status of a MOC in the MOC set to flag it asdeprecatedor back tovalid,
or asremoved(WARNING: once removed, a MOC cannot be flagged back asdeprecatedorvalid)appenda new MOC at the end of the MOC set, the ID of a removed MOC can be reusedpurgea MOC set to physically remove all MOCs flagged asremoved.
This process basically re-write the whole MOC set file.Example of usage ofchgstatus> mocset list mocset.bin
id,status,depth,n_ranges,byte_size
1,valid,11,1673,13384
2,valid,11,36022,288176
...
> mocset chgstatus mocset.bin 1 deprecated
> mocset list mocset.bin
id,status,depth,n_ranges,byte_size
1,deprecated,11,1673,13384
2,valid,11,36022,288176
...
> mocset chgstatus mocset.bin 1 valid
> mocset list mocset.bin
id,status,depth,n_ranges,byte_size
1,valid,11,1673,13384
2,valid,11,36022,288176
...
> mocset chgstatus mocset.bin 1 removed
> mocset list mocset.bin
id,status,depth,n_ranges,byte_size
1,removed,11,1673,13384
2,valid,11,36022,288176
...Example of usage ofappend:> mocset append mocset.bin 3000 MY_PATH/my_new_moc.fitsExample of usage ofpurge:> mocset purge mocset.binHelp messages:> mocset chgstatus --help
Change the status flag of the given MOCs identifiers (valid, deprecated, removed)

Usage: mocset chgstatus <FILE> <NEW_STATUS> [IDS]...

Arguments:
  <FILE>        The moc-set to be updated
  <NEW_STATUS>  New status flag to be set
  [IDS]...      Comma separated list of identifiers of the MOCs we want to modify the flag

Options:
  -h, --help  Print help> mocset append --help
Append the given MOCs to an existing mocset

Usage: mocset append <FILE> <ID> <MOC_PATH>

Arguments:
  <FILE>      The moc-set to be updated
  <ID>        Identifier of the MOC we want to append. 'moc_id' must be a positive integer smaller than 281_474_976_710_655 (can be stored on 6 bytes). Use a negative value to flag as deprecated
  <MOC_PATH>  Path of the MOC to be append

Options:
  -h, --help  Print help> mocset purge --help
Purge the mocset removing physically the MOCs flagged as 'removed'

Usage: mocset purge [OPTIONS] <FILE>

Arguments:
  <FILE>  The moc-set file to be purge

Options:
  -n, --n128 <N128>  n x 128 - 1 = number of MOCs that can be stored in this purged moc set, if larger than the current value
  -h, --help         Print helpImplementation informationPrincipleWe resort on mapped files and the system disk cache.
Although the firstmoc-setquery may still be quite long,
the performances of subsequent queries will depend on the ability
of the system to keep the file's data in the disk cache.RemarkIf used on a server with a lot of I/O, the data may not stay in the disk cache for long.
If we nevertheless want to always answer quickly, there is at least three options:write a daemon loading the full dataset in memorycreate a VM (or a docker?) containing only themoc-settoolmy favourite one: use shared memory, resorting e.g. on theshared_memory crateFile structureThe structure of the file is made of two main blocks:a fixed length header, made ofa fixed length metadataa fixed length cumulative byte indexa extensible data partMetadata partA fixed size (chosen at creation) buffer ofnu64elements.the firstu64contains an indication on the buffer sizeall otheru64values, one per MOC in themoc-set, are made of 3 components:au8flag taking 4 possible values (only 2 bits are used, 6 are remaining):ValidMOCDeprecatedMOCRemovedMOCFreeslotthe depth of the MOC, stored on `u8 (max depth = 64, thus 2 bits are remaining and could be used)the MOC identifier, stored on the 6 remaining bytes.We stop reading the metadata when we encounter the firstFreeflag.The file can storen - 1MOCs.Cumulative byte indexA fixed size (chosen at creation) buffer ofnu64elements.the firstu64contains the byte size of the header, i.e. the size of the file with no data
it also correspond to the starting byte of the first MOC that will be inserted in themoc-set;all other elements at indexicontains the size of the file if only storing the
$i+1$ first MOCs.To get the size (in bytes) of the MOC at indexi, we can simply perform the operation:cumulIndex[i + 1] - cumulIndex[i].Data partA simple concatenation of the MOCs ranges.According to the depth of each MOC, the ranges are either ranges ofu32or ranges ofu64.Updates and Reading while writingThe only MOC information that can be modified (re-written) is its status flag.But the status flag is coded on a single byte and a CPU read and write single bytes atomically.Although, several cores may see the byte in a different state, it is not critical:
a MOC flagged as 'deprecated' or 'deleted' is still physically stored in themoc-set,
and we can accept a small delay in changing its state.Read operation orderdo not need a read lockread the metadata part of the headerread the cumulative bytes index in the headerread the MOC(s)Write Operation orderask for a write lock (create a .lock file)append the new MOC(s) to the end of the fileappend the new entries to the cumulative bytes indexpossibly update the flag of existing MOCs and append new entries to the end of the headerThe header, i.e. both the metadata and the cumulative byte index, is of fixed size,
with free space reserved at file creation.If a problem occurs while writing, the update is simply going to be ignored
(except the part written in the meta): at the next write operation, if the meta
has not been written (and it is the last part to be written), the code assumes
that the cumulative index beyond the know meta elements is free, the same for the
data byte beyond the last known cumulative index byte.If a process is reading while another is writing, the newly written part will also
simply be ignored if the metadata part has not yet been updated/written.It works well because we only append new MOCs (we never delete information),
we only update metadata to flag a MOC as removed of deprecated, and we
respect the read/write order as described above.Mocset file before adding a new MOCAdd a new moc step 1/3add a new moc step 2/3add a new moc step 3/3Mocset file after having added a new MOCPurgeThe purge operation is the only one removing physically stored MOCs.It actually consists in building a newmoc-set, removing the MOCs flagged as removed,
in a temporary file, and then making a move replacing the previousmoc-setby the new one.Thepurgeoperation uses a write lock.RecallDrop linux disk cache, in root mode:sync; echo 3 > /proc/sys/vm/drop_cachesBuild for being compatible with a large range of systems, avoiding the GLIBC version error
(seethis post):cargo build --target x86_64-unknown-linux-musl