From 86f4cafca554113c1664c770be17fe9e2f231f85 Mon Sep 17 00:00:00 2001 From: ben-burlingham Date: Thu, 14 Jan 2016 21:15:59 -0800 Subject: [PATCH] Data retrieval for station meta and 2015 station data. --- .gitignore | 4 +- california.js | 39 ------- data/map.json | 14 +++ gradients-v1.js | 155 --------------------------- gradients-v2.js | 184 -------------------------------- index.html | 29 ++++- scrape.js | 47 ++++++++ server/downloader.js | 47 ++++++++ server/meteorological.js | 68 ++++++++++++ server/stations.js | 132 +++++++++++++++++++++++ gradients-v3.js => view/mesh.js | 111 +++++++------------ view/meshface.js | 13 +++ view/meshseed.js | 11 ++ view/svg.js | 102 ++++++++++++++++++ 14 files changed, 498 insertions(+), 458 deletions(-) delete mode 100644 california.js create mode 100644 data/map.json delete mode 100644 gradients-v1.js delete mode 100644 gradients-v2.js create mode 100644 scrape.js create mode 100644 server/downloader.js create mode 100644 server/meteorological.js create mode 100644 server/stations.js rename gradients-v3.js => view/mesh.js (59%) create mode 100644 view/meshface.js create mode 100644 view/meshseed.js create mode 100644 view/svg.js diff --git a/.gitignore b/.gitignore index 6034bb1..35b9803 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ -natural-earth +experiments +d3.min.js +node_modules diff --git a/california.js b/california.js deleted file mode 100644 index e2518dd..0000000 --- a/california.js +++ /dev/null @@ -1,39 +0,0 @@ -var file = 'natural-earth/california.geojson'; - -d3.json(file, function(error, mapJson) { - - var projection = - d3.geo.mercator() - .center([-122, 38]) - .scale(1960) - .translate([w / 2, h / 2]); - - var path = d3.geo.path().projection(projection); - - svg.selectAll('.subunit') - .data(mapJson.features) - .enter().append('path') - .attr('d', path) - .attr('stroke', 'lime'); - - var buoys = []; - buoys.push(projection([-122.634, 37.786])); - buoys.push(projection([-122.839, 37.755])); - buoys.push(projection([-122.977, 37.996])); - - svg.selectAll('.buoy') - .data(buoys) - .enter().append('circle') - .attr('cx', function(d) { return d[0]; }) - .attr('cy', function(d) { return d[1]; }) - .attr('r', 5) - .attr('fill', 'crimson'); -}); - -var w = 450; -var h = 450; - -var svg = d3.select("body").append("svg") - .attr("style", "border:1px solid lime") - .attr("width", w) - .attr("height", h); diff --git a/data/map.json b/data/map.json new file mode 100644 index 0000000..0275a3a --- /dev/null +++ b/data/map.json @@ -0,0 +1,14 @@ +{ +"type": "FeatureCollection", + +"features": [ +{ "type": "Feature", "properties": { "featurecla": "Admin-1 scale rank", "scalerank": 2.0, "adm1_code": "USA-3521", "diss_me": 3521, "adm1_cod_1": "USA-3521", "iso_3166_2": "US-CA", "wikipedia": "http:\/\/en.wikipedia.org\/wiki\/California", "sr_sov_a3": "US1", "sr_adm0_a3": "USA", "iso_a2": "US", "adm0_sr": 8, "admin0_lab": 2, "name": "California", "name_alt": "CA|Calif.", "name_local": null, "type": "State", "type_en": "State", "code_local": "US06", "code_hasc": "US.CA", "note": null, "hasc_maybe": null, "region": "West", "region_cod": null, "region_big": "Pacific", "big_code": null, "provnum_ne": 0, "gadm_level": 1, "check_me": 0, "scaleran_1": 2, "datarank": 1, "abbrev": "Calif.", "postal": "CA", "area_sqkm": 0.0, "sameascity": -99, "labelrank": 0, "featurec_1": "Admin-1 scale rank", "admin": "United States of America", "name_len": 10, "mapcolor9": 1, "mapcolor13": 1 }, "geometry": { "type": "MultiPolygon", "coordinates": [ [ [ [ -118.3503931559667, 32.827612107285823 ], [ -118.40858224510049, 32.818515427251334 ], [ -118.47320382959678, 32.838928025686265 ], [ -118.52891000847967, 32.935607716874308 ], [ -118.59016977644157, 33.011160698272057 ], [ -118.55710092752381, 33.032660943232806 ], [ -118.50749216098298, 32.959898489285244 ], [ -118.38318185350654, 32.849463914760072 ], [ -118.3503931559667, 32.827612107285823 ] ] ], [ [ [ -119.43802869550358, 33.217198303788017 ], [ -119.48249586028575, 33.215341614264027 ], [ -119.54367872394782, 33.224614075555259 ], [ -119.57517652788374, 33.278326235807306 ], [ -119.52512830820113, 33.282050601183762 ], [ -119.47879896073064, 33.27463482941652 ], [ -119.44205518491498, 33.232447327807172 ], [ -119.43802869550358, 33.217198303788017 ] ] ], [ [ [ -118.3479651773584, 33.385761542640061 ], [ -118.29747750453399, 33.312131168737473 ], [ -118.37023446531732, 33.321216862443485 ], [ -118.44630380415657, 33.317086002910919 ], [ -118.46935312144214, 33.357142156782693 ], [ -118.49203988988565, 33.412820869844133 ], [ -118.50733285921902, 33.427004219994579 ], [ -118.55942453601085, 33.431970040496488 ], [ -118.56332468264407, 33.437078683269483 ], [ -118.56942209498602, 33.464181955787801 ], [ -118.55484873017227, 33.477079905498549 ], [ -118.39170724445671, 33.415095039852808 ], [ -118.3479651773584, 33.385761542640061 ] ] ], [ [ [ -120.04357315219701, 33.918862148913753 ], [ -120.11390213437204, 33.904854580019965 ], [ -120.16713089616816, 33.918082119587105 ], [ -120.25190690037866, 34.013838959177441 ], [ -120.07184097554097, 34.026506195988752 ], [ -119.99440383879788, 33.984933928777927 ], [ -119.98393936085965, 33.973332365835326 ], [ -120.04357315219701, 33.918862148913753 ] ] ], [ [ [ -120.30660233703546, 34.024869233035737 ], [ -120.35970475605329, 34.022265473170769 ], [ -120.4415583968711, 34.032933198187102 ], [ -120.41292802468517, 34.056323091657426 ], [ -120.36773576221907, 34.073296969258045 ], [ -120.35332169916917, 34.060585787132517 ], [ -120.30660233703546, 34.024869233035737 ] ] ], [ [ [ -119.88239272612654, 34.079680026142164 ], [ -119.67886549668292, 34.028461762469583 ], [ -119.56912855402025, 34.052961275122932 ], [ -119.54928724466963, 34.028154145270399 ], [ -119.56220716703741, 34.006609954995419 ], [ -119.80958083370059, 33.967795256249111 ], [ -119.88551833659733, 33.994931487753014 ], [ -119.89241775092306, 34.032164155189015 ], [ -119.91804885541637, 34.06784775030021 ], [ -119.88239272612654, 34.079680026142164 ] ] ], [ [ [ -124.22840851691393, 42.000767974792105 ], [ -123.9556564313139, 42.000559234549776 ], [ -123.69200651891278, 42.000559234549776 ], [ -123.42835111334723, 42.000559234549776 ], [ -123.1647012009461, 42.000559234549776 ], [ -122.90104579538064, 42.000559234549776 ], [ -122.6373903898151, 42.000559234549776 ], [ -122.37374047741396, 42.000559234549776 ], [ -122.11006309919139, 42.000559234549776 ], [ -121.84638022780459, 42.000559234549776 ], [ -121.58273031540337, 42.000559234549776 ], [ -121.31907490983791, 42.000559234549776 ], [ -121.05541950427246, 42.000559234549776 ], [ -120.79176959187124, 42.000559234549776 ], [ -120.52811418630579, 42.000559234549776 ], [ -120.26446427390457, 42.000559234549776 ], [ -120.00080886833911, 42.000559234549776 ], [ -120.00075942986064, 41.813055565287719 ], [ -120.00070449821794, 41.625518937039978 ], [ -120.00065505973947, 41.438015267777921 ], [ -120.00062759391813, 41.250500612187317 ], [ -120.00060012809678, 41.062974970268137 ], [ -120.00055068961831, 40.875460314677518 ], [ -120.00050125113984, 40.687956645415461 ], [ -120.00044631949714, 40.500420017167734 ], [ -120.00039688101867, 40.312883388920007 ], [ -120.00034194937597, 40.12537971965795 ], [ -120.00029251089751, 39.937876050395893 ], [ -120.00024307241904, 39.750339422148166 ], [ -120.00018814077633, 39.562802793900424 ], [ -120.00013870229787, 39.375299124638367 ], [ -120.00008377065517, 39.187784469047763 ], [ -120.00006179799806, 39.000258827128583 ], [ -119.76583876660227, 38.83678225839251 ], [ -119.53161573520642, 38.673294703327791 ], [ -119.29738721064632, 38.509796161934595 ], [ -119.06316417925045, 38.346319593198423 ], [ -118.82894114785459, 38.182843024462265 ], [ -118.59471811645881, 38.019333496740508 ], [ -118.36049508506295, 37.85585692800435 ], [ -118.12627205366708, 37.692380359268277 ], [ -117.94294868241606, 37.554578840339651 ], [ -117.75960333850793, 37.416788307739601 ], [ -117.57625250143556, 37.278964816153952 ], [ -117.39293462334876, 37.141174283553902 ], [ -117.20961125209774, 37.003372764625375 ], [ -117.02626041502529, 36.86554927303964 ], [ -116.84291507111715, 36.72775874043959 ], [ -116.65959169986613, 36.589968207839611 ], [ -116.40568116771928, 36.391785827232674 ], [ -116.15174316975109, 36.193625419282775 ], [ -115.8978271444399, 35.995475997661508 ], [ -115.64391661229305, 35.797293617054571 ], [ -115.39000608014621, 35.599122222776195 ], [ -115.13609554799936, 35.400961814826367 ], [ -114.88217952268828, 35.202812393205015 ], [ -114.61054254943551, 34.991116828488877 ], [ -114.61093256409883, 34.907401004982837 ], [ -114.55747308940326, 34.79457141083504 ], [ -114.39800103742147, 34.589698356144716 ], [ -114.37887933259046, 34.540633412866747 ], [ -114.37691277978107, 34.539699574940563 ], [ -114.37102410768144, 34.488492297596544 ], [ -114.30802849980959, 34.432890488834822 ], [ -114.15879021286658, 34.355217146028046 ], [ -114.12506767740045, 34.317182476608458 ], [ -114.12501823892198, 34.314084331959016 ], [ -114.12561150066341, 34.286508647313333 ], [ -114.16351433414056, 34.253362894095751 ], [ -114.329320004528, 34.142027440629967 ], [ -114.41828179991387, 34.051148530913174 ], [ -114.50905084634518, 33.903920742093781 ], [ -114.4950982090941, 33.784653159419072 ], [ -114.49925653444811, 33.685875079479111 ], [ -114.52155878139261, 33.60760847493092 ], [ -114.55600092137834, 33.535626050310078 ], [ -114.60261042022667, 33.469894846630758 ], [ -114.65115900606303, 33.430618722085548 ], [ -114.70170161053014, 33.417808663003164 ], [ -114.71942805163604, 33.330862858905157 ], [ -114.70438776785915, 33.169781309791709 ], [ -114.64808832723493, 33.071750300192711 ], [ -114.55054620925618, 33.036791802765372 ], [ -114.49086297944037, 32.98361797261196 ], [ -114.46900018563764, 32.912305714032279 ], [ -114.47753106975215, 32.842004197678598 ], [ -114.51646661811242, 32.77275736886515 ], [ -114.58217584913461, 32.734722699445477 ], [ -114.72090021966095, 32.724483441242391 ], [ -114.72094965813942, 32.724483441242391 ], [ -114.72477290047271, 32.71534281589377 ], [ -114.83905818315263, 32.704719036191584 ], [ -115.12521358957646, 32.683306681859129 ], [ -115.41137448916453, 32.661883341198099 ], [ -115.69750792293115, 32.640460000537168 ], [ -115.98366332935497, 32.619047646204713 ], [ -116.26982422894304, 32.597624305543782 ], [ -116.55595766270966, 32.576200964882766 ], [ -116.84211306913349, 32.554788610550297 ], [ -117.12827396872154, 32.533365269889281 ], [ -117.13046574126606, 32.539770299430508 ], [ -117.13739262141314, 32.649172159072656 ], [ -117.18374394154065, 32.687876994533475 ], [ -117.24348210299918, 32.664025675264249 ], [ -117.27068974563866, 32.80626567092461 ], [ -117.25575932514717, 32.873370165671993 ], [ -117.26296635667215, 32.938870656451861 ], [ -117.31882634415459, 33.100051082522334 ], [ -117.46742193087772, 33.295486880993245 ], [ -117.78851386207177, 33.53847150940296 ], [ -117.95209480092902, 33.619605545701155 ], [ -118.080513995281, 33.72215193633172 ], [ -118.16190071713567, 33.750705404217832 ], [ -118.264403162452, 33.758615560769549 ], [ -118.2941706196423, 33.712308185956182 ], [ -118.41043893462439, 33.743937825834628 ], [ -118.39294869958226, 33.858294519650123 ], [ -118.50619577421473, 34.017409515954263 ], [ -118.59885446915561, 34.035031586939006 ], [ -118.83204478566836, 34.024462738879528 ], [ -119.14373241962858, 34.112023777375988 ], [ -119.23584179814226, 34.164120947332059 ], [ -119.26764721927745, 34.257438821985623 ], [ -119.4136884846141, 34.338572858283804 ], [ -119.6060536042569, 34.418004013657558 ], [ -119.7132032665475, 34.399656844988961 ], [ -119.85330092814179, 34.411961532958379 ], [ -120.05297744943076, 34.469266222644372 ], [ -120.16955887477637, 34.476451281512148 ], [ -120.39647050452616, 34.459554308211345 ], [ -120.48119157709397, 34.471639269609874 ], [ -120.55982073048403, 34.543885366115838 ], [ -120.64469561165139, 34.579986441711689 ], [ -120.626716484989, 34.668942743933236 ], [ -120.63762041606901, 34.749351682547491 ], [ -120.62490374077923, 34.811984741577355 ], [ -120.66301531449872, 34.949291875721492 ], [ -120.6336104061504, 35.07645862861937 ], [ -120.65908770204418, 35.12240345459076 ], [ -120.7070430261391, 35.15764759656026 ], [ -120.85737445277228, 35.209689834873622 ], [ -120.88488971261094, 35.274948626425576 ], [ -120.86029132300074, 35.365443014643283 ], [ -120.89958942020299, 35.425120751294777 ], [ -121.02283954697495, 35.480777491699193 ], [ -121.13794880429565, 35.607131256284852 ], [ -121.28383076786842, 35.676323153455506 ], [ -121.34385457386909, 35.792228919595722 ], [ -121.43374471401691, 35.863859781703233 ], [ -121.46501180505338, 35.927393719673709 ], [ -121.66435324332173, 36.154041677538466 ], [ -121.87741111277737, 36.331064389369409 ], [ -121.9101778376601, 36.432918641301718 ], [ -121.91864829696758, 36.572346136854868 ], [ -121.83514121370396, 36.657457224085995 ], [ -121.79000388288055, 36.732285107799804 ], [ -121.79452475707645, 36.800960647528932 ], [ -121.80744467944432, 36.851234086946683 ], [ -121.8806685591907, 36.938937947714216 ], [ -122.16421471258523, 36.990969199699016 ], [ -122.39490014618985, 37.207520721631674 ], [ -122.40846826194185, 37.373172583419567 ], [ -122.49923730837315, 37.542614728555172 ], [ -122.50045129767726, 37.652763658538284 ], [ -122.51422266050727, 37.771987295898761 ], [ -122.44562402507795, 37.798002921891175 ], [ -122.38410058523102, 37.788543693014724 ], [ -122.3902749018728, 37.741082753704269 ], [ -122.36970849483824, 37.655861803187655 ], [ -122.2975942342748, 37.591866439418361 ], [ -122.22866051582487, 37.563906233273592 ], [ -122.16599999097365, 37.501668682071276 ], [ -122.11904991594048, 37.48282712861819 ], [ -122.07052879592547, 37.478278788600953 ], [ -122.09654442191787, 37.518203106530201 ], [ -122.12414207922068, 37.543812238366492 ], [ -122.15804039594344, 37.626440415346615 ], [ -122.22220055464093, 37.732041005312489 ], [ -122.29599023030738, 37.790356437224482 ], [ -122.33343163798563, 37.896605220574479 ], [ -122.36547326518451, 37.921203610184669 ], [ -122.38544641047764, 37.960578611686735 ], [ -122.3142330288549, 38.00734741229892 ], [ -122.21703148706101, 38.040646974116036 ], [ -122.08672813736376, 38.049611818208099 ], [ -121.71685690743391, 38.034066163318151 ], [ -121.63810141126564, 38.06126831279331 ], [ -121.57301290780612, 38.052402345658273 ], [ -121.52534322825333, 38.0559179707924 ], [ -121.62572531216072, 38.083933108579778 ], [ -121.68220602720584, 38.074781496902595 ], [ -121.74863486274775, 38.080461428759897 ], [ -121.88077292931186, 38.075012209801962 ], [ -121.93415549970761, 38.086822512986892 ], [ -121.99311912500373, 38.12013306113257 ], [ -122.03151085010114, 38.123516850324172 ], [ -122.15377770046837, 38.065531008268387 ], [ -122.20829735586841, 38.072529299551043 ], [ -122.33712853754074, 38.135887456264861 ], [ -122.39335107386513, 38.144808355042677 ], [ -122.48388940739707, 38.108839115389252 ], [ -122.49489221543394, 37.953580320404079 ], [ -122.46691003663213, 37.838212884362569 ], [ -122.52132532191101, 37.826402581177724 ], [ -122.58418909384032, 37.874072260730522 ], [ -122.68069849693595, 37.902340084074474 ], [ -122.76038233786626, 37.945648191195325 ], [ -122.87293178063624, 38.026046143481018 ], [ -122.9319997760536, 38.055456544993497 ], [ -122.99879116043739, 37.988615722131236 ], [ -123.00145534510938, 38.019278565097814 ], [ -122.96815029012794, 38.097006839547284 ], [ -122.97760402584015, 38.227326668737312 ], [ -122.87680995461233, 38.123363041724538 ], [ -122.90815394994861, 38.196586921470917 ], [ -122.98654689727501, 38.277094737042098 ], [ -123.04618068861235, 38.305054943186782 ], [ -123.1211623809258, 38.449283464313659 ], [ -123.28973111294215, 38.535866719569704 ], [ -123.42478604973473, 38.675623804979146 ], [ -123.70112517160437, 38.907292514988519 ], [ -123.71952177875143, 39.110968059867439 ], [ -123.82029387732214, 39.368399710312737 ], [ -123.77778777218505, 39.514957333090898 ], [ -123.78350066302792, 39.618723206189891 ], [ -123.83292815514787, 39.775509100842662 ], [ -123.88447600867674, 39.860795969330447 ], [ -124.10851471351221, 40.094530109106152 ], [ -124.32403352056174, 40.251964197142961 ], [ -124.35653657355944, 40.371077971218121 ], [ -124.37165376163607, 40.49120248751921 ], [ -124.32452241218191, 40.598088477924769 ], [ -124.28367524265502, 40.710533550573565 ], [ -124.25390778546473, 40.740306500928007 ], [ -124.24233368834356, 40.727902936001747 ], [ -124.25060090057295, 40.703919780789995 ], [ -124.22000946874195, 40.696482036365644 ], [ -124.20843537162079, 40.746096296070796 ], [ -124.19024201155173, 40.77172740056411 ], [ -124.22248688582863, 40.775034285455895 ], [ -124.21918000093684, 40.790744735273954 ], [ -124.19990448750629, 40.822055771624655 ], [ -124.13309113046539, 40.969777945228536 ], [ -124.14001251744813, 41.155886350765506 ], [ -124.06851898444738, 41.384171271583263 ], [ -124.07190277363898, 41.459515512738669 ], [ -124.11768829784651, 41.621750626349289 ], [ -124.16324310915459, 41.719001606621653 ], [ -124.24460785835214, 41.787940818235818 ], [ -124.20874298881996, 41.88855361504266 ], [ -124.21166535221276, 41.984618071832266 ], [ -124.22840851691393, 42.000767974792105 ] ] ] ] } } +, +{ "type": "Feature", "properties": { "featurecla": "Admin-1 scale rank", "scalerank": 2.0, "adm1_code": "USA-3518", "diss_me": 3518, "adm1_cod_1": "USA-3518", "iso_3166_2": "US-ID", "wikipedia": "http:\/\/en.wikipedia.org\/wiki\/Idaho", "sr_sov_a3": "US1", "sr_adm0_a3": "USA", "iso_a2": "US", "adm0_sr": 1, "admin0_lab": 2, "name": "Idaho", "name_alt": "ID|Idaho", "name_local": null, "type": "State", "type_en": "State", "code_local": "US16", "code_hasc": "US.ID", "note": null, "hasc_maybe": null, "region": "West", "region_cod": null, "region_big": "Mountain", "big_code": null, "provnum_ne": 0, "gadm_level": 1, "check_me": 0, "scaleran_1": 2, "datarank": 1, "abbrev": "Idaho", "postal": "ID", "area_sqkm": 0.0, "sameascity": -99, "labelrank": 0, "featurec_1": "Admin-1 scale rank", "admin": "United States of America", "name_len": 5, "mapcolor9": 1, "mapcolor13": 1 }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -114.0, 42.001108550976959 ], [ -114.04259330901996, 42.001108550976959 ], [ -114.41435418745931, 42.001053619334265 ], [ -114.78611506589867, 42.000976715034447 ], [ -115.15787594433802, 42.00089981073463 ], [ -115.52963682277738, 42.000844879091922 ], [ -115.90139770121672, 42.000767974792105 ], [ -116.27315308649185, 42.000691070492287 ], [ -116.64491396493121, 42.000636138849593 ], [ -117.01667484337055, 42.000559234549776 ], [ -117.01711429651235, 42.223779457908698 ], [ -117.01758121547547, 42.446966722281935 ], [ -117.01802066861727, 42.670164972983834 ], [ -117.01846012175896, 42.893352237357085 ], [ -117.01867010996995, 43.0 ], [ -114.0, 43.0 ], [ -114.0, 42.001108550976959 ] ] ] } } +, +{ "type": "Feature", "properties": { "featurecla": "Admin-1 scale rank", "scalerank": 2.0, "adm1_code": "USA-3523", "diss_me": 3523, "adm1_cod_1": "USA-3523", "iso_3166_2": "US-NV", "wikipedia": "http:\/\/en.wikipedia.org\/wiki\/Nevada", "sr_sov_a3": "US1", "sr_adm0_a3": "USA", "iso_a2": "US", "adm0_sr": 1, "admin0_lab": 2, "name": "Nevada", "name_alt": "NV|Nev.", "name_local": null, "type": "State", "type_en": "State", "code_local": "US32", "code_hasc": "US.NV", "note": null, "hasc_maybe": null, "region": "West", "region_cod": null, "region_big": "Mountain", "big_code": null, "provnum_ne": 0, "gadm_level": 1, "check_me": 0, "scaleran_1": 2, "datarank": 1, "abbrev": "Nev.", "postal": "NV", "area_sqkm": 0.0, "sameascity": -99, "labelrank": 0, "featurec_1": "Admin-1 scale rank", "admin": "United States of America", "name_len": 6, "mapcolor9": 1, "mapcolor13": 1 }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -114.04026970053292, 37.004152793951924 ], [ -114.04117057947352, 36.612490181354922 ], [ -114.04287895356211, 36.181771170783698 ], [ -114.0619237540933, 36.175212332642907 ], [ -114.11943718402162, 36.076664965602397 ], [ -114.18320732805587, 36.030302659146322 ], [ -114.23214592855547, 36.031697922871423 ], [ -114.2815734206754, 36.060328295057339 ], [ -114.33154473605821, 36.116292652660938 ], [ -114.40608697522985, 36.148306814038371 ], [ -114.50527704249025, 36.156425710832444 ], [ -114.59297541009346, 36.147559743697407 ], [ -114.66914362588975, 36.121719898961665 ], [ -114.7157586179023, 36.084981616310237 ], [ -114.74136225657435, 36.013515549130922 ], [ -114.73232050818248, 35.983665694476578 ], [ -114.68710077989502, 35.917341229055914 ], [ -114.68368952488207, 35.813597328614044 ], [ -114.6770757550985, 35.729804600808187 ], [ -114.6501262911799, 35.683508212323382 ], [ -114.64516596384223, 35.630543122412298 ], [ -114.66206293714303, 35.545355130881354 ], [ -114.64852778037671, 35.475921534482687 ], [ -114.59098688462704, 35.352775777831972 ], [ -114.61052057677838, 34.997609748658398 ], [ -114.61054254943551, 34.991116828488877 ], [ -114.88217952268828, 35.202812393205015 ], [ -115.13609554799936, 35.400961814826367 ], [ -115.39000608014621, 35.599122222776195 ], [ -115.64391661229305, 35.797293617054571 ], [ -115.8978271444399, 35.995475997661508 ], [ -116.15174316975109, 36.193625419282775 ], [ -116.40568116771928, 36.391785827232674 ], [ -116.65959169986613, 36.589968207839611 ], [ -116.84291507111715, 36.72775874043959 ], [ -117.02626041502529, 36.86554927303964 ], [ -117.20961125209774, 37.003372764625375 ], [ -117.39293462334876, 37.141174283553902 ], [ -117.57625250143556, 37.278964816153952 ], [ -117.75960333850793, 37.416788307739601 ], [ -117.94294868241606, 37.554578840339651 ], [ -118.12627205366708, 37.692380359268277 ], [ -118.36049508506295, 37.85585692800435 ], [ -118.59471811645881, 38.019333496740508 ], [ -118.82894114785459, 38.182843024462265 ], [ -119.06316417925045, 38.346319593198423 ], [ -119.29738721064632, 38.509796161934595 ], [ -119.53161573520642, 38.673294703327791 ], [ -119.76583876660227, 38.83678225839251 ], [ -120.00006179799806, 39.000258827128583 ], [ -120.00008377065517, 39.187784469047763 ], [ -120.00013870229787, 39.375299124638367 ], [ -120.00018814077633, 39.562802793900424 ], [ -120.00024307241904, 39.750339422148166 ], [ -120.00029251089751, 39.937876050395893 ], [ -120.00034194937597, 40.12537971965795 ], [ -120.00039688101867, 40.312883388920007 ], [ -120.00044631949714, 40.500420017167734 ], [ -120.00050125113984, 40.687956645415461 ], [ -120.00055068961831, 40.875460314677518 ], [ -120.00060012809678, 41.062974970268137 ], [ -120.00062759391813, 41.250500612187317 ], [ -120.00065505973947, 41.438015267777921 ], [ -120.00070449821794, 41.625518937039978 ], [ -120.00075942986064, 41.813055565287719 ], [ -120.00080886833911, 42.000559234549776 ], [ -119.62780653477421, 42.000559234549776 ], [ -119.25478222855229, 42.000559234549776 ], [ -118.88175792233037, 42.000559234549776 ], [ -118.50875558876555, 42.000559234549776 ], [ -118.13573128254353, 42.000559234549776 ], [ -117.76270148315737, 42.000559234549776 ], [ -117.3897046427568, 42.000559234549776 ], [ -117.01667484337055, 42.000559234549776 ], [ -116.64491396493121, 42.000636138849593 ], [ -116.27315308649185, 42.000691070492287 ], [ -115.90139770121672, 42.000767974792105 ], [ -115.52963682277738, 42.000844879091922 ], [ -115.15787594433802, 42.00089981073463 ], [ -114.78611506589867, 42.000976715034447 ], [ -114.41435418745931, 42.001053619334265 ], [ -114.04259330901996, 42.001108550976959 ], [ -114.04251640472015, 41.844937890722626 ], [ -114.04246696624168, 41.688800189453978 ], [ -114.04239006194186, 41.532662488185316 ], [ -114.0423076644778, 41.376491827930998 ], [ -114.04223076017799, 41.220321167676666 ], [ -114.04215385587817, 41.064183466408004 ], [ -114.04207695157835, 40.908045765139271 ], [ -114.04200004727853, 40.751875104884938 ], [ -114.04192314297872, 40.595715430959167 ], [ -114.041846238679, 40.439577729690505 ], [ -114.04179130703621, 40.283429042093296 ], [ -114.04171440273639, 40.127269368167525 ], [ -114.04163749843666, 39.971098707913193 ], [ -114.04158805995819, 39.814939033987422 ], [ -114.04151115565837, 39.658768373733103 ], [ -114.04142875819423, 39.502630672464441 ], [ -114.04135185389451, 39.346492971195794 ], [ -114.04127494959469, 39.190322310941461 ], [ -114.04122551111622, 39.034151650687129 ], [ -114.04114860681641, 38.877991976761358 ], [ -114.04107170251659, 38.721821316507025 ], [ -114.04101677087388, 38.565683615238378 ], [ -114.04093986657406, 38.409545913969716 ], [ -114.04086296227425, 38.253375253715397 ], [ -114.04078605797443, 38.097204593461065 ], [ -114.04070915367461, 37.941066892192403 ], [ -114.0406322493748, 37.784929190923755 ], [ -114.04055534507498, 37.628758530669423 ], [ -114.04047294761094, 37.472598856743652 ], [ -114.04039604331112, 37.316461155475004 ], [ -114.04034660483265, 37.160312467877695 ], [ -114.04026970053292, 37.004152793951924 ] ] ] } } +, +{ "type": "Feature", "properties": { "featurecla": "Admin-1 scale rank", "scalerank": 2.0, "adm1_code": "USA-3525", "diss_me": 3525, "adm1_cod_1": "USA-3525", "iso_3166_2": "US-OR", "wikipedia": "http:\/\/en.wikipedia.org\/wiki\/Oregon", "sr_sov_a3": "US1", "sr_adm0_a3": "USA", "iso_a2": "US", "adm0_sr": 6, "admin0_lab": 2, "name": "Oregon", "name_alt": "OR|Ore.", "name_local": null, "type": "State", "type_en": "State", "code_local": "US41", "code_hasc": "US.OR", "note": null, "hasc_maybe": null, "region": "West", "region_cod": null, "region_big": "Pacific", "big_code": null, "provnum_ne": 0, "gadm_level": 1, "check_me": 0, "scaleran_1": 2, "datarank": 1, "abbrev": "Ore.", "postal": "OR", "area_sqkm": 0.0, "sameascity": -99, "labelrank": 0, "featurec_1": "Admin-1 scale rank", "admin": "United States of America", "name_len": 6, "mapcolor9": 1, "mapcolor13": 1 }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -117.01867010996995, 43.0 ], [ -117.01846012175896, 42.893352237357085 ], [ -117.01802066861727, 42.670164972983834 ], [ -117.01758121547547, 42.446966722281935 ], [ -117.01711429651235, 42.223779457908698 ], [ -117.01667484337055, 42.000559234549776 ], [ -117.3897046427568, 42.000559234549776 ], [ -117.76270148315737, 42.000559234549776 ], [ -118.13573128254353, 42.000559234549776 ], [ -118.50875558876555, 42.000559234549776 ], [ -118.88175792233037, 42.000559234549776 ], [ -119.25478222855229, 42.000559234549776 ], [ -119.62780653477421, 42.000559234549776 ], [ -120.00080886833911, 42.000559234549776 ], [ -120.26446427390457, 42.000559234549776 ], [ -120.52811418630579, 42.000559234549776 ], [ -120.79176959187124, 42.000559234549776 ], [ -121.05541950427246, 42.000559234549776 ], [ -121.31907490983791, 42.000559234549776 ], [ -121.58273031540337, 42.000559234549776 ], [ -121.84638022780459, 42.000559234549776 ], [ -122.11006309919139, 42.000559234549776 ], [ -122.37374047741396, 42.000559234549776 ], [ -122.6373903898151, 42.000559234549776 ], [ -122.90104579538064, 42.000559234549776 ], [ -123.1647012009461, 42.000559234549776 ], [ -123.42835111334723, 42.000559234549776 ], [ -123.69200651891278, 42.000559234549776 ], [ -123.9556564313139, 42.000559234549776 ], [ -124.22840851691393, 42.000767974792105 ], [ -124.35527314577686, 42.122881016559695 ], [ -124.40999604825501, 42.304342205122488 ], [ -124.42051545783593, 42.381004805703263 ], [ -124.40617280592161, 42.583680594684644 ], [ -124.44379548802085, 42.67020891829798 ], [ -124.53962923191101, 42.812866394443013 ], [ -124.49857332214178, 42.936858098391717 ], [ -124.46166934734761, 43.0 ], [ -117.01867010996995, 43.0 ] ] ] } } + +] +} diff --git a/gradients-v1.js b/gradients-v1.js deleted file mode 100644 index 7eee2e5..0000000 --- a/gradients-v1.js +++ /dev/null @@ -1,155 +0,0 @@ -'use strict'; - -/** - * - */ -function Point(x, y, name, hex) { - if (typeof x !== typeof 1) { - throw new Error("x = " + x + " must be of type 'number'; it is currently of type '" + (typeof x) + "'."); - } - - if (typeof y !== typeof 1) { - throw new Error("y = " + y + " must be of type 'number'; it is currently of type '" + (typeof y) + "'."); - } - - if (x < 0 || y < 0) { - throw new Error("X and Y must be greater than 0."); - } - - this.x = x; - this.y = y; - this.name = name; - this.hex = hex; -} - -/** - * - */ -function Triangle(a, b, c) { - if (!(a instanceof Point) || !(b instanceof Point) || !(c instanceof Point)) { - throw new Error("All three arguments must be of type Point."); - } - - this.a = a; - this.b = b; - this.c = c; -} - -/** - * - */ -Triangle.prototype = { - /** - * - */ - findMidpoint: function(p1, p2) { - var minX = Math.min(p1.x, p2.x); - var maxX = Math.max(p1.x, p2.x); - var minY = Math.min(p1.y, p2.y); - var maxY = Math.max(p1.y, p2.y); - - var midX = minX + ((maxX - minX) / 2); - var midY = minY + ((maxY - minY) / 2); - - return new Point(midX, midY); - }, - - /** - * - */ - buildGradient: function(p) { - var opp1, opp2; - - if (p === this.a) { - opp1 = this.b; - opp2 = this.c; - } - - if (p === this.b) { - opp1 = this.a; - opp2 = this.c; - } - - if (p === this.c) { - opp1 = this.a; - opp2 = this.b; - } - - var fill = d3.select(document.createElementNS(d3.ns.prefix.svg, "linearGradient")) - .attr("gradientUnits", "userSpaceOnUse") - .attr("id", `fill${p.name}`) - .attr("x1", p.x) - .attr("y1", p.y) - .attr("x2", this.findMidpoint(opp1, opp2).x) - .attr("y2", this.findMidpoint(opp1, opp2).y) - - fill.append("stop") - .attr("offset", "0%") - .attr("stop-color", p.hex) - - fill.append("stop") - .attr("offset", "100%") - .attr("stop-color", "#fff") - .attr("stop-opacity", "0") - - return function() { - return fill.node(); - }; - }, - - /** - * - */ - buildPath: function(p) { - var path = d3.select(document.createElementNS(d3.ns.prefix.svg, "path")) - .attr("d", `M${this.a.x},${this.a.y} L${this.b.x},${this.b.y} L${this.c.x},${this.c.y} Z`) - .attr("fill", `url(#fill${p.name})`) - - return function() { - return path.node(); - }; - } -}; - -/** - * - */ -(function() { - var svg = d3.select("body").append("svg") - .attr("width", 500) - .attr("height", 500); - - var defs = svg.append("defs"); - - var points = [ - [ - new Point(100, 0, 'A1', '#f00'), - new Point(200, 200, 'B1', '#0f0'), - new Point(0, 200, 'C1', '#00f') - ], - - [ - new Point(100, 0, 'A2', '#f00'), - new Point(200, 200, 'B2', '#0f0'), - new Point(300, 0, 'C2', '#00f') - ], - - [ - new Point(300, 300, 'A3', '#f00'), - new Point(300, 0, 'C3', '#00f'), - new Point(200, 200, 'B3', '#0f0'), - ], - ]; - - for (var i = 0, len = points.length; i < len; i++) { - var T = new Triangle(points[i][0], points[i][1], points[i][2]); - - defs.append(T.buildGradient(points[i][0])); - defs.append(T.buildGradient(points[i][1])); - defs.append(T.buildGradient(points[i][2])); - - svg.append(T.buildPath(points[i][0])); - svg.append(T.buildPath(points[i][1])); - svg.append(T.buildPath(points[i][2])); - } -}()); diff --git a/gradients-v2.js b/gradients-v2.js deleted file mode 100644 index d71e753..0000000 --- a/gradients-v2.js +++ /dev/null @@ -1,184 +0,0 @@ -'use strict'; - -/** - * - */ -function Point(x, y, hex) { - if (typeof x !== typeof 1) { - throw new Error("x = " + x + " must be of type 'number'; it is currently of type '" + (typeof x) + "'."); - } - - if (typeof y !== typeof 1) { - throw new Error("y = " + y + " must be of type 'number'; it is currently of type '" + (typeof y) + "'."); - } - - if (x < 0 || y < 0) { - throw new Error("X and Y must be greater than 0."); - } - - this.x = x; - this.y = y; - this.hex = hex; -} - -/** - * - */ -function Triangle(p1, p2, p3) { - if (!(p1 instanceof Point) || !(p2 instanceof Point) || !(p3 instanceof Point)) { - throw new Error("All three arguments must be of type Point."); - } - - this.vertexA = p1; - this.vertexB = p2; - this.vertexC = p3; - - // Midpoints on each edge. - this.midpointAB = this.findMidpoint(this.vertexA, this.vertexB); - this.midpointAC = this.findMidpoint(this.vertexA, this.vertexC); - this.midpointBC = this.findMidpoint(this.vertexB, this.vertexC); - - // Midpoints of segments between midpoints. - this.midpointABAC = this.findMidpoint(this.midpointAB, this.midpointAC); - this.midpointABBC = this.findMidpoint(this.midpointAB, this.midpointBC); - this.midpointACBC = this.findMidpoint(this.midpointAC, this.midpointBC); - - // Paths created from a vertex and the midpoints on its two adjacent edges. - this.subpathA = [this.vertexA, this.midpointAB, this.midpointAC]; - this.subpathB = [this.vertexB, this.midpointAB, this.midpointBC]; - this.subpathC = [this.vertexC, this.midpointAC, this.midpointBC]; -} - -/** - * - */ -Triangle.prototype = { - /** - * - */ - findMidpoint: function(p1, p2) { - var minX = Math.min(p1.x, p2.x); - var maxX = Math.max(p1.x, p2.x); - var minY = Math.min(p1.y, p2.y); - var maxY = Math.max(p1.y, p2.y); - - var midX = minX + ((maxX - minX) / 2); - var midY = minY + ((maxY - minY) / 2); - - return new Point(midX, midY); - }, - - - /** - * - */ - buildPath: function(p1, p2, p3, fill) { - var path = d3.select(document.createElementNS(d3.ns.prefix.svg, "path")) - .attr("d", `M${p1.x},${p1.y} L${p2.x},${p2.y} L${p3.x},${p3.y} Z`) - // .attr("stroke", "#000") - - var hex = /^#[\da-f]{3}$|^#[\da-f]{6}$/i; - - if (fill !== undefined) { - if (fill.match(hex)) { - path.attr("fill", fill).attr("stroke", fill); - } - else { - path.attr("fill", `url(#${fill})`) - // .attr("stroke", `url(#${fill})`); - } - } - - return function() { - return path.node(); - }; - }, - - /** - * - */ - buildGradient: function(p1, p2, id, hex) { - var fill = d3.select(document.createElementNS(d3.ns.prefix.svg, "linearGradient")) - .attr("gradientUnits", "userSpaceOnUse") - .attr("id", id) - .attr("x1", p1.x) - .attr("y1", p1.y) - .attr("x2", p2.x) - .attr("y2", p2.y) - - fill.append("stop") - .attr("offset", "0%") - .attr("stop-color", hex) - - fill.append("stop") - .attr("offset", "100%") - .attr("stop-color", "#fff") - .attr("stop-opacity", "0") - - return function() { - return fill.node(); - }; - }, -}; - -/** - * - */ -(function() { - var svg = d3.select("body").append("svg") - .attr("width", 500) - .attr("height", 500); - - var defs = svg.append("defs"); - - var points = [ - [ - new Point(100, 0, '#f00'), - new Point(200, 200, '#0f0'), - new Point(0, 300, '#00f') - ] - - // [ - // new Point(100, 0, '#f00'), - // new Point(200, 200, '#0f0'), - // new Point(300, 0, '#00f') - // ], - - // [ - // new Point(300, 300, '#f00'), - // new Point(200, 200, '#0f0'), - // new Point(300, 0, '#00f') - // ], - ]; - - var subpathA; - var subpathB; - var subpathC; - var subpathM1; - var subpathM2; - var subpathM3; - - for (var i = 0, len = points.length; i < len; i++) { - var T = new Triangle(points[i][0], points[i][1], points[i][2]); - - subpathA = T.buildPath(T.subpathA[0], T.subpathA[1], T.subpathA[2], points[i][0].hex); - subpathB = T.buildPath(T.subpathB[0], T.subpathB[1], T.subpathB[2], points[i][1].hex); - subpathC = T.buildPath(T.subpathC[0], T.subpathC[1], T.subpathC[2], points[i][2].hex); - - defs.append(T.buildGradient(T.midpointACBC, T.midpointBC, `fill-${i}-0`, points[i][0].hex)); - defs.append(T.buildGradient(T.midpointABBC, T.midpointAC, `fill-${i}-1`, points[i][1].hex)); - defs.append(T.buildGradient(T.midpointABAC, T.midpointAB, `fill-${i}-2`, points[i][2].hex)); - - subpathM1 = T.buildPath(T.midpointAB, T.midpointAC, T.midpointBC, `fill-${i}-0`); - subpathM2 = T.buildPath(T.midpointAB, T.midpointAC, T.midpointBC, `fill-${i}-1`); - subpathM3 = T.buildPath(T.midpointAB, T.midpointAC, T.midpointBC, `fill-${i}-2`); - - svg.append(subpathA); - svg.append(subpathB); - svg.append(subpathC); - - svg.append(subpathM1); - svg.append(subpathM2); - svg.append(subpathM3); - } -}()); diff --git a/index.html b/index.html index 84395b8..0d63d80 100644 --- a/index.html +++ b/index.html @@ -61,27 +61,44 @@ + + + + + +NEXT: +http://bl.ocks.org/mbostock/4699541 + +http://bl.ocks.org/mbostock/6123708 + - - + + +

ETL Stage

+ diff --git a/scrape.js b/scrape.js new file mode 100644 index 0000000..910a9a9 --- /dev/null +++ b/scrape.js @@ -0,0 +1,47 @@ +var stations = require('./server/stations.js'); +var meteo = require('./server/meteorological.js'); +var Promise = require('es6-promise').Promise; +var chalk = require('chalk'); + +//===== Weather data scraper +// var date = new Date(); + +var len = stations.stations.length; + +//===== Meteorological data scraping - careful! +(function scrapeMeteo(currentStation) { + if (currentStation !== 0) { + console.log(chalk.cyan('=== Finished.\n')); + } + + if (currentStation >= len) { + // if (currentStation > 1) { + return; + } + + var arr = []; + + console.log(chalk.cyan('\n=== (' + currentStation + ') Starting ' + stations.stations[currentStation])); + + // Monthly files + // for (var m = 0; m < 12; m++) { + // arr.push(meteo.getInconsistent(stations.stations[currentStation], m, 2015)); + // arr.push(meteo.getMonth(stations.stations[currentStation], m)); + // } + + // TODO refactor to above format + // TODO refactor station requests into promises format + // for (var year = 1982; year < date.getFullYear() - 1; year++) { + // getYear(stationIDs[i], year); + // } + + // getCurrent(stationIDs[i]); + + // Wait for all to resolve, then recurse. + Promise.all(arr).then(scrapeMeteo.bind(null, currentStation + 1)); + +})(0); + +//===== Station data scraping - shouldn't need to change often. +// stations.downloadAllMetadata(); +// stations.parseAllMetadata(); diff --git a/server/downloader.js b/server/downloader.js new file mode 100644 index 0000000..0afa0e4 --- /dev/null +++ b/server/downloader.js @@ -0,0 +1,47 @@ +'use strict' + +var http = require('http'); +var fs = require('fs'); +var Promise = require('es6-promise').Promise; + +module.exports = { + + downloadedCount: 0, + + downloadedSize: 0, + + /** + * + */ + mkdir: function(path) { + try { + fs.mkdirSync(path); + } catch(e) { + if (e.code !== 'EEXIST'){ + console.log(e); + } + } + }, + + /** + * + */ + download: function(url, filename) { + return new Promise(function(resolve, reject) { + var file = fs.createWriteStream(filename); + + http.get(url, function(response) { + var pipe = response.pipe(file); + + pipe.on('finish', function() { + module.exports.downloadedSize += pipe.bytesWritten; + module.exports.downloadedCount++; + + console.log(url + ' --> ' + filename + ' --> ' + pipe.bytesWritten + ' bytes | ' + module.exports.downloadedSize + ' bytes total | ' + module.exports.downloadedCount + ' file(s)'); + + resolve(pipe.bytesWritten); + }); + }); + }); + } +}; diff --git a/server/meteorological.js b/server/meteorological.js new file mode 100644 index 0000000..7435e09 --- /dev/null +++ b/server/meteorological.js @@ -0,0 +1,68 @@ +'use strict' + +var downloader = require('./downloader.js'); +var dir = 'data/meteorological/' +var Promise = require('es6-promise').Promise; + +module.exports = { + months: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'], + + /** + * + */ + getYear: function(buoy, yyyy) { + var filename = buoy + 'h' + yyyy + '.txt'; + + var url = 'http://www.ndbc.noaa.gov/view_text_file.php?'+ + 'filename=' + filename + '.gz&dir=data/historical/stdmet/'; + + return downloader.download(url, dir + filename); + }, + + /** + * + */ + getMonth: function(buoy, m, yyyy) { + var month = m + 1; + month = (month == 10 ? 'a' : month); + month = (month == 11 ? 'b' : month); + month = (month == 12 ? 'c' : month); + + var filename = buoy + month.toString() + yyyy + '.txt'; + + var url = 'http://www.ndbc.noaa.gov/view_text_file.php?' + + 'filename=' + filename + '.gz&dir=data/stdmet/' + this.months[m] + '/'; + + var path = dir + buoy + '/'; + downloader.mkdir(path); + return downloader.download(url, path + filename); + }, + + /** + * + */ + getCurrent: function(buoy) { + var filename = buoy + '.txt'; + + var url = 'http://www.ndbc.noaa.gov/data/realtime2/' + filename; + + return downloader.download(url, dir + filename); + }, + + /** + * + */ + getInconsistent: function(buoy, m) { + var url = 'http://www.ndbc.noaa.gov/data/stdmet/' + this.months[m] + '/' + buoy + '.txt'; + + var month = m + 1; + month = (month == 10 ? 'a' : month); + month = (month == 11 ? 'b' : month); + month = (month == 12 ? 'c' : month); + + var path = dir + buoy + '/'; + downloader.mkdir(path); + + return downloader.download(url, path + buoy.toString() + month + '2015-newest.txt'); + } +}; diff --git a/server/stations.js b/server/stations.js new file mode 100644 index 0000000..61018f3 --- /dev/null +++ b/server/stations.js @@ -0,0 +1,132 @@ +'use strict' + +var fs = require('fs'); +var downloader = require('./downloader'); +var xml2js = require ('xml2js'); +var dir = 'data/stations/'; + +module.exports = { + /** + * Add station IDs here, A-Z 0-9 + */ + stations: [ + 'ANVC1', + 'BDXC1', + 'CECC1', + 'CPXC1', + 'HBYC1', + 'ICAC1', + 'NTBC1', + 'PRYC1', + 'PTGC1', + '46011', + '46012', + '46013', + '46014', + '46022', + '46025', + '46027', + '46028', + '46042', + '46053', + '46054', + '46092', + '46213', + '46214', + '46215', + '46216', + '46217', + '46221', + '46223', + '46225', + '46232', + '46234', + '46237', + '46239', + '46240', + '46242', + '46244', + '46253', + '46254', + '46256', + '46257' + ], + + /** + * Downloads each station's data XML. + */ + downloadAllMetadata: function() { + var len = this.stations.length; + var url; + + for (var i = 0; i < len; i++) { + url = 'http://www.ndbc.noaa.gov/get_observation_as_xml.php?station=' + this.stations[i]; + downloader.download(url, dir + this.stations[i] + '.txt'); + } + }, + + /** + * + */ + parseAllMetadata: function() { + function done() { + if (data.length === len) { + fs.writeFile(outfile, data.join('\n'), function(err) { + if (err) { + throw new Error(err) + } + + console.log('Station data written to ' + outfile); + }); + } + }; + + function next() { + // Wait for other concurrent files to finish. + if (count !== data.length) { + return; + } + + var concurrent = 3; + var tmp; + + for (var i = 0; i < concurrent; i++) { + tmp = count + i; + + if (tmp === len) { + break; + } + + // console.log(tmp + "(" + count + ")" + " reading " + dir + module.exports.stations[tmp] + '.txt'); + fs.readFile(dir + module.exports.stations[tmp] + '.txt', 'utf8', thenParse); + } + + count += concurrent; + }; + + function thenParse(err, xml) { + if (err) { + throw new Error(err) + } + + xml2js.parseString(xml, thenReport); + }; + + function thenReport(err, json) { + if (err) { + throw new Error(err) + } + + data.push(JSON.stringify(json.observation.$)); + next(); + done(); + }; + + var outfile = 'data-stations.json'; + var len = module.exports.stations.length; + var count = 0; + var data = []; + + next(); + }, +}; diff --git a/gradients-v3.js b/view/mesh.js similarity index 59% rename from gradients-v3.js rename to view/mesh.js index fc851ca..e511165 100644 --- a/gradients-v3.js +++ b/view/mesh.js @@ -3,42 +3,24 @@ /** * */ -function Mesh() { - // // Color seed coordinants - // this.seeds = []; - +function Mesh(width, height, side) { // Length of a side - this.side = 10; - - // // How far a seed will extend its reach - this.seedWeight = 40; - - // - this.width = 400; - this.height = 400; - - var faces = this.initFaces(this.width, this.height, this.side); + this.side = side; + this.width = width; + this.height = height; - this.addSeed(new Seed(0, 0, '#ff0000', this.seedWeight), faces); - this.addSeed(new Seed(0, 39, '#0000ff', this.seedWeight), faces); - this.addSeed(new Seed(39, 0, '#00ff00', this.seedWeight), faces); - this.addSeed(new Seed(39, 39, '#ffff00', this.seedWeight), faces); + // How far a seed will extend its reach + this.seedWeight = 5; - this.calculateFills(faces); + // Color seed coordinants + // this.addSeed(new MeshSeed(0, 0, '#ff0000', this.seedWeight), faces); + // this.addSeed(new MeshSeed(0, this.height - 1, '#0000ff', this.seedWeight), faces); + // this.addSeed(new MeshSeed(this.width - 1, 0, '#00ff00', this.seedWeight), faces); + // this.addSeed(new MeshSeed(this.width - 1, this.height - 1, '#ffff00', this.seedWeight), faces); - var svg = d3.select("body").append("svg").attr('width', this.width).attr('height', this.height); + // this.calculateFills(faces); - svg.selectAll('rect') - .data(faces) - .enter() - .append('rect') - .attr('id', function(d) { return d.id; }) - .attr('x', function(d) { return d.x; }) - .attr('y', function(d) { return d.y; }) - .attr('width', function(d) { return d.side; }) - .attr('height', function(d) { return d.side; }) - .attr('fill', function(d) { return d.fill; }) - .attr('stroke', 0) + // var svg = d3.select("body").append("svg").attr('width', this.width).attr('height', this.height); // d3.select(id).attr('fill', seed.fill); // for (var i = 0, len = seeds.length; i < len; i++) { @@ -46,28 +28,6 @@ function Mesh() { // } }; -/** - * - */ -function Face(id, x, y, side) { - this.id = id; - this.x = x; - this.y = y; - this.side = side; - this.fill = '#fff'; - this.colors = []; -}; - -/** - * - */ -function Seed(x, y, fill, weight) { - this.x = x; - this.y = y; - this.fill = fill; - this.weight = weight; -}; - /** * */ @@ -82,7 +42,7 @@ Mesh.prototype = { for (var w = 0; w < countW; w++) { for (var h = 0; h < countH; h++) { - all.push(new Face( + all.push(new MeshFace( `f-${w}-${h}-${all.length}`, w * side, h * side, @@ -99,7 +59,7 @@ Mesh.prototype = { */ calculateFills: function(faces) { var len = faces.length; - var hexes; + var hexes, colors; var populate = function(color) { var arr = []; @@ -110,17 +70,18 @@ Mesh.prototype = { }; for (var i = 0; i < len; i++) { - var hexes = []; + hexes = []; for (var j = 0; j < faces[i].colors.length; j++) { - hexes = hexes.concat(populate(faces[i].colors[j])); + colors = faces[i].colors[j]; + hexes = hexes.concat(populate(colors)); } while (hexes.length < this.seedWeight) { - hexes.push('#ccccff'); + hexes.push('#ffffff'); } - faces[i].fill = Compositor.averageHexCollection(hexes); + faces[i].fill = this.averageHexCollection(hexes); } }, @@ -128,25 +89,34 @@ Mesh.prototype = { * */ addSeed: function(seed, faces) { + var faceX = Math.floor(seed.x / this.side); + var faceY = Math.floor(seed.y / this.side); + + var countX = this.width / this.side; + var countY = this.height / this.side; + var n = this.seedWeight; var len = faces.length; var delta, index, weight; - var countX = this.width / this.side; - var countY = this.height / this.side; - for (var x = (seed.x - n + 1); x < seed.x + n; x++) { + for (var x = (faceX - n + 1); x < faceX + n; x++) { if (x < 0 || x >= countX) { continue; } - for (var y = (seed.y - n + 1); y < seed.y + n; y++) { + for (var y = (faceY - n + 1); y < faceY + n; y++) { if (y < 0 || y >= countY) { continue; } - // Diamond shapes around source drop weighting incrementally. - delta = Math.abs(seed.x - x) + Math.abs(seed.y - y); - weight = Math.max(0, n - delta); + // Weights decrement outwards from source in a given shape: + + // Diamonds: + // delta = Math.abs(faceX - x) + Math.abs(faceY - y); + // weight = Math.max(0, n - delta); + + // Squares: + var weight = n - Math.max(Math.abs(faceX - x), Math.abs(faceY - y)); index = x * countY + y; @@ -156,12 +126,7 @@ Mesh.prototype = { } } }, -}; -/** - * - */ -var Compositor = { /** * */ @@ -205,7 +170,5 @@ var Compositor = { var b = this.averageComponentCollection(allB); return '#' + r + g + b; - }, + } }; - -var mesh = new Mesh(); diff --git a/view/meshface.js b/view/meshface.js new file mode 100644 index 0000000..0ace38a --- /dev/null +++ b/view/meshface.js @@ -0,0 +1,13 @@ +'use strict'; + +/** + * + */ +function MeshFace(id, x, y, side) { + this.id = id; + this.x = x; + this.y = y; + this.side = side; + this.fill = '#fff'; + this.colors = []; +}; diff --git a/view/meshseed.js b/view/meshseed.js new file mode 100644 index 0000000..25c1020 --- /dev/null +++ b/view/meshseed.js @@ -0,0 +1,11 @@ +'use strict'; + +/** + * + */ +function MeshSeed(x, y, fill, weight) { + this.x = x; + this.y = y; + this.fill = fill; + this.weight = weight; +}; diff --git a/view/svg.js b/view/svg.js new file mode 100644 index 0000000..3a143ea --- /dev/null +++ b/view/svg.js @@ -0,0 +1,102 @@ +'use strict'; + +(function() { + var mapFile = 'data/map.json'; + var stationsFile = 'data/stations.json'; + + var w = 450; + var h = 450; + var s = 25; + + var svg = d3.select("body").append("svg") + .attr("style", "border:1px solid lime") + .attr("width", w) + .attr("height", h); + + var projection = d3.geo.mercator() + .center([-122, 38]) + .scale(1960) + .translate([w / 2, h / 2]); + + /** + * + */ + function drawMap() { + d3.json(mapFile, function(error, json) { + var path = d3.geo.path().projection(projection); + + svg.selectAll('.subunit') + .data(json.features) + .enter().append('path') + .attr('d', path) + .attr('stroke', 'lime'); + }); + }; + + /** + * + */ + function drawStations() { + function randomColor() { + var hexes = ['#ff0000', '#ffff00', '#ff00ff', '#00ffff', '#0000ff', '#00ff00']; + var random = Math.floor(Math.random() * hexes.length); + return hexes[random]; + }; + + d3.json(stationsFile, function(error, json) { + var tmp; + var zzz = -1; + + json.stations.forEach(function(station) { + // console.log(station); + + tmp = projection([station.lon, station.lat]); + station.x = tmp[0]; + station.y = tmp[1]; + + if (zzz === -1) { + zzz = 0 + mesh.addSeed(new MeshSeed(tmp[0], tmp[1], randomColor(), mesh.seedWeight), faces); + } + }); + + drawMesh(); + + svg.selectAll('.station') + .data(json.stations) + .enter().append('circle') + .attr('cx', function(d) { return d.x; }) + .attr('cy', function(d) { return d.y; }) + .attr('r', 5) + .attr('fill', 'crimson'); + }); + }; + + /** + * + */ + function drawMesh() { + mesh.calculateFills(faces); + + svg.selectAll('rect') + .data(faces) + .enter() + .append('rect') + .attr('id', function(d) { return d.id; }) + .attr('x', function(d) { return d.x; }) + .attr('y', function(d) { return d.y; }) + .attr('width', function(d) { return d.side; }) + .attr('height', function(d) { return d.side; }) + .attr('fill', function(d) { return d.fill; }) + // .attr('stroke', '#aaa') + + drawMap(); + }; + + var mesh = new Mesh(w, h, s); + var faces = mesh.initFaces(w, h, s); + + // TODO update to promise chain + // TODO add render type to mesh + drawStations(); +})();