{ "cells": [ { "cell_type": "markdown", "id": "bedf0a03", "metadata": {}, "source": [ "# Writing a station-to-archive ``.dat`` file\n", "\n", "WRMC-style archives use **logical records** (LR0001, …) and minute blocks (**LR0100**, **LR4000**). In Python, `bsrn.archive` mirrors rBSRN: build objects, call **`get_bsrn_format()`**, join the strings.\n", "\n", "The first code cell is the **metadata** part. The second cell follows the rBSRN pattern for **LR0100** (`yearMonth`, then ``rep(1, n)``-style placeholders for selected columns). The full month string is large; we **print a short preview** only (no temp file)." ] }, { "cell_type": "markdown", "id": "705254a9", "metadata": {}, "source": [ "## 1. Metadata and instrument LRs\n", "\n", "Same pattern as rBSRN: `LR0001$new(...)`, `LR0002$new(...)`, …, then `get_bsrn_format()` and concatenate." ] }, { "cell_type": "code", "execution_count": 1, "id": "e2e3721e", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2,093 characters (metadata only)\n", "*C0001\n", " 82 7 2020 1\n", " 2 3 4 5 21 22 23 -1\n", "*U0002\n", " -1 -1 -1\n", "John Doe +1 800 123 4567 +1 800 123 4567 \n", "XXX john.doe@example.com \n", "123 Main Street, Anytown, USA 12345 \n", " -1 -1 -1\n", "Jane Doe +1 800 123 4567 +1 800 123 4567 \n", "XXX jane.doe@example.com \n", "123 Main Street, Anytown, USA 12345 \n", "*U0003\n", "Example commentary (tutorial placeholder). \n", "*U0004\n", " -1 -1 -1\n", " 11 1\n", "123 Main Street, Anytown, USA 12345 \n", "+1 800 123 4567 XXX \n", "XXX XXX \n", " 159.099 235.484 116 XXXXX\n", " -1 -1 -1\n", " 0 0 15 0 30 0 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1\n", "*U0007\n", " -1 -1 -1\n", "XXX \n", "XXX \n", "XXX \n", "XXX \n", "XXX \n", "N N N N N N\n", "*U0008\n", " -1 -1 -1 N\n", "Kipp & Zonen CMP 22 140114 XXX 82001\n", "XXX \n", " -1 2 -1.000 -1.000 -1.000 -1.000 -1.000 -1.000 -1 -1\n", "PMOD-WRC N. Mingard \n", "05/16/17 06/30/17 1 8.8000 0.0600\n", "XXX XXX -1 -1.0000 -1.0000\n", "XXX XXX -1 -1.0000 -1.0000\n", "uV/W.m2 \n", "XXX \n", "*U0009\n", " -1 -1 -1 2 82001 -1\n", "\n" ] } ], "source": [ "import bsrn.archive as archive\n", "\n", "# LR0001 … LR0008 (same idea as rBSRN LR0001$new(...), LR0002$new(...), …)\n", "lr0001 = archive.LR0001(stationNumber=82, month=7, year=2020, version=1)\n", "lr0002 = archive.LR0002(\n", " scientistName=\"John Doe\",\n", " scientistTel=\"+1 800 123 4567\",\n", " scientistFax=\"+1 800 123 4567\",\n", " scientistMail=\"john.doe@example.com\",\n", " scientistAddress=\"123 Main Street, Anytown, USA 12345\",\n", " deputyName=\"Jane Doe\",\n", " deputyTel=\"+1 800 123 4567\",\n", " deputyFax=\"+1 800 123 4567\",\n", " deputyMail=\"jane.doe@example.com\",\n", " deputyAddress=\"123 Main Street, Anytown, USA 12345\",\n", ")\n", "lr0003 = archive.LR0003(message=\"Example commentary (tutorial placeholder).\")\n", "\n", "lr0004 = archive.LR0004(\n", " surfaceType=11,\n", " topographyType=1,\n", " address=\"123 Main Street, Anytown, USA 12345\",\n", " telephone=\"+1 800 123 4567\",\n", " latitude=69.099 + 90.0,\n", " longitude=235.484,\n", " altitude=116,\n", " azimuth=\"0,15,30\",\n", " elevation=\"0,0,0\",\n", ")\n", "lr0007 = archive.LR0007()\n", "\n", "lr0008 = archive.LR0008(\n", " manufacturer=\"Kipp & Zonen\",\n", " model=\"CMP 22\",\n", " serialNumber=\"140114\",\n", " identification=82001,\n", " radiationQuantityMeasured=2,\n", " pyrgeometerDome=2,\n", " location=\"PMOD-WRC\",\n", " person=\"N. Mingard\",\n", " startOfCalibPeriod1=\"05/16/17\",\n", " endOfCalibPeriod1=\"06/30/17\",\n", " numOfComp1=1,\n", " meanCalibCoeff1=8.8000,\n", " stdErrorCalibCoeff1=0.0600,\n", " remarksOnCalib1=\"uV/W.m2\",\n", ")\n", "\n", "blocks = (\n", " lr0001.get_bsrn_format(),\n", " lr0002.get_bsrn_format(),\n", " lr0003.get_bsrn_format(),\n", " lr0004.get_bsrn_format(),\n", " lr0007.get_bsrn_format(synop=lr0004.synop),\n", " lr0008.get_bsrn_format(printLr=True),\n", " lr0008.get_bsrn_format(printLr=True, LR0009Format=True),\n", ")\n", "text = \"\\n\".join(b.rstrip(\"\\n\") for b in blocks) + \"\\n\"\n", "print(f\"{len(text):,} characters (metadata only)\")\n", "print(text)" ] }, { "cell_type": "markdown", "id": "545dd3a6", "metadata": {}, "source": [ "## 2. LR0100 / LR4000 (minute data) and full file string\n", "\n", "Run §1 first. **LR0100** in rBSRN: create the object, set `yearMonth`, then assign columns with `rep(1, 44640)` (here `pd.Series([1] * n)` for `ghi_avg` / `dhi_avg`). Other minute columns must be present for the formatter: we fill them with missing (`NaN`) except the two demo series.\n", "\n", "**Sphinx:** pre-rendered outputs; run locally to refresh." ] }, { "cell_type": "code", "execution_count": 2, "id": "0e3b78b1", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "9,376,601 characters (full month; preview only below)\n", "--- first lines ---\n", "*C0001\n", " 82 7 2020 1\n", " 2 3 4 5 21 22 23 -1\n", "*U0002\n", " -1 -1 -1\n", "John Doe +1 800 123 4567 +1 800 123 4567 \n", "XXX john.doe@example.com \n", "123 Main Street, Anytown, USA 12345 \n", " -1 -1 -1\n", "Jane Doe +1 800 123 4567 +1 800 123 4567 \n", "XXX jane.doe@example.com \n", "123 Main Street, Anytown, USA 12345 \n", "*U0003\n", "Example commentary (tutorial placeholder). \n", "@LR4000CONST, 50783, 61008, CAL_20211026_KZ_CH1_50783_61008, 9.62, ND, 0.02,&\n", "0.9974, ND, ND\n", "...\n", "--- last lines ---\n", " 31 1436 -99.99 -99.99 -99.99 -99.99 -999.9 -99.99 -99.99 -99.99 -99.99 -999.9\n", " 31 1437 -99.99 -99.99 -99.99 -99.99 -999.9 -99.99 -99.99 -99.99 -99.99 -999.9\n", " 31 1438 -99.99 -99.99 -99.99 -99.99 -999.9 -99.99 -99.99 -99.99 -99.99 -999.9\n", " 31 1439 -99.99 -99.99 -99.99 -99.99 -999.9 -99.99 -99.99 -99.99 -99.99 -999.9\n" ] } ], "source": [ "# Run §1 first. In production you might write the final string to disk, e.g.:\n", "# filename = f\"{stn}{MM}{YY2}.dat\"\n", "# out_path = os.path.join(input_dir, filename)\n", "# open(out_path, \"w\", encoding=\"ascii\", newline=\"\\n\").write(text)\n", "# or concatenate all LR strings and use a small helper like cat_to_file(out_path, *archive_blocks).\n", "# Here we only print the full-month example to the screen (no tempfile).\n", "\n", "import pandas as pd\n", "\n", "import bsrn.archive as archive\n", "from bsrn.archive.specs import LR_SPECS\n", "\n", "# LR0100 — same spirit as rBSRN:\n", "# lr0100 <- LR0100$new(); lr0100$yearMonth <- \"2020-07\"\n", "# lr0100$global2_avg <- rep(1, 44640); lr0100$diffuse_avg <- rep(1, 44640)\n", "# Python: set year_month and n, then fill columns (global2_avg -> ghi_avg, diffuse_avg -> dhi_avg).\n", "# Other LR0100 minute columns must be filled for formatting: we use NaN except the two demo columns.\n", "year_month = \"2020-07\"\n", "n = 44640 # 31 * 1440 for July; same as rep(1, 44640) in R\n", "\n", "def missing_col():\n", " return pd.Series([float('nan')] * n)\n", "\n", "lr0100_kw = {\"yearMonth\": year_month}\n", "for name in LR_SPECS[\"LR0100\"]:\n", " if name != \"yearMonth\":\n", " lr0100_kw[name] = missing_col()\n", "lr0100_kw[\"ghi_avg\"] = pd.Series([1] * n) # placeholder; replace with your GHI series\n", "lr0100_kw[\"dhi_avg\"] = pd.Series([1] * n) # placeholder; replace with your diffuse series\n", "lr0100 = archive.LR0100(**lr0100_kw)\n", "\n", "# LR4000 — e.g. lr4000$bodyT_down <- rep(1, 44640); here all missing except you add your own\n", "lr4000_kw = {\"yearMonth\": year_month}\n", "for name in LR_SPECS[\"LR4000\"]:\n", " if name != \"yearMonth\":\n", " lr4000_kw[name] = missing_col()\n", "lr4000 = archive.LR4000(**lr4000_kw)\n", "\n", "lr4000const = archive.LR4000CONST(\n", " serialNumber_Manufacturer=\"050783\",\n", " serialNumber_WRMC=\"61008\",\n", " yyyymmdd=20211026,\n", " manufact=\"KZ\",\n", " model=\"CH1\",\n", " C=9.62,\n", " k0=\"ND\",\n", " k1=0.02,\n", " k2=0.9974,\n", " k3=\"ND\",\n", " f=\"ND\",\n", ")\n", "const_line = lr4000const.get_bsrn_format(method=2)\n", "\n", "blocks = (\n", " lr0001.get_bsrn_format(),\n", " lr0002.get_bsrn_format(),\n", " lr0003.get_bsrn_format(const_line),\n", " lr0004.get_bsrn_format(),\n", " lr0007.get_bsrn_format(synop=lr0004.synop),\n", " lr0008.get_bsrn_format(printLr=True),\n", " lr0008.get_bsrn_format(printLr=True, LR0009Format=True),\n", " lr0100.get_bsrn_format(changed=True),\n", " lr4000.get_bsrn_format(changed=True),\n", ")\n", "text = \"\\n\".join(b.rstrip(\"\\n\") for b in blocks) + \"\\n\"\n", "print(f\"{len(text):,} characters (full month; preview only below)\")\n", "lines = text.splitlines()\n", "print(\"--- first lines ---\")\n", "print(\"\\n\".join(lines[:16]))\n", "print(\"...\")\n", "print(\"--- last lines ---\")\n", "print(\"\\n\".join(lines[-4:]))" ] } ], "metadata": { "kernelspec": { "display_name": "base", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.13.9" }, "nbsphinx": { "execute": "never" } }, "nbformat": 4, "nbformat_minor": 5 }